genwrap.h 8.04 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 2003 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 */
51
	#ifdef _THREAD_SAFE
52
		#include <pthread.h>/* Check for GNU PTH libs */
53
54
55
		#ifdef _PTH_PTHREAD_H_
			#include <pth.h>
		#endif
56
	#endif
57
58
#elif defined(_WIN32)
	#include <process.h>	/* getpid() */
59
60
#endif

61
62
63
64
65
66
67
/* utime() support */
#if defined(_MSC_VER) || defined(__WATCOMC__)
	#include <sys/utime.h>
#else
	#include <utime.h>
#endif

68
#if defined(__cplusplus)
69
70
71
72
73
74
75
76
77
78
extern "C" {
#endif

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

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

79
	#define DESCRIBE_COMPILER(str) sprintf(str,"BCC %X.%02X" \
80
81
82
83
		,__BORLANDC__>>8,__BORLANDC__&0xff);	

#elif defined(_MSC_VER)

84
	#define DESCRIBE_COMPILER(str) sprintf(str,"MSC %u", _MSC_VER);
85
86
87

#elif defined(__GNUC__)

88
	#define DESCRIBE_COMPILER(str) sprintf(str,"GCC %u.%02u" \
89
90
		,__GNUC__,__GNUC_MINOR__);

rswindell's avatar
rswindell committed
91
92
93
94
95
#elif defined(__WATCOMC__)

	#define DESCRIBE_COMPILER(str) sprintf(str,"WATC %d" \
		,__WATCOMC__);

96
97
98
99
100
#elif defined(__DMC__)	/* Digital Mars C/C++ */

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

101
102
#else /* Unknown compiler */

103
	#define DESCRIBE_COMPILER(str) strcpy(str,"UNKNOWN COMPILER");
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#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"
124
125
#elif defined(__OpenBSD__)
	#define PLATFORM_DESC	"OpenBSD"
deuce's avatar
deuce committed
126
127
#elif defined(__NetBSD__)
	#define PLATFORM_DESC	"NetBSD"
deuce's avatar
deuce committed
128
129
#elif defined(__APPLE__) && defined(__MACH__) && defined(__POWERPC__)
	#define PLATFORM_DESC	"Mac OS X"
130
131
#elif defined(BSD)
	#define PLATFORM_DESC	"BSD"
132
133
134
135
#elif defined(__solaris__)
	#define PLATFORM_DESC	"Solaris"
#elif defined(__sun__)
	#define PLATFORM_DESC	"SunOS"
136
137
#elif defined(__gnu__)
	#define PLATFORM_DESC	"GNU/Hurd"
deuce's avatar
deuce committed
138
139
#elif defined(__QNX__)
	#define PLATFORM_DESC	"QNX"
140
141
142
#elif defined(__unix__)
	#define PLATFORM_DESC	"Unix"
#else
143
	#error "Need to describe target platform"
144
145
146
147
148
149
#endif

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

150
151
#define snprintf	safe_snprintf

152
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__DMC__)
153
	#define vsnprintf		_vsnprintf
154
155
#endif

rswindell's avatar
rswindell committed
156
157
158
159
160
#if defined(__WATCOMC__)
	#define vsnprintf(s,l,f,a)	vsprintf(s,f,a)
#endif

#if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
161
162
163
164
165
166
	DLLEXPORT char* DLLCALL ultoa(ulong, char*, int radix);
#endif

#if defined(__unix__)
	DLLEXPORT char*	DLLCALL strupr(char* str);
	DLLEXPORT char*	DLLCALL strlwr(char* str);
167
	DLLEXPORT char* DLLCALL	strrev(char* str);
168
	#if !defined(stricmp)
169
170
171
172
173
		#define stricmp(x,y)		strcasecmp(x,y)
		#define strnicmp(x,y,z)		strncasecmp(x,y,z)
	#endif
#endif

174
175
176
177
178
179
#if defined(__unix__)
	#define STRERROR(x)		strerror(x)
#else
	#define STRERROR(x)		truncsp(strerror(x))
#endif

180
181
182
/**********************************/
/* Common Utility Macro Functions */
/**********************************/
183
184
185

#if defined(_WIN32)

186
	#define YIELD()			Sleep(1) /* Must sleep at least 1ms to avoid 100% CPU utilization */
187
	#define	MAYBE_YIELD()	Sleep(0)
188
	#define SLEEP(x)		Sleep(x)
189
	#define BEEP(freq,dur)	Beep((DWORD)(freq),(DWORD)(dur))
190
191
	#define	popen			_popen
	#define pclose			_pclose
192
	#define tzname			_tzname
193
194
195

#elif defined(__OS2__)

196
	#define YIELD()			DosSleep(1)	/* Must sleep at least 1ms to avoid 100% CPU utilization */
197
	#define	MAYBE_YIELD()	DosSleep(0)
198
199
	#define SLEEP(x)		DosSleep(x)
	#define BEEP(freq,dur)	DosBeep(freq,dur)
200
201
202

#elif defined(__unix__)

203
	#if defined(_PTH_PTHREAD_H_)
204
205
206
		#define SLEEP(x)		({ int y=x; struct timeval tv; \
								tv.tv_sec=(y/1000); tv.tv_usec=((y%1000)*1000); \
								pth_nap(tv); })
207
208
209
210
211
212
	#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

213
	#define YIELD()			SLEEP(1)
214

215
216
	#if defined(_THREAD_SAFE)
		#if defined(__FreeBSD__)
217
			#define MAYBE_YIELD()			pthread_yield()
218
		#elif defined(_PTH_PTHREAD_H_)
219
			#define MAYBE_YIELD()			pth_yield(NULL)
220
		#elif defined(_POSIX_PRIORITY_SCHEDULING)
221
			#define MAYBE_YIELD()			sched_yield()
222
		#else
223
			#define MAYBE_YIELD()			YIELD()
224
225
226
		#endif
	#else
		#if defined(_POSIX_PRIORITY_SCHEDULING)
227
			#define	MAYBE_YIELD()			sched_yield()
228
		#else
229
			#define MAYBE_YIELD()			YIELD()
230
231
		#endif
	#endif
232
233
234
235
236
237
238
239
240
241
242
243

	/*
	 * 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

244
245
	#define BEEP(freq,dur)	unix_beep(freq,dur)
	DLLEXPORT void	DLLCALL	unix_beep(int freq, int dur);
246
247
248

#else	/* Unsupported OS */

249
	#error "Unsupported Target: Need some macros and/or function prototypes here."
250
251

#endif
252

253
254
255
256
257
258
/* Win32 implementations of recursive (thread-safe) std C time functions on Unix */

#if !defined(__unix__)	

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

259
260
	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);
261
262
	DLLEXPORT char*	        DLLCALL		ctime_r(const time_t *t, char *buf);
	DLLEXPORT char*	        DLLCALL		asctime_r(const struct tm *tm, char *buf);
263
264
#endif

265
266
267
268
269
#if defined(__solaris__)
	#define CTIME_R(x,y)	ctime_r(x,y,sizeof y)
#else
	#define CTIME_R(x,y)	ctime_r(x,y)
#endif
270

271
DLLEXPORT int		DLLCALL	xp_random(int);
272
DLLEXPORT char*		DLLCALL os_version(char *str);
273
DLLEXPORT char*		DLLCALL	lastchar(const char* str);
deuce's avatar
deuce committed
274
DLLEXPORT int		DLLCALL safe_snprintf(char *dst, size_t size, char *fmt, ...);
275
276
277

#if defined(_WIN32)
#define		msclock()	clock()
deuce's avatar
deuce committed
278
#define		MSCLOCKS_PER_SEC	CLOCKS_PER_SEC
279
280
281
282
#else
#define		MSCLOCKS_PER_SEC	1000
clock_t		msclock(void);
#endif
283
#if defined(__cplusplus)
284
285
286
287
}
#endif

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