summaryrefslogtreecommitdiff
path: root/snprintfv/compat.h
blob: 0145b97c465323f1499ac95f467af707c24fda67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*  -*- Mode: C -*-
 * --------------------------------------------------------------------
 * compat.h.in --- verbose but portable cpp defines for snprintfv
 * Copyright (C) 1999 Gary V. Vaughan
 * Originally by Gary V. Vaughan, 1999
 * This file is part of Snprintfv
 *
 * Snprintfv is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * Snprintfv program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * As a special exception to the GNU General Public License, if you
 * distribute this file as part of a program that also links with and
 * uses the libopts library from AutoGen, you may include it under
 * the same distribution terms used by the libopts library.
 *
 * Code:  */

#ifndef SNPRINTFV_COMPAT_H
#define SNPRINTFV_COMPAT_H 1

#define  _GNU_SOURCE    1 /* for strsignal in GNU's libc */
#define  __USE_GNU      1 /* exact same thing as above   */
#define  __EXTENSIONS__ 1 /* and another way to call for it */

#ifdef __cplusplus
extern "C" {
#define SNV_END_EXTERN_C }
#else
#define SNV_END_EXTERN_C
#endif /* __cplusplus */

#define NO_FLOAT_PRINTING

#ifdef HAVE_SYS_TYPES_H
#  include <sys/types.h>
#endif

#include <stdio.h>

#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif

#ifdef HAVE_ERRNO_H
#  include <errno.h>
#  ifndef errno
     /* Some sytems #define this! */
     extern int errno;
#  endif
#endif

#if defined( HAVE_LIMITS_H )
#  include <limits.h>

#elif defined( HAVE_SYS_LIMITS_H )
#  include <sys/limits.h>

#elif defined( HAVE_VALUES_H )
#  ifndef MAXINT
#    include <values.h>
#  endif /* MAXINT */
#endif

#if defined( HAVE_STRING_H )
#  include <string.h>

#elif defined( HAVE_STRINGS_H )
#  include <strings.h>
#endif

#if defined( HAVE_MEMORY_H )
#  include <memory.h>
#endif

#if defined( HAVE_INTTYPES_H )
#  include <inttypes.h>

#elif defined( HAVE_STDINT_H )
#  include <stdint.h>
#endif

#ifndef HAVE_UINTMAX_T
#  if defined( HAVE_LONG_LONG )
     typedef long long intmax_t;
     typedef unsigned long long uintmax_t;
#  else
     typedef long intmax_t;
     typedef unsigned long uintmax_t;
#  endif
#endif

#if defined( HAVE_STDARG_H )
#  include <stdarg.h>
#  ifndef   VA_START
#    define VA_START(a, f)  va_start(a, f)
#    define VA_END(a)	    va_end(a)
#  endif /* VA_START */
#  define SNV_USING_STDARG_H
#elif defined( HAVE_VARARGS_H )
#  include <varargs.h>
#  ifndef   VA_START
#    define VA_START(a, f) va_start(a)
#    define VA_END(a)	 va_end(a)
#  endif /* VA_START */
#  undef  SNV_USING_STDARG_H
#else
#  include "must-have-stdarg-or-varargs"
#endif

#if HAVE_RUNETYPE_H
# include <runetype.h>
#endif

#ifdef HAVE_WCHAR_H
# include <wchar.h>
#endif

#ifdef HAVE_WCHAR_T
typedef wchar_t snv_wchar_t;
#else
typedef int snv_wchar_t;
#endif

#ifdef HAVE_WINT_T
typedef wint_t snv_wint_t;
#else
typedef int snv_wint_t;
#endif

/* inline and const keywords are (mostly) handled by config.h */
#ifdef __GNUC__
#  ifndef const
#    define const	__const
#  endif
#  ifndef inline
#    define inline	__inline
#  endif
#  ifndef signed
#    define signed	__signed
#  endif
#else
#  ifndef __STDC__
#    undef  signed
#    define signed
#  endif
#endif

#ifdef __STDC__
#  define _SNV_STR(x)		#x
   typedef void *snv_pointer;
   typedef const void *snv_constpointer;
#else
#  define _SNV_STR(x)		"x"
   typedef char *snv_pointer;
   typedef char *snv_constpointer;
#endif

#if defined(HAVE_FPUTC_UNLOCKED) && defined(HAVE_FLOCKFILE)
#  define SNV_FPUTC_UNLOCKED fputc_unlocked
#  define SNV_PUTC_UNLOCKED putc_unlocked
#  define SNV_WITH_LOCKED_FP(fp, tmp_var) \
     for (flockfile (fp), tmp_var = 1; \
       tmp_var--; funlockfile (fp))
#else
#  define SNV_FPUTC_UNLOCKED fputc
#  define SNV_PUTC_UNLOCKED putc
#  define SNV_WITH_LOCKED_FP(fp, tmp_var) \
     for (tmp_var = 1; tmp_var--; )
#endif

/*
 * Define macros for storing integers inside pointers.
 * Be aware that it is only safe to use these macros to store `int'
 * values in `char*' (or `void*') words, and then extract them later.
 * Although it will work the other way round on many common
 * architectures, it is not portable to assume a `char*' can be
 * stored in an `int' and extracted later without loss of the msb's
 */
#define SNV_POINTER_TO_LONG(p)	((long)(p))
#define SNV_POINTER_TO_ULONG(p)	((unsigned long)(p))
#define SNV_LONG_TO_POINTER(i)	((snv_pointer)(long)(i))
#define SNV_ULONG_TO_POINTER(u)	((snv_pointer)(unsigned long)(u))

#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#else
typedef enum {
    false = 0,
    true  = 1
} bool;
#endif

#ifdef __CYGWIN32__
#  ifndef __CYGWIN__
#    define __CYGWIN__
#  endif
#endif
#ifdef __CYGWIN__
#  ifndef _WIN32
#    define _WIN32
#  endif
#endif

#ifndef PARAMS
#  define PARAMS(args)      args
#endif

#undef SNV_STMT_START
#undef SNV_STMT_END
#if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
#  define SNV_STMT_START	(void)(
#  define SNV_STMT_END		)

#elif (defined (sun) || defined (__sun__))
#  define SNV_STMT_START	if (1)
#  define SNV_STMT_END		else (void)0

#else
#  define SNV_STMT_START	do
#  define SNV_STMT_END		while (false)
#endif

#ifdef _WIN32
#  ifdef DLL_EXPORT
#    define SNV_SCOPE	extern __declspec(dllexport)
#  else
#    ifdef LIBSNPRINTFV_DLL_IMPORT
#      define SNV_SCOPE	extern __declspec(dllimport)
#    endif
#  endif
#endif
#ifndef SNV_SCOPE
#  define SNV_SCOPE	extern
#endif

#undef SNV_GNUC_PRINTF
#undef SNV_GNUC_NORETURN
#if	__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#  define SNV_GNUC_PRINTF( args, format_idx, arg_idx )		\
  	args __attribute__((format (printf, format_idx, arg_idx)))
#  define SNV_GNUC_NORETURN						\
	__attribute__((noreturn))
#  define SNV_ASSERT_FCN  	 " (", __PRETTY_FUNCTION__, ")"
#else /* !__GNUC__ */
#  define SNV_GNUC_PRINTF( args, format_idx, arg_idx ) args
#  define SNV_GNUC_NORETURN
#  define SNV_ASSERT_FCN		"", "", ""
#endif /* !__GNUC__ */

#define SNV_ASSERT_FMT  "file %s: line %d%s%s%s: assertion \"%s\" failed.\n"

#define snv_assert(expr)		snv_fassert(stderr, expr)
#define snv_fassert(stream, expr)   SNV_STMT_START {        \
    if (!(expr))   {                                        \
    fprintf (stream, SNV_ASSERT_FMT, __FILE__, __LINE__,    \
             SNV_ASSERT_FCN, _SNV_STR(expr));               \
    exit(EXIT_FAILURE);                                     \
    }; } SNV_STMT_END

#define return_if_fail(expr)		freturn_if_fail(stderr, expr)
#define freturn_if_fail(expr)       SNV_STMT_START {        \
    if (!(expr))   {                                        \
    fprintf (stream, SNV_ASSERT_FMT, __FILE__, __LINE__,    \
             SNV_ASSERT_FCN, _SNV_STR(expr));               \
    return;                                                 \
    }; } SNV_STMT_END

#define return_val_if_fail(expr, val)	freturn_val_if_fail(stderr, expr, val)
#define freturn_val_if_fail(stream, expr, val)  SNV_STMT_START {    \
    if (!(expr))  {                                                 \
    fprintf (stream, SNV_ASSERT_FMT, __FILE__, __LINE__,            \
             SNV_ASSERT_FCN, _SNV_STR(expr));                       \
    return val;                                                     \
    }; } SNV_STMT_END

#ifndef MAX
#define MAX(a,b)	((a) > (b) ? (a) : (b))
#endif

#ifndef MIN
#define MIN(a,b)	((a) < (b) ? (a) : (b))
#endif

#ifndef ABS
#define ABS(a)		((a) < 0 ? -(a) : (a))
#endif

typedef SNV_LONG_DOUBLE snv_long_double;

#ifndef HAVE_STRTOUL
extern unsigned long
strtoul( const char *nptrm, char **endptr, register int base );
#endif

SNV_END_EXTERN_C
#endif /* SNPRINTFV_COMPAT_H */

/*
 * Local Variables:
 * mode: C
 * c-file-style: "gnu"
 * indent-tabs-mode: nil
 * End:
 * end of snprintfv/compat.h */