|
|
1.1 root 1: /* genwrap.h */
2:
3: /* General cross-platform development wrappers */
4:
5: /* $Id: genwrap.h,v 1.69 2004/11/17 11:11:29 rswindell Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This library is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU Lesser General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU Lesser General Public License for more details: lgpl.txt or *
18: * http://www.fsf.org/copyleft/lesser.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #ifndef _GENWRAP_H
39: #define _GENWRAP_H
40:
41: #include <stdio.h> /* sprintf */
42: #include "gen_defs.h" /* ulong */
43: #include "wrapdll.h" /* DLLEXPORT and DLLCALL */
44:
45: #if defined(__unix__)
46: #include <sched.h> /* sched_yield */
47: #include <time.h> /* clock_t */
48: #include <sys/time.h> /* struct timeval */
49: #include <strings.h> /* strcasecmp() */
50: #include <unistd.h> /* usleep */
51: #ifdef _THREAD_SAFE
52: #include <pthread.h>/* Check for GNU PTH libs */
53: #ifdef _PTH_PTHREAD_H_
54: #include <pth.h>
55: #endif
56: #endif
57: #elif defined(_WIN32)
58: #include <process.h> /* getpid() */
59: #endif
60:
61: /* utime() support */
62: #if defined(_MSC_VER) || defined(__WATCOMC__)
63: #include <sys/utime.h>
64: #else
65: #include <utime.h>
66: #endif
67:
68: #if defined(__cplusplus)
69: extern "C" {
70: #endif
71:
72: /*********************/
73: /* Compiler-specific */
74: /*********************/
75:
76: /* Compiler Description */
77: #if defined(__BORLANDC__)
78:
79: #define DESCRIBE_COMPILER(str) SAFEPRINTF2(str,"BCC %X.%02X" \
80: ,__BORLANDC__>>8,__BORLANDC__&0xff);
81:
82: #elif defined(_MSC_VER)
83:
84: #define DESCRIBE_COMPILER(str) SAFEPRINTF(str,"MSC %u", _MSC_VER);
85:
86: #elif defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
87:
88: #define DESCRIBE_COMPILER(str) SAFEPRINTF3(str,"GCC %u.%u.%u" \
89: ,__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);
90:
91: #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
92:
93: #define DESCRIBE_COMPILER(str) SAFEPRINTF2(str,"GCC %u.%u" \
94: ,__GNUC__,__GNUC_MINOR__);
95:
96: #elif defined(__WATCOMC__)
97:
98: #define DESCRIBE_COMPILER(str) SAFEPRINTF(str,"WATC %d" \
99: ,__WATCOMC__);
100:
101: #elif defined(__DMC__) /* Digital Mars C/C++ */
102:
103: #define DESCRIBE_COMPILER(str) SAFEPRINTF(str,"DMC %X.%02X" \
104: ,__DMC__>>8,__DMC__&0xff);
105:
106: #else /* Unknown compiler */
107:
108: #define DESCRIBE_COMPILER(str) SAFECOPY(str,"UNKNOWN COMPILER");
109:
110: #endif
111:
112: /**********/
113: /* Macros */
114: /**********/
115:
116: /* Target Platform Description */
117: #if defined(_WIN64)
118: #define PLATFORM_DESC "Win64"
119: #elif defined(_WIN32)
120: #define PLATFORM_DESC "Win32"
121: #elif defined(__OS2__)
122: #define PLATFORM_DESC "OS/2"
123: #elif defined(__MSDOS__)
124: #define PLATFORM_DESC "DOS"
125: #elif defined(__linux__)
126: #define PLATFORM_DESC "Linux"
127: #elif defined(__FreeBSD__)
128: #define PLATFORM_DESC "FreeBSD"
129: #elif defined(__OpenBSD__)
130: #define PLATFORM_DESC "OpenBSD"
131: #elif defined(__NetBSD__)
132: #define PLATFORM_DESC "NetBSD"
133: #elif defined(__APPLE__) && defined(__MACH__) && defined(__POWERPC__)
134: #define PLATFORM_DESC "MacOSX"
135: #elif defined(BSD)
136: #define PLATFORM_DESC "BSD"
137: #elif defined(__solaris__)
138: #define PLATFORM_DESC "Solaris"
139: #elif defined(__sun__)
140: #define PLATFORM_DESC "SunOS"
141: #elif defined(__gnu__)
142: #define PLATFORM_DESC "GNU/Hurd"
143: #elif defined(__QNX__)
144: #define PLATFORM_DESC "QNX"
145: #elif defined(__unix__)
146: #define PLATFORM_DESC "Unix"
147: #else
148: #error "Need to describe target platform"
149: #endif
150:
151: /*********************/
152: /* String Functionss */
153: /*********************/
154:
155: #define snprintf safe_snprintf
156:
157: #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__DMC__)
158: #define vsnprintf _vsnprintf
159: #endif
160:
161: #if defined(__WATCOMC__)
162: #define vsnprintf(s,l,f,a) vsprintf(s,f,a)
163: #endif
164:
165: #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
166: DLLEXPORT char* DLLCALL ultoa(ulong, char*, int radix);
167: #endif
168:
169: #if defined(__unix__)
170: DLLEXPORT char* DLLCALL strupr(char* str);
171: DLLEXPORT char* DLLCALL strlwr(char* str);
172: DLLEXPORT char* DLLCALL strrev(char* str);
173: #if !defined(stricmp)
174: #define stricmp(x,y) strcasecmp(x,y)
175: #define strnicmp(x,y,z) strncasecmp(x,y,z)
176: #endif
177: #endif
178:
179: /* Truncate white-space chars off end of string */
180: DLLEXPORT char* DLLCALL truncsp(char* str);
181: /* Truncate white-space chars off end of every \n-terminated line in string */
182: DLLEXPORT char* DLLCALL truncsp_lines(char* str);
183: /* Truncate new-line chars off end of string */
184: DLLEXPORT char* DLLCALL truncnl(char* str);
185:
186: #if defined(__unix__)
187: #define STRERROR(x) strerror(x)
188: #else
189: #define STRERROR(x) truncsp(strerror(x))
190: #endif
191:
192: /*********************/
193: /* Utility Functions */
194: /*********************/
195: /* Thunking for multi-threaded specific implementations of "errno" */
196: DLLEXPORT int DLLCALL get_errno(void);
197:
198: /**********************************/
199: /* Common Utility Macro Functions */
200: /**********************************/
201:
202: #if defined(_WIN32)
203:
204: #define YIELD() Sleep(1) /* Must sleep at least 1ms to avoid 100% CPU utilization */
205: #define MAYBE_YIELD() Sleep(0)
206: #define SLEEP(x) Sleep(x)
207: #define BEEP(freq,dur) Beep((DWORD)(freq),(DWORD)(dur))
208: #define popen _popen
209: #define pclose _pclose
210: #define tzname _tzname
211:
212: #elif defined(__OS2__)
213:
214: #define YIELD() DosSleep(1) /* Must sleep at least 1ms to avoid 100% CPU utilization */
215: #define MAYBE_YIELD() DosSleep(0)
216: #define SLEEP(x) DosSleep(x)
217: #define BEEP(freq,dur) DosBeep(freq,dur)
218:
219: #elif defined(__unix__) || defined(__APPLE__)
220:
221: #if defined(_PTH_PTHREAD_H_)
222: #define SLEEP(x) ({ int y=x; struct timeval tv; \
223: tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
224: pth_nap(tv); })
225: #else
226: #define SLEEP(x) ({ int y=x; struct timeval tv; \
227: tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
228: select(0,NULL,NULL,NULL,&tv); })
229: #endif
230:
231: #define YIELD() SLEEP(1)
232:
233: #if defined(_THREAD_SAFE)
234: #if defined(__FreeBSD__)
235: #define MAYBE_YIELD() pthread_yield()
236: #elif defined(_PTH_PTHREAD_H_)
237: #define MAYBE_YIELD() pth_yield(NULL)
238: #elif defined(_POSIX_PRIORITY_SCHEDULING)
239: #define MAYBE_YIELD() sched_yield()
240: #else
241: #define MAYBE_YIELD() YIELD()
242: #endif
243: #else
244: #if defined(_POSIX_PRIORITY_SCHEDULING)
245: #define MAYBE_YIELD() sched_yield()
246: #else
247: #define MAYBE_YIELD() YIELD()
248: #endif
249: #endif
250:
251: /*
252: * QNX doesn't support fork() in threaded apps (yet) using vfork() instead
253: * works, but relies on undefined behaviours not being nasty. On most OSs
254: * vfork() will share the stack between the parent and child...
255: */
256: #if defined(__QNX__)
257: #define FORK() vfork()
258: #else
259: #define FORK() fork()
260: #endif
261:
262: #define BEEP(freq,dur) unix_beep(freq,dur)
263: DLLEXPORT void DLLCALL unix_beep(int freq, int dur);
264:
265: #else /* Unsupported OS */
266:
267: #error "Unsupported Target: Need some macros and/or function prototypes here."
268:
269: #endif
270:
271: /* Win32 implementations of recursive (thread-safe) std C time functions on Unix */
272:
273: #if !defined(__unix__)
274:
275: #include <time.h> /* time_t, etc. */
276:
277: DLLEXPORT struct tm* DLLCALL gmtime_r(const time_t* t, struct tm* tm);
278: DLLEXPORT struct tm* DLLCALL localtime_r(const time_t* t, struct tm* tm);
279: DLLEXPORT char* DLLCALL ctime_r(const time_t *t, char *buf);
280: DLLEXPORT char* DLLCALL asctime_r(const struct tm *tm, char *buf);
281: #endif
282:
283: #if defined(__solaris__)
284: #define CTIME_R(x,y) ctime_r(x,y)
285: /* #define CTIME_R(x,y) ctime_r(x,y,sizeof y) */
286: #else
287: #define CTIME_R(x,y) ctime_r(x,y)
288: #endif
289:
290: DLLEXPORT int DLLCALL xp_random(int);
291: DLLEXPORT char* DLLCALL os_version(char *str);
292: DLLEXPORT char* DLLCALL lastchar(const char* str);
293: DLLEXPORT int DLLCALL safe_snprintf(char *dst, size_t size, const char *fmt, ...);
294:
295: /* C string/char escape-sequence processing */
296: DLLEXPORT char* DLLCALL unescape_cstr(char* str);
297: DLLEXPORT char DLLCALL unescape_char_ptr(const char* str, char** endptr);
298: DLLEXPORT char DLLCALL unescape_char(char ch);
299:
300: #if !defined(__unix__)
301: #define msclock() clock()
302: #define MSCLOCKS_PER_SEC CLOCKS_PER_SEC
303: #else
304: #define MSCLOCKS_PER_SEC 1000
305: clock_t msclock(void);
306: #endif
307:
308: #if defined(__cplusplus)
309: }
310: #endif
311:
312: #endif /* Don't add anything after this line */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.