libmp3splt
src/freedb_utils.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-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 
00037 #include <string.h>
00038 
00039 #include "splt.h"
00040 
00041 static void splt_fu_free_freedb_search(splt_state *state);
00042 static int splt_fu_append_first_result(splt_freedb_results *res,
00043     const char *album_name);
00044 static int splt_fu_append_next_result(splt_freedb_results *res,
00045     const char *album_name);
00046 static int splt_fu_append_first_revision(splt_freedb_one_result *prev,
00047     const char *album_name);
00048 static int splt_fu_append_next_revision(splt_freedb_one_result *prev,
00049     const char *album_name);
00050 
00051 void splt_fu_set_default_values(splt_state *state)
00052 {
00053   splt_freedb *fdb = &state->fdb;
00054   fdb->search_results = NULL;
00055   fdb->cdstate = NULL;
00056 }
00057 
00058 void splt_fu_freedb_free_search(splt_state *state)
00059 {
00060   splt_fu_free_freedb_search(state);
00061   splt_cd_state *cdstate = state->fdb.cdstate;
00062   if (cdstate != NULL)
00063   { 
00064     free(cdstate);
00065     cdstate = NULL;
00066   }
00067 }
00068 
00069 int splt_fu_freedb_init_search(splt_state *state)
00070 {
00071   int error = SPLT_OK;
00072   splt_freedb *fdb = &state->fdb;
00073 
00074   if ((fdb->cdstate = malloc(sizeof(splt_cd_state))) == NULL)
00075   {
00076     error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00077   }
00078   else
00079   {
00080     fdb->cdstate->foundcd = 0;
00081     if ((fdb->search_results = malloc(sizeof(splt_freedb_results))) == NULL)
00082     {
00083       free(fdb->cdstate);
00084       fdb->cdstate = NULL;
00085       error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00086     }
00087     else
00088     {
00089       fdb->search_results->number = 0;
00090       fdb->search_results->results = NULL;
00091     }
00092   }
00093 
00094   return error;
00095 }
00096 
00097 int splt_fu_freedb_append_result(splt_state *state, const char *album_name, int revision)
00098 {
00099   splt_freedb_results *res = state->fdb.search_results;
00100 
00101   if (album_name == NULL)
00102   {
00103     return SPLT_OK;
00104   }
00105 
00106   if (res->number == 0)
00107   {
00108     return splt_fu_append_first_result(res, album_name);
00109   }
00110 
00111   if (revision != -1)
00112   {
00113     return splt_fu_append_next_result(res, album_name);
00114   }
00115 
00116   splt_freedb_one_result *prev = &res->results[res->number-1];
00117 
00118   if (prev->revision_number == 0)
00119   {
00120     return splt_fu_append_first_revision(prev, album_name);
00121   }
00122 
00123   return splt_fu_append_next_revision(prev, album_name);
00124 }
00125 
00126 int splt_fu_freedb_get_found_cds(splt_state *state)
00127 {
00128   return state->fdb.cdstate->foundcd;
00129 }
00130 
00131 void splt_fu_freedb_found_cds_next(splt_state *state)
00132 {
00133   state->fdb.cdstate->foundcd = splt_fu_freedb_get_found_cds(state) + 1;
00134 }
00135 
00136 void splt_fu_freedb_set_disc(splt_state *state, int index,
00137     const char *discid, const char *category, int category_size)
00138 {
00139   splt_cd_state *cdstate = state->fdb.cdstate;
00140 
00141   if ((index >= 0) && (index < SPLT_MAXCD))
00142   {
00143     memset(cdstate->discs[index].category, '\0', 20);
00144     snprintf(cdstate->discs[index].category, category_size,"%s",category);
00145 #ifdef __WIN32__
00146     //snprintf seems buggy
00147     cdstate->discs[index].category[category_size-1] = '\0';
00148 #endif
00149     splt_d_print_debug(state,"Setting disc category _%s_\n", cdstate->discs[index].category);
00150 
00151     memset(cdstate->discs[index].discid, '\0', SPLT_DISCIDLEN+1);
00152     snprintf(cdstate->discs[index].discid,SPLT_DISCIDLEN+1,"%s",discid);
00153 #ifdef __WIN32__
00154     //snprintf seems buggy
00155     cdstate->discs[index].discid[SPLT_DISCIDLEN] = '\0';
00156 #endif
00157     splt_d_print_debug(state,"Setting disc id _%s_\n", cdstate->discs[index].discid);
00158   }
00159   else
00160   {
00161     splt_e_error(SPLT_IERROR_INT, __func__, index, NULL);
00162   }
00163 }
00164 
00165 const char *splt_fu_freedb_get_disc_category(splt_state *state, int index)
00166 {
00167   splt_cd_state *cdstate = state->fdb.cdstate;
00168 
00169   if ((index >= 0) && (index < cdstate->foundcd))
00170   {
00171     return cdstate->discs[index].category;
00172   }
00173   else
00174   {
00175     splt_e_error(SPLT_IERROR_INT, __func__, index, NULL);
00176     return NULL;
00177   }
00178 }
00179 
00180 const char *splt_fu_freedb_get_disc_id(splt_state *state, int index)
00181 {
00182   splt_cd_state *cdstate = state->fdb.cdstate;
00183 
00184   if ((index >= 0) && (index < cdstate->foundcd))
00185   {
00186     return cdstate->discs[index].discid;
00187   }
00188   else
00189   {
00190     splt_e_error(SPLT_IERROR_INT, __func__, index, NULL);
00191     return NULL;
00192   }
00193 }
00194 
00195 static void splt_fu_free_freedb_search(splt_state *state)
00196 {
00197   splt_freedb_results *res = state->fdb.search_results;
00198 
00199   if (res)
00200   {
00201     int i = 0;
00202     for(i = 0; i < res->number;i++)
00203     {
00204       if (res->results[i].revisions)
00205       {
00206         free(res->results[i].revisions);
00207         res->results[i].revisions = NULL;
00208       }
00209 
00210       if (res->results[i].name)
00211       {
00212         free(res->results[i].name);
00213         res->results[i].name = NULL;
00214       }
00215     }
00216 
00217     if (res->results)
00218     {
00219       free(res->results);
00220       res->results = NULL;
00221     }
00222 
00223     res->number = 0;
00224 
00225     free(state->fdb.search_results);
00226     state->fdb.search_results = NULL;
00227   }
00228 }
00229 
00230 static int splt_fu_append_first_result(splt_freedb_results *res,
00231     const char *album_name)
00232 {
00233   int error = SPLT_OK;
00234 
00235   res->results = malloc(sizeof(splt_freedb_one_result));
00236   if (res->results == NULL)
00237   {
00238     return SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00239   }
00240   memset(res->results, 0x0, sizeof(splt_freedb_one_result));
00241 
00242   res->results[0].revisions = NULL;
00243   error = splt_su_copy(album_name, &res->results[0].name);
00244   if (error < 0) { return error; }
00245 
00246   res->results[0].revision_number = 0;
00247   res->results[0].id = 0;
00248   res->number++;
00249 
00250   return error;
00251 }
00252 
00253 static int splt_fu_append_next_result(splt_freedb_results *res,
00254     const char *album_name)
00255 {
00256   int error = SPLT_OK;
00257 
00258   res->results = realloc(res->results, (res->number + 1) * sizeof(splt_freedb_one_result));
00259   if (res->results == NULL)
00260   {
00261     return SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00262   }
00263   memset(&res->results[res->number], 0x0, sizeof(splt_freedb_one_result));
00264 
00265   error = splt_su_copy(album_name, &res->results[res->number].name);
00266   if (error < 0) { return error; }
00267 
00268   splt_freedb_one_result *prev = &res->results[res->number-1];
00269 
00270   res->results[res->number].revision_number = 0;
00271   res->results[res->number].id = (prev->id + prev->revision_number + 1);
00272   res->number++;
00273 
00274   return error;
00275 }
00276 
00277 static int splt_fu_append_first_revision(splt_freedb_one_result *prev,
00278     const char *album_name)
00279 {
00280   prev->revisions = malloc(sizeof(int));
00281   if (prev->revisions == NULL)
00282   {
00283     return SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;              
00284   }
00285 
00286   prev->revisions[0] = atoi(album_name);
00287   prev->revision_number++;
00288 
00289   return SPLT_OK;
00290 }
00291 
00292 static int splt_fu_append_next_revision(splt_freedb_one_result *prev,
00293     const char *album_name)
00294 {
00295   prev->revisions = realloc(prev->revisions, (prev->revision_number + 1) * sizeof(int));
00296   if (prev->revisions == NULL)
00297   {
00298     return SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;              
00299   }
00300 
00301   prev->revisions[prev->revision_number] = atoi(album_name);
00302   prev->revision_number++;
00303 
00304   return SPLT_OK;
00305 }
00306