00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00031 #include <string.h>
00032 #include <ctype.h>
00033 #include <math.h>
00034
00035 #include "splt.h"
00036 #include "cddb_cue_common.h"
00037
00038 #include "cddb.h"
00039
00040 static void splt_cddb_process_line(char **l, cddb_utils *cdu, splt_state *state);
00041 static void splt_cddb_process_disc_length_line(const char *line_content, cddb_utils *cdu, splt_state *state);
00042 static void splt_cddb_convert_points(cddb_utils *cdu, splt_state *state);
00043 static void splt_cddb_process_offset_line(const char *line_content, cddb_utils *cdu, splt_state *state);
00044 static cddb_utils *splt_cddb_cdu_new(splt_state *state, int *error);
00045 static void splt_cddb_cdu_free(cddb_utils **cdu);
00046 static void splt_cddb_process_year_line(const char *line_content,
00047 cddb_utils *cdu, splt_state *state);
00048 static void splt_cddb_process_genre_line(char *line_content, cddb_utils *cdu, splt_state *state);
00049 static void splt_cddb_process_dtitle_line(const char *line_content, cddb_utils *cdu, splt_state *state);
00050 static void splt_cddb_process_ttitle_line(const char *line_content, cddb_utils *cdu, splt_state *state);
00051 static void splt_cddb_process_id3g_line(const char *line_content, cddb_utils *cdu, splt_state *state);
00052
00053 int splt_cddb_put_splitpoints(const char *file, splt_state *state, int *error)
00054 {
00055 if (file == NULL)
00056 {
00057 splt_e_set_error_data(state, file);
00058 *error = SPLT_INVALID_CDDB_FILE;
00059 return 0;
00060 }
00061
00062 splt_c_put_info_message_to_client(state,
00063 _(" reading informations from CDDB file %s ...\n"),file);
00064
00065 splt_t_free_splitpoints_tags(state);
00066
00067 *error = SPLT_CDDB_OK;
00068
00069 int err = SPLT_OK;
00070 FILE *file_input = NULL;
00071 char *line = NULL;
00072 int tracks = 0;
00073
00074 cddb_utils *cdu = splt_cddb_cdu_new(state, &err);
00075 if (err < 0) { *error = err; return tracks; }
00076 cdu->file = file;
00077
00078 if (!(file_input=splt_io_fopen(file, "r")))
00079 {
00080 splt_cddb_cdu_free(&cdu);
00081 splt_e_set_strerror_msg_with_data(state, file);
00082 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00083 return tracks;
00084 }
00085
00086 if (fseek(file_input, 0, SEEK_SET) != 0)
00087 {
00088 splt_e_set_strerror_msg_with_data(state, file);
00089 *error = SPLT_ERROR_SEEKING_FILE;
00090 goto function_end;
00091 }
00092
00093 err = splt_tu_set_tags_field(state, 0, SPLT_TAGS_GENRE, SPLT_UNDEFINED_GENRE);
00094 if (err < 0) { *error = err; goto function_end; }
00095
00096 while ((line = splt_io_readline(file_input, error)) != NULL)
00097 {
00098 if (*error < 0) { goto function_end; }
00099
00100 splt_cddb_process_line(&line, cdu, state);
00101 tracks = cdu->tracks;
00102 if (cdu->error < 0) { *error = cdu->error; goto function_end; }
00103 }
00104
00105 splt_cc_put_filenames_from_tags(state, tracks, error);
00106
00107 function_end:
00108 splt_cddb_cdu_free(&cdu);
00109
00110 if (line)
00111 {
00112 free(line);
00113 line = NULL;
00114 }
00115
00116 if (fclose(file_input) != 0)
00117 {
00118 splt_e_set_strerror_msg_with_data(state, file);
00119 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00120 }
00121 file_input = NULL;
00122
00123 if (*error >= 0)
00124 {
00125 splt_c_put_info_message_to_client(state, _(" Tracks: %d\n\n"), tracks);
00126 }
00127
00128 return tracks;
00129 }
00130
00131 static void splt_cddb_process_line(char **l, cddb_utils *cdu, splt_state *state)
00132 {
00133 if (!l || !*l) { return; }
00134
00135 char *line = *l;
00136
00137 splt_su_line_to_unix(line);
00138 splt_su_str_cut_last_char(line);
00139
00140 char *line_content = NULL;
00141 if ((line_content = strstr(line, "Track frame offset")) != NULL)
00142 {
00143 cdu->read_offsets = SPLT_TRUE;
00144 }
00145 else if ((line_content = strstr(line, "Disc length")) != NULL)
00146 {
00147 splt_cddb_process_disc_length_line(line_content, cdu, state);
00148 }
00149 else if (cdu->read_offsets)
00150 {
00151 splt_cddb_process_offset_line(line, cdu, state);
00152 }
00153 else if ((line_content = strstr(line, "YEAR")) != NULL)
00154 {
00155 splt_cddb_process_year_line(line_content, cdu, state);
00156 }
00157 else if ((line_content = strstr(line, "GENRE")) != NULL)
00158 {
00159 splt_cddb_process_genre_line(line_content, cdu, state);
00160 }
00161 else if ((line_content = strstr(line, "DTITLE")) != NULL)
00162 {
00163 splt_cddb_process_dtitle_line(line_content, cdu, state);
00164 }
00165 else if ((line_content = strstr(line, "TTITLE")) != NULL)
00166 {
00167 splt_cddb_process_ttitle_line(line_content, cdu, state);
00168 }
00169 else if ((line_content = strstr(line, "ID3G")) != NULL)
00170 {
00171 splt_cddb_process_id3g_line(line_content, cdu, state);
00172 }
00173
00174 free(*l);
00175 *l = NULL;
00176 }
00177
00178 static void splt_cddb_process_id3g_line(const char *line_content, cddb_utils *cdu, splt_state *state)
00179 {
00180 int err = SPLT_OK;
00181
00182 int id3g = atoi(line_content + 6);
00183 if (id3g < SPLT_ID3V1_NUMBER_OF_GENRES)
00184 {
00185 err = splt_tu_set_tags_field(state, 0, SPLT_TAGS_GENRE, splt_id3v1_genres[id3g]);
00186 if (err < 0) { cdu->error = err; return; }
00187 }
00188 }
00189
00190 static void splt_cddb_process_ttitle_line(const char *line_content, cddb_utils *cdu, splt_state *state)
00191 {
00192 int err = SPLT_OK;
00193
00194 int index = atoi(line_content+6);
00195
00196 char *equal_ptr = NULL;
00197 if ((equal_ptr = strchr(line_content, '=')) == NULL)
00198 {
00199 splt_e_set_error_data(state, cdu->file);
00200 cdu->error = SPLT_INVALID_CDDB_FILE;
00201 return;
00202 }
00203
00204 if (equal_ptr == line_content)
00205 {
00206 splt_e_set_error_data(state, cdu->file);
00207 cdu->error = SPLT_INVALID_CDDB_FILE;
00208 return;
00209 }
00210
00211 char *slash = strchr(equal_ptr, '/');
00212 if (slash != NULL)
00213 {
00214 char *title = splt_su_trim_spaces(slash + 1);
00215 err = splt_tu_set_tags_field(state, index, SPLT_TAGS_TITLE, title);
00216 if (err < 0) { cdu->error = err; return; }
00217 *slash = '\0';
00218
00219 char *performer = splt_su_trim_spaces(equal_ptr + 1);
00220 err = splt_tu_set_tags_field(state, index, SPLT_TAGS_PERFORMER, performer);
00221 if (err < 0) { cdu->error = err; return; }
00222 }
00223 else
00224 {
00225 char *title = splt_su_trim_spaces(equal_ptr + 1);
00226 err = splt_tu_set_tags_field(state, index, SPLT_TAGS_TITLE, title);
00227 if (err < 0) { cdu->error = err; return; }
00228 }
00229 }
00230
00231 static void splt_cddb_process_dtitle_line(const char *line_content, cddb_utils *cdu, splt_state *state)
00232 {
00233 int err = SPLT_OK;
00234
00235 char *equal_ptr = NULL;
00236 if ((equal_ptr = strchr(line_content, '=')) == NULL)
00237 {
00238 splt_e_set_error_data(state, cdu->file);
00239 cdu->error = SPLT_INVALID_CDDB_FILE;
00240 return;
00241 }
00242
00243 int we_have_album = SPLT_FALSE;
00244
00245 char *slash = strchr(equal_ptr, '/');
00246 if (slash != NULL)
00247 {
00248 char *album = splt_su_trim_spaces(slash + 1);
00249 err = splt_tu_set_tags_field(state, 0, SPLT_TAGS_ALBUM, album);
00250 if (err < 0) { cdu->error = err; return; }
00251 *slash = '\0';
00252 we_have_album = SPLT_TRUE;
00253 }
00254
00255 char *artist = splt_su_trim_spaces(equal_ptr + 1);
00256 err = splt_tu_set_tags_field(state, 0, SPLT_TAGS_ARTIST, artist);
00257 if (err < 0) { cdu->error = err; return; }
00258
00259 splt_c_put_info_message_to_client(state, _("\n Artist: %s\n"), artist);
00260 if (we_have_album)
00261 {
00262 splt_c_put_info_message_to_client(state, _(" Album: %s\n"),
00263 splt_tu_get_tags_field(state, 0, SPLT_TAGS_ALBUM));
00264 }
00265 }
00266
00267 static void splt_cddb_process_genre_line(char *line_content, cddb_utils *cdu, splt_state *state)
00268 {
00269 char *genre = line_content + 6;
00270 splt_su_cut_spaces_from_end(genre);
00271
00272 if (*genre == '\0')
00273 {
00274 return;
00275 }
00276
00277 int err = splt_tu_set_tags_field(state, 0, SPLT_TAGS_GENRE, genre);
00278 if (err < 0) { cdu->error = err; }
00279 }
00280
00281 static void splt_cddb_process_year_line(const char *line_content,
00282 cddb_utils *cdu, splt_state *state)
00283 {
00284 int err = splt_tu_set_tags_field(state, 0, SPLT_TAGS_YEAR, line_content+5);
00285 if (err < 0) { cdu->error = err; }
00286 }
00287
00288 static void splt_cddb_process_disc_length_line(const char *line_content,
00289 cddb_utils *cdu, splt_state *state)
00290 {
00291 int err = SPLT_OK;
00292
00293 cdu->read_offsets = SPLT_FALSE;
00294
00295 splt_t_set_splitnumber(state, cdu->tracks);
00296 splt_t_clean_split_data(state, cdu->tracks);
00297
00298 double value = splt_su_str_line_to_double(line_content);
00299 err = splt_sp_append_splitpoint(state, value * 100, NULL, SPLT_SPLITPOINT);
00300
00301 if (err < 0) { cdu->error = err; return; }
00302
00303 splt_cddb_convert_points(cdu, state);
00304 }
00305
00306 static void splt_cddb_convert_points(cddb_utils *cdu, splt_state *state)
00307 {
00308 int err = SPLT_OK;
00309
00310 long first_point = splt_sp_get_splitpoint_value(state, 0, &err);
00311 if (err < 0) { cdu->error = err; return; }
00312
00313 int i = 0;
00314 for (i = cdu->tracks - 1; i >= 0; i--)
00315 {
00316 long point = splt_sp_get_splitpoint_value(state, i, &err);
00317 if (err < 0) { cdu->error = err; return; }
00318
00319
00320 long difference = point - first_point;
00321 float value = (float) difference / 75.f;
00322 err = splt_sp_set_splitpoint_value(state, i, (long) ceilf(value));
00323 if (err < 0) { cdu->error = err; return; }
00324 }
00325 }
00326
00327 static void splt_cddb_process_offset_line(const char *line,
00328 cddb_utils *cdu, splt_state *state)
00329 {
00330 if (splt_su_str_line_has_digit(line))
00331 {
00332 int err = SPLT_OK;
00333
00334 double value = splt_su_str_line_to_double(line);
00335 err = splt_sp_append_splitpoint(state, value * 100, NULL, SPLT_SPLITPOINT);
00336
00337 if (err < 0) { cdu->error = err; return; }
00338
00339 cdu->tracks++;
00340 }
00341 }
00342
00343 static cddb_utils *splt_cddb_cdu_new(splt_state *state, int *error)
00344 {
00345 cddb_utils *cdu = malloc(sizeof(cddb_utils));
00346 if (cdu == NULL)
00347 {
00348 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00349 return NULL;
00350 }
00351
00352 cdu->read_offsets = SPLT_FALSE;
00353 cdu->error = SPLT_OK;
00354 cdu->tracks = 0;
00355 cdu->file = NULL;
00356 cdu->field_counter = 0;
00357
00358 return cdu;
00359 }
00360
00361 static void splt_cddb_cdu_free(cddb_utils **cdu)
00362 {
00363 if (!cdu || !*cdu)
00364 {
00365 return;
00366 }
00367
00368 free(*cdu);
00369 *cdu = NULL;
00370 }
00371