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
00027
00028 #include <string.h>
00029
00030 #include "splt.h"
00031 #include "tags_utils.h"
00032
00033 void splt_tu_free_original_tags(splt_state *state)
00034 {
00035 splt_tags *tags = &state->original_tags.tags;
00036
00037 if (tags->year)
00038 {
00039 free(tags->year);
00040 tags->year = NULL;
00041 }
00042 if (tags->artist)
00043 {
00044 free(tags->artist);
00045 tags->artist = NULL;
00046 }
00047 if (tags->album)
00048 {
00049 free(tags->album);
00050 tags->album = NULL;
00051 }
00052 if (tags->title)
00053 {
00054 free(tags->title);
00055 tags->title = NULL;
00056 }
00057 if (tags->comment)
00058 {
00059 free(tags->comment);
00060 tags->comment = NULL;
00061 }
00062 if (tags->genre)
00063 {
00064 free(tags->genre);
00065 tags->genre = NULL;
00066 }
00067 tags->track = -1;
00068
00069 int err = SPLT_OK;
00070 splt_p_clear_original_tags(state, &err);
00071 }
00072
00074 splt_tags *splt_tu_get_tags(splt_state *state, int *tags_number)
00075 {
00076 *tags_number = state->split.real_tagsnumber;
00077 return state->split.tags;
00078 }
00079
00080 void splt_tu_auto_increment_tracknumber(splt_state *state)
00081 {
00082 int current_split = splt_t_get_current_split_file_number(state) - 1;
00083 int old_current_split = current_split;
00084
00085 int remaining_tags_like_x = splt_o_get_int_option(state, SPLT_OPT_ALL_REMAINING_TAGS_LIKE_X);
00086 if (remaining_tags_like_x != -1)
00087 {
00088 if (current_split >= state->split.real_tagsnumber)
00089 {
00090 current_split = remaining_tags_like_x;
00091 }
00092
00093 if (splt_o_get_int_option(state, SPLT_OPT_AUTO_INCREMENT_TRACKNUMBER_TAGS) > 0)
00094 {
00095 if (current_split == remaining_tags_like_x)
00096 {
00097 if ((old_current_split > 0) &&
00098 (old_current_split-1 < state->split.real_tagsnumber) &&
00099 (old_current_split != remaining_tags_like_x))
00100 {
00101 int *prev_track = (int *)splt_tu_get_tags_field(state, old_current_split - 1, SPLT_TAGS_TRACK);
00102 int previous_track = 0;
00103 if (prev_track != NULL)
00104 {
00105 previous_track = *prev_track;
00106 }
00107 splt_tu_set_tags_field(state, remaining_tags_like_x, SPLT_TAGS_TRACK, &previous_track);
00108 }
00109
00110 if (old_current_split != current_split)
00111 {
00112 int tracknumber = 1;
00113 if (splt_tu_tags_exists(state, current_split))
00114 {
00115 int *track = (int *)splt_tu_get_tags_field(state, current_split, SPLT_TAGS_TRACK);
00116 if (track != NULL)
00117 {
00118 tracknumber = *track;
00119 }
00120 }
00121 int new_tracknumber = tracknumber + 1;
00122 splt_tu_set_tags_field(state, current_split, SPLT_TAGS_TRACK, &new_tracknumber);
00123 splt_tu_set_like_x_tags_field(state, SPLT_TAGS_TRACK, &new_tracknumber);
00124 }
00125 }
00126 }
00127 }
00128 }
00129
00130 void splt_tu_get_original_tags(splt_state *state, int *err)
00131 {
00132 if (! splt_io_input_is_stdin(state))
00133 {
00134 splt_tu_free_original_tags(state);
00135 splt_p_set_original_tags(state, err);
00136 }
00137 }
00138
00139 int splt_tu_append_tags(splt_state *state,
00140 const char *title, const char *artist,
00141 const char *album, const char *performer,
00142 const char *year, const char *comment,
00143 int track, const char *genre, int set_original_tags)
00144 {
00145 int error = SPLT_OK;
00146 int old_tagsnumber = state->split.real_tagsnumber;
00147
00148 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_TITLE, title);
00149 if (error != SPLT_OK)
00150 return error;
00151
00152 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_ARTIST, artist);
00153 if (error != SPLT_OK)
00154 return error;
00155
00156 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_ALBUM, album);
00157 if (error != SPLT_OK)
00158 return error;
00159
00160 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_PERFORMER, performer);
00161 if (error != SPLT_OK)
00162 return error;
00163
00164 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_YEAR, year);
00165 if (error != SPLT_OK)
00166 return error;
00167
00168 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_COMMENT, comment);
00169 if (error != SPLT_OK)
00170 return error;
00171
00172 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_TRACK, &track);
00173 if (error != SPLT_OK)
00174 return error;
00175
00176 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_ORIGINAL, &set_original_tags);
00177 if (error != SPLT_OK)
00178 return error;
00179
00180 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_GENRE, genre);
00181 return error;
00182 }
00183
00184 int splt_tu_append_original_tags(splt_state *state)
00185 {
00186 int err = SPLT_OK;
00187
00188 char *new_title = NULL;
00189 char *new_artist = NULL;
00190 char *new_album = NULL;
00191 char *new_year = NULL;
00192 char *new_comment = NULL;
00193 char *new_genre = NULL;
00194
00195 splt_tags *tags = &state->original_tags.tags;
00196
00197 new_title = splt_su_replace_all(tags->title, "@", "@@", &err);
00198 if (err != SPLT_OK) { goto end; }
00199
00200 new_artist = splt_su_replace_all(tags->artist, "@", "@@", &err);
00201 if (err != SPLT_OK) { goto end; }
00202
00203 new_album = splt_su_replace_all(tags->album, "@", "@@", &err);
00204 if (err != SPLT_OK) { goto end; }
00205
00206 new_year = splt_su_replace_all(tags->year, "@", "@@", &err);
00207 if (err != SPLT_OK) { goto end; }
00208
00209 new_comment = splt_su_replace_all(tags->comment, "@", "@@", &err);
00210 if (err != SPLT_OK) { goto end; }
00211
00212 new_genre = splt_su_replace_all(tags->genre, "@", "@@", &err);
00213 if (err != SPLT_OK) { goto end; }
00214
00215 err = splt_tu_append_tags(state, new_title, new_artist, new_album, NULL,
00216 new_year, new_comment, tags->track, new_genre, SPLT_TRUE);
00217
00218 end:
00219 if (new_title) { free(new_title); }
00220 if (new_artist) { free(new_artist); }
00221 if (new_album) { free(new_album); }
00222 if (new_year) { free(new_year); }
00223 if (new_comment) { free(new_comment); }
00224 if (new_genre) { free(new_genre); }
00225
00226 return err;
00227 }
00228
00229 int splt_tu_append_only_non_null_previous_tags(splt_state *state,
00230 const char *title, const char *artist,
00231 const char *album, const char *performer,
00232 const char *year, const char *comment,
00233 int track, const char *genre, int set_original_tags)
00234 {
00235 int error = SPLT_OK;
00236 int old_tagsnumber = state->split.real_tagsnumber-1;
00237
00238 if (old_tagsnumber >= 0)
00239 {
00240 if (title != NULL)
00241 {
00242 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_TITLE, title);
00243 }
00244 if (error != SPLT_OK)
00245 return error;
00246
00247 if (artist != NULL)
00248 {
00249 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_ARTIST, artist);
00250 }
00251 if (error != SPLT_OK)
00252 return error;
00253
00254 if (album != NULL)
00255 {
00256 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_ALBUM, album);
00257 }
00258 if (error != SPLT_OK)
00259 return error;
00260
00261 if (performer != NULL)
00262 {
00263 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_PERFORMER, performer);
00264 }
00265 if (error != SPLT_OK)
00266 return error;
00267
00268 if (year != NULL)
00269 {
00270 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_YEAR, year);
00271 }
00272 if (error != SPLT_OK)
00273 return error;
00274
00275 if (comment != NULL)
00276 {
00277 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_COMMENT, comment);
00278 }
00279 if (error != SPLT_OK)
00280 return error;
00281
00282 if (track != -1)
00283 {
00284 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_TRACK, &track);
00285 }
00286 if (error != SPLT_OK)
00287 return error;
00288
00289 if (set_original_tags != -1)
00290 {
00291 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_ORIGINAL, &set_original_tags);
00292 }
00293 if (error != SPLT_OK)
00294 return error;
00295
00296 if (genre != NULL)
00297 {
00298 error = splt_tu_set_tags_field(state, old_tagsnumber, SPLT_TAGS_GENRE, genre);
00299 }
00300 }
00301
00302 return error;
00303 }
00304
00305 void splt_tu_reset_tags(splt_tags *tags)
00306 {
00307 tags->title = NULL;
00308 tags->artist = NULL;
00309 tags->album = NULL;
00310 tags->performer = NULL;
00311 tags->year = NULL;
00312 tags->comment = NULL;
00313 tags->track = -1;
00314 tags->genre = NULL;
00315 tags->tags_version = 0;
00316 tags->set_original_tags = SPLT_FALSE;
00317 }
00318
00319 splt_tags *splt_tu_new_tags(splt_state *state, int *error)
00320 {
00321 splt_tags *tags = malloc(sizeof(splt_tags));
00322
00323 if (tags == NULL)
00324 {
00325 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00326 return NULL;
00327 }
00328
00329 memset(tags, sizeof(splt_tags), '\0');
00330
00331 splt_tu_reset_tags(tags);
00332
00333 return tags;
00334 }
00335
00336 static void splt_tu_set_empty_tags(splt_state *state, int index)
00337 {
00338 splt_tu_reset_tags(&state->split.tags[index]);
00339 }
00340
00341 int splt_tu_new_tags_if_necessary(splt_state *state, int index)
00342 {
00343 int error = SPLT_OK;
00344
00345 if (!state->split.tags)
00346 {
00347 if ((index > state->split.real_tagsnumber) || (index < 0))
00348 {
00349 splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00350 }
00351 else
00352 {
00353 state->split.tags = splt_tu_new_tags(state, &error);
00354 if (error < 0)
00355 {
00356 return error;
00357 }
00358 else
00359 {
00360 splt_tu_set_empty_tags(state, index);
00361 state->split.real_tagsnumber++;
00362 }
00363 }
00364 }
00365 else
00366 {
00367 if ((index > state->split.real_tagsnumber) || (index < 0))
00368 {
00369 splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00370 }
00371 else
00372 {
00373 if (index == state->split.real_tagsnumber)
00374 {
00375 if ((state->split.tags = realloc(state->split.tags,
00376 sizeof(splt_tags) * (index+1))) == NULL)
00377 {
00378 error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00379 return error;
00380 }
00381 else
00382 {
00383 splt_tu_set_empty_tags(state,index);
00384 state->split.real_tagsnumber++;
00385 }
00386 }
00387 }
00388 }
00389
00390 return error;
00391 }
00392
00393 int splt_tu_tags_exists(splt_state *state, int index)
00394 {
00395 if ((index >= 0) && (index < state->split.real_tagsnumber))
00396 {
00397 return SPLT_TRUE;
00398 }
00399 else
00400 {
00401 return SPLT_FALSE;
00402 }
00403 }
00404
00405 int splt_tu_set_tags_field(splt_state *state, int index,
00406 int tags_field, const void *data)
00407 {
00408 int error = SPLT_OK;
00409
00410 error = splt_tu_new_tags_if_necessary(state,index);
00411 if (error != SPLT_OK) { return error; }
00412
00413 if ((index >= state->split.real_tagsnumber) || (index < 0))
00414 {
00415 error = SPLT_ERROR_INEXISTENT_SPLITPOINT;
00416 splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00417 return error;
00418 }
00419 else
00420 {
00421 splt_tu_set_field_on_tags(&state->split.tags[index], tags_field, data);
00422 }
00423
00424 if (error != SPLT_OK)
00425 {
00426 splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00427 }
00428
00429 return error;
00430 }
00431
00432 int splt_tu_set_like_x_tags_field(splt_state *state, int tags_field, const void *data)
00433 {
00434 return splt_tu_set_field_on_tags(&state->split.tags_like_x, tags_field, data);
00435 }
00436
00437 int splt_tu_set_original_tags_field(splt_state *state, int tags_field, const void *data)
00438 {
00439 return splt_tu_set_field_on_tags(&state->original_tags.tags, tags_field, data);
00440 }
00441
00442 void splt_tu_set_original_tags_data(splt_state *state, void *data)
00443 {
00444 state->original_tags.all_original_tags = data;
00445 }
00446
00447 void *splt_tu_get_original_tags_data(splt_state *state)
00448 {
00449 return state->original_tags.all_original_tags;
00450 }
00451
00452 splt_tags *splt_tu_get_original_tags_tags(splt_state *state)
00453 {
00454 return &state->original_tags.tags;
00455 }
00456
00457 static char *splt_tu_get_replaced_with_tags(const char *word,
00458 const splt_tags *tags, const splt_tags *original_tags,
00459 int track, int *error, int replace_tags_in_tags,
00460 int current_split, splt_state *state)
00461 {
00462 int err = SPLT_OK;
00463
00464 if (current_split == -1)
00465 {
00466 current_split = splt_t_get_current_split_file_number(state) - 1;
00467 }
00468
00469 long mins = -1; long secs = -1; long hundr = -1;
00470 long point_value = splt_sp_get_splitpoint_value(state, current_split, &err);
00471 splt_co_get_mins_secs_hundr(point_value, &mins, &secs, &hundr);
00472 long next_mins = -1; long next_secs = -1; long next_hundr = -1;
00473 long next_point_value = -1;
00474 if (splt_sp_splitpoint_exists(state, current_split + 1))
00475 {
00476 next_point_value = splt_sp_get_splitpoint_value(state, current_split + 1, &err);
00477 long total_time = splt_t_get_total_time(state);
00478 if (total_time > 0 && next_point_value > total_time)
00479 {
00480 next_point_value = total_time;
00481 }
00482 splt_co_get_mins_secs_hundr(next_point_value, &next_mins, &next_secs, &next_hundr);
00483 }
00484 short write_eof = SPLT_FALSE;
00485 if (next_point_value == LONG_MAX)
00486 {
00487 write_eof = SPLT_TRUE;
00488 }
00489
00490 long mMsShH_value = 1;
00491 short eof_written = SPLT_FALSE;
00492
00493 char *word_with_tags = NULL;
00494
00495 if (!replace_tags_in_tags)
00496 {
00497 err = splt_su_copy(word, &word_with_tags);
00498 if (err < 0) { *error = err; }
00499 return word_with_tags;
00500 }
00501
00502 char buffer[256] = { '\0' };
00503
00504 if (word == NULL)
00505 {
00506 return NULL;
00507 }
00508
00509 const splt_tags *tags_to_replace = tags;
00510
00511 int counter = 0;
00512 const char *ptr = NULL;
00513 for (ptr = word; *ptr != '\0'; ptr++)
00514 {
00515 if (*ptr == '@')
00516 {
00517 tags_to_replace = tags;
00518 }
00519 else if (*ptr == '#')
00520 {
00521 tags_to_replace = original_tags;
00522 }
00523
00524 const char *title = tags_to_replace->title;
00525 const char *artist = tags_to_replace->artist;
00526 const char *album= tags_to_replace->album;
00527 const char *performer = tags_to_replace->performer;
00528 const char *year = tags_to_replace->year;
00529 const char *comment = tags_to_replace->comment;
00530 const char *genre = tags_to_replace->genre;
00531 int real_track = tags_to_replace->track;
00532
00533 if (*ptr == '@' || *ptr == '#')
00534 {
00535 err = splt_su_append(&word_with_tags, buffer, counter, NULL);
00536 if (err != SPLT_OK) { goto error; }
00537
00538 memset(buffer, 256, '\0');
00539 counter = 0;
00540
00541 ptr++;
00542
00543 switch (*ptr)
00544 {
00545 case 's':
00546 mMsShH_value = secs;
00547 goto put_value;
00548 case 'S':
00549 mMsShH_value = next_secs;
00550 goto put_value;
00551 case 'm':
00552 mMsShH_value = mins;
00553 goto put_value;
00554 case 'M':
00555 mMsShH_value = next_mins;
00556 goto put_value;
00557 case 'h':
00558 mMsShH_value = hundr;
00559 goto put_value;
00560 case 'H':
00561 mMsShH_value = next_hundr;
00562 put_value:
00563 if (!eof_written)
00564 {
00565 if (write_eof &&
00566 (*ptr == 'S' || *ptr == 'M' || *ptr == 'H'))
00567 {
00568 write_eof = SPLT_FALSE;
00569 eof_written = SPLT_TRUE;
00570
00571 err = splt_su_append_str(&word_with_tags, "EOF", NULL);
00572 if (err != SPLT_OK) { goto error; }
00573 }
00574 else if (mMsShH_value != -1)
00575 {
00576 char temp[6] = { '\0' };
00577 temp[0] = '%';
00578 temp[1] = '0';
00579 char number_of_digits = '2';
00580 if (*ptr == 'M' || *ptr == 'm')
00581 {
00582 number_of_digits = splt_of_get_number_of_digits_from_total_time(state);
00583 }
00584 temp[2] = number_of_digits;
00585 temp[3] = 'l';
00586 temp[4] = 'd';
00587
00588 char mMsShH_str[100] = { '\0' };
00589 snprintf(mMsShH_str, 100, temp, mMsShH_value);
00590
00591 err = splt_su_append_str(&word_with_tags, mMsShH_str, NULL);
00592 if (err != SPLT_OK) { goto error; }
00593 }
00594 }
00595 break;
00596 case 'a':
00597 if (artist != NULL)
00598 {
00599 err = splt_su_append_str(&word_with_tags, artist, NULL);
00600 if (err != SPLT_OK) { goto error; }
00601 }
00602 break;
00603 case 'p':
00604 if (performer != NULL)
00605 {
00606 err = splt_su_append_str(&word_with_tags, performer, NULL);
00607 if (err != SPLT_OK) { goto error; }
00608 }
00609 break;
00610 case 'b':
00611 if (album != NULL)
00612 {
00613 err = splt_su_append_str(&word_with_tags, album, NULL);
00614 if (err != SPLT_OK) { goto error; }
00615 }
00616 break;
00617 case 't':
00618 if (title != NULL)
00619 {
00620 err = splt_su_append_str(&word_with_tags, title, NULL);
00621 if (err != SPLT_OK) { goto error; }
00622 }
00623 break;
00624 case 'c':
00625 if (comment != NULL)
00626 {
00627 err = splt_su_append_str(&word_with_tags, comment, NULL);
00628 if (err != SPLT_OK) { goto error; }
00629 }
00630 break;
00631 case 'g':
00632 if (genre != NULL)
00633 {
00634 err = splt_su_append_str(&word_with_tags, genre, NULL);
00635 if (err != SPLT_OK) { goto error; }
00636 }
00637 break;
00638 case 'y':
00639 if (year != NULL)
00640 {
00641 err = splt_su_append(&word_with_tags, year, NULL);
00642 if (err != SPLT_OK) { goto error; }
00643 }
00644 break;
00645 case 'N':
00646 case 'n':
00647 ;
00648 int track_to_set = track;
00649
00650 if (*ptr == 'n')
00651 {
00652 track_to_set = real_track;
00653 }
00654
00655 char temp[5] = { '\0' };
00656 temp[0] = '%';
00657 temp[1] = '0';
00658 temp[2] = splt_of_get_oformat_number_of_digits_as_char(state);
00659 temp[3] = 'd';
00660
00661 char track_str[10] = { '\0' };
00662 snprintf(track_str, 10, temp, track_to_set);
00663
00664 err = splt_su_append_str(&word_with_tags, track_str, NULL);
00665 if (err != SPLT_OK) { goto error; }
00666 break;
00667 case '@':
00668 err = splt_su_append_str(&word_with_tags, "@", NULL);
00669 if (err != SPLT_OK) { goto error; }
00670 break;
00671 default:
00672 err = splt_su_append(&word_with_tags, (ptr-1), 2, NULL);
00673 if (err != SPLT_OK) { goto error; }
00674 break;
00675 }
00676 }
00677 else
00678 {
00679 buffer[counter] = *ptr;
00680 counter++;
00681
00682 if (counter == 255)
00683 {
00684 err = splt_su_append(&word_with_tags, buffer, counter, NULL);
00685 if (err != SPLT_OK) { goto error; }
00686 memset(buffer, 256, '\0');
00687 counter = 0;
00688 }
00689 }
00690 }
00691
00692 err = splt_su_append(&word_with_tags, buffer, counter, NULL);
00693 if (err != SPLT_OK) { goto error; }
00694
00695 return word_with_tags;
00696
00697 error:
00698 if (word_with_tags)
00699 {
00700 free(word_with_tags);
00701 }
00702
00703 *error = err;
00704
00705 return NULL;
00706 }
00707
00708 void splt_tu_free_one_tags(splt_tags **tags)
00709 {
00710 if (!tags || !*tags)
00711 {
00712 return;
00713 }
00714
00715 splt_tu_free_one_tags_content(*tags);
00716
00717 free(*tags);
00718 *tags = NULL;
00719 }
00720
00721 void splt_tu_free_one_tags_content(splt_tags *tags)
00722 {
00723 if (tags)
00724 {
00725 if (tags->title)
00726 {
00727 free(tags->title);
00728 tags->title = NULL;
00729 }
00730 if (tags->artist)
00731 {
00732 free(tags->artist);
00733 tags->artist = NULL;
00734 }
00735 if (tags->album)
00736 {
00737 free(tags->album);
00738 tags->album = NULL;
00739 }
00740 if (tags->performer)
00741 {
00742 free(tags->performer);
00743 tags->performer = NULL;
00744 }
00745 if (tags->year)
00746 {
00747 free(tags->year);
00748 tags->year = NULL;
00749 }
00750 if (tags->comment)
00751 {
00752 free(tags->comment);
00753 tags->comment = NULL;
00754 }
00755 if (tags->genre)
00756 {
00757 free(tags->genre);
00758 tags->genre = NULL;
00759 }
00760
00761 tags->track = -1;
00762 }
00763 }
00764
00765 void splt_tu_append_tags_to_state(splt_state *state, splt_tags *tags,
00766 int append_new_tags, int *error)
00767 {
00768 int err = SPLT_OK;
00769
00770 if (append_new_tags)
00771 {
00772 err = splt_tu_append_tags(state, tags->title, tags->artist, tags->album,
00773 tags->performer, tags->year, tags->comment, tags->track, tags->genre,
00774 SPLT_FALSE);
00775 }
00776 else
00777 {
00778 err = splt_tu_append_only_non_null_previous_tags(state, tags->title,
00779 tags->artist, tags->album, tags->performer, tags->year,
00780 tags->comment, tags->track, tags->genre,
00781 SPLT_TRUE);
00782 }
00783
00784 if (err < 0)
00785 {
00786 *error = err;
00787 }
00788 }
00789
00790 void splt_tu_set_new_tags_where_current_tags_are_null(splt_state *state,
00791 splt_tags *current_tags, splt_tags *new_tags,
00792 int index, int *error)
00793 {
00794 if (!current_tags->title)
00795 {
00796 splt_tu_set_tags_field(state, index, SPLT_TAGS_TITLE, new_tags->title);
00797 }
00798 if (!current_tags->artist)
00799 {
00800 splt_tu_set_tags_field(state, index, SPLT_TAGS_ARTIST, new_tags->artist);
00801 }
00802 if (!current_tags->album)
00803 {
00804 splt_tu_set_tags_field(state, index, SPLT_TAGS_ALBUM, new_tags->album);
00805 }
00806 if (!current_tags->performer)
00807 {
00808 splt_tu_set_tags_field(state, index, SPLT_TAGS_PERFORMER,
00809 new_tags->performer);
00810 }
00811 if (!current_tags->year)
00812 {
00813 splt_tu_set_tags_field(state, index, SPLT_TAGS_YEAR, new_tags->year);
00814 }
00815 if (!current_tags->comment)
00816 {
00817 splt_tu_set_tags_field(state, index, SPLT_TAGS_COMMENT, new_tags->comment);
00818 }
00819 if (current_tags->track < 0)
00820 {
00821 splt_tu_set_tags_field(state, index, SPLT_TAGS_TRACK, &new_tags->track);
00822 }
00823 if (!current_tags->genre)
00824 {
00825 splt_tu_set_tags_field(state, index, SPLT_TAGS_GENRE, new_tags->genre);
00826 }
00827 splt_tu_set_tags_field(state, index, SPLT_TAGS_ORIGINAL, &new_tags->set_original_tags);
00828 }
00829
00830 int splt_tu_has_one_tag_set(splt_tags *tags)
00831 {
00832 if (tags->title != NULL ||
00833 tags->artist != NULL ||
00834 tags->album != NULL ||
00835 tags->performer != NULL ||
00836 tags->year != NULL ||
00837 tags->comment != NULL ||
00838 tags->track != -1 ||
00839 tags->genre != NULL)
00840 {
00841 return SPLT_TRUE;
00842 }
00843
00844 return SPLT_FALSE;
00845 }
00846
00847 void splt_tu_copy_tags(splt_tags *from, splt_tags *to, int *error)
00848 {
00849 int err = SPLT_OK;
00850
00851 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_TITLE, from->title);
00852 if (err < 0) { goto error; }
00853
00854 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_ARTIST, from->artist);
00855 if (err < 0) { goto error; }
00856
00857 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_ALBUM, from->album);
00858 if (err < 0) { goto error; }
00859
00860 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_YEAR, from->year);
00861 if (err < 0) { goto error; }
00862
00863 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_COMMENT, from->comment);
00864 if (err < 0) { goto error; }
00865
00866 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_PERFORMER, from->performer);
00867 if (err < 0) { goto error; }
00868
00869 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_TRACK, &from->track);
00870 if (err < 0) { goto error; }
00871
00872 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_ORIGINAL, &from->set_original_tags);
00873 if (err < 0) { goto error; }
00874
00875 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_GENRE, from->genre);
00876 if (err < 0) { goto error; }
00877
00878 err = splt_tu_set_field_on_tags(to, SPLT_TAGS_VERSION, &from->tags_version);
00879 if (err < 0) { goto error; }
00880
00881 return;
00882
00883 error:
00884 *error = err;
00885 }
00886
00887 static splt_tags *splt_tu_get_tags_to_replace_in_tags(splt_state *state)
00888 {
00889 int current_tags_number = splt_t_get_current_split_file_number(state) - 1;
00890
00891 int remaining_tags_like_x = splt_o_get_int_option(state, SPLT_OPT_ALL_REMAINING_TAGS_LIKE_X);
00892
00893 if ((current_tags_number >= state->split.real_tagsnumber) &&
00894 (remaining_tags_like_x != -1))
00895 {
00896 return splt_tu_get_tags_like_x(state);
00897 }
00898
00899 return splt_tu_get_tags_at(state, current_tags_number);
00900 }
00901
00902 int splt_tu_set_tags_in_tags(splt_state *state, int current_split)
00903 {
00904 int err = SPLT_OK;
00905
00906 splt_tags *tags = splt_tu_get_tags_to_replace_in_tags(state);
00907 splt_tags *cur_tags = splt_tu_get_current_tags(state);
00908
00909 if (!tags || !cur_tags)
00910 {
00911 return err;
00912 }
00913
00914 int track = -1;
00915 if (tags->track > 0)
00916 {
00917 track = tags->track;
00918 }
00919 else if (splt_tu_has_one_tag_set(tags))
00920 {
00921 if (current_split != -1)
00922 {
00923 track = current_split + 1;
00924 }
00925 else
00926 {
00927 track = splt_t_get_current_split_file_number(state);
00928 }
00929 }
00930
00931 cur_tags->track = track;
00932 cur_tags->tags_version = tags->tags_version;
00933
00934 int replace_tags_in_tags = splt_o_get_int_option(state, SPLT_OPT_REPLACE_TAGS_IN_TAGS);
00935
00936 splt_tags *original_tags = splt_tu_get_original_tags_tags(state);
00937
00938 char *t = splt_tu_get_replaced_with_tags(tags->title, tags, original_tags,
00939 track, &err, replace_tags_in_tags, current_split, state);
00940 if (err != SPLT_OK) { return err; }
00941 char *y = splt_tu_get_replaced_with_tags(tags->year, tags, original_tags, track, &err, replace_tags_in_tags,
00942 current_split, state);
00943 if (err != SPLT_OK) { return err; }
00944 char *a = splt_tu_get_replaced_with_tags(tags->artist, tags, original_tags, track, &err, replace_tags_in_tags,
00945 current_split, state);
00946 if (err != SPLT_OK) { return err; }
00947 char *al = splt_tu_get_replaced_with_tags(tags->album, tags, original_tags, track, &err, replace_tags_in_tags,
00948 current_split, state);
00949 if (err != SPLT_OK) { return err; }
00950 char *c = splt_tu_get_replaced_with_tags(tags->comment, tags, original_tags, track, &err, replace_tags_in_tags,
00951 current_split, state);
00952 if (err != SPLT_OK) { return err; }
00953 char *g = splt_tu_get_replaced_with_tags(tags->genre, tags, original_tags, track, &err, replace_tags_in_tags,
00954 current_split, state);
00955 if (err != SPLT_OK) { return err; }
00956
00957 splt_su_free_replace(&cur_tags->title, t);
00958 splt_su_free_replace(&cur_tags->year, y);
00959 splt_su_free_replace(&cur_tags->artist, a);
00960 splt_su_free_replace(&cur_tags->album, al);
00961 splt_su_free_replace(&cur_tags->comment, c);
00962 splt_su_free_replace(&cur_tags->genre, g);
00963
00964 return err;
00965 }
00966
00967 splt_tags *splt_tu_get_tags_at(splt_state *state, int tags_index)
00968 {
00969 if (!splt_tu_tags_exists(state, tags_index))
00970 {
00971 return NULL;
00972 }
00973
00974 return &state->split.tags[tags_index];
00975 }
00976
00977 splt_tags splt_tu_get_last_tags(splt_state *state)
00978 {
00979 return state->split.tags[state->split.real_tagsnumber-1];
00980 }
00981
00982 void *splt_tu_get_tags_field(splt_state *state, int index, int tags_field)
00983 {
00984 if ((index >= state->split.real_tagsnumber) || (index < 0))
00985 {
00986 splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
00987 return NULL;
00988 }
00989 else
00990 {
00991 switch(tags_field)
00992 {
00993 case SPLT_TAGS_TITLE:
00994 return state->split.tags[index].title;
00995 break;
00996 case SPLT_TAGS_ARTIST:
00997 return state->split.tags[index].artist;
00998 break;
00999 case SPLT_TAGS_ALBUM:
01000 return state->split.tags[index].album;
01001 break;
01002 case SPLT_TAGS_YEAR:
01003 return state->split.tags[index].year;
01004 break;
01005 case SPLT_TAGS_COMMENT:
01006 return state->split.tags[index].comment;
01007 break;
01008 case SPLT_TAGS_PERFORMER:
01009 return state->split.tags[index].performer;
01010 break;
01011 case SPLT_TAGS_TRACK:
01012 return &state->split.tags[index].track;
01013 break;
01014 case SPLT_TAGS_VERSION:
01015 return &state->split.tags[index].tags_version;
01016 break;
01017 case SPLT_TAGS_GENRE:
01018 return state->split.tags[index].genre;
01019 break;
01020 case SPLT_TAGS_ORIGINAL:
01021 return &state->split.tags[index].set_original_tags;
01022 break;
01023 default:
01024 splt_e_error(SPLT_IERROR_INT,__func__, index, NULL);
01025 return NULL;
01026 }
01027 }
01028
01029 return NULL;
01030 }
01031
01032 splt_tags *splt_tu_get_tags_like_x(splt_state *state)
01033 {
01034 return &state->split.tags_like_x;
01035 }
01036
01037 void splt_tu_free_tags(splt_state *state)
01038 {
01039 if (state->split.tags)
01040 {
01041 int i = 0;
01042 for (i = 0; i < state->split.real_tagsnumber; i++)
01043 {
01044 splt_tu_free_one_tags_content(&state->split.tags[i]);
01045 }
01046 free(state->split.tags);
01047 state->split.tags = NULL;
01048 }
01049
01050 state->split.real_tagsnumber = 0;
01051
01052 splt_tu_free_one_tags_content(splt_tu_get_tags_like_x(state));
01053 }
01054
01055 splt_tags *splt_tu_get_current_tags(splt_state *state)
01056 {
01057 int current_tags_number = splt_t_get_current_split_file_number(state) - 1;
01058
01059 int remaining_tags_like_x = splt_o_get_int_option(state, SPLT_OPT_ALL_REMAINING_TAGS_LIKE_X);
01060 if ((current_tags_number >= state->split.real_tagsnumber) &&
01061 (remaining_tags_like_x != -1))
01062 {
01063 current_tags_number = remaining_tags_like_x;
01064 }
01065
01066 return splt_tu_get_tags_at(state, current_tags_number);
01067 }
01068
01069 char *splt_tu_get_artist_or_performer_ptr(splt_tags *tags)
01070 {
01071 if (!tags)
01072 {
01073 return NULL;
01074 }
01075
01076 char *artist_or_performer = tags->artist;
01077
01078 if (tags->performer == NULL)
01079 {
01080 return artist_or_performer;
01081 }
01082
01083 if (tags->performer[0] != '\0')
01084 {
01085 return tags->performer;
01086 }
01087
01088 return artist_or_performer;
01089 }
01090
01091 int splt_tu_copy_first_common_tags_on_all_tracks(splt_state *state, int tracks)
01092 {
01093 int err = SPLT_OK;
01094
01095 char *artist0 = NULL;
01096 char *album0 = NULL;
01097 char *year0 = NULL;
01098 char *genre0 = NULL;
01099
01100 char *first_artist = (char *)splt_tu_get_tags_field(state, 0, SPLT_TAGS_ARTIST);
01101 err = splt_su_copy(first_artist, &artist0);
01102 if (err < 0) { goto function_end; }
01103
01104 char *first_album = (char *)splt_tu_get_tags_field(state, 0, SPLT_TAGS_ALBUM);
01105 err = splt_su_copy(first_album, &album0);
01106 if (err < 0) { goto function_end; }
01107
01108 char *first_year = (char *)splt_tu_get_tags_field(state, 0, SPLT_TAGS_YEAR);
01109 err = splt_su_copy(first_year, &year0);
01110 if (err < 0) { goto function_end; }
01111
01112 char *first_genre = (char *)splt_tu_get_tags_field(state, 0, SPLT_TAGS_GENRE);
01113 err = splt_su_copy(first_genre, &genre0);
01114 if (err < 0) { goto function_end; }
01115
01116 int i = 0;
01117 for (i = 0; i < tracks;i++)
01118 {
01119 if (i != 0)
01120 {
01121 err = splt_tu_set_tags_field(state, i, SPLT_TAGS_ARTIST, artist0);
01122 if (err != SPLT_OK) { break; }
01123
01124 err = splt_tu_set_tags_field(state, i, SPLT_TAGS_ALBUM, album0);
01125 if (err != SPLT_OK) { break; }
01126
01127 err = splt_tu_set_tags_field(state, i, SPLT_TAGS_YEAR, year0);
01128 if (err != SPLT_OK) { break; }
01129
01130 err = splt_tu_set_tags_field(state, i, SPLT_TAGS_GENRE, genre0);
01131 if (err != SPLT_OK) { break; }
01132 }
01133 }
01134
01135 function_end:
01136 if (artist0)
01137 {
01138 free(artist0);
01139 artist0 = NULL;
01140 }
01141 if (album0)
01142 {
01143 free(album0);
01144 album0 = NULL;
01145 }
01146 if (year0)
01147 {
01148 free(year0);
01149 year0 = NULL;
01150 }
01151 if (genre0)
01152 {
01153 free(genre0);
01154 genre0 = NULL;
01155 }
01156
01157 return err;
01158 }
01159
01160 int splt_tu_set_field_on_tags(splt_tags *tags, int tags_field, const void *data)
01161 {
01162 int err = SPLT_OK;
01163
01164 switch (tags_field)
01165 {
01166 case SPLT_TAGS_TITLE:
01167 err = splt_su_copy((char *)data, &tags->title);
01168 break;
01169 case SPLT_TAGS_ARTIST:
01170 err = splt_su_copy((char *)data, &tags->artist);
01171 break;
01172 case SPLT_TAGS_ALBUM:
01173 err = splt_su_copy((char *)data, &tags->album);
01174 break;
01175 case SPLT_TAGS_YEAR:
01176 err = splt_su_copy((char *)data, &tags->year);
01177 break;
01178 case SPLT_TAGS_COMMENT:
01179 err = splt_su_copy((char *)data, &tags->comment);
01180 break;
01181 case SPLT_TAGS_PERFORMER:
01182 err = splt_su_copy((char *)data, &tags->performer);
01183 break;
01184 case SPLT_TAGS_TRACK:
01185 tags->track = *((int *)data);
01186 break;
01187 case SPLT_TAGS_GENRE:
01188 err = splt_su_copy((char *)data, &tags->genre);
01189 break;
01190 case SPLT_TAGS_VERSION:
01191 tags->tags_version = *((int *)data);
01192 break;
01193 case SPLT_TAGS_ORIGINAL:
01194 tags->set_original_tags = *((int *)data);
01195 break;
01196 default:
01197 splt_e_error(SPLT_IERROR_INT,__func__, -500, NULL);
01198 break;
01199 }
01200
01201 return err;
01202 }
01203
01204