home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / scc_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-11-28  |  4.9 KB  |  192 lines

  1. /* ScummC
  2.  * Copyright (C) 2004-2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /**
  21.  * @file scc_util.c
  22.  * @ingroup utils
  23.  * @brief Common stuff and portabilty helpers
  24.  */
  25.  
  26. // This file should contain generic stuff that can be helpfull anywhere.
  27. #include "config.h"
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <inttypes.h>
  33. #include <errno.h>
  34. #include <stdarg.h> 
  35.  
  36. #ifdef IS_MINGW
  37. #include <windows.h>
  38. #endif
  39.  
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #include "scc_fd.h"
  44.  
  45. #include "scc_util.h"
  46.  
  47. int scc_log_level = 2;
  48.  
  49. void scc_log(int lvl,char* fmt, ...) {
  50.     va_list ap;
  51.     if(lvl > scc_log_level) return;
  52.     va_start(ap, fmt);
  53.     vfprintf(stderr,fmt,ap);
  54.     va_end(ap);
  55. }
  56.  
  57. // work around incomplete libc's
  58. #ifndef HAVE_ASPRINTF
  59.  
  60. int vasprintf(char **strp, const char *fmt, va_list ap) {
  61.     /* Guess we need no more than 100 bytes. */
  62.     int n, size = 100;
  63.     char *p;
  64.     if ((p = malloc (size)) == NULL)
  65.         return -1;
  66.     while (1) {
  67.         /* Try to print in the allocated space. */
  68.         n = vsnprintf (p, size, fmt, ap);
  69.         /* If that worked, return the string. */
  70.         if (n > -1 && n < size) {
  71.             strp[0] = p;
  72.             return n;
  73.         }
  74.         /* Else try again with more space. */
  75.         if (n > -1)    /* glibc 2.1 */
  76.             size = n+1; /* precisely what is needed */
  77.         else           /* glibc 2.0 */
  78.             size *= 2;  /* twice the old size */
  79.         if ((p = realloc (p, size)) == NULL)
  80.             return -1;
  81.     }
  82. }
  83.  
  84. int asprintf(char **strp, const char *fmt, ...) {
  85.     va_list ap;
  86.     int n;
  87.     va_start(ap, fmt);
  88.     n = vasprintf(strp,fmt,ap);
  89.     va_end(ap);
  90.     return n;
  91. }        
  92.  
  93. #endif
  94.  
  95. // this should probably go into some common util stuff
  96. // or in scc_fd dunno
  97. scc_data_t* scc_data_load(char* path) {
  98.   scc_data_t* data;
  99.   scc_fd_t* fd;
  100.   int len;
  101.  
  102.   fd = new_scc_fd(path,O_RDONLY,0);
  103.   if(!fd) {
  104.     scc_log(LOG_ERR,"Failed to open %s.\n",path);
  105.     return NULL;
  106.   }
  107.   // get file size
  108.   scc_fd_seek(fd,0,SEEK_END);
  109.   len = scc_fd_pos(fd);
  110.   scc_fd_seek(fd,0,SEEK_SET);
  111.  
  112.   data = malloc(sizeof(scc_data_t) + len);
  113.   data->size = len;
  114.  
  115.   if(scc_fd_read(fd,data->data,len) != len) {
  116.     scc_log(LOG_ERR,"Failed to load %s\n",path);
  117.     free(data);
  118.     scc_fd_close(fd);
  119.     return NULL;
  120.   }
  121.  
  122.   scc_fd_close(fd);
  123.   return data;
  124. }
  125.  
  126. //
  127. // Windows glob implementation from MPlayer.
  128. //
  129. #ifdef IS_MINGW
  130.  
  131. int glob(const char *pattern, int flags,
  132.           int (*errfunc)(const char *epath, int eerrno), glob_t *pglob)
  133. {
  134.     HANDLE searchhndl;
  135.     WIN32_FIND_DATA found_file;
  136.  
  137.     if(errfunc)
  138.         scc_log(LOG_ERR,"glob():ERROR: Sorry, errfunc not supported "
  139.                 "by this implementation\n");
  140.     if(flags)
  141.         scc_log(LOG_ERR,"glob():ERROR:Sorry, no flags supported "
  142.                 "by this implementation\n");
  143.  
  144.     //scc_log(LOG_DBG,"PATTERN \"%s\"\n",pattern);
  145.  
  146.     pglob->gl_pathc = 0;
  147.     searchhndl = FindFirstFile( pattern,&found_file);
  148.  
  149.     if(searchhndl == INVALID_HANDLE_VALUE) {
  150.         if(GetLastError() == ERROR_FILE_NOT_FOUND) {
  151.             pglob->gl_pathc = 0;
  152.             //scc_log(LOG_DBG,"could not find a file matching your search criteria\n");
  153.             return 1;
  154.         } else {
  155.             //scc_log(LOG_DBG,"glob():ERROR:FindFirstFile: %i\n",GetLastError());
  156.             return 1;
  157.         }
  158.     }
  159.  
  160.     pglob->gl_pathv = malloc(sizeof(char*));
  161.     pglob->gl_pathv[0] = strdup(found_file.cFileName);
  162.     pglob->gl_pathc++;
  163.  
  164.     while(1) {
  165.         if(!FindNextFile(searchhndl,&found_file)) {
  166.             if(GetLastError()==ERROR_NO_MORE_FILES) {
  167.                 //scc_log(LOG_DBG,"glob(): no more files found\n");
  168.                 break;
  169.             } else {
  170.                 //scc_log(LOG_DBG,"glob():ERROR:FindNextFile:%i\n",GetLastError());
  171.                 return 1;
  172.             }
  173.         } else {
  174.             //scc_log(LOG_DBG,"glob: found file %s\n",found_file.cFileName);
  175.             pglob->gl_pathc++;
  176.             pglob->gl_pathv = realloc(pglob->gl_pathv,pglob->gl_pathc * sizeof(char*));
  177.             pglob->gl_pathv[pglob->gl_pathc-1] = strdup(found_file.cFileName);
  178.         }
  179.     }
  180.     FindClose(searchhndl);
  181.     return 0;
  182. }
  183.  
  184. void globfree(glob_t *pglob) {
  185.     int i;
  186.     for(i=0; i <pglob->gl_pathc ;i++)
  187.         free(pglob->gl_pathv[i]);
  188.     free(pglob->gl_pathv);
  189. }
  190.  
  191. #endif // IS_MINGW
  192.