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_int_option(state, SPLT_OPT_PARAM_SHOTS) < 0)
00233 {
00234 splt_o_set_int_option(state, SPLT_OPT_PARAM_SHOTS, SPLT_DEFAULT_PARAM_SHOTS);
00235 }
00236
00237 if (splt_o_get_float_option(state, SPLT_OPT_PARAM_MIN_TRACK_LENGTH) < 0.f)
00238 {
00239 splt_o_set_float_option(state, SPLT_OPT_PARAM_MIN_TRACK_LENGTH,
00240 SPLT_DEFAULT_PARAM_MINIMUM_TRACK_LENGTH);
00241 }
00242 }
00243
00244 if (!splt_o_get_int_option(state,SPLT_OPT_AUTO_ADJUST))
00245 {
00246 splt_o_set_int_option(state,SPLT_OPT_PARAM_GAP, 0);
00247 }
00248
00249 if ((splt_o_get_int_option(state,SPLT_OPT_INPUT_NOT_SEEKABLE)) &&
00250 (splt_o_get_int_option(state,SPLT_OPT_AUTO_ADJUST) ||
00251 (split_mode == SPLT_OPTION_SILENCE_MODE) ||
00252 (split_mode == SPLT_OPTION_TRIM_SILENCE_MODE) ||
00253 (split_mode == SPLT_OPTION_ERROR_MODE) ||
00254 (split_mode == SPLT_OPTION_WRAP_MODE)))
00255 {
00256 splt_o_set_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE, SPLT_FALSE);
00257 }
00258 }
00259
00260 void splt_check_file_type(splt_state *state, int *error)
00261 {
00262 int err = SPLT_OK;
00263
00264 splt_d_print_debug(state,"Detecting file format...\n");
00265 const char *filename = splt_t_get_filename_to_split(state);
00266
00267 splt_d_print_debug(state,"Checking the format of _%s_\n", filename);
00268
00269 splt_plugins *pl = state->plug;
00270 int plugin_found = SPLT_FALSE;
00271 int i = 0;
00272 for (i = 0;i < pl->number_of_plugins_found;i++)
00273 {
00274 splt_p_set_current_plugin(state, i);
00275 err = SPLT_OK;
00276
00277 if (splt_o_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE) &&
00278 ! splt_io_input_is_stdin(state))
00279 {
00280 const char *extension = splt_p_get_extension(state, &err);
00281 const char *upper_extension = splt_p_get_extension(state, &err);
00282 if (err == SPLT_OK)
00283 {
00284 if (splt_su_str_ends_with(filename, extension) ||
00285 splt_su_str_ends_with(filename, upper_extension))
00286 {
00287 plugin_found = SPLT_TRUE;
00288 break;
00289 }
00290 }
00291 }
00292 else
00293 {
00294 if (splt_p_check_plugin_is_for_file(state, &err))
00295 {
00296 if (err == SPLT_OK)
00297 {
00298 plugin_found = SPLT_TRUE;
00299 break;
00300 }
00301 }
00302 }
00303 }
00304
00305 if (! plugin_found)
00306 {
00307 splt_e_set_error_data(state, filename);
00308 *error = SPLT_ERROR_NO_PLUGIN_FOUND_FOR_FILE;
00309 splt_d_print_debug(state,"No plugin found !\n");
00310 splt_d_print_debug(state,"Verifying if the file _%s_ is a file ...\n", filename);
00311
00312 FILE *test = NULL;
00313 if ((test = splt_io_fopen(filename,"r")) == NULL)
00314 {
00315 splt_e_set_strerror_msg_with_data(state, filename);
00316 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00317 return;
00318 }
00319
00320 if (fclose(test) != 0)
00321 {
00322 splt_e_set_strerror_msg_with_data(state, filename);
00323 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00324 return;
00325 }
00326 }
00327 }
00328
00329 int splt_check_is_the_same_file(splt_state *state, const char *file1,
00330 const char *file2, int *error)
00331 {
00332 if (file1 == NULL || file2 == NULL)
00333 {
00334 return SPLT_FALSE;
00335 }
00336
00337 FILE *file1_ = NULL;
00338 FILE *file2_ = NULL;
00339
00340 if (file1[strlen(file1) - 1] == '-')
00341 {
00342 return SPLT_FALSE;
00343 }
00344
00345 splt_d_print_debug(state,"Checking if _%s_ is like _%s_ \n", file1, file2);
00346
00347 int is_file1 = splt_io_check_if_file(state, file1);
00348 int is_file2 = splt_io_check_if_file(state, file2);
00349 if (!is_file1 || !is_file2)
00350 {
00351 return SPLT_FALSE;
00352 }
00353
00354 if ((file1_ = splt_io_fopen(file1,"r")) == NULL)
00355 {
00356 goto end;
00357 }
00358
00359 if ((file2_ = splt_io_fopen(file2,"r")) == NULL)
00360 {
00361 goto end;
00362 }
00363
00364
00365
00366 #ifdef __WIN32__
00367 BY_HANDLE_FILE_INFORMATION handle_info_file1;
00368 BY_HANDLE_FILE_INFORMATION handle_info_file2;
00369 int file1_d = fileno(file1_);
00370 int file2_d = fileno(file2_);
00371
00372 if (!GetFileInformationByHandle((HANDLE)_get_osfhandle(file1_d), &handle_info_file1))
00373 {
00374 goto end;
00375 }
00376 if (!GetFileInformationByHandle((HANDLE)_get_osfhandle(file2_d), &handle_info_file2))
00377 {
00378 goto end;
00379 }
00380
00381 if ((handle_info_file1.nFileIndexHigh == handle_info_file2.nFileIndexHigh)&&
00382 (handle_info_file1.nFileIndexLow == handle_info_file2.nFileIndexLow))
00383 {
00384 close_files(state, file1, &file1_,file2, &file2_,error);
00385 return SPLT_TRUE;
00386 }
00387 #else
00388 int file1_d = fileno(file1_);
00389 struct stat file1_stat;
00390 if (fstat(file1_d, &file1_stat) != 0)
00391 {
00392 goto end;
00393 }
00394
00395 int file2_d = fileno(file2_);
00396 struct stat file2_stat;
00397 if (fstat(file2_d,&file2_stat) != 0)
00398 {
00399 goto end;
00400 }
00401
00402 if ((file1_stat.st_ino == file2_stat.st_ino) &&
00403 (file1_stat.st_dev == file2_stat.st_dev))
00404 {
00405 close_files(state, file1, &file1_,file2, &file2_,error);
00406 return SPLT_TRUE;
00407 }
00408 #endif
00409
00410 end:
00411 close_files(state, file1, &file1_,file2, &file2_,error);
00412
00413 return SPLT_FALSE;
00414 }
00415
00416 static void close_files(splt_state *state, const char *file1, FILE **f1,
00417 const char *file2, FILE **f2, int *error)
00418 {
00419 if (*f1)
00420 {
00421 if (fclose(*f1) != 0)
00422 {
00423 splt_e_set_strerror_msg_with_data(state, file1);
00424 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00425 }
00426 else
00427 {
00428 *f1 = NULL;
00429 }
00430 }
00431
00432 if (*f2)
00433 {
00434 if (fclose(*f2) != 0)
00435 {
00436 splt_e_set_strerror_msg_with_data(state, file2);
00437 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
00438 }
00439 else
00440 {
00441 *f2 = NULL;
00442 }
00443 }
00444 }
00445