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
00029
00030
00031
00032
00033
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <errno.h>
00042 #include <string.h>
00043 #include <ctype.h>
00044 #include <math.h>
00045
00046 #include "splt.h"
00047
00048
00049 static long splt_audacity_get_begin(splt_audacity *sa)
00050 {
00051 return sa->begin;
00052 }
00053
00054 static long splt_audacity_get_end(splt_audacity *sa)
00055 {
00056 return sa->end;
00057 }
00058
00059 static const char *splt_audacity_get_name(splt_audacity *sa)
00060 {
00061 return sa->name;
00062 }
00063
00064 static int splt_audacity_append_splitpoints(splt_state *state,
00065 splt_audacity *previous_aud, splt_audacity *aud, int *append_begin_point)
00066 {
00067 int err = SPLT_OK;
00068
00069 long previous_begin_point = -1;
00070 long previous_end_point = -1;
00071
00072 if (previous_aud)
00073 {
00074 previous_begin_point = splt_audacity_get_begin(previous_aud);
00075 previous_end_point = splt_audacity_get_end(previous_aud);
00076 }
00077
00078 long start_point = splt_audacity_get_begin(aud);
00079
00080 if (previous_begin_point != -1 && previous_end_point != -1)
00081 {
00082 if (*append_begin_point)
00083 {
00084 err = splt_sp_append_splitpoint(state, previous_begin_point,
00085 splt_audacity_get_name(previous_aud), SPLT_SPLITPOINT);
00086 if (err < 0) { return err; }
00087 }
00088
00089 if (start_point == previous_end_point)
00090 {
00091 err = splt_sp_append_splitpoint(state, previous_end_point,
00092 splt_audacity_get_name(aud), SPLT_SPLITPOINT);
00093 *append_begin_point = SPLT_FALSE;
00094 if (err < 0) { return err; }
00095 }
00096 else
00097 {
00098 err = splt_sp_append_splitpoint(state, previous_end_point, "skip", SPLT_SKIPPOINT);
00099 *append_begin_point = SPLT_TRUE;
00100 if (err < 0) { return err; }
00101 }
00102 }
00103
00104 return err;
00105 }
00106
00107 static splt_audacity *splt_audacity_new()
00108 {
00109 splt_audacity *sa = malloc(sizeof(splt_audacity));
00110 if (!sa)
00111 {
00112 return NULL;
00113 }
00114
00115 sa->begin = -1;
00116 sa->end = -1;
00117 sa->name = NULL;
00118
00119 return sa;
00120 }
00121
00122 static void splt_audacity_free(splt_audacity **sa)
00123 {
00124 if (sa)
00125 {
00126 if (*sa)
00127 {
00128 splt_audacity *s = *sa;
00129
00130 if (s->name)
00131 {
00132 free(s->name);
00133 s->name = NULL;
00134 }
00135
00136 free(*sa);
00137 *sa = NULL;
00138 }
00139 }
00140 }
00141
00142 static int splt_audacity_set_name(splt_audacity *sa, const char *name)
00143 {
00144 return splt_su_copy(name, &sa->name);
00145 }
00146
00147 static long to_hundreths(char *str)
00148 {
00149 long hun = 0;
00150 long seconds = 0, hundreths = 0;
00151 sscanf(str, "%ld.%4ld", &seconds, &hundreths);
00152
00153 if (hundreths >= 100)
00154 {
00155 double hun_in_double = (double)hundreths;
00156 hundreths = round(hun_in_double / 100.0);
00157 }
00158
00159 return seconds * 100 + hundreths;
00160 }
00161
00162 static char *splt_audacity_set_begin(splt_audacity *sa, char *str)
00163 {
00164 int hun = to_hundreths(str);
00165 if (hun == -1) { return NULL; }
00166 sa->begin = hun;
00167 return strchr(str, '\t');
00168 }
00169
00170 static char *splt_audacity_set_end(splt_audacity *sa, char *str)
00171 {
00172 int hun = to_hundreths(str);
00173 if (hun == -1) { return NULL; }
00174 sa->end = hun;
00175 return strchr(str, '\t');
00176 }
00177
00178 static splt_audacity *splt_audacity_process_line(splt_state *state, char *line,
00179 splt_audacity *previous_aud, int *append_begin_point, int *error)
00180 {
00181 splt_audacity *aud = splt_audacity_new();
00182 if (!aud)
00183 {
00184 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00185 return NULL;
00186 }
00187
00188 char *ptr = line;
00189
00190 errno = 0;
00191 ptr = splt_audacity_set_begin(aud, ptr);
00192 if (ptr == NULL || *ptr == '\0') {
00193 *error = SPLT_INVALID_AUDACITY_FILE;
00194 goto error;
00195 }
00196 ptr++;
00197
00198 errno = 0;
00199 ptr = splt_audacity_set_end(aud, ptr);
00200 if (ptr == NULL || *ptr == '\0')
00201 {
00202 *error = SPLT_INVALID_AUDACITY_FILE;
00203 goto error;
00204 }
00205 ptr++;
00206
00207 ptr = splt_su_trim_spaces(ptr);
00208
00209 int err = splt_audacity_set_name(aud, ptr);
00210 if (err < 0) { *error = err; goto error; }
00211
00212 err = splt_audacity_append_splitpoints(state, previous_aud, aud, append_begin_point);
00213 if (err < 0) { *error = err; goto error; }
00214
00215 return aud;
00216
00217 error:
00218 splt_audacity_free(&aud);
00219 return NULL;
00220 }
00221
00222 int splt_audacity_put_splitpoints(const char *file, splt_state *state, int *error)
00223 {
00224 int tracks = -1;
00225
00226 if (file == NULL)
00227 {
00228 *error = SPLT_INVALID_AUDACITY_FILE;
00229 return tracks;
00230 }
00231
00232 splt_t_free_splitpoints_tags(state);
00233
00234 *error = SPLT_AUDACITY_OK;
00235
00236 splt_c_put_info_message_to_client(state,
00237 _(" reading informations from audacity labels file '%s' ...\n"), file);
00238
00239 FILE *file_input = NULL;
00240
00241 if (!(file_input = splt_io_fopen(file, "r")))
00242 {
00243 splt_e_set_strerror_msg_with_data(state, file);
00244 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00245 return tracks;
00246 }
00247
00248 if (fseek(file_input, 0, SEEK_SET) != 0)
00249 {
00250 splt_e_set_strerror_msg_with_data(state, file);
00251 *error = SPLT_ERROR_SEEKING_FILE;
00252 goto end;
00253 }
00254
00255 int append_begin_point = SPLT_TRUE;
00256 int err = SPLT_OK;
00257
00258 splt_audacity *aud = NULL;
00259 splt_audacity *previous_aud = NULL;
00260
00261 tracks = 0;
00262 char *line = NULL;
00263 while ((line = splt_io_readline(file_input, error)) != NULL)
00264 {
00265 if (*error < 0) { goto end; }
00266
00267 if (splt_su_is_empty_line(line))
00268 {
00269 free(line);
00270 line = NULL;
00271 continue;
00272 }
00273
00274 aud = splt_audacity_process_line(state, line, previous_aud,
00275 &append_begin_point, &err);
00276 if (err < 0) { goto end; }
00277
00278 if (previous_aud)
00279 {
00280 splt_audacity_free(&previous_aud);
00281 }
00282 previous_aud = aud;
00283
00284 free(line);
00285 line = NULL;
00286
00287 tracks++;
00288 }
00289
00290 if (previous_aud)
00291 {
00292 err = splt_audacity_append_splitpoints(state, previous_aud, aud, &append_begin_point);
00293 if (err < 0) { *error = err; }
00294 }
00295
00296 end:
00297 if (line)
00298 {
00299 free(line);
00300 line = NULL;
00301 }
00302
00303 if (previous_aud)
00304 {
00305 splt_audacity_free(&previous_aud);
00306 }
00307
00308 if (fclose(file_input) != 0)
00309 {
00310 splt_e_set_strerror_msg_with_data(state, file);
00311 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00312 }
00313 file_input = NULL;
00314
00315 return tracks;
00316 }
00317