libmp3splt
src/win32.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 #ifdef __WIN32__
00038 
00039 #include "splt.h"
00040 
00041 int scandir(const char *dir, struct dirent ***namelist,
00042                 int(*filter)(const struct dirent *),
00043                 int(*compar)(const struct dirent **, const struct dirent **))
00044 {
00045   struct dirent **files = NULL;
00046   struct dirent *file = NULL;
00047   DIR *directory = NULL;
00048   int number_of_files = 0;
00049 
00050   directory = opendir(dir);
00051   if (directory == NULL)
00052   {
00053     return -2;
00054   }
00055 
00056   int free_memory = 0;
00057   int we_have_error = 0;
00058 
00059   while ((file = readdir(directory)))
00060   {
00061     if ((filter == NULL) || (filter(file)))
00062     {
00063       if (files == NULL)
00064       {
00065         files = malloc((sizeof *files));
00066       }
00067       else
00068       {
00069         files = realloc(files, (sizeof *files) * (number_of_files + 1));
00070       }
00071       if (files == NULL)
00072       {
00073         free_memory = 1;
00074         we_have_error = 1;
00075         break;
00076       }
00077 
00078       files[number_of_files] = malloc(sizeof(struct dirent));
00079       if (files[number_of_files] == NULL)
00080       {
00081         free_memory = 1;
00082         we_have_error = 1;
00083         break;
00084       }
00085 
00086       *files[number_of_files] = *file;
00087       number_of_files++;
00088     }
00089   }
00090 
00091   if (namelist)
00092   {
00093     *namelist = files;
00094   }
00095   else
00096   {
00097     free_memory = 1;
00098   }
00099 
00100   if (free_memory)
00101   {
00102     while (number_of_files--)
00103     {
00104       if (files[number_of_files])
00105       {
00106         free(files[number_of_files]);
00107         files[number_of_files] = NULL;
00108       }
00109     }
00110     free(files);
00111     files = NULL;
00112   }
00113 
00114   if (closedir(directory) == -1)
00115   {
00116     return -2;
00117   }
00118 
00119   qsort(*namelist, number_of_files, sizeof **namelist,
00120       (int (*)(const void *, const void *)) compar);
00121 
00122   if (we_have_error)
00123   {
00124     errno = ENOMEM;
00125     return -1;
00126   }
00127 
00128   return number_of_files;
00129 }
00130 
00131 int wscandir(const char *dir, struct _wdirent ***namelist,
00132                 int(*filter)(const struct _wdirent *),
00133                 int(*compar)(const struct _wdirent **, const struct _wdirent **))
00134 {
00135   struct _wdirent **files = NULL;
00136   struct _wdirent *file = NULL;
00137   _WDIR *directory = NULL;
00138   int number_of_files = 0;
00139 
00140   wchar_t *wdir = splt_w32_utf8_to_utf16(dir);
00141   directory = _wopendir(wdir);
00142   if (wdir) { free(wdir); wdir = NULL; }
00143   if (directory == NULL)
00144   {
00145     return -2;
00146   }
00147 
00148   int free_memory = 0;
00149   int we_have_error = 0;
00150 
00151   while ((file = _wreaddir(directory)))
00152   {
00153     if ((filter == NULL) || (filter(file)))
00154     {
00155       if (files == NULL)
00156       {
00157         files = malloc((sizeof *files));
00158       }
00159       else
00160       {
00161         files = realloc(files, (sizeof *files) * (number_of_files + 1));
00162       }
00163       if (files == NULL)
00164       {
00165         free_memory = 1;
00166         we_have_error = 1;
00167         break;
00168       }
00169 
00170       files[number_of_files] = malloc(sizeof(struct _wdirent));
00171       if (files[number_of_files] == NULL)
00172       {
00173         free_memory = 1;
00174         we_have_error = 1;
00175         break;
00176       }
00177 
00178       *files[number_of_files] = *file;
00179       number_of_files++;
00180     }
00181   }
00182 
00183   if (namelist)
00184   {
00185     *namelist = files;
00186   }
00187   else
00188   {
00189     free_memory = 1;
00190   }
00191 
00192   if (free_memory)
00193   {
00194     while (number_of_files--)
00195     {
00196       if (files[number_of_files])
00197       {
00198         free(files[number_of_files]);
00199         files[number_of_files] = NULL;
00200       }
00201     }
00202     free(files);
00203     files = NULL;
00204   }
00205 
00206   if (_wclosedir(directory) == -1)
00207   {
00208     return -2;
00209   }
00210 
00211   qsort(*namelist, number_of_files, sizeof **namelist,
00212       (int (*)(const void *, const void *)) compar);
00213 
00214   if (we_have_error)
00215   {
00216     errno = ENOMEM;
00217     return -1;
00218   }
00219 
00220   return number_of_files;
00221 }
00222 
00223 int alphasort(const struct dirent **a, const struct dirent **b)
00224 {
00225   char *name_a = (char *)(*a)->d_name;
00226   char *name_b = (char *)(*b)->d_name;
00227 
00228   return strcoll(name_a, name_b);
00229 }
00230 
00231 int walphasort(const struct _wdirent **a, const struct _wdirent **b)
00232 {
00233   char *name_a = splt_w32_utf16_to_utf8((*a)->d_name);
00234   char *name_b = splt_w32_utf16_to_utf8((*b)->d_name);
00235 
00236   int ret = strcoll(name_a, name_b);
00237 
00238   if (name_a)
00239   {
00240     free(name_a);
00241     name_a = NULL;
00242   }
00243 
00244   if (name_b)
00245   {
00246     free(name_b);
00247     name_b = NULL;
00248   }
00249 
00250   return ret;
00251 }
00252 
00253 static wchar_t *splt_w32_encoding_to_utf16(UINT encoding, const char *source)
00254 {
00255   wchar_t *dest = NULL;
00256 
00257   int converted_size = MultiByteToWideChar(encoding, 0, source, -1, NULL, 0);
00258   if (converted_size > 0)
00259   {
00260     dest = malloc(sizeof(wchar_t) * converted_size);
00261     if (dest)
00262     {
00263       MultiByteToWideChar(encoding, 0, source, -1, dest, converted_size);
00264     }
00265   }
00266 
00267   return dest;
00268 }
00269 
00270 wchar_t *splt_w32_utf8_to_utf16(const char *source)
00271 {
00272   return splt_w32_encoding_to_utf16(CP_UTF8, source);
00273 }
00274 
00275 
00276 static char *splt_w32_utf16_to_encoding(UINT encoding, const wchar_t *source)
00277 {
00278   char *dest = NULL;
00279 
00280   int converted_size = WideCharToMultiByte(encoding, 0, source, -1, NULL, 0, NULL, NULL);
00281   if (converted_size > 0)
00282   {
00283     dest = malloc(sizeof(char *) * converted_size);
00284     if (dest)
00285     {
00286       WideCharToMultiByte(encoding, 0, source, -1, dest, converted_size, NULL, NULL);
00287     }
00288   }
00289 
00290   return dest;
00291 }
00292 
00293 char *splt_w32_utf16_to_utf8(const wchar_t *source)
00294 {
00295   return splt_w32_utf16_to_encoding(CP_UTF8, source);
00296 }
00297 
00298 int splt_w32_check_if_encoding_is_utf8(const char *source)
00299 {
00300   int is_utf8 = SPLT_FALSE;
00301 
00302   if (source)
00303   {
00304     wchar_t *source_wchar = splt_w32_utf8_to_utf16(source);
00305     if (source_wchar)
00306     {
00307       char *source2 = splt_w32_utf16_to_utf8(source_wchar);
00308       if (source2)
00309       {
00310         if (strcmp(source, source2) == 0)
00311         {
00312           is_utf8 = SPLT_TRUE;
00313         }
00314 
00315         free(source2);
00316         source2 = NULL;
00317       }
00318 
00319       free(source_wchar);
00320       source_wchar = NULL;
00321     }
00322   }
00323 
00324   return is_utf8;
00325 }
00326 
00327 int splt_w32_str_starts_with_drive_root_directory(const char *str)
00328 {
00329   if (strlen(str) > 2 &&
00330       str[1] == ':' &&
00331       str[2] == SPLT_DIRCHAR)
00332   {
00333     return SPLT_TRUE;
00334   }
00335 
00336   return SPLT_FALSE;
00337 }
00338 
00339 int splt_w32_str_is_drive_root_directory(const char *str)
00340 {
00341   return strlen(str) == 3 &&
00342     splt_w32_str_starts_with_drive_root_directory(str);
00343 }
00344 
00345 #endif
00346