libmp3splt
src/client.c
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 
00033 #include <string.h>
00034 
00035 #include "splt.h"
00036 
00037 static void splt_c_put_message_to_client(splt_state *state, const char *message,
00038     splt_message_type mess_type);
00039 static int splt_c_append_to_m3u_file(splt_state *state, const char *filename);
00040 static void splt_c_set_filename_shorted_from_current_point_name(splt_state *state);
00041 
00042 int splt_c_put_split_file(splt_state *state, const char *filename)
00043 {
00044   int error = SPLT_OK;
00045 
00046   if (state->split.file_split != NULL)
00047   {
00048     state->split.file_split(filename, state->split.p_bar->user_data);
00049     error = splt_c_append_to_m3u_file(state, filename);
00050   }
00051   else
00052   {
00053     //splt_e_error(SPLT_IERROR_INT,__func__, -500, NULL);
00054   }
00055 
00056   return error;
00057 }
00058 
00059 void splt_c_put_progress_text(splt_state *state, int type)
00060 {
00061   splt_progress *p_bar = state->split.p_bar;
00062   if (p_bar->progress == NULL) { return; }
00063 
00064   splt_c_set_filename_shorted_from_current_point_name(state);
00065 
00066   p_bar->current_split = splt_t_get_current_split_file_number(state);
00067   p_bar->max_splits = state->split.splitnumber - 1;
00068   p_bar->progress_type = type;
00069 }
00070 
00071 void splt_c_put_info_message_to_client(splt_state *state, const char *message, ...)
00072 {
00073   va_list ap;
00074   char *mess = NULL;
00075 
00076   va_start(ap, message);
00077   mess = splt_su_format_messagev(state, message, ap);
00078   va_end(ap);
00079 
00080   if (mess)
00081   {
00082     splt_c_put_message_to_client(state, mess, SPLT_MESSAGE_INFO);
00083 
00084     free(mess);
00085     mess = NULL;
00086   }
00087 }
00088 
00089 void splt_c_put_debug_message_to_client(splt_state *state, const char *message, ...)
00090 {
00091   va_list ap;
00092   char *mess = NULL;
00093 
00094   va_start(ap, message);
00095   mess = splt_su_format_messagev(state, message, ap);
00096   va_end(ap);
00097 
00098   if (mess)
00099   {
00100     splt_c_put_message_to_client(state, mess, SPLT_MESSAGE_DEBUG);
00101 
00102     free(mess);
00103     mess = NULL;
00104   }
00105 }
00106 
00107 void splt_c_update_progress(splt_state *state, double current_point,
00108     double total_points, int progress_stage,
00109     float progress_start, int refresh_rate)
00110 {
00111   splt_progress *p_bar = state->split.p_bar;
00112   if (p_bar->progress == NULL) { return; }
00113 
00114   if (splt_o_get_iopt(state, SPLT_INTERNAL_PROGRESS_RATE) > refresh_rate)
00115   {
00116     p_bar->percent_progress = (float) (current_point / total_points);
00117 
00118     p_bar->percent_progress = p_bar->percent_progress / progress_stage + progress_start;
00119 
00120     if (p_bar->percent_progress < 0)
00121     {
00122       p_bar->percent_progress = 0;
00123     }
00124     if (p_bar->percent_progress > 1)
00125     {
00126       p_bar->percent_progress = 1;
00127     }
00128 
00129     p_bar->progress(p_bar);
00130     splt_o_set_iopt(state, SPLT_INTERNAL_PROGRESS_RATE, 0);
00131   }
00132   else
00133   {
00134     splt_o_set_iopt(state, SPLT_INTERNAL_PROGRESS_RATE,
00135         splt_o_get_iopt(state, SPLT_INTERNAL_PROGRESS_RATE) + 1);
00136   }
00137 }
00138 
00139 static void splt_c_put_message_to_client(splt_state *state, const char *message,
00140     splt_message_type mess_type)
00141 {
00142   if (!splt_o_messages_locked(state))
00143   {
00144     if (state->split.put_message != NULL)
00145     {
00146       state->split.put_message(message, mess_type);
00147     }
00148     else
00149     {
00150       //splt_e_error(SPLT_IERROR_INT,__func__, -500, NULL);
00151     }
00152   }
00153 }
00154 
00155 static int splt_c_append_to_m3u_file(splt_state *state, const char *filename)
00156 {
00157   int err = SPLT_OK;
00158 
00159   if (splt_o_get_int_option(state, SPLT_OPT_PRETEND_TO_SPLIT))
00160   {
00161     return err;
00162   }
00163 
00164   char *new_m3u_file = splt_t_get_m3u_file_with_path(state, &err); 
00165   if (err < 0 || !new_m3u_file) { return err; }
00166 
00167   FILE *file_input = NULL;
00168   if ((file_input = splt_io_fopen(new_m3u_file, "a+")) != NULL)
00169   {
00170     fprintf(file_input, "%s\n", splt_su_get_fname_without_path(filename));
00171 
00172     if (fclose(file_input) != 0)
00173     {
00174       splt_e_set_strerror_msg_with_data(state, new_m3u_file);
00175       err = SPLT_ERROR_CANNOT_CLOSE_FILE;
00176     }
00177   }
00178   else
00179   {
00180     splt_e_set_strerror_msg_with_data(state, new_m3u_file);
00181     err = SPLT_ERROR_CANNOT_OPEN_FILE;
00182   }
00183 
00184   free(new_m3u_file);
00185   new_m3u_file = NULL;
00186 
00187   return err;
00188 }
00189 
00190 static void splt_c_set_filename_shorted_from_current_point_name(splt_state *state)
00191 {
00192   int err = SPLT_OK;
00193   splt_progress *p_bar = state->split.p_bar;
00194 
00195   char filename_shorted[512] = { '\0' };
00196 
00197   int curr_split = splt_t_get_current_split(state);
00198   const char *point_name = splt_sp_get_splitpoint_name(state, curr_split, &err);
00199   if (point_name != NULL)
00200   {
00201     const char *extension = splt_p_get_extension(state, &err);
00202     if (err >= 0)
00203     {
00204       size_t max_size = p_bar->progress_text_max_char;
00205       if (max_size >= 512) { max_size = 511; }
00206 
00207       snprintf(filename_shorted, max_size, "%s%s", point_name, extension);
00208 
00209       if (strlen(point_name) > max_size)
00210       {
00211         size_t size = strlen(filename_shorted);
00212         filename_shorted[size-1] = '.';
00213         filename_shorted[size-2] = '.';
00214         filename_shorted[size-3] = '.';
00215       }
00216     }
00217   }
00218 
00219   snprintf(p_bar->filename_shorted, 512, "%s", filename_shorted);
00220 }
00221