libmp3splt
src/mp3splt.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-2006 Alexandru Munteanu - io_alex_2002@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 
00067 #include <sys/stat.h>
00068 #include <string.h>
00069 
00070 #include <ltdl.h>
00071 
00072 #include "splt.h"
00073 
00074 int global_debug = SPLT_FALSE;
00075 
00098 splt_state *mp3splt_new_state(int *error)
00099 {
00100   splt_state *state = NULL;
00101 
00102   int erro = SPLT_OK;
00103   int *err = &erro;
00104   if (error != NULL) { err = error; }
00105 
00106   if (lt_dlinit() != 0)
00107   {
00108     *err = SPLT_ERROR_CANNOT_INIT_LIBLTDL;
00109   }
00110   else
00111   {
00112 
00113 #ifdef ENABLE_NLS
00114 # ifndef __WIN32__
00115     bindtextdomain(MP3SPLT_LIB_GETTEXT_DOMAIN, LOCALEDIR);
00116 # endif
00117     bind_textdomain_codeset(MP3SPLT_LIB_GETTEXT_DOMAIN, "UTF-8");
00118 #endif
00119 
00120     state = splt_t_new_state(state, err);
00121   }
00122 
00123   return state;
00124 }
00125 
00130 int mp3splt_find_plugins(splt_state *state)
00131 {
00132   return splt_p_find_get_plugins_data(state);
00133 }
00134 
00144 void mp3splt_free_state(splt_state *state, int *error)
00145 {
00146   if (!state)
00147   {
00148     return;
00149   }
00150 
00151   int erro = SPLT_OK;
00152   int *err = &erro;
00153   if (error != NULL) { err = error; }
00154 
00155   if (state != NULL)
00156   {
00157     if (!splt_o_library_locked(state))
00158     {
00159       splt_o_lock_library(state);
00160 
00161       splt_t_free_state(state);
00162     }
00163     else
00164     {
00165       *err = SPLT_ERROR_LIBRARY_LOCKED;
00166     }
00167   }
00168   else
00169   {
00170     *err = SPLT_ERROR_STATE_NULL;
00171   }
00172 }
00173 
00175 int mp3splt_set_path_of_split(splt_state *state, const char *path)
00176 {
00177   int error = SPLT_OK;
00178 
00179   if (state != NULL)
00180   {
00181     if (!splt_o_library_locked(state))
00182     {
00183       splt_o_lock_library(state);
00184 
00185       error = splt_t_set_path_of_split(state, path);
00186 
00187       splt_o_unlock_library(state);
00188     }
00189     else
00190     {
00191       error = SPLT_ERROR_LIBRARY_LOCKED;
00192     }
00193   }
00194   else
00195   {
00196     error = SPLT_ERROR_STATE_NULL;
00197   }
00198 
00199   return error;
00200 }
00201 
00207 int mp3splt_set_m3u_filename(splt_state *state, const char *filename)
00208 {
00209   int error = SPLT_OK;
00210 
00211   if (state != NULL)
00212   {
00213     if (!splt_o_library_locked(state))
00214     {
00215       splt_o_lock_library(state);
00216 
00217       error = splt_t_set_m3u_filename(state, filename);
00218 
00219       splt_o_unlock_library(state);
00220     }
00221     else
00222     {
00223       error = SPLT_ERROR_LIBRARY_LOCKED;
00224     }
00225   }
00226   else
00227   {
00228     error = SPLT_ERROR_STATE_NULL;
00229   }
00230 
00231   return error;
00232 }
00233 
00235 int mp3splt_set_silence_log_filename(splt_state *state, const char *filename)
00236 {
00237   int error = SPLT_OK;
00238 
00239   if (state != NULL)
00240   {
00241     if (!splt_o_library_locked(state))
00242     {
00243       splt_o_lock_library(state);
00244 
00245       error = splt_t_set_silence_log_fname(state, filename);
00246 
00247       splt_o_unlock_library(state);
00248     }
00249     else
00250     {
00251       error = SPLT_ERROR_LIBRARY_LOCKED;
00252     }
00253   }
00254   else
00255   {
00256     error = SPLT_ERROR_STATE_NULL;
00257   }
00258 
00259   return error;
00260 }
00261 
00266 char *mp3splt_get_filename_to_split(splt_state *state)
00267 {
00268   return splt_t_get_filename_to_split(state); 
00269 }
00270 
00276 int mp3splt_set_filename_to_split(splt_state *state, const char *filename)
00277 {
00278   int error = SPLT_OK;
00279 
00280   if (state != NULL)
00281   {
00282     if (!splt_o_library_locked(state))
00283     {
00284       splt_o_lock_library(state);
00285 
00286       error = splt_t_set_filename_to_split(state, filename);
00287 
00288       splt_o_unlock_library(state);
00289     }
00290     else
00291     {
00292       error = SPLT_ERROR_LIBRARY_LOCKED;
00293     }
00294   }
00295   else
00296   {
00297     error = SPLT_ERROR_STATE_NULL;
00298   }
00299 
00300   return error;
00301 }
00302 
00303 int mp3splt_set_input_filename_regex(splt_state *state, const char *regex)
00304 {
00305   int error = SPLT_OK;
00306 
00307   if (state != NULL)
00308   {
00309     if (!splt_o_library_locked(state))
00310     {
00311       splt_o_lock_library(state);
00312 
00313       error = splt_t_set_input_filename_regex(state, regex);
00314 
00315       splt_o_unlock_library(state);
00316     }
00317     else
00318     {
00319       error = SPLT_ERROR_LIBRARY_LOCKED;
00320     }
00321   }
00322   else
00323   {
00324     error = SPLT_ERROR_STATE_NULL;
00325   }
00326 
00327   return error;
00328 }
00329 
00330 int mp3splt_set_default_comment_tag(splt_state *state, const char *default_comment)
00331 {
00332   int error = SPLT_OK;
00333 
00334   if (state != NULL)
00335   {
00336     if (!splt_o_library_locked(state))
00337     {
00338       splt_o_lock_library(state);
00339 
00340       error = splt_t_set_default_comment_tag(state, default_comment);
00341 
00342       splt_o_unlock_library(state);
00343     }
00344     else
00345     {
00346       error = SPLT_ERROR_LIBRARY_LOCKED;
00347     }
00348   }
00349   else
00350   {
00351     error = SPLT_ERROR_STATE_NULL;
00352   }
00353 
00354   return error;
00355 }
00356 
00357 int mp3splt_set_default_genre_tag(splt_state *state, const char *default_genre_tag)
00358 {
00359   int error = SPLT_OK;
00360 
00361   if (state != NULL)
00362   {
00363     if (!splt_o_library_locked(state))
00364     {
00365       splt_o_lock_library(state);
00366 
00367       error = splt_t_set_default_genre_tag(state, default_genre_tag);
00368 
00369       splt_o_unlock_library(state);
00370     }
00371     else
00372     {
00373       error = SPLT_ERROR_LIBRARY_LOCKED;
00374     }
00375   }
00376   else
00377   {
00378     error = SPLT_ERROR_STATE_NULL;
00379   }
00380 
00381   return error;
00382 }
00383 
00384 
00385 /************************************/
00386 /* Set callback functions           */
00387 
00394 int mp3splt_set_message_function(splt_state *state, 
00395     void (*message_cb)(const char *, splt_message_type))
00396 {
00397   int error = SPLT_OK;
00398 
00399   if (state != NULL)
00400   {
00401     state->split.put_message = message_cb;
00402   }
00403   else
00404   {
00405     error = SPLT_ERROR_STATE_NULL;
00406   }
00407 
00408   return error;
00409 }
00410 
00417 int mp3splt_set_split_filename_function(splt_state *state,
00418     void (*file_cb)(const char *,int b))
00419 {
00420   int error = SPLT_OK;
00421 
00422   if (state != NULL)
00423   {
00424     state->split.file_split = file_cb;
00425   }
00426   else
00427   {
00428     error = SPLT_ERROR_STATE_NULL;
00429   }
00430 
00431   return error;
00432 }
00433 
00440 int mp3splt_set_progress_function(splt_state *state,
00441     void (*progress_cb)(splt_progress *p_bar))
00442 {
00443   int error = SPLT_OK;
00444 
00445   if (state != NULL)
00446   {
00447     state->split.p_bar->progress = progress_cb;
00448   }
00449   else
00450   {
00451     error = SPLT_ERROR_STATE_NULL;
00452   }
00453 
00454   return error;
00455 }
00456 int mp3splt_set_silence_level_function(splt_state *state,
00464   void (*get_silence_cb)(long time, float level, void *user_data),
00465   void *data)
00466 {
00467   int error = SPLT_OK;
00468 
00469   if (state != NULL)
00470   {
00471     state->split.get_silence_level = get_silence_cb;
00472     state->split.silence_level_client_data = data;
00473   }
00474   else
00475   {
00476     error = SPLT_ERROR_STATE_NULL;
00477   }
00478 
00479   return error;
00480 }
00481 
00482 /************************************/
00483 /* Splitpoints                      */
00484 
00494 int mp3splt_append_splitpoint(splt_state *state,
00495     long split_value, const char *name, int type)
00496 {
00497   int error = SPLT_OK;
00498 
00499   if (state != NULL)
00500   {
00501     if (!splt_o_library_locked(state))
00502     {
00503       splt_o_lock_library(state);
00504 
00505       error = splt_sp_append_splitpoint(state, split_value, name, type);
00506 
00507       splt_o_unlock_library(state);
00508     }
00509     else
00510     {
00511       error = SPLT_ERROR_LIBRARY_LOCKED;
00512     }
00513   }
00514   else
00515   {
00516     error = SPLT_ERROR_STATE_NULL;
00517   }
00518 
00519   return error;
00520 }
00521 
00529 const splt_point *mp3splt_get_splitpoints(splt_state *state,
00530     int *splitpoints_number, int *error)
00531 {
00532   int erro = SPLT_OK;
00533   int *err = &erro;
00534   if (error != NULL) { err = error; }
00535 
00536   if (state != NULL)
00537   {
00538     return splt_sp_get_splitpoints(state, splitpoints_number);
00539   }
00540   else
00541   {
00542     *err = SPLT_ERROR_STATE_NULL;
00543     return 0;
00544   }
00545 }
00546 
00552 void mp3splt_erase_all_splitpoints(splt_state *state, int *error)
00553 {
00554   int erro = SPLT_OK;
00555   int *err = &erro;
00556   if (error != NULL) { err = error; }
00557 
00558   if (state != NULL)
00559   {
00560     if (!splt_o_library_locked(state))
00561     {
00562       splt_o_lock_library(state);
00563 
00564       splt_sp_free_splitpoints(state);
00565 
00566       splt_o_unlock_library(state);
00567     }
00568     else
00569     {
00570       *err = SPLT_ERROR_LIBRARY_LOCKED;
00571     }
00572   }
00573   else
00574   {
00575     *err = SPLT_ERROR_STATE_NULL;
00576   }
00577 }
00578 
00579 /************************************/
00580 /* Tags                             */
00581 
00583 int mp3splt_append_tags(splt_state *state, 
00584     const char *title, const char *artist,
00585     const char *album, const char *performer,
00586     const char *year, const char *comment,
00587     int track, const char *genre)
00588 {
00589   int error = SPLT_OK;
00590 
00591   if (state != NULL)
00592   {
00593     if (!splt_o_library_locked(state))
00594     {
00595       splt_o_lock_library(state);
00596 
00597       error = splt_tu_append_tags(state, title, artist,
00598           album, performer, year, comment, track, genre);
00599 
00600       splt_o_unlock_library(state);
00601     }
00602     else
00603     {
00604       error = SPLT_ERROR_LIBRARY_LOCKED;
00605     }
00606   }
00607   else
00608   {
00609     error = SPLT_ERROR_STATE_NULL;
00610   }
00611 
00612   return error;
00613 }
00614 
00616 const splt_tags *mp3splt_get_tags(splt_state *state,
00617     int *tags_number, int *error)
00618 {
00619   int erro = SPLT_OK;
00620   int *err = &erro;
00621   if (error != NULL) { err = error; }
00622 
00623   if (state != NULL)
00624   {
00625     return splt_tu_get_tags(state,tags_number);
00626   }
00627   else
00628   {
00629     *err = SPLT_ERROR_STATE_NULL;
00630     return NULL;
00631   }
00632 }
00633 
00635 int mp3splt_put_tags_from_string(splt_state *state, const char *tags, int *error)
00636 {
00637   int ambigous = SPLT_FALSE;
00638   int erro = SPLT_OK;
00639   int *err = &erro;
00640   if (error != NULL) { err = error; }
00641 
00642   if (state != NULL)
00643   {
00644     if (!splt_o_library_locked(state))
00645     {
00646       splt_o_lock_library(state);
00647 
00648       ambigous = splt_tp_put_tags_from_string(state, tags, err);
00649 
00650       splt_o_unlock_library(state);
00651     }
00652     else
00653     {
00654       *err = SPLT_ERROR_LIBRARY_LOCKED;
00655     }
00656   }
00657   else
00658   {
00659     *err = SPLT_ERROR_STATE_NULL;
00660   }
00661 
00662   return ambigous;
00663 }
00664 
00666 void mp3splt_erase_all_tags(splt_state *state, int *error)
00667 {
00668   int erro = SPLT_OK;
00669   int *err = &erro;
00670   if (error != NULL) { err = error; }
00671 
00672   if (state != NULL)
00673   {
00674     if (!splt_o_library_locked(state))
00675     {
00676       splt_o_lock_library(state);
00677 
00678       splt_tu_free_tags(state);
00679 
00680       splt_o_unlock_library(state);
00681     }
00682     else
00683     {
00684       *err = SPLT_ERROR_LIBRARY_LOCKED;
00685     }
00686   }
00687   else
00688   {
00689     *err = SPLT_ERROR_STATE_NULL;
00690   }
00691 }
00692 
00693 /************************************/
00694 /* Options                          */
00695 
00696 static int mp3splt_set_option(splt_state *state, int option_name, void *value)
00697 {
00698   int error = SPLT_OK;
00699 
00700   if (state != NULL)
00701   {
00702     if (!splt_o_library_locked(state))
00703     {
00704       splt_o_lock_library(state);
00705 
00706       splt_o_set_option(state, option_name, value);
00707 
00708       splt_o_unlock_library(state);
00709     }
00710     else
00711     {
00712       error = SPLT_ERROR_LIBRARY_LOCKED;
00713     }
00714   }
00715   else
00716   {
00717     error = SPLT_ERROR_STATE_NULL;
00718   }
00719 
00720   return error;
00721 }
00722 
00723 int mp3splt_set_int_option(splt_state *state, int option_name, int value)
00724 {
00725   return mp3splt_set_option(state, option_name, &value);
00726 }
00727 
00728 int mp3splt_set_long_option(splt_state *state, int option_name, long value)
00729 {
00730   return mp3splt_set_option(state, option_name, &value);
00731 }
00732 
00733 int mp3splt_set_float_option(splt_state *state, int option_name, float value)
00734 {
00735   return mp3splt_set_option(state, option_name, &value);
00736 }
00737 
00738 int mp3splt_get_int_option(splt_state *state, int option_name, int *error)
00739 {
00740   int erro = SPLT_OK;
00741   int *err = &erro;
00742   if (error != NULL) { err = error; }
00743 
00744   if (state != NULL)
00745   {
00746     return splt_o_get_int_option(state, option_name);
00747   }
00748   else
00749   {
00750     *err = SPLT_ERROR_STATE_NULL;
00751     return 0;
00752   }
00753 }
00754 
00755 long mp3splt_get_long_option(splt_state *state, int option_name, int *error)
00756 {
00757   int erro = SPLT_OK;
00758   int *err = &erro;
00759   if (error != NULL) { err = error; }
00760 
00761   if (state != NULL)
00762   {
00763     return splt_o_get_long_option(state, option_name);
00764   }
00765   else
00766   {
00767     *err = SPLT_ERROR_STATE_NULL;
00768     return 0;
00769   }
00770 }
00771 
00772 float mp3splt_get_float_option(splt_state *state, int option_name, int *error)
00773 {
00774   int erro = SPLT_OK;
00775   int *err = &erro;
00776   if (error != NULL) { err = error; }
00777 
00778   if (state != NULL)
00779   {
00780     return splt_o_get_float_option(state, option_name);
00781   }
00782   else
00783   {
00784     *err = SPLT_ERROR_STATE_NULL;
00785     return 0;
00786   }
00787 }
00788 
00789 /************************************/
00790 /* Split functions                  */
00791 
00797 int mp3splt_split(splt_state *state)
00798 {
00799   int error = SPLT_OK;
00800 
00801   if (state != NULL)
00802   {
00803     if (!splt_o_library_locked(state))
00804     {
00805       splt_o_lock_library(state);
00806 
00807       splt_d_print_debug(state,"Starting to split file ...\n");
00808 
00809       char *new_filename_path = NULL;
00810       char *fname_to_split = splt_t_get_filename_to_split(state);
00811 
00812       splt_d_print_debug(state,"Original filename/path to split is _%s_\n", fname_to_split);
00813 
00814       if (splt_io_input_is_stdin(state))
00815       {
00816         splt_o_set_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE, SPLT_TRUE);
00817       }
00818 
00819       splt_t_set_stop_split(state, SPLT_FALSE);
00820 
00821       splt_o_set_default_iopts(state);
00822 
00823       //we put the real splitnumber in the splitnumber variable
00824       //that could be changed (see splitnumber in mp3splt.h)
00825       state->split.splitnumber = state->split.real_splitnumber;
00826       splt_t_set_current_split(state,0);
00827 
00828       if (!splt_io_check_if_file(state, fname_to_split))
00829       {
00830         error = SPLT_ERROR_INEXISTENT_FILE;
00831         splt_o_unlock_library(state);
00832         return error;
00833       }
00834 
00835 #ifndef __WIN32__
00836       char *linked_fname = splt_io_get_linked_fname(fname_to_split, NULL);
00837       if (linked_fname)
00838       {
00839         splt_c_put_info_message_to_client(state, 
00840             _(" info: resolving linked filename to '%s'\n"), linked_fname);
00841 
00842         splt_t_set_filename_to_split(state, linked_fname);
00843         fname_to_split = splt_t_get_filename_to_split(state);
00844 
00845         free(linked_fname);
00846         linked_fname = NULL;
00847       }
00848 #endif
00849 
00850       //if the new_filename_path is "", we put the directory of
00851       //the current song
00852       new_filename_path = splt_check_put_dir_of_cur_song(fname_to_split,
00853           splt_t_get_path_of_split(state), &error);
00854       if (error < 0)
00855       {
00856         splt_o_unlock_library(state);
00857         return error;
00858       }
00859 
00860       //checks and sets correct options
00861       splt_check_set_correct_options(state);
00862 
00863       //if we have compatible options
00864       //this function is optional,
00865       if (! splt_check_compatible_options(state))
00866       {
00867         error = SPLT_ERROR_INCOMPATIBLE_OPTIONS;
00868         goto function_end;
00869       }
00870 
00871       int split_type = splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE);
00872 
00873       //normal split checks
00874       if (split_type == SPLT_OPTION_NORMAL_MODE)
00875       {
00876         if (! splt_o_get_int_option(state, SPLT_OPT_PRETEND_TO_SPLIT))
00877         {
00878           //check if we have at least 2 splitpoints
00879           if (splt_t_get_splitnumber(state) < 2)
00880           {
00881             error = SPLT_ERROR_SPLITPOINTS;
00882             goto function_end;
00883           }
00884         }
00885 
00886         //we check if the splitpoints are in order
00887         splt_check_if_points_in_order(state, &error);
00888         if (error < 0) { goto function_end; }
00889       }
00890 
00891       splt_t_set_new_filename_path(state, new_filename_path, &error);
00892       if (error < 0) { goto function_end; }
00893 
00894       splt_d_print_debug(state, "new fname path = _%s_\n", new_filename_path); 
00895 
00896       error = splt_io_create_directories(state, new_filename_path);
00897       if (error < 0) { goto function_end; }
00898 
00899       splt_check_if_fname_path_is_correct(state, new_filename_path, &error);
00900       if (error < 0) { goto function_end; }
00901 
00902       //we check if mp3 or ogg
00903       splt_check_file_type(state, &error);
00904       if (error < 0) { goto function_end; }
00905 
00906       int tags_option = splt_o_get_int_option(state, SPLT_OPT_TAGS);
00907       if (tags_option == SPLT_TAGS_ORIGINAL_FILE)
00908       {
00909         splt_tp_put_tags_from_string(state, SPLT_ORIGINAL_TAGS_DEFAULT, &error);
00910         if (error < 0) { goto function_end; }
00911       }
00912       else if (tags_option == SPLT_TAGS_FROM_FILENAME_REGEX)
00913       {
00914         int regex_error = SPLT_OK;
00915         splt_tp_put_tags_from_filename(state, &regex_error);
00916         if (regex_error < 0) { error = regex_error; goto function_end; }
00917       }
00918 
00919       const char *plugin_name = splt_p_get_name(state,&error);
00920       if (error < 0) { goto function_end; }
00921 
00922       splt_c_put_info_message_to_client(state, 
00923           _(" info: file matches the plugin '%s'\n"), plugin_name);
00924 
00925       //print the new m3u fname
00926       char *m3u_fname_with_path = splt_t_get_m3u_file_with_path(state, &error);
00927       if (error < 0) { goto function_end; }
00928       if (m3u_fname_with_path)
00929       {
00930         splt_c_put_info_message_to_client(state, 
00931             _(" M3U file '%s' will be created.\n"), m3u_fname_with_path);
00932 
00933         free(m3u_fname_with_path);
00934         m3u_fname_with_path = NULL;
00935       }
00936 
00937       //init the plugin for split
00938       splt_p_init(state, &error);
00939       if (error < 0) { goto function_end; }
00940 
00941       splt_d_print_debug(state,"Parse type of split ...\n");
00942 
00943       if (splt_o_get_int_option(state, SPLT_OPT_AUTO_ADJUST)
00944           && !splt_o_get_int_option(state, SPLT_OPT_QUIET_MODE))
00945       {
00946         if ((split_type != SPLT_OPTION_WRAP_MODE)
00947             && (split_type != SPLT_OPTION_SILENCE_MODE)
00948             && (split_type != SPLT_OPTION_ERROR_MODE))
00949         {
00950           splt_c_put_info_message_to_client(state, 
00951               _(" Working with SILENCE AUTO-ADJUST (Threshold:"
00952                 " %.1f dB Gap: %d sec Offset: %.2f)\n"),
00953               splt_o_get_float_option(state, SPLT_OPT_PARAM_THRESHOLD),
00954               splt_o_get_int_option(state, SPLT_OPT_PARAM_GAP),
00955               splt_o_get_float_option(state, SPLT_OPT_PARAM_OFFSET));
00956         }
00957       }
00958 
00959       switch (split_type)
00960       {
00961         case SPLT_OPTION_WRAP_MODE:
00962           splt_s_wrap_split(state, &error);
00963           break;
00964         case SPLT_OPTION_SILENCE_MODE:
00965           splt_s_silence_split(state, &error);
00966           break; 
00967         case SPLT_OPTION_TIME_MODE:
00968           splt_s_time_split(state, &error);
00969           break;
00970         case SPLT_OPTION_LENGTH_MODE:
00971           splt_s_equal_length_split(state, &error);
00972           break;
00973         case SPLT_OPTION_ERROR_MODE:
00974           splt_s_error_split(state, &error);
00975           break;
00976         default:
00977           if (split_type == SPLT_OPTION_NORMAL_MODE)
00978           {
00979             splt_check_points_inf_song_length(state, &error);
00980             if (error < 0) { goto function_end; }
00981           }
00982 
00983           splt_s_normal_split(state, &error);
00984           break;
00985       }
00986 
00987       //ends the 'init' of the plugin for the split
00988       splt_p_end(state, &error);
00989 
00990 function_end:
00991       if (new_filename_path)
00992       {
00993         free(new_filename_path);
00994         new_filename_path = NULL;
00995       }
00996 
00997       splt_o_unlock_library(state);
00998     }
00999     else
01000     {
01001       error = SPLT_ERROR_LIBRARY_LOCKED;
01002     }
01003   }
01004   else
01005   {
01006     error = SPLT_ERROR_STATE_NULL;
01007   }
01008 
01009   return error;
01010 }
01011 
01015 void mp3splt_stop_split(splt_state *state, int *error)
01016 {
01017   int erro = SPLT_OK;
01018   int *err = &erro;
01019   if (error != NULL) { err = error; }
01020 
01021   if (state != NULL)
01022   {
01023     splt_t_set_stop_split(state, SPLT_TRUE);
01024   }
01025   else
01026   {
01027     *err = SPLT_ERROR_STATE_NULL;
01028   }
01029 }
01030 
01031 /************************************/
01032 /*    Cddb and Cue functions        */
01033 
01040 void mp3splt_put_cue_splitpoints_from_file(splt_state *state,
01041     const char *file, int *error)
01042 {
01043   int erro = SPLT_OK;
01044   int *err = &erro;
01045   if (error != NULL) { err = error; }
01046 
01047   if (state != NULL)
01048   {
01049     if (!splt_o_library_locked(state))
01050     {
01051       splt_o_lock_library(state);
01052 
01053       splt_cue_put_splitpoints(file, state, err);
01054 
01055       splt_o_unlock_library(state);
01056     }
01057     else
01058     {
01059       *err = SPLT_ERROR_LIBRARY_LOCKED;
01060     }
01061   }
01062   else
01063   {
01064     *err = SPLT_ERROR_STATE_NULL;
01065   }
01066 }
01067 
01075 void mp3splt_put_cddb_splitpoints_from_file(splt_state *state,
01076     const char *file, int *error)
01077 {
01078   int erro = SPLT_OK;
01079   int *err = &erro;
01080   if (error != NULL) { err = error; }
01081 
01082   if (state != NULL)
01083   {
01084     if (!splt_o_library_locked(state))
01085     {
01086       splt_o_lock_library(state);
01087 
01088       splt_cddb_put_splitpoints(file, state, err);
01089 
01090       splt_o_unlock_library(state);
01091     }
01092     else
01093     {
01094       *err = SPLT_ERROR_LIBRARY_LOCKED;
01095     }
01096   }
01097   else
01098   {
01099     *err = SPLT_ERROR_STATE_NULL;
01100   }
01101 }
01102 
01110 void mp3splt_put_audacity_labels_splitpoints_from_file(splt_state *state,
01111     const char *file, int *error)
01112 {
01113   int erro = SPLT_OK;
01114   int *err = &erro;
01115   if (error != NULL) { err = error; }
01116 
01117   if (state != NULL)
01118   {
01119     if (!splt_o_library_locked(state))
01120     {
01121       splt_o_lock_library(state);
01122 
01123       splt_audacity_put_splitpoints(file, state, err);
01124 
01125       splt_o_unlock_library(state);
01126     }
01127     else
01128     {
01129       *err = SPLT_ERROR_LIBRARY_LOCKED;
01130     }
01131   }
01132   else
01133   {
01134     *err = SPLT_ERROR_STATE_NULL;
01135   }
01136 }
01137 
01138 /************************************/
01139 /*    Freedb functions              */
01140 
01155 const splt_freedb_results *mp3splt_get_freedb_search(splt_state *state,
01156     const char *search_string,
01157     int *error,
01158     int search_type,
01159     const char search_server[256],
01160     int port)
01161 {
01162   int erro = SPLT_OK;
01163   int *err = &erro;
01164   if (error != NULL) { err = error; }
01165 
01166   if (search_string == NULL)
01167   {
01168     *err = SPLT_FREEDB_NO_CD_FOUND;
01169     return NULL;
01170   }
01171  
01172   if (state != NULL)
01173   {
01174     //we copy the search string, in order not to modify the original one
01175     char *search = strdup(search_string);
01176     if (search != NULL)
01177     {
01178       *err = splt_freedb_process_search(state, search, search_type,
01179           search_server, port);
01180 
01181       free(search);
01182       search = NULL;
01183 
01184       return state->fdb.search_results;
01185     }
01186     else
01187     {
01188       *err = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
01189       return NULL;
01190     }
01191   }
01192   else
01193   {
01194     *err = SPLT_ERROR_STATE_NULL;
01195     return NULL;
01196   }
01197 }
01198 
01220 void mp3splt_write_freedb_file_result(splt_state *state, int disc_id,
01221     const char *cddb_file, int *error, int cddb_get_type,
01222     const char cddb_get_server[256], int port)
01223 {
01224   int erro = SPLT_OK;
01225   int *err = &erro;
01226   if (error != NULL) { err = error; }
01227 
01228   if (state != NULL)
01229   {
01230     if (!splt_o_library_locked(state))
01231     {
01232       splt_o_lock_library(state);
01233 
01234       char *freedb_file_content = NULL;
01235       freedb_file_content = splt_freedb_get_file(state, disc_id, err,
01236           cddb_get_type, cddb_get_server, port);
01237 
01238       //if no error, write file
01239       if (*err == SPLT_FREEDB_FILE_OK)
01240       {
01241         if (! splt_o_get_int_option(state, SPLT_OPT_PRETEND_TO_SPLIT))
01242         {
01243           //we write the result to the file
01244           FILE *output = NULL;
01245           if (!(output = splt_io_fopen(cddb_file, "w")))
01246           {
01247             splt_e_set_strerror_msg_with_data(state, cddb_file);
01248             *err = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01249           }
01250           else
01251           {
01252             fprintf(output,"%s",freedb_file_content);
01253             if (fclose(output) != 0)
01254             {
01255               splt_e_set_strerror_msg_with_data(state, cddb_file);
01256               *err = SPLT_ERROR_CANNOT_CLOSE_FILE;
01257             }
01258             output = NULL;
01259           }
01260         }
01261       }
01262 
01263       //free some memory
01264       if (freedb_file_content)
01265       {
01266         free(freedb_file_content);
01267         freedb_file_content = NULL;
01268       }
01269 
01270       splt_o_unlock_library(state);
01271     }
01272     else
01273     {
01274       *err = SPLT_ERROR_LIBRARY_LOCKED;
01275     }
01276   }
01277   else
01278   {
01279     *err = SPLT_ERROR_STATE_NULL;
01280   }
01281 }
01282 
01293 void mp3splt_export_to_cue(splt_state *state, const char *out_file,
01294     short stop_at_total_time, int *error)
01295 {
01296   int erro = SPLT_OK;
01297   int *err = &erro;
01298   if (error != NULL) { err = error; }
01299 
01300   if (state != NULL)
01301   {
01302     if (!splt_o_library_locked(state))
01303     {
01304       splt_o_lock_library(state);
01305 
01306       splt_cue_export_to_file(state, out_file, stop_at_total_time, err);
01307 
01308       splt_o_unlock_library(state);
01309     }
01310     else
01311     {
01312       *err = SPLT_ERROR_LIBRARY_LOCKED;
01313     }
01314   }
01315   else
01316   {
01317     *err = SPLT_ERROR_STATE_NULL;
01318   }
01319 }
01320 
01322 void mp3splt_set_oformat(splt_state *state,
01323     const char *format_string, int *error)
01324 {
01325   int erro = SPLT_OK;
01326   int *err = &erro;
01327   if (error != NULL) { err = error; }
01328 
01329   if (state != NULL)
01330   {
01331     if (!splt_o_library_locked(state))
01332     {
01333       splt_o_lock_library(state);
01334 
01335       splt_of_set_oformat(state, format_string, err, SPLT_FALSE);
01336 
01337       splt_o_unlock_library(state);
01338     }
01339     else
01340     {
01341       *err = SPLT_ERROR_LIBRARY_LOCKED;
01342     }
01343   }
01344   else
01345   {
01346     *err = SPLT_ERROR_STATE_NULL;
01347   }
01348 }
01349 
01350 /************************************/
01351 /* Other utilities                  */
01352 
01358 const splt_syncerrors *mp3splt_get_syncerrors(splt_state *state,
01359     int *error)
01360 {
01361   int erro = SPLT_OK;
01362   int *err = &erro;
01363   if (error != NULL) { err = error; }
01364 
01365   if (state != NULL)
01366   {
01367     if (!splt_o_library_locked(state))
01368     {
01369       splt_o_lock_library(state);
01370 
01371       //we check the format of the filename
01372       splt_check_file_type(state, err);
01373 
01374       if (*err >= 0)
01375       {
01376         splt_o_lock_messages(state);
01377         splt_p_init(state, err);
01378         if (*err >= 0)
01379         {
01380           splt_o_unlock_messages(state);
01381           splt_p_search_syncerrors(state, err);
01382           splt_p_end(state, err);
01383         }
01384         else
01385         {
01386           splt_o_unlock_messages(state);
01387         }
01388       }
01389 
01390       splt_o_unlock_library(state);
01391 
01392       if (*err < 0)
01393       {
01394         return NULL;
01395       }
01396     }
01397     else
01398     {
01399       *err = SPLT_ERROR_LIBRARY_LOCKED;
01400       return NULL;
01401     }
01402 
01403     return state->serrors;
01404   }
01405   else
01406   {
01407     *err = SPLT_ERROR_STATE_NULL;
01408     return NULL;
01409   }
01410 }
01411 
01417 const splt_wrap *mp3splt_get_wrap_files(splt_state *state,
01418     int *error)
01419 {
01420   int erro = SPLT_OK;
01421   int *err = &erro;
01422   if (error != NULL) { err = error; }
01423 
01424   if (state != NULL)
01425   {
01426     if (!splt_o_library_locked(state))
01427     {
01428       splt_o_lock_library(state);
01429 
01430       //we check the format of the filename
01431       splt_check_file_type(state, err);
01432 
01433       int old_split_mode = splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE);
01434       splt_o_set_int_option(state, SPLT_OPT_SPLIT_MODE, SPLT_OPTION_WRAP_MODE);
01435       if (*err >= 0)
01436       {
01437         splt_o_lock_messages(state);
01438         splt_p_init(state, err);
01439         if (*err >= 0)
01440         {
01441           splt_o_unlock_messages(state);
01442           splt_p_dewrap(state, SPLT_TRUE, NULL, err);
01443           splt_p_end(state, err);
01444         }
01445         else
01446         {
01447           splt_o_unlock_messages(state);
01448         }
01449       }
01450       splt_o_set_int_option(state, SPLT_OPT_SPLIT_MODE, old_split_mode);
01451 
01452       splt_o_unlock_library(state);
01453     }
01454     else
01455     {
01456       *err = SPLT_ERROR_LIBRARY_LOCKED;
01457     }
01458 
01459     return state->wrap;
01460   }
01461   else
01462   {
01463     *err = SPLT_ERROR_STATE_NULL;
01464     return NULL;
01465   }
01466 }
01467 
01469 int mp3splt_set_silence_points(splt_state *state, int *error)
01470 {
01471   int erro = SPLT_OK;
01472   int *err = &erro;
01473   if (error != NULL) { err = error; }
01474 
01475   int silence_mode = SPLT_OPTION_SILENCE_MODE;
01476   mp3splt_set_option(state, SPLT_OPT_SPLIT_MODE, &silence_mode);
01477 
01478   int found_splitpoints = -1;
01479 
01480   if (state != NULL)
01481   {
01482     if (!splt_o_library_locked(state))
01483     {
01484       splt_o_lock_library(state);
01485 
01486       splt_t_set_stop_split(state, SPLT_FALSE);
01487 
01488       splt_check_file_type(state, err);
01489 
01490       if (*err >= 0)
01491       {
01492         splt_p_init(state, err);
01493         if (*err >= 0)
01494         {
01495           found_splitpoints = splt_s_set_silence_splitpoints(state, err);
01496           splt_p_end(state, err);
01497         }
01498       }
01499 
01500       splt_o_unlock_library(state);
01501     }
01502     else
01503     {
01504       *err = SPLT_ERROR_LIBRARY_LOCKED;
01505     }
01506   }
01507   else
01508   {
01509     *err = SPLT_ERROR_STATE_NULL;
01510   }
01511 
01512   return found_splitpoints;
01513 }
01514 
01516 int mp3splt_count_silence_points(splt_state *state, int *error)
01517 {
01518   int number_of_tracks = mp3splt_set_silence_points(state, error) - 1;
01519 
01520   return number_of_tracks;
01521 }
01522 
01524 void mp3splt_get_version(char *version)
01525 {
01526   snprintf(version,20,"%s",SPLT_PACKAGE_VERSION);
01527 }
01528 
01539 char *mp3splt_get_strerror(splt_state *state, int error_code)
01540 {
01541   return splt_e_strerror(state, error_code);
01542 }
01543 
01550 int mp3splt_append_plugins_scan_dir(splt_state *state, char *dir)
01551 {
01552   return splt_p_append_plugin_scan_dir(state, dir);
01553 }
01554 
01555 #ifdef __WIN32__
01556 
01563 char *mp3splt_win32_utf16_to_utf8(const wchar_t *source)
01564 {
01565   return splt_w32_utf16_to_utf8(source);
01566 }
01567 #endif
01568 
01581 char **mp3splt_find_filenames(splt_state *state, const char *filename,
01582     int *num_of_files_found, int *error)
01583 {
01584   int erro = SPLT_OK;
01585   int *err = &erro;
01586   if (error != NULL) { err = error; }
01587 
01588   char **found_files = NULL;
01589 
01590   if (state != NULL)
01591   {
01592     if (!splt_o_library_locked(state))
01593     {
01594       splt_o_lock_library(state);
01595 
01596       *num_of_files_found = 0;
01597 
01598       if (splt_io_check_if_file(state, filename))
01599       {
01600         if (splt_p_file_is_supported_by_plugins(state, filename))
01601         {
01602           found_files = malloc(sizeof(char *));
01603           if (!found_files)
01604           {
01605             *err = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
01606             return NULL;
01607           }
01608 
01609           int fname_size = strlen(filename) + 1;
01610           found_files[0] = malloc(sizeof(char) * fname_size);
01611           memset(found_files[0], '\0', fname_size);
01612 
01613           if (!found_files[0])
01614           {
01615             free(found_files);
01616             return NULL;
01617           }
01618 
01619           strncat(found_files[0], filename, fname_size);
01620           *num_of_files_found = 1;
01621         }
01622       }
01623       else
01624       {
01625         char *dir = strdup(filename);
01626         if (dir == NULL)
01627         {
01628           *err = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
01629           return NULL;
01630         }
01631 
01632         if (dir[strlen(dir)-1] == SPLT_DIRCHAR)
01633         {
01634           dir[strlen(dir)-1] = '\0';
01635         }
01636 
01637         splt_io_find_filenames(state, dir, &found_files, num_of_files_found, err);
01638 
01639         if (dir)
01640         {
01641           free(dir);
01642           dir = NULL;
01643         }
01644       }
01645 
01646       splt_o_unlock_library(state);
01647     }
01648     else
01649     {
01650       *err = SPLT_ERROR_LIBRARY_LOCKED;
01651     }
01652   }
01653   else
01654   {
01655     *err = SPLT_ERROR_STATE_NULL;
01656     return NULL;
01657   }
01658 
01659   return found_files;
01660 }
01661 
01663 
01665 int mp3splt_u_check_if_directory(const char *fname)
01666 {
01667   return splt_io_check_if_directory(fname);
01668 }
01669 
01670 void mp3splt_free_one_tag(splt_tags *tags)
01671 {
01672   splt_tu_free_one_tags(&tags);
01673 }
01674 
01675 splt_tags *mp3splt_parse_filename_regex(splt_state *state, int *error)
01676 {
01677   splt_tags *tags = NULL;
01678   int erro = SPLT_OK;
01679   int *err = &erro;
01680   if (error != NULL) { err = error; }
01681 
01682   if (state != NULL)
01683   {
01684     if (!splt_o_library_locked(state))
01685     {
01686       splt_o_lock_library(state);
01687 
01688       tags = splt_fr_parse_from_state(state, error);
01689 
01690       splt_o_unlock_library(state);
01691     }
01692     else
01693     {
01694       *err = SPLT_ERROR_LIBRARY_LOCKED;
01695     }
01696   }
01697   else
01698   {
01699     *err = SPLT_ERROR_STATE_NULL;
01700   }
01701 
01702   return tags;
01703 }
01704