genwrap.h 9.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* genwrap.h */

/* General cross-platform development wrappers */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html		*
12
 *																			*
13
14
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
15
16
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
17
18
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#ifndef _GENWRAP_H
#define _GENWRAP_H

#include <stdio.h>		/* sprintf */
#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 */
deuce's avatar
deuce committed
49
	#include <strings.h>	/* strcasecmp() */
50
	#include <unistd.h>		/* usleep */
rswindell's avatar
rswindell committed
51

52
	#ifdef _THREAD_SAFE
53
		#include <pthread.h>/* Check for GNU PTH libs */
54
55
56
		#ifdef _PTH_PTHREAD_H_
			#include <pth.h>
		#endif
rswindell's avatar
rswindell committed
57
		#define GetCurrentThreadId()	pthread_self()
58
	#endif
59
60
#elif defined(_WIN32)
	#include <process.h>	/* getpid() */
61
62
#endif

63
64
65
66
67
#if !defined(_WIN32)
	/* Simple Win32 function equivalents */
	#define GetCurrentProcessId()		getpid()
#endif

68
69
70
71
72
73
74
/* utime() support */
#if defined(_MSC_VER) || defined(__WATCOMC__)
	#include <sys/utime.h>
#else
	#include <utime.h>
#endif

75
#if defined(__cplusplus)
76
77
78
79
80
81
82
83
84
85
extern "C" {
#endif

/*********************/
/* Compiler-specific */
/*********************/

/* Compiler Description */
#if defined(__BORLANDC__)

86
	#define DESCRIBE_COMPILER(str) SAFEPRINTF2(str,"BCC %X.%02X" \
87
88
89
90
		,__BORLANDC__>>8,__BORLANDC__&0xff);	

#elif defined(_MSC_VER)

91
	#define DESCRIBE_COMPILER(str) SAFEPRINTF(str,"MSC %u", _MSC_VER);
92

93
94
#elif defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)

95
	#define DESCRIBE_COMPILER(str) SAFEPRINTF3(str,"GCC %u.%u.%u" \
96
97
98
99
		,__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);

#elif defined(__GNUC__) && defined(__GNUC_MINOR__)

100
	#define DESCRIBE_COMPILER(str) SAFEPRINTF2(str,"GCC %u.%u" \
101
102
		,__GNUC__,__GNUC_MINOR__);

rswindell's avatar
rswindell committed
103
104
#elif defined(__WATCOMC__)

105
	#define DESCRIBE_COMPILER(str) SAFEPRINTF(str,"WATC %d" \
rswindell's avatar
rswindell committed
106
107
		,__WATCOMC__);

108
109
#elif defined(__DMC__)	/* Digital Mars C/C++ */

110
	#define DESCRIBE_COMPILER(str) SAFEPRINTF(str,"DMC %X.%02X" \
111
112
		,__DMC__>>8,__DMC__&0xff);	

113
114
#else /* Unknown compiler */

115
	#define DESCRIBE_COMPILER(str) SAFECOPY(str,"UNKNOWN COMPILER");
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

#endif

/**********/
/* Macros */
/**********/

/* Target Platform Description */
#if defined(_WIN64)
	#define PLATFORM_DESC	"Win64"
#elif defined(_WIN32)
	#define PLATFORM_DESC	"Win32"
#elif defined(__OS2__)
	#define PLATFORM_DESC	"OS/2"
#elif defined(__MSDOS__)
	#define PLATFORM_DESC	"DOS"
#elif defined(__linux__)
	#define PLATFORM_DESC	"Linux"
#elif defined(__FreeBSD__)
	#define PLATFORM_DESC	"FreeBSD"
136
137
#elif defined(__OpenBSD__)
	#define PLATFORM_DESC	"OpenBSD"
deuce's avatar
deuce committed
138
139
#elif defined(__NetBSD__)
	#define PLATFORM_DESC	"NetBSD"
deuce's avatar
deuce committed
140
#elif defined(__APPLE__) && defined(__MACH__) && defined(__POWERPC__)
141
	#define PLATFORM_DESC	"MacOSX"
142
143
#elif defined(BSD)
	#define PLATFORM_DESC	"BSD"
144
145
146
147
#elif defined(__solaris__)
	#define PLATFORM_DESC	"Solaris"
#elif defined(__sun__)
	#define PLATFORM_DESC	"SunOS"
148
149
#elif defined(__gnu__)
	#define PLATFORM_DESC	"GNU/Hurd"
deuce's avatar
deuce committed
150
151
#elif defined(__QNX__)
	#define PLATFORM_DESC	"QNX"
152
153
154
#elif defined(__unix__)
	#define PLATFORM_DESC	"Unix"
#else
155
	#error "Need to describe target platform"
156
157
158
159
160
161
#endif

/*********************/
/* String Functionss */
/*********************/

162
163
#define snprintf	safe_snprintf

164
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__DMC__)
165
	#define vsnprintf		_vsnprintf
166
167
#endif

rswindell's avatar
rswindell committed
168
169
170
171
172
#if defined(__WATCOMC__)
	#define vsnprintf(s,l,f,a)	vsprintf(s,f,a)
#endif

#if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
173
174
175
176
177
178
	DLLEXPORT char* DLLCALL ultoa(ulong, char*, int radix);
#endif

#if defined(__unix__)
	DLLEXPORT char*	DLLCALL strupr(char* str);
	DLLEXPORT char*	DLLCALL strlwr(char* str);
179
	DLLEXPORT char* DLLCALL	strrev(char* str);
180
	#if !defined(stricmp)
181
182
		#define stricmp			strcasecmp
		#define strnicmp		strncasecmp
183
184
185
	#endif
#endif

186
187
/* Truncate white-space chars off end of string */
DLLEXPORT char*		DLLCALL truncsp(char* str);
188
189
/* Truncate white-space chars off end of every \n-terminated line in string */
DLLEXPORT char*		DLLCALL truncsp_lines(char* str);
190
191
192
/* Truncate new-line chars off end of string */
DLLEXPORT char*		DLLCALL truncnl(char* str);

193
194
195
196
197
198
#if defined(__unix__)
	#define STRERROR(x)		strerror(x)
#else
	#define STRERROR(x)		truncsp(strerror(x))
#endif

199
200
201
202
203
204
/*********************/
/* Utility Functions */
/*********************/
/* Thunking for multi-threaded specific implementations of "errno" */
DLLEXPORT int DLLCALL	get_errno(void);

205
206
207
/**********************************/
/* Common Utility Macro Functions */
/**********************************/
208
209
210

#if defined(_WIN32)

211
	#define YIELD()			Sleep(1) /* Must sleep at least 1ms to avoid 100% CPU utilization */
212
	#define	MAYBE_YIELD()	Sleep(0)
213
	#define SLEEP(x)		Sleep(x)
214
215
	#define	popen			_popen
	#define pclose			_pclose
216
	#define tzname			_tzname
217
218
219

#elif defined(__OS2__)

220
	#define YIELD()			DosSleep(1)	/* Must sleep at least 1ms to avoid 100% CPU utilization */
221
	#define	MAYBE_YIELD()	DosSleep(0)
222
	#define SLEEP(x)		DosSleep(x)
223

224
#elif defined(__unix__) || defined(__APPLE__)
225

226
	#if defined(_PTH_PTHREAD_H_)
227
228
229
		#define SLEEP(x)		({ int y=x; struct timeval tv; \
								tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
								pth_nap(tv); })
230
231
232
233
234
235
	#else
		#define SLEEP(x)		({	int y=x; struct timeval tv; \
								tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
								select(0,NULL,NULL,NULL,&tv); })
	#endif

236
	#define YIELD()			SLEEP(1)
237

238
239
	#if defined(_THREAD_SAFE)
		#if defined(__FreeBSD__)
240
			#define MAYBE_YIELD()			pthread_yield()
241
		#elif defined(_PTH_PTHREAD_H_)
242
			#define MAYBE_YIELD()			pth_yield(NULL)
243
		#elif defined(_POSIX_PRIORITY_SCHEDULING)
244
			#define MAYBE_YIELD()			sched_yield()
245
		#else
246
			#define MAYBE_YIELD()			YIELD()
247
248
249
		#endif
	#else
		#if defined(_POSIX_PRIORITY_SCHEDULING)
250
			#define	MAYBE_YIELD()			sched_yield()
251
		#else
252
			#define MAYBE_YIELD()			YIELD()
253
254
		#endif
	#endif
255
256
257
258
259
260
261
262
263
264
265
266

	/*
	 * QNX doesn't support fork() in threaded apps (yet) using vfork() instead
	 * works, but relies on undefined behaviours not being nasty.  On most OSs
	 * vfork() will share the stack between the parent and child...
	 */
	#if defined(__QNX__)
		#define FORK()	vfork()
	#else
		#define FORK()	fork()
	#endif

267
268
269

#else	/* Unsupported OS */

270
	#error "Unsupported Target: Need some macros and/or function prototypes here."
271
272

#endif
273

274
275
276
277
278
279
280
/* Command processor/shell environment variable name */
#ifdef __unix__
	#define OS_CMD_SHELL_ENV_VAR	"SHELL"
#else	/* DOS/Windows/OS2 */
	#define OS_CMD_SHELL_ENV_VAR	"COMSPEC"
#endif

281
282
283
284
285
286
/* Win32 implementations of recursive (thread-safe) std C time functions on Unix */

#if !defined(__unix__)	

	#include <time.h>		/* time_t, etc. */

287
288
	DLLEXPORT struct tm*    DLLCALL		gmtime_r(const time_t* t, struct tm* tm);
	DLLEXPORT struct tm*    DLLCALL		localtime_r(const time_t* t, struct tm* tm);
289
290
	DLLEXPORT char*	        DLLCALL		ctime_r(const time_t *t, char *buf);
	DLLEXPORT char*	        DLLCALL		asctime_r(const struct tm *tm, char *buf);
291
292
#endif

293
#if defined(__solaris__)
294
	#define CTIME_R(x,y)	ctime_r(x,y)
deuce's avatar
deuce committed
295
	/* #define CTIME_R(x,y)	ctime_r(x,y,sizeof y) */
296
297
298
#else
	#define CTIME_R(x,y)	ctime_r(x,y)
#endif
299

300
301
/* Mimic the Borland randomize() and random() CRTL functions */
DLLEXPORT unsigned	DLLCALL xp_randomize(void);
302
DLLEXPORT int		DLLCALL	xp_random(int);
303

304
DLLEXPORT long double  	DLLCALL	xp_timer(void);
305
DLLEXPORT char*		DLLCALL os_version(char *str);
306
DLLEXPORT char*		DLLCALL os_cmdshell(void);
307
DLLEXPORT char*		DLLCALL	lastchar(const char* str);
308
DLLEXPORT int		DLLCALL safe_snprintf(char *dst, size_t size, const char *fmt, ...);
309

310
/* C string/char escape-sequence processing */
311
312
313
314
315
DLLEXPORT char*		DLLCALL c_escape_str(const char* src, char* dst, size_t maxlen, BOOL ctrl_only);
DLLEXPORT char*		DLLCALL c_escape_char(char ch);
DLLEXPORT char*		DLLCALL c_unescape_str(char* str);
DLLEXPORT char		DLLCALL c_unescape_char_ptr(const char* str, char** endptr);
DLLEXPORT char		DLLCALL c_unescape_char(char ch);
316

317
/* Microsoft (e.g. DOS/Win32) real-time system clock API (ticks since process started) */
318
typedef		clock_t				msclock_t;
319
#if defined(_WIN32) || defined(__OS2__)
320
321
	#define		MSCLOCKS_PER_SEC	CLOCKS_PER_SEC	/* e.g. 18.2 on DOS, 1000.0 on Win32 */
	#define		msclock()			clock()
322
#else
323
324
	#define		MSCLOCKS_PER_SEC	1000
	msclock_t	msclock(void);
325
#endif
rswindell's avatar
rswindell committed
326

327
#if defined(__cplusplus)
328
329
330
331
}
#endif

#endif	/* Don't add anything after this line */