libmp3splt
src/oformat_parser.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * libmp3splt -- library based on mp3splt,
00004  *               for mp3/ogg splitting without decoding
00005  *
00006  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
00007  * Copyright (c) 2005-2011 Alexandru Munteanu - io_fx@yahoo.fr
00008  *
00009  * http://mp3splt.sourceforge.net
00010  *
00011  *********************************************************/
00012 
00013 /**********************************************************
00014  *
00015  * This program is free software; you can redistribute it and/or
00016  * modify it under the terms of the GNU General Public License
00017  * as published by the Free Software Foundation; either version 2
00018  * of the License, or (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00028  * 02111-1307,
00029  * USA.
00030  *
00031  *********************************************************/
00032 
00037 #include <string.h>
00038 #include <ctype.h>
00039 #include <math.h>
00040 
00041 #include "splt.h"
00042 
00051 static short splt_u_output_variable_is_valid(char v, int *amb)
00052 {
00053   switch (v)
00054   {
00055     case 's':
00056     case 'S':
00057     case 'm':
00058     case 'M':
00059     case 'h':
00060     case 'H':
00061     case 'a':
00062     case 'A':
00063     case 'b':
00064     case 'f':
00065     case 'g':
00066     case 'p':
00067       break;
00068     case 't':
00069     case 'l':
00070     case 'L':
00071     case 'u':
00072     case 'U':
00073     case 'n':
00074     case 'N':
00075       *amb = SPLT_OUTPUT_FORMAT_OK;
00076       break;
00077     default:
00078       return SPLT_FALSE;
00079   }
00080 
00081   return SPLT_TRUE;
00082 }
00083 
00084 int splt_of_parse_outformat(char *s, splt_state *state)
00085 {
00086   char *ptrs = NULL, *ptre = NULL;
00087   int i=0, amb = SPLT_OUTPUT_FORMAT_AMBIGUOUS, len=0;
00088 
00089   size_t size = strlen(s);
00090   for (i = 0; i < size; i++)
00091   {
00092     if (s[i]=='+') 
00093     {
00094       s[i]=' ';
00095     }
00096     else 
00097     {
00098       if (s[i] == SPLT_VARCHAR) 
00099       {
00100         s[i]='%';
00101       }
00102     }
00103   }
00104 
00105   ptrs = s;
00106   i = 0;
00107   ptre = strchr(ptrs+1, '%');
00108   if (s[0] != '%')
00109   {
00110     if (ptre==NULL)
00111     {
00112       len = strlen(ptrs);
00113     }
00114     else
00115     {
00116       len = ptre-ptrs;
00117     }
00118     if (len > SPLT_MAXOLEN)
00119     {
00120       len = SPLT_MAXOLEN;
00121     }
00122     strncpy(state->oformat.format[i++], ptrs, len);
00123   }
00124   else
00125   {
00126     ptre = s;
00127   }
00128 
00129   //if stdout, NOT ambiguous
00130   if (splt_io_input_is_stdout(state))
00131   {
00132     return SPLT_OUTPUT_FORMAT_OK;
00133   }
00134 
00135   char err[2] = { '\0' };
00136 
00137   if (ptre == NULL)
00138   {
00139     splt_e_set_error_data(state, err);
00140     return SPLT_OUTPUT_FORMAT_AMBIGUOUS;
00141   }
00142   ptrs = ptre;
00143 
00144   char *last_ptre = NULL;
00145   while (((ptre = strchr(ptrs+1, '%')) != NULL) && (i < SPLT_OUTNUM))
00146   {
00147     char cf = *(ptrs+1);
00148 
00149     len = ptre-ptrs;
00150     if (len > SPLT_MAXOLEN)
00151     {
00152       len = SPLT_MAXOLEN;
00153     }
00154 
00155     if (!splt_u_output_variable_is_valid(cf, &amb))
00156     {
00157       err[0] = cf;
00158       splt_e_set_error_data(state, err);
00159       return SPLT_OUTPUT_FORMAT_ERROR;
00160     }
00161 
00162     strncpy(state->oformat.format[i++], ptrs, len);
00163     ptrs = ptre;
00164     last_ptre = ptre;
00165   }
00166 
00167   if (last_ptre && *last_ptre != '\0')
00168   {
00169     char v = *(last_ptre+1);
00170     if (!splt_u_output_variable_is_valid(v, &amb))
00171     {
00172       err[0] = v;
00173       splt_e_set_error_data(state, err);
00174       return SPLT_OUTPUT_FORMAT_ERROR;
00175     }
00176   }
00177 
00178   strncpy(state->oformat.format[i], ptrs, strlen(ptrs));
00179 
00180   if (ptrs[1]=='t')
00181   {
00182     amb = SPLT_OUTPUT_FORMAT_OK;
00183   }
00184 
00185   if (ptrs[1]=='n')
00186   {
00187     amb = SPLT_OUTPUT_FORMAT_OK;
00188   }
00189 
00190   return amb;
00191 }
00192 
00193 static const char *splt_u_get_format_ptr(const char *format, char *temp)
00194 {
00195   int format_length = strlen(format);
00196   const char *format_ptr = format;
00197 
00198   if ((format_length > 2) && isdigit(format[2]))
00199   {
00200     temp[2] = format[2];
00201     format_ptr = format + 1;
00202   }
00203 
00204   return format_ptr;
00205 }
00206 
00207 static int splt_u_get_requested_num_of_digits(splt_state *state, const char *format,
00208     int *requested_num_of_digits, int is_alpha)
00209 {
00210   int format_length = strlen(format);
00211   int number_of_digits = 0;
00212   if (is_alpha)
00213   {
00214     number_of_digits = state->oformat.output_alpha_format_digits;
00215   }
00216   else
00217   {
00218     number_of_digits = splt_of_get_oformat_number_of_digits_as_int(state);
00219   }
00220   int max_number_of_digits = number_of_digits;
00221   *requested_num_of_digits = number_of_digits;
00222 
00223   if ((format_length > 2) && isdigit(format[2]))
00224   {
00225     *requested_num_of_digits = format[2] - '0';
00226   }
00227 
00228   if (*requested_num_of_digits > number_of_digits)
00229   {
00230     max_number_of_digits = *requested_num_of_digits;
00231   }
00232 
00233   return max_number_of_digits;
00234 }
00235 
00249 static void splt_u_alpha_track(splt_state *state, int nfield,
00250     char *fm, int fm_length, int number_of_digits, int tracknumber)
00251 {
00252   char *format = state->oformat.format[nfield];
00253   int lowercase = (toupper(format[1]) == 'L');
00254   char a = lowercase ? 'a' : 'A';
00255   int zerobased = tracknumber - 1;
00256   int i = 1, min_digits = state->oformat.output_alpha_format_digits;
00257 
00258   if (number_of_digits > 1)
00259   {
00260     /* Padding required => simple base-26 encoding */
00261     if (number_of_digits < min_digits)
00262       number_of_digits = min_digits;
00263     for (i = 1; i <= number_of_digits; ++ i, zerobased /= 26)
00264     {
00265       int digit = (zerobased % 26);
00266       fm[number_of_digits - i] = a + digit;
00267     }
00268   }
00269   else
00270   {
00271     /* No padding: First letter base-26, others base-27 */
00272     number_of_digits = min_digits;
00273 
00274     /* Start with the first, base-26 'digit' */
00275     fm[number_of_digits - 1] = a + (zerobased % 26);
00276 
00277     /* Now handle all other digits */
00278     zerobased /= 26;
00279     for (i = 2; i <= number_of_digits; ++ i, zerobased /= 27)
00280     {
00281       int digit = (zerobased % 27);
00282       fm[number_of_digits - i] = a + digit - 1;
00283     }
00284   }
00285 
00286   int offset = 0;
00287   if ((strlen(format) > 2) && isdigit(format[2]))
00288   {
00289     offset = 1;
00290   }
00291   snprintf(fm + number_of_digits, fm_length - number_of_digits,
00292       "%s", format + 2 + offset);
00293 }
00294 
00295 static char splt_of_get_number_of_digits_from_total_time(splt_state *state)
00296 {
00297   long total_time = splt_t_get_total_time(state);
00298   if (total_time > 0)
00299   {
00300     long minutes = total_time / 100 / 60;
00301     int i = (int) (log10l((long double) minutes));
00302     char number_of_digits = (char) (i + '1');
00303     if (number_of_digits == '1')
00304     {
00305       return '2';
00306     }
00307 
00308     return number_of_digits;
00309   }
00310 
00311   return '2';
00312 }
00313 
00323 int splt_of_put_output_format_filename(splt_state *state, int current_split)
00324 {
00325   int error = SPLT_OK;
00326 
00327   int output_filenames = splt_o_get_int_option(state, SPLT_OPT_OUTPUT_FILENAMES);
00328   if (output_filenames == SPLT_OUTPUT_CUSTOM)
00329   {
00330     return error;
00331   }
00332 
00333   char *temp = NULL;
00334   char *fm = NULL;
00335   int i = 0;
00336   char *output_filename = NULL;
00337   int output_filename_size = 0;
00338 
00339   char *title = NULL;
00340   char *artist = NULL;
00341   char *album = NULL;
00342   char *genre = NULL;
00343   char *performer = NULL;
00344   char *artist_or_performer = NULL;
00345   char *original_filename = NULL;
00346 
00347   int split_file_number = splt_t_get_current_split_file_number(state);
00348   int tags_index = split_file_number - 1;
00349 
00350   if (current_split == -1)
00351   {
00352     current_split = splt_t_get_current_split_file_number(state) - 1;
00353   }
00354 
00355   long mins = -1;
00356   long secs = -1;
00357   long hundr = -1;
00358   long point_value = splt_sp_get_splitpoint_value(state, current_split, &error);
00359   splt_co_get_mins_secs_hundr(point_value, &mins, &secs, &hundr);
00360   long next_mins = -1;
00361   long next_secs = -1;
00362   long next_hundr = -1;
00363   long next_point_value = -1;
00364   if (splt_sp_splitpoint_exists(state, current_split + 1))
00365   {
00366     next_point_value = splt_sp_get_splitpoint_value(state, current_split + 1, &error);
00367     long total_time = splt_t_get_total_time(state);
00368     if (total_time > 0 && next_point_value > total_time)
00369     {
00370       next_point_value = total_time;
00371     }
00372     splt_co_get_mins_secs_hundr(next_point_value, &next_mins, &next_secs, &next_hundr);
00373   }
00374 
00375   int fm_length = 0;
00376 
00377   //if we get the tags from the first file
00378   int remaining_tags_like_x = 
00379     splt_o_get_int_option(state,SPLT_OPT_ALL_REMAINING_TAGS_LIKE_X);
00380   if ((tags_index >= state->split.real_tagsnumber) &&
00381       (remaining_tags_like_x != -1))
00382   {
00383     tags_index = remaining_tags_like_x;
00384   }
00385 
00386   const char *output_format = splt_of_get_oformat(state);
00387   short write_eof = SPLT_FALSE;
00388   if ((next_point_value == LONG_MAX) &&
00389       (strcmp(output_format, SPLT_DEFAULT_OUTPUT) == 0))
00390   {
00391     write_eof = SPLT_TRUE;
00392   }
00393 
00394   splt_d_print_debug(state,"The output format is _%s_\n", output_format);
00395 
00396   long mMsShH_value = -1;
00397   short eof_written = SPLT_FALSE;
00398 
00399   for (i = 0; i < SPLT_OUTNUM; i++)
00400   {
00401     if (strlen(state->oformat.format[i]) == 0)
00402     {
00403       break;
00404     }
00405     //if we have some % in the format (@ has been converted to %)
00406     if (state->oformat.format[i][0] == '%')
00407     {
00408       //we allocate memory for the temp variable
00409       if (temp)
00410       {
00411         free(temp);
00412         temp = NULL;
00413       }
00414 
00415       int temp_len = strlen(state->oformat.format[i])+10;
00416       if ((temp = malloc(temp_len * sizeof(char))) == NULL)
00417       {
00418         error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00419         goto end;
00420       }
00421       memset(temp, 0x0, temp_len);
00422 
00423       temp[0] = '%';
00424       temp[1] = 's';
00425       char char_variable = state->oformat.format[i][1];
00426       switch (char_variable)
00427       {
00428         case 's':
00429           mMsShH_value = secs;
00430           goto put_value;
00431         case 'S':
00432           mMsShH_value = next_secs;
00433           goto put_value;
00434         case 'm':
00435           mMsShH_value = mins;
00436           goto put_value;
00437         case 'M':
00438           mMsShH_value = next_mins;
00439           goto put_value;
00440         case 'h':
00441           mMsShH_value = hundr;
00442           goto put_value;
00443         case 'H':
00444           mMsShH_value = next_hundr;
00445 put_value:
00446           if (!eof_written)
00447           {
00448             if (write_eof &&
00449                 (char_variable == 'S' ||
00450                  char_variable == 'M' ||
00451                  char_variable == 'H'))
00452             {
00453               write_eof = SPLT_FALSE;
00454               eof_written = SPLT_TRUE;
00455 
00456               fm_length = strlen(temp) + 4;
00457               if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00458               {
00459                 error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00460                 goto end;
00461               }
00462               snprintf(fm, fm_length, temp, "EOF");
00463             }
00464             else if (mMsShH_value != -1)
00465             {
00466               temp[1] = '0';
00467               char number_of_digits = '2';
00468               if (char_variable == 'M' || char_variable == 'm')
00469               {
00470                 number_of_digits = splt_of_get_number_of_digits_from_total_time(state);
00471               }
00472               temp[2] = number_of_digits;
00473               temp[3] = 'l';
00474               temp[4] = 'd';
00475 
00476               const char *format = NULL;
00477               int offset = 5;
00478 
00479               //don't print out @h or @H if 0 for default output
00480               if ((strcmp(state->oformat.format_string, SPLT_DEFAULT_OUTPUT) == 0) &&
00481                   (mMsShH_value == 0) &&
00482                   (char_variable == 'h' || char_variable == 'H'))
00483               {
00484                 if (char_variable == 'h')
00485                 {
00486                   format = state->oformat.format[i]+2;
00487                   offset = 0;
00488                 }
00489                 else
00490                 {
00491                   output_filename[strlen(output_filename)-1] = '\0';
00492                   break;
00493                 }
00494               }
00495               else
00496               {
00497                 format = splt_u_get_format_ptr(state->oformat.format[i], temp);
00498               }
00499 
00500               int requested_num_of_digits = 0;
00501               int max_number_of_digits = splt_u_get_requested_num_of_digits(state,
00502                   state->oformat.format[i], &requested_num_of_digits, SPLT_FALSE);
00503 
00504               snprintf(temp + offset, temp_len, format + 2);
00505 
00506               fm_length = strlen(temp) + 1 + max_number_of_digits;
00507               if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00508               {
00509                 error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00510                 goto end;
00511               }
00512 
00513               snprintf(fm, fm_length, temp, mMsShH_value);
00514             }
00515           }
00516           break;
00517         case 'A':
00518           if (splt_tu_tags_exists(state,tags_index))
00519           {
00520             artist_or_performer =
00521               (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_PERFORMER);
00522             splt_su_clean_string(state, artist_or_performer, &error);
00523             if (error < 0) { goto end; };
00524 
00525             if (artist_or_performer == NULL || artist_or_performer[0] == '\0')
00526             {
00527               artist_or_performer = 
00528                 (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_ARTIST);
00529               splt_su_clean_string(state, artist_or_performer, &error);
00530               if (error < 0) { goto end; };
00531             }
00532           }
00533           else
00534           {
00535             artist_or_performer = NULL;
00536           }
00537 
00538           //
00539           if (artist_or_performer != NULL)
00540           {
00541             snprintf(temp+2,temp_len, state->oformat.format[i]+2);
00542 
00543             int artist_length = 0;
00544             artist_length = strlen(artist_or_performer);
00545             fm_length = strlen(temp) + artist_length + 1;
00546           }
00547           else
00548           {
00549             snprintf(temp,temp_len, state->oformat.format[i]+2);
00550             fm_length = strlen(temp) + 1;
00551           }
00552 
00553           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00554           {
00555             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00556             goto end;
00557           }
00558 
00559           //
00560           if (artist_or_performer != NULL)
00561           {
00562             snprintf(fm, fm_length, temp, artist_or_performer);
00563           }
00564           else
00565           {
00566             snprintf(fm, fm_length, temp);
00567           }
00568 
00569           break;
00570         case 'a':
00571           if (splt_tu_tags_exists(state,tags_index))
00572           {
00573             //we get the artist
00574             artist =
00575               (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_ARTIST);
00576             splt_su_clean_string(state, artist, &error);
00577             if (error < 0) { goto end; };
00578           }
00579           else
00580           {
00581             artist = NULL;
00582           }
00583 
00584           //
00585           if (artist != NULL)
00586           {
00587             snprintf(temp+2,temp_len, state->oformat.format[i]+2);
00588 
00589             int artist_length = 0;
00590             artist_length = strlen(artist);
00591             fm_length = strlen(temp) + artist_length + 1;
00592           }
00593           else
00594           {
00595             snprintf(temp,temp_len, state->oformat.format[i]+2);
00596             fm_length = strlen(temp) + 1;
00597           }
00598 
00599           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00600           {
00601             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00602             goto end;
00603           }
00604 
00605           //
00606           if (artist != NULL)
00607           {
00608             snprintf(fm, fm_length, temp, artist);
00609           }
00610           else
00611           {
00612             snprintf(fm, fm_length, temp);
00613           }
00614           break;
00615         case 'b':
00616           if (splt_tu_tags_exists(state,tags_index))
00617           {
00618             //we get the album
00619             album =
00620               (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_ALBUM);
00621             splt_su_clean_string(state, album, &error);
00622             if (error < 0) { goto end; };
00623           }
00624           else
00625           {
00626             album = NULL;
00627           }
00628 
00629           //
00630           if (album != NULL)
00631           {
00632             int album_length = 0;
00633             album_length = strlen(album);
00634             snprintf(temp+2, temp_len, state->oformat.format[i]+2);
00635 
00636             fm_length = strlen(temp) + album_length + 1;
00637           }
00638           else
00639           {
00640             snprintf(temp,temp_len, state->oformat.format[i]+2);
00641             fm_length = strlen(temp) + 1;
00642           }
00643 
00644           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00645           {
00646             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00647             goto end;
00648           }
00649 
00650           //
00651           if (album != NULL)
00652           {
00653             snprintf(fm, fm_length, temp, album);
00654           }
00655           else
00656           {
00657             snprintf(fm, fm_length, "%s", temp);
00658           }
00659           break;
00660         case 'g':
00661           if (splt_tu_tags_exists(state,tags_index))
00662           {
00663             //we get the genre
00664             genre =
00665               (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_GENRE);
00666             splt_su_clean_string(state, genre, &error);
00667             if (error < 0) { goto end; };
00668           }
00669           else
00670           {
00671             genre = NULL;
00672           }
00673 
00674           //
00675           if (genre != NULL)
00676           {
00677             int genre_length = 0;
00678             genre_length = strlen(genre);
00679             snprintf(temp+2, temp_len, state->oformat.format[i]+2);
00680 
00681             fm_length = strlen(temp) + genre_length + 1;
00682           }
00683           else
00684           {
00685             snprintf(temp,temp_len, state->oformat.format[i]+2);
00686             fm_length = strlen(temp) + 1;
00687           }
00688 
00689           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00690           {
00691             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00692             goto end;
00693           }
00694 
00695           //
00696           if (genre != NULL)
00697           {
00698             snprintf(fm, fm_length, temp, genre);
00699           }
00700           else
00701           {
00702             snprintf(fm, fm_length, "%s", temp);
00703           }
00704           break;
00705         case 't':
00706           if (splt_tu_tags_exists(state,tags_index))
00707           {
00708             title = (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_TITLE);
00709             splt_su_clean_string(state, title, &error);
00710             if (error < 0) { goto end; };
00711           }
00712           else
00713           {
00714             title = NULL;
00715           }
00716 
00717           //
00718           if (title != NULL)
00719           {
00720             int title_length = 0;
00721             title_length = strlen(title);
00722             snprintf(temp+2, temp_len, state->oformat.format[i]+2);
00723 
00724             fm_length = strlen(temp) + title_length + 1;
00725           }
00726           else
00727           {
00728             snprintf(temp,temp_len, state->oformat.format[i]+2);
00729             fm_length = strlen(temp) + 1;
00730           }
00731 
00732           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00733           {
00734             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00735             goto end;
00736           }
00737 
00738           //
00739           if (title != NULL)
00740           {
00741             snprintf(fm, fm_length, temp, title);
00742           }
00743           else
00744           {
00745             snprintf(fm, fm_length, temp);
00746           }
00747           break;
00748         case 'p':
00749           if (splt_tu_tags_exists(state,tags_index))
00750           {
00751             performer = (char *)splt_tu_get_tags_field(state,tags_index, SPLT_TAGS_PERFORMER);
00752             splt_su_clean_string(state, performer, &error);
00753             if (error < 0) { goto end; };
00754           }
00755           else
00756           {
00757             performer = NULL;
00758           }
00759 
00760           //
00761           if (performer != NULL)
00762           {
00763             int performer_length = 0;
00764             performer_length = strlen(performer);
00765             snprintf(temp+2, temp_len, state->oformat.format[i]+2);
00766 
00767             fm_length = strlen(temp) + performer_length + 1;
00768           }
00769           else
00770           {
00771             snprintf(temp,temp_len, state->oformat.format[i]+2);
00772             fm_length = strlen(temp) + 1;
00773           }
00774 
00775           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00776           {
00777             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00778             goto end;
00779           }
00780 
00781           if (performer != NULL)
00782           {
00783             snprintf(fm, fm_length, temp, performer);
00784           }
00785           else
00786           {
00787             snprintf(fm, fm_length, temp);
00788           }
00789           break;
00790         case 'l':
00791         case 'L':
00792         case 'u':
00793         case 'U':
00794         case 'n':
00795         case 'N':
00796           temp[1] = '0';
00797           temp[2] = splt_of_get_oformat_number_of_digits_as_char(state);
00798           temp[3] = 'd';
00799 
00800           int tracknumber = split_file_number;
00801 
00802           //if not time split, or normal split, or silence split or error,
00803           //we put the track number from the tags
00804           int split_mode = splt_o_get_int_option(state,SPLT_OPT_SPLIT_MODE);
00805           if ((isupper(state->oformat.format[i][1])) ||
00806               ((split_mode != SPLT_OPTION_TIME_MODE) &&
00807                (split_mode != SPLT_OPTION_NORMAL_MODE) &&
00808                (split_mode != SPLT_OPTION_SILENCE_MODE) &&
00809                (split_mode != SPLT_OPTION_ERROR_MODE) &&
00810                (split_mode != SPLT_OPTION_LENGTH_MODE)))
00811           {
00812             if (splt_tu_tags_exists(state, tags_index))
00813             {
00814               int *tags_track = (int *)splt_tu_get_tags_field(state, tags_index, SPLT_TAGS_TRACK);
00815               if (tags_track && *tags_track > 0)
00816               {
00817                 tracknumber = *tags_track;
00818               }
00819             }
00820           }
00821 
00822           int requested_num_of_digits = 0;
00823           int max_num_of_digits = splt_u_get_requested_num_of_digits(state,
00824               state->oformat.format[i], &requested_num_of_digits, SPLT_FALSE);
00825 
00826           int alpha_requested_num_of_digits = 0;
00827           int alpha_max_num_of_digits = splt_u_get_requested_num_of_digits(state,
00828               state->oformat.format[i], &alpha_requested_num_of_digits, SPLT_TRUE);
00829 
00830           int is_numeric = toupper(state->oformat.format[i][1]) == 'N';
00831           if (is_numeric)
00832           {
00833             const char *format = splt_u_get_format_ptr(state->oformat.format[i], temp);
00834 
00835             snprintf(temp + 4, temp_len, format + 2);
00836             fm_length = strlen(temp) + 1 + max_num_of_digits;
00837           }
00838           else
00839           {
00840             fm_length = strlen(state->oformat.format[i]) + 1 + alpha_max_num_of_digits;
00841           }
00842 
00843           if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00844           {
00845             error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00846             goto end;
00847           }
00848           memset(fm, '\0', fm_length);
00849 
00850           if (is_numeric)
00851           {
00852             snprintf(fm, fm_length, temp, tracknumber);
00853           }
00854           else
00855           {
00856             splt_u_alpha_track(state, i, fm, fm_length,
00857                 alpha_requested_num_of_digits, tracknumber);
00858           }
00859           break;
00860         case 'f':
00861           if (splt_t_get_filename_to_split(state) != NULL)
00862           {
00863             original_filename = strdup(splt_su_get_fname_without_path(splt_t_get_filename_to_split(state)));
00864             if (original_filename)
00865             {
00866               snprintf(temp+2,temp_len, state->oformat.format[i]+2);
00867 
00868               splt_su_cut_extension(original_filename);
00869 
00870               int filename_length = strlen(original_filename);
00871 
00872               fm_length = strlen(temp) + filename_length;
00873               if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00874               {
00875                 error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00876                 goto end;
00877               }
00878 
00879               snprintf(fm, fm_length, temp, original_filename);
00880               free(original_filename);
00881               original_filename = NULL;
00882             }
00883             else
00884             {
00885               error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00886               goto end;
00887             }
00888           }
00889           break;
00890       }
00891     }
00892     else
00893     {
00894       fm_length = SPLT_MAXOLEN;
00895       if ((fm = malloc(fm_length * sizeof(char))) == NULL)
00896       {
00897         error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00898         goto end;
00899       }
00900 
00901       strncpy(fm, state->oformat.format[i], SPLT_MAXOLEN);
00902     }
00903 
00904     int fm_size = 7;
00905     if (fm != NULL)
00906     {
00907       fm_size = strlen(fm);
00908     }
00909 
00910     //allocate memory for the output filename
00911     if (!output_filename)
00912     {
00913       if ((output_filename = malloc((1+fm_size)*sizeof(char))) == NULL)
00914       {
00915         error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00916         goto end;
00917       }
00918       output_filename_size = fm_size;
00919       output_filename[0] = '\0';
00920     }
00921     else
00922     {
00923       output_filename_size += fm_size+1;
00924       if ((output_filename = realloc(output_filename, output_filename_size
00925               * sizeof(char))) == NULL)
00926       {
00927         error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00928         goto end;
00929       }
00930     }
00931 
00932     if (fm != NULL)
00933     {
00934       strcat(output_filename, fm);
00935     }
00936 
00937     //we free fm
00938     if (fm)
00939     {
00940       free(fm);
00941       fm = NULL;
00942     }
00943   }
00944 
00945   splt_d_print_debug(state,"The new output filename is _%s_\n", output_filename);
00946   int cur_splt = splt_t_get_current_split(state);
00947   int name_error = splt_sp_set_splitpoint_name(state, cur_splt, output_filename);
00948   if (name_error != SPLT_OK) { error = name_error; }
00949 
00950 end:
00951   if (output_filename)
00952   {
00953     free(output_filename);
00954     output_filename = NULL;
00955   }
00956   if (fm)
00957   {
00958     free(fm);
00959     fm = NULL;
00960   }
00961   if (temp)
00962   {
00963     free(temp);
00964     temp = NULL;
00965   }
00966 
00967   return error;
00968 }
00969