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.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-11-23  |  8.6 KB  |  224 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. /// @defgroup utils Common utilities
  21. /**
  22.  * @file scc_util.h
  23.  * @ingroup utils
  24.  * @brief Common stuff and portabilty helpers
  25.  */
  26.  
  27.  
  28. #define SCC_SWAP_16(x) ((((x)>>8)&0xFF)|(((x)<<8)&0xFF00))
  29. #define SCC_SWAP_32(x) (  SCC_SWAP_16((uint16_t)(((x)>>16)&0xFFFF)) | (SCC_SWAP_16((uint16_t)(x))<<16) )
  30.  
  31. #ifdef IS_LITTLE_ENDIAN
  32. #define SCC_TO_16BE(x) SCC_SWAP_16(x)
  33. #define SCC_TO_32BE(x) SCC_SWAP_32(x)
  34. #define SCC_TO_16LE(x) (x)
  35. #define SCC_TO_32LE(x) (x)
  36.  
  37. #define MKID(a,b,c,d) ((uint32_t) \
  38.             ((a) & 0x000000FF) | \
  39.             (((b) << 8) & 0x0000FF00) | \
  40.             (((c) << 16) & 0x00FF0000) | \
  41.             (((d) << 24) & 0xFF000000))
  42.  
  43. #define UNMKID(a) (a)&0xFF, ((a)>>8)&0xFF, ((a)>>16)&0xFF, ((a)>>24)&0xFF
  44.  
  45. #define SCC_GET_16(x,at) SCC_GET_16LE(x,at)
  46. #define SCC_GET_32(x,at) SCC_GET_32LE(x,at)
  47.  
  48. #define SCC_SET_16(x,at,v) SCC_SET_16LE(x,at,v)
  49. #define SCC_SET_32(x,at,v) SCC_SET_32LE(x,at,v)
  50.  
  51. #elif defined IS_BIG_ENDIAN
  52. #define SCC_TO_16BE(x) (x)
  53. #define SCC_TO_32BE(x) (x)
  54. #define SCC_TO_16LE(x) SCC_SWAP_16(x)
  55. #define SCC_TO_32LE(x) SCC_SWAP_32(x)
  56.  
  57. #define MKID(a,b,c,d) ((uint32_t) \
  58.             ((d) & 0x000000FF) | \
  59.             (((c) << 8) & 0x0000FF00) | \
  60.             (((b) << 16) & 0x00FF0000) | \
  61.             (((a) << 24) & 0xFF000000))
  62.  
  63. #define UNMKID(a) ((a)>>24)&0xFF, ((a)>>16)&0xFF, ((a)>>8)&0xFF, (a)&0xFF
  64.  
  65. #define SCC_GET_16(x,at) SCC_GET_16BE(x,at)
  66. #define SCC_GET_32(x,at) SCC_GET_32BE(x,at)
  67.  
  68. #define SCC_SET_16(x,at,v) SCC_SET_16BE(x,at,v)
  69. #define SCC_SET_32(x,at,v) SCC_SET_32BE(x,at,v)
  70.  
  71. #else
  72. #error "Endianness is not defined !!!"
  73. #endif
  74.  
  75. #define SCC_SET_16LE(x,at,v)  { uint16_t tmp__ = (v); \
  76.                                 ((uint8_t*)((x)+(at)))[0] = tmp__ & 0xFF; \
  77.                                 ((uint8_t*)((x)+(at)))[1] = (tmp__>>8) & 0xFF; }
  78. #define SCC_SET_S16LE(x,at,v) { int16_t tmp__ = (v); \
  79.                                 ((uint8_t*)((x)+(at)))[0] = tmp__ & 0xFF; \
  80.                                 ((uint8_t*)((x)+(at)))[1] = (tmp__>>8) & 0xFF; }
  81.  
  82. #define SCC_SET_16BE(x,at,v)  { uint16_t tmp__ = (v); \
  83.                                 ((uint8_t*)((x)+(at)))[1] = tmp__ & 0xFF; \
  84.                                 ((uint8_t*)((x)+(at)))[0] = (tmp__>>8) & 0xFF; }
  85. #define SCC_SET_S16BE(x,at,v) { int16_t tmp__ = (v); \
  86.                                 ((uint8_t*)((x)+(at)))[1] = tmp__ & 0xFF; \
  87.                                 ((uint8_t*)((x)+(at)))[0] = (tmp__>>8) & 0xFF; }
  88.  
  89.  
  90. #define SCC_SET_32LE(x,at,v)  { uint32_t tmp__ = (v); \
  91.                                 ((uint8_t*)((x)+(at)))[0] = tmp__ & 0xFF; \
  92.                                 ((uint8_t*)((x)+(at)))[1] = (tmp__>>8) & 0xFF; \
  93.                                 ((uint8_t*)((x)+(at)))[2] = (tmp__>>16) & 0xFF; \
  94.                                 ((uint8_t*)((x)+(at)))[3] = (tmp__>>24) & 0xFF; }
  95. #define SCC_SET_S32LE(x,at,v) { int32_t tmp__ = (v);  \
  96.                                 ((uint8_t*)((x)+(at)))[0] = tmp__ & 0xFF; \
  97.                                 ((uint8_t*)((x)+(at)))[1] = (tmp__>>8) & 0xFF; \
  98.                                 ((uint8_t*)((x)+(at)))[2] = (tmp__>>16) & 0xFF; \
  99.                                 ((uint8_t*)((x)+(at)))[3] = (tmp__>>24) & 0xFF; }
  100.  
  101. #define SCC_SET_32BE(x,at,v)  { uint32_t tmp__ = (v);  \
  102.                                 ((uint8_t*)((x)+(at)))[3] = tmp__ & 0xFF; \
  103.                                 ((uint8_t*)((x)+(at)))[2] = (tmp__>>8) & 0xFF; \
  104.                                 ((uint8_t*)((x)+(at)))[1] = (tmp__>>16) & 0xFF; \
  105.                                 ((uint8_t*)((x)+(at)))[0] = (tmp__>>24) & 0xFF; }
  106. #define SCC_SET_S32BE(x,at,v) { int32_t tmp__ = (v);  \
  107.                                 ((uint8_t*)((x)+(at)))[3] = tmp__ & 0xFF; \
  108.                                 ((uint8_t*)((x)+(at)))[2] = (tmp__>>8) & 0xFF; \
  109.                                 ((uint8_t*)((x)+(at)))[1] = (tmp__>>16) & 0xFF; \
  110.                                 ((uint8_t*)((x)+(at)))[0] = (tmp__>>24) & 0xFF; }
  111.  
  112.  
  113. #define SCC_GET_16LE(x,at)  ((uint16_t) (  ((uint8_t*)((x)+(at)))[0] | \
  114.                                           (((uint8_t*)((x)+(at)))[1] << 8) ) )
  115. #define SCC_GET_S16LE(x,at)  ((int16_t) (  ((uint8_t*)((x)+(at)))[0] | \
  116.                                           (((uint8_t*)((x)+(at)))[1] << 8) ) )
  117.  
  118. #define SCC_GET_16BE(x,at)  ((uint16_t) (  ((uint8_t*)((x)+(at)))[1] | \
  119.                                           (((uint8_t*)((x)+(at)))[0] << 8) ) )
  120. #define SCC_GET_S16BE(x,at)  ((int16_t) (  ((uint8_t*)((x)+(at)))[1] | \
  121.                                           (((uint8_t*)((x)+(at)))[0] << 8) ) )
  122.  
  123. #define SCC_GET_32LE(x,at)  ((uint32_t) (  ((uint8_t*)((x)+(at)))[0] | \
  124.                                           (((uint8_t*)((x)+(at)))[1] << 8) | \
  125.                                           (((uint8_t*)((x)+(at)))[2] << 16) | \
  126.                                           (((uint8_t*)((x)+(at)))[3] << 24) ) )
  127. #define SCC_GET_S32LE(x,at)  ((int32_t) (  ((uint8_t*)((x)+(at)))[0] | \
  128.                                           (((uint8_t*)((x)+(at)))[1] << 8) | \
  129.                                           (((uint8_t*)((x)+(at)))[2] << 16) | \
  130.                                           (((uint8_t*)((x)+(at)))[3] << 24) ) )
  131.  
  132. #define SCC_GET_32BE(x,at)  ((uint32_t) (  ((uint8_t*)((x)+(at)))[3] | \
  133.                                           (((uint8_t*)((x)+(at)))[2] << 8) | \
  134.                                           (((uint8_t*)((x)+(at)))[1] << 16) | \
  135.                                           (((uint8_t*)((x)+(at)))[0] << 24) ) )
  136. #define SCC_GET_S32BE(x,at)  ((int32_t) (  ((uint8_t*)((x)+(at)))[3] | \
  137.                                           (((uint8_t*)((x)+(at)))[2] << 8) | \
  138.                                           (((uint8_t*)((x)+(at)))[1] << 16) | \
  139.                                           (((uint8_t*)((x)+(at)))[0] << 24) ) )
  140.  
  141. // Some lib c (OpenBSD at least) miss the PRI stuff.
  142. // We might need a configure check for that.
  143. #ifndef PRIu64
  144. #define PRIu64 "llu"
  145. #endif
  146.  
  147. // A coomon struct to hold some data
  148. typedef struct scc_data scc_data_t;
  149. struct scc_data {
  150.   uint32_t size;
  151.   uint8_t data[0];
  152. };
  153.  
  154. // the bloody SCC_LIST :)
  155. #define SCC_LIST_ADD(list,last,c) if(c){                  \
  156.   if(last) last->next = c;                                \
  157.   else list = c;                                          \
  158.   for(last = c ; last && last->next ; last = last->next); \
  159. }
  160.  
  161. #define SCC_LIST_FREE(list,last) while(list) {            \
  162.   last = list->next;                                      \
  163.   free(list);                                             \
  164.   list = last;                                            \
  165. }
  166.  
  167. #define SCC_LIST_FREE_CB(list,last,cb) while(list) {      \
  168.   last = list->next;                                      \
  169.   cb(list);                                               \
  170.   list = last;                                            \
  171. }
  172.  
  173. // Locale independant char test
  174. #define SCC_ISALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
  175. #define SCC_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
  176. #define SCC_ISALNUM(c) (SCC_ISALPHA(c) || SCC_ISDIGIT(c))
  177.  
  178. #define SCC_TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? (c)-'a'+'A' : (c))
  179. #define SCC_TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c)-'A'+'a' : (c))
  180.  
  181. #ifndef HAVE_ASPRINTF
  182. #ifndef va_start
  183. #include <stdarg.h>
  184. #endif
  185. int vasprintf(char **strp, const char *fmt, va_list ap);
  186. int asprintf(char **strp, const char *fmt, ...);
  187. #endif
  188.  
  189. #ifdef __GNUC__
  190. #define PRINTF_ATTRIB(fmt,args) __attribute__ ((format (printf, fmt, args)))
  191. #define PACKED_ATTRIB __attribute__ ((__packed__))
  192. #else
  193. #define PRINTF_ATTRIB(fmt,args)
  194. #define PACKED_ATTRIB
  195. #endif
  196.  
  197. scc_data_t* scc_data_load(char* path);
  198.  
  199.  
  200. #ifdef IS_MINGW
  201. typedef struct {
  202.   size_t gl_pathc;
  203.   char **gl_pathv;
  204.   size_t gl_offs;
  205. } glob_t;
  206.  
  207. void globfree(glob_t *pglob);
  208.  
  209. int  glob(const char *pattern, int flags, int (*errfunc)(const char *epath, int eerrno), glob_t *pglob);
  210.  
  211. #endif
  212.  
  213. #define LOG_ERR     0
  214. #define LOG_WARN    1
  215. #define LOG_MSG     2
  216. #define LOG_V       3
  217. #define LOG_DBG     4
  218. #define LOG_DBG1    5
  219. #define LOG_DBG2    6
  220.  
  221. extern int scc_log_level;
  222.  
  223. void scc_log(int lvl,char* msg, ...) PRINTF_ATTRIB(2,3);
  224.