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 #include <sys/stat.h>
00034 #include <string.h>
00035
00036 #include "splt.h"
00037
00038 #ifdef __WIN32__
00039 #include "windows.h"
00040 #endif
00041
00042 static void close_files(splt_state *state, const char *file1, FILE **f1,
00043 const char *file2, FILE **f2, int *error);
00044
00045 void splt_check_points_inf_song_length_and_convert_negatives(splt_state *state, int *error)
00046 {
00047 if (splt_io_input_is_stdin(state))
00048 {
00049 return;
00050 }
00051
00052 int splitnumber = splt_t_get_splitnumber(state);
00053 if (splitnumber < 1)
00054 {
00055 return;
00056 }
00057
00058 int err = SPLT_OK;
00059 long total_time = splt_t_get_total_time(state);
00060 if (total_time == 0)
00061 {
00062 return;
00063 }
00064
00065 int i = 0;
00066 for (i = 0; i < splitnumber; i++)
00067 {
00068 long splitpoint_value = splt_sp_get_splitpoint_value(state, i, &err);
00069 if (err < SPLT_OK) { *error = err; return; }
00070
00071 if (splitpoint_value < 0)
00072 {
00073 splitpoint_value = total_time + splitpoint_value;
00074 splt_sp_set_splitpoint_value(state, i, splitpoint_value);
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 }
00095 }
00096
00097 void splt_check_if_points_in_order(splt_state *state, int *error)
00098 {
00099 int splitnumber = splt_t_get_splitnumber(state);
00100 if (splitnumber < 1)
00101 {
00102 return;
00103 }
00104
00105 int err = SPLT_OK;
00106 int i = 0;
00107 for (i = 0; i < (splitnumber-1); i++)
00108 {
00109 long splitpoint_value = splt_sp_get_splitpoint_value(state, i, &err);
00110 if (err != SPLT_OK) { *error = err; return; }
00111 long next_splitpoint_value = splt_sp_get_splitpoint_value(state, i+1, &err);
00112 if (err != SPLT_OK) { *error = err; return; }
00113
00114 if (splitpoint_value < 0)
00115 {
00116 splt_e_set_error_data_from_splitpoint(state, splitpoint_value);
00117 *error = SPLT_ERROR_NEGATIVE_SPLITPOINT;
00118 return;
00119 }
00120
00121 if (next_splitpoint_value < 0)
00122 {
00123 splt_e_set_error_data_from_splitpoint(state, next_splitpoint_value);
00124 *error = SPLT_ERROR_NEGATIVE_SPLITPOINT;
00125 return;
00126 }
00127
00128 if (splitpoint_value > next_splitpoint_value)
00129 {
00130 splt_e_set_error_data_from_splitpoints(state, splitpoint_value, next_splitpoint_value);
00131 *error = SPLT_ERROR_SPLITPOINTS_NOT_IN_ORDER;
00132 return;
00133 }
00134
00135 if (splitpoint_value == next_splitpoint_value)
00136 {
00137 splt_e_set_error_data_from_splitpoint(state, splitpoint_value);
00138 *error = SPLT_ERROR_EQUAL_SPLITPOINTS;
00139 return;
00140 }
00141 }
00142 }
00143
00144 void splt_check_if_fname_path_is_correct(splt_state *state,
00145 const char *fname_path, int *error)
00146 {
00147 splt_d_print_debug(state,"Check if the new filename path is correct _%s_\n",fname_path);
00148
00149 char current_directory[4] = { '\0' };
00150 snprintf(current_directory, 4, "%c%c", '.', SPLT_DIRCHAR);
00151
00152 if ((strcmp(fname_path, "") != 0) &&
00153 (strcmp(fname_path, current_directory) != 0))
00154 {
00155 if (!splt_io_check_if_directory(fname_path))
00156 {
00157 splt_e_set_strerr_msg_with_data(state,
00158 _("directory does not exists"), fname_path);
00159 *error = SPLT_ERROR_INCORRECT_PATH;
00160 }
00161 }
00162 }
00163
00164 char *splt_check_put_dir_of_cur_song(const char *filename,
00165 const char *path, int *error)
00166 {
00167 int err = SPLT_OK;
00168 char *filename_path = NULL;
00169
00170 if (path == NULL || path[0] == '\0')
00171 {
00172 err = splt_su_copy(filename, &filename_path);
00173 if (err < 0) { *error = err; return NULL; }
00174
00175 char *c = strrchr(filename_path, SPLT_DIRCHAR);
00176 if (c == NULL)
00177 {
00178 filename_path[0] = '\0';
00179 return filename_path;
00180 }
00181
00182 *(c+1) = '\0';
00183 return filename_path;
00184 }
00185
00186 err = splt_su_copy(path, &filename_path);
00187 if (err < 0) { *error = err; return NULL; }
00188
00189 return filename_path;
00190 }
00191
00192 int splt_check_compatible_options(splt_state *state)
00193 {
00194 return SPLT_TRUE;
00195 }
00196
00197 void splt_check_set_correct_options(splt_state *state)
00198 {
00199 splt_d_print_debug(state,"Check and set correct options...\n");
00200
00201 int split_mode = splt_o_get_int_option(state,SPLT_OPT_SPLIT_MODE);
00202
00203 if ((split_mode == SPLT_OPTION_SILENCE_MODE)
00204 || (split_mode == SPLT_OPTION_TRIM_SILENCE_MODE)
00205 || splt_o_get_int_option(state,SPLT_OPT_AUTO_ADJUST))
00206 {
00207 splt_o_set_int_option(state, SPLT_OPT_FRAME_MODE, SPLT_OPT_FRAME_MODE);
00208
00209 if ((splt_o_get_float_option(state,SPLT_OPT_PARAM_THRESHOLD) < -96.f) ||
00210 (splt_o_get_float_option(state,SPLT_OPT_PARAM_THRESHOLD) > 0.f))
00211 {
00212 splt_o_set_float_option(state,SPLT_OPT_PARAM_THRESHOLD, SPLT_DEFAULT_PARAM_THRESHOLD);
00213 }
00214
00215 if ((splt_o_get_float_option(state,SPLT_OPT_PARAM_OFFSET) < -2.f) ||
00216 (splt_o_get_float_option(state,SPLT_OPT_PARAM_OFFSET) > 2.f))
00217 {
00218 splt_o_set_float_option(state,SPLT_OPT_PARAM_OFFSET, SPLT_DEFAULT_PARAM_OFFSET);
00219 }
00220
00221 if (splt_o_get_int_option(state,SPLT_OPT_PARAM_GAP) < 0)
00222 {
00223 splt_o_set_int_option(state,SPLT_OPT_PARAM_GAP, SPLT_DEFAULT_PARAM_GAP);
00224 }
00225
00226 if (splt_o_get_float_option(state,SPLT_OPT_PARAM_MIN_LENGTH) < 0.f)
00227 {
00228 splt_o_set_float_option(state, SPLT_OPT_PARAM_MIN_LENGTH, SPLT_DEFAULT_PARAM_MINIMUM_LENGTH);
00229 splt_o_set_int_option(state, SPLT_OPT_AUTO_ADJUST, SPLT_FALSE);
00230 }
00231
00232 if (splt_o_get_float_option(state, SPLT_OPT_PARAM_MIN_TRACK_LENGTH) < 0.f)
00233 {
00234 splt_o_set_float_option(state, SPLT_OPT_PARAM_MIN_TRACK_LENGTH,
00235 SPLT_DEFAULT_PARAM_MINIMUM_TRACK_LENGTH);
00236 }
00237 }
00238
00239 if (!splt_o_get_int_option(state,SPLT_OPT_AUTO_ADJUST))
00240 {
00241 splt_o_set_int_option(state,SPLT_OPT_PARAM_GAP, 0);
00242 }
00243
00244 if ((splt_o_get_int_option(state,SPLT_OPT_INPUT_NOT_SEEKABLE)) &&
00245 (splt_o_get_int_option(state,SPLT_OPT_AUTO_ADJUST) ||
00246 (split_mode == SPLT_OPTION_SILENCE_MODE) ||
00247 (split_mode == SPLT_OPTION_TRIM_SILENCE_MODE) ||
00248 (split_mode == SPLT_OPTION_ERROR_MODE) ||
00249 (split_mode == SPLT_OPTION_WRAP_MODE)))
00250 {
00251 splt_o_set_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE, SPLT_FALSE);
00252 }
00253 }
00254
00255 void splt_check_file_type(splt_state *state, int *error)
00256 {
00257 int err = SPLT_OK;
00258
00259 splt_d_print_debug(state,"Detecting file format...\n");
00260 const char *filename = splt_t_get_filename_to_split(state);
00261
00262 splt_d_print_debug(state,"Checking the format of _%s_\n", filename);
00263
00264 splt_plugins *pl = state->plug;
00265 int plugin_found = SPLT_FALSE;
00266 int i = 0;
00267 for (i = 0;i < pl->number_of_plugins_found;i++)
00268 {
00269 splt_p_set_current_plugin(state, i);
00270 err = SPLT_OK;
00271
00272 if (splt_o_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE) &&
00273 ! splt_io_input_is_stdin(state))
00274 {
00275 const char *extension = splt_p_get_extension(state, &err);
00276 const char *upper_extension = splt_p_get_extension(state, &err);
00277 if (err == SPLT_OK)
00278 {
00279 if (splt_su_str_ends_with(filename, extension) ||
00280 splt_su_str_ends_with(filename, upper_extension))
00281 {
00282 plugin_found = SPLT_TRUE;
00283 break;
00284 }
00285 }
00286 }
00287 else
00288 {
00289 if (splt_p_check_plugin_is_for_file(state, &err))
00290 {
00291 if (err == SPLT_OK)
00292 {
00293 plugin_found = SPLT_TRUE;
00294 break;
00295 }
00296 }
00297 }
00298 }
00299
00300 if (! plugin_found)
00301 {
00302 splt_e_set_error_data(state, filename);
00303 *error = SPLT_ERROR_NO_PLUGIN_FOUND_FOR_FILE;
00304 splt_d_print_debug(state,"No plugin found !\n");
00305 splt_d_print_debug(state,"Verifying if the file _%s_ is a file ...\n", filename);
00306
00307 FILE *test = NULL;
00308 if ((test = splt_io_fopen(filename,"r")) == NULL)
00309 {
00310 splt_e_set_strerror_msg_with_data(state, filename);
00311 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00312 return;
00313 }
00314
00315 if (fclose(test) != 0)
00316 {
00317 splt_e_set_strerror_msg_with_data(state, filename);
00318 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00319 return;
00320 }
00321 }
00322 }
00323
00324 int splt_check_is_the_same_file(splt_state *state, const char *file1,
00325 const char *file2, int *error)
00326 {
00327 if (file1 == NULL || file2 == NULL)
00328 {
00329 return SPLT_FALSE;
00330 }
00331
00332 FILE *file1_ = NULL;
00333 FILE *file2_ = NULL;
00334
00335 if (file1[strlen(file1) - 1] == '-')
00336 {
00337 return SPLT_FALSE;
00338 }
00339
00340 splt_d_print_debug(state,"Checking if _%s_ is like _%s_ \n", file1, file2);
00341
00342 int is_file1 = splt_io_check_if_file(state, file1);
00343 int is_file2 = splt_io_check_if_file(state, file2);
00344 if (!is_file1 || !is_file2)
00345 {
00346 return SPLT_FALSE;
00347 }
00348
00349 if ((file1_ = splt_io_fopen(file1,"r")) == NULL)
00350 {
00351 goto end;
00352 }
00353
00354 if ((file2_ = splt_io_fopen(file2,"r")) == NULL)
00355 {
00356 goto end;
00357 }
00358
00359
00360
00361 #ifdef __WIN32__
00362 BY_HANDLE_FILE_INFORMATION handle_info_file1;
00363 BY_HANDLE_FILE_INFORMATION handle_info_file2;
00364 int file1_d = fileno(file1_);
00365 int file2_d = fileno(file2_);
00366
00367 if (!GetFileInformationByHandle((HANDLE)_get_osfhandle(file1_d), &handle_info_file1))
00368 {
00369 goto end;
00370 }
00371 if (!GetFileInformationByHandle((HANDLE)_get_osfhandle(file2_d), &handle_info_file2))
00372 {
00373 goto end;
00374 }
00375
00376 if ((handle_info_file1.nFileIndexHigh == handle_info_file2.nFileIndexHigh)&&
00377 (handle_info_file1.nFileIndexLow == handle_info_file2.nFileIndexLow))
00378 {
00379 close_files(state, file1, &file1_,file2, &file2_,error);
00380 return SPLT_TRUE;
00381 }
00382 #else
00383 int file1_d = fileno(file1_);
00384 struct stat file1_stat;
00385 if (fstat(file1_d, &file1_stat) != 0)
00386 {
00387 goto end;
00388 }
00389
00390 int file2_d = fileno(file2_);
00391 struct stat file2_stat;
00392 if (fstat(file2_d,&file2_stat) != 0)
00393 {
00394 goto end;
00395 }
00396
00397 if ((file1_stat.st_ino == file2_stat.st_ino) &&
00398 (file1_stat.st_dev == file2_stat.st_dev))
00399 {
00400 close_files(state, file1, &file1_,file2, &file2_,error);
00401 return SPLT_TRUE;
00402 }
00403 #endif
00404
00405 end:
00406 close_files(state, file1, &file1_,file2, &file2_,error);
00407
00408 return SPLT_FALSE;
00409 }
00410
00411 static void close_files(splt_state *state, const char *file1, FILE **f1,
00412 const char *file2, FILE **f2, int *error)
00413 {
00414 if (*f1)
00415 {
00416 if (fclose(*f1) != 0)
00417 {
00418 splt_e_set_strerror_msg_with_data(state, file1);
00419 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00420 }
00421 else
00422 {
00423 *f1 = NULL;
00424 }
00425 }
00426
00427 if (*f2)
00428 {
00429 if (fclose(*f2) != 0)
00430 {
00431 splt_e_set_strerror_msg_with_data(state, file2);
00432 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00433 }
00434 else
00435 {
00436 *f2 = NULL;
00437 }
00438 }
00439 }
00440