'LibPst'
define.h
Go to the documentation of this file.
1 /***
2  * define.h
3  * Part of the LibPST project
4  * Written by David Smith
5  * dave.s@earthcorp.com
6  */
7 
8 #ifndef DEFINEH_H
9 #define DEFINEH_H
10 
11 #ifdef HAVE_CONFIG_H
12  #include "config.h"
13 #endif
14 
15 #include "libpst.h"
16 #include "timeconv.h"
17 #include "libstrfunc.h"
18 #include "vbuf.h"
19 
20 
21 #ifdef HAVE_STRING_H
22  #include <string.h>
23 #endif
24 
25 #ifdef HAVE_CTYPE_H
26  #include <ctype.h>
27 #endif
28 
29 #ifdef HAVE_LIMITS_H
30  #include <limits.h>
31 #endif
32 
33 #ifdef HAVE_WCHAR_H
34  #include <wchar.h>
35 #endif
36 
37 #ifdef HAVE_SIGNAL_H
38  #include <signal.h>
39 #endif
40 
41 #ifdef HAVE_ERRNO_H
42  #include <errno.h>
43 #endif
44 
45 #ifdef HAVE_ICONV
46  #include <iconv.h>
47 #endif
48 
49 #ifdef HAVE_REGEX_H
50  #include <regex.h>
51 #endif
52 
53 #ifdef HAVE_GD_H
54  #include <gd.h>
55 #endif
56 
57 
58 #define PERM_DIRS 0777
59 
60 #ifdef _WIN32
61  #include <direct.h>
62 
63  #define D_MKDIR(x) mkdir(x)
64  #define chdir _chdir
65  #define strcasecmp _stricmp
66  #define vsnprintf _vsnprintf
67  #define snprintf _snprintf
68  #ifdef _MSC_VER
69  #define ftello _ftelli64
70  #define fseeko _fseeki64
71  #elif defined (__MINGW32__)
72  #define ftello ftello64
73  #define fseeko fseeko64
74  #else
75  #error Only MSC and mingw supported for Windows
76  #endif
77  #ifndef UINT64_MAX
78  #define UINT64_MAX ((uint64_t)0xffffffffffffffff)
79  #endif
80  #ifndef PRIx64
81  #define PRIx64 "I64x"
82  #endif
83  int __cdecl _fseeki64(FILE *, __int64, int);
84  __int64 __cdecl _ftelli64(FILE *);
85 
86  #ifdef __MINGW32__
87  #include <getopt.h>
88  #else
89  #include "XGetopt.h"
90  #endif
91  #include <process.h>
92  #undef gmtime_r
93  #define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
94  #define ctime_r(tp,tmp) (ctime(tp)?(strcpy((tmp),ctime((tp))),(tmp)):0)
95 #else
96  #ifdef __DJGPP__
97  #define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
98  #define ctime_r(tp,tmp) (ctime(tp)?(strcpy((tmp),ctime((tp))),(tmp)):0)
99  #define fseeko(stream, offset, whence) fseek(stream, (long)offset, whence)
100  #define ftello ftell
101  #endif
102  #ifdef HAVE_UNISTD_H
103  #include <unistd.h>
104  #else
105  #include "XGetopt.h"
106  #endif
107  #define D_MKDIR(x) mkdir(x, PERM_DIRS)
108 #endif
109 
110 #ifdef HAVE_SYS_STAT_H
111  #include <sys/stat.h>
112 #endif
113 
114 #ifdef HAVE_SYS_TYPES_H
115  #include <sys/types.h>
116 #endif
117 
118 #ifdef HAVE_SYS_SHM_H
119  #include <sys/shm.h>
120 #endif
121 
122 #ifdef HAVE_SYS_WAIT_H
123  #include <sys/wait.h>
124 #endif
125 
126 #ifdef HAVE_DIRENT_H
127  #include <dirent.h>
128 #endif
129 
130 #ifdef HAVE_SEMAPHORE_H
131  #include <semaphore.h>
132 #endif
133 
134 
135 void pst_debug_lock();
136 void pst_debug_unlock();
137 void pst_debug_setlevel(int level);
138 void pst_debug_init(const char* fname, void* output_mutex);
139 void pst_debug_func(int level, const char* function);
140 void pst_debug_func_ret(int level);
141 void pst_debug(int level, int line, const char *file, const char *fmt, ...);
142 void pst_debug_hexdump(int level, int line, const char *file, const char* buf, size_t size, int cols, int delta);
143 void pst_debug_hexdumper(FILE* out, const char* buf, size_t size, int cols, int delta);
144 void pst_debug_close();
145 void* pst_malloc(size_t size);
146 void *pst_realloc(void *ptr, size_t size);
147 
148 #define MESSAGEPRINT1(...) pst_debug(1, __LINE__, __FILE__, __VA_ARGS__)
149 #define MESSAGEPRINT2(...) pst_debug(2, __LINE__, __FILE__, __VA_ARGS__)
150 #define MESSAGEPRINT3(...) pst_debug(3, __LINE__, __FILE__, __VA_ARGS__)
151 
152 #define WARN(x) { \
153  MESSAGEPRINT3 x; \
154  pst_debug_lock(); \
155  printf x; \
156  fflush(stdout); \
157  pst_debug_unlock(); \
158 }
159 
160 #define DIE(x) { \
161  WARN(x); \
162  exit(EXIT_FAILURE); \
163 }
164 
165 #define DEBUG_WARN(x) MESSAGEPRINT3 x
166 #define DEBUG_INFO(x) MESSAGEPRINT2 x
167 #define DEBUG_HEXDUMP(x, s) pst_debug_hexdump(1, __LINE__, __FILE__, (char*)x, s, 0x10, 0)
168 #define DEBUG_HEXDUMPC(x, s, c) pst_debug_hexdump(1, __LINE__, __FILE__, (char*)x, s, c, 0)
169 
170 
171 #define DEBUG_ENT(x) \
172  { \
173  pst_debug_func(1, x); \
174  pst_debug(1, __LINE__, __FILE__, "Entering function\n"); \
175  }
176 #define DEBUG_RET() \
177  { \
178  pst_debug(1, __LINE__, __FILE__, "Leaving function\n"); \
179  pst_debug_func_ret(1); \
180  }
181 
182 #define DEBUG_INIT(fname,mutex) {pst_debug_init(fname,mutex);}
183 #define DEBUG_CLOSE() {pst_debug_close();}
184 #define RET_DERROR(res, ret_val, x) if (res) { DIE(x);}
185 
186 
187 
188 #if BYTE_ORDER == BIG_ENDIAN
189 # define LE64_CPU(x) \
190  x = ((((x) & UINT64_C(0xff00000000000000)) >> 56) | \
191  (((x) & UINT64_C(0x00ff000000000000)) >> 40) | \
192  (((x) & UINT64_C(0x0000ff0000000000)) >> 24) | \
193  (((x) & UINT64_C(0x000000ff00000000)) >> 8 ) | \
194  (((x) & UINT64_C(0x00000000ff000000)) << 8 ) | \
195  (((x) & UINT64_C(0x0000000000ff0000)) << 24) | \
196  (((x) & UINT64_C(0x000000000000ff00)) << 40) | \
197  (((x) & UINT64_C(0x00000000000000ff)) << 56));
198 # define LE32_CPU(x) \
199  x = ((((x) & 0xff000000) >> 24) | \
200  (((x) & 0x00ff0000) >> 8 ) | \
201  (((x) & 0x0000ff00) << 8 ) | \
202  (((x) & 0x000000ff) << 24));
203 # define LE16_CPU(x) \
204  x = ((((x) & 0xff00) >> 8) | \
205  (((x) & 0x00ff) << 8));
206 #elif BYTE_ORDER == LITTLE_ENDIAN
207 # define LE64_CPU(x) {}
208 # define LE32_CPU(x) {}
209 # define LE16_CPU(x) {}
210 #else
211 # error "Byte order not supported by this library"
212 #endif // BYTE_ORDER
213 
214 
215 #define PST_LE_GET_UINT64(p) \
216  (uint64_t)((((uint8_t const *)(p))[0] << 0) | \
217  (((uint8_t const *)(p))[1] << 8) | \
218  (((uint8_t const *)(p))[2] << 16) | \
219  (((uint8_t const *)(p))[3] << 24) | \
220  (((uint8_t const *)(p))[4] << 32) | \
221  (((uint8_t const *)(p))[5] << 40) | \
222  (((uint8_t const *)(p))[6] << 48) | \
223  (((uint8_t const *)(p))[7] << 56))
224 
225 #define PST_LE_GET_INT64(p) \
226  (int64_t)((((uint8_t const *)(p))[0] << 0) | \
227  (((uint8_t const *)(p))[1] << 8) | \
228  (((uint8_t const *)(p))[2] << 16) | \
229  (((uint8_t const *)(p))[3] << 24) | \
230  (((uint8_t const *)(p))[4] << 32) | \
231  (((uint8_t const *)(p))[5] << 40) | \
232  (((uint8_t const *)(p))[6] << 48) | \
233  (((uint8_t const *)(p))[7] << 56))
234 
235 #define PST_LE_GET_UINT32(p) \
236  (uint32_t)((((uint8_t const *)(p))[0] << 0) | \
237  (((uint8_t const *)(p))[1] << 8) | \
238  (((uint8_t const *)(p))[2] << 16) | \
239  (((uint8_t const *)(p))[3] << 24))
240 
241 #define PST_LE_GET_INT32(p) \
242  (int32_t)((((uint8_t const *)(p))[0] << 0) | \
243  (((uint8_t const *)(p))[1] << 8) | \
244  (((uint8_t const *)(p))[2] << 16) | \
245  (((uint8_t const *)(p))[3] << 24))
246 
247 #define PST_LE_GET_UINT16(p) \
248  (uint16_t)((((uint8_t const *)(p))[0] << 0) | \
249  (((uint8_t const *)(p))[1] << 8))
250 
251 #define PST_LE_GET_INT16(p) \
252  (int16_t)((((uint8_t const *)(p))[0] << 0) | \
253  (((uint8_t const *)(p))[1] << 8))
254 
255 #define PST_LE_GET_UINT8(p) (*(uint8_t const *)(p))
256 
257 #define PST_LE_GET_INT8(p) (*(int8_t const *)(p))
258 
259 #define MAXDATEFMTLEN 40
260 
261 #endif //DEFINEH_H
void pst_debug_unlock()
Definition: debug.c:36
void pst_debug_func_ret(int level)
Definition: debug.c:69
void pst_debug_hexdump(int level, int line, const char *file, const char *buf, size_t size, int cols, int delta)
Definition: debug.c:109
void * pst_realloc(void *ptr, size_t size)
Definition: debug.c:179
void pst_debug_init(const char *fname, void *output_mutex)
Definition: debug.c:44
void pst_debug_lock()
Definition: debug.c:28
void pst_debug_hexdumper(FILE *out, const char *buf, size_t size, int cols, int delta)
Definition: debug.c:119
void pst_debug_func(int level, const char *function)
Definition: debug.c:59
void * pst_malloc(size_t size)
Definition: debug.c:169
void pst_debug_close()
Definition: debug.c:157
void pst_debug_setlevel(int level)
Definition: debug.c:23
void pst_debug(int level, int line, const char *file, const char *fmt,...)
Definition: debug.c:95