]> pd.if.org Git - pdclib.old/blob - internals/_PDCLIB_int.h
Minor cleanups.
[pdclib.old] / internals / _PDCLIB_int.h
1 /* $Id$ */
2
3 /* PDCLib internal integer logic <_PDCLIB_int.h>
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* -------------------------------------------------------------------------- */
10 /* You should not have to edit anything in this file; if you DO have to, it   */
11 /* would be considered a bug / missing feature: notify the author(s).         */
12 /* -------------------------------------------------------------------------- */
13
14 #ifndef _PDCLIB_CONFIG_H
15 #define _PDCLIB_CONFIG_H _PDCLIB_CONFIG_H
16 #include <_PDCLIB_config.h>
17 #endif
18
19 #ifndef _PDCLIB_AUX_H
20 #define _PDCLIB_AUX_H _PDCLIB_AUX_H
21 #include <_PDCLIB_aux.h>
22 #endif
23
24 /* null pointer constant */
25 #define _PDCLIB_NULL 0
26
27 /* -------------------------------------------------------------------------- */
28 /* Limits of native datatypes                                                 */
29 /* -------------------------------------------------------------------------- */
30 /* The definition of minimum limits for unsigned datatypes is done because    */
31 /* later on we will "construct" limits for other abstract types:              */
32 /* USHRT -> _PDCLIB_ + USHRT + _MIN -> _PDCLIB_USHRT_MIN -> 0                 */
33 /* INT -> _PDCLIB_ + INT + _MIN -> _PDCLIB_INT_MIN -> ... you get the idea.   */
34 /* -------------------------------------------------------------------------- */
35
36 /* Setting 'char' limits                                                      */
37 #define _PDCLIB_CHAR_BIT    8
38 #define _PDCLIB_UCHAR_MIN   0
39 #define _PDCLIB_UCHAR_MAX   0xff
40 #define _PDCLIB_SCHAR_MIN   (-0x7f - 1)
41 #define _PDCLIB_SCHAR_MAX   0x7f
42 #ifdef  _PDCLIB_CHAR_SIGNED
43 #define _PDCLIB_CHAR_MIN    _PDCLIB_SCHAR_MIN
44 #define _PDCLIB_CHAR_MAX    _PDCLIB_SCHAR_MAX
45 #else
46 #define _PDCLIB_CHAR_MIN    0
47 #define _PDCLIB_CHAR_MAX    _PDCLIB_UCHAR_MAX
48 #endif
49
50 /* Setting 'short' limits                                                     */
51 #if     _PDCLIB_SHRT_BYTES == 2
52 #define _PDCLIB_SHRT_MAX      0x7fff
53 #define _PDCLIB_SHRT_MIN      (-0x7fff - 1)
54 #define _PDCLIB_USHRT_MAX     0xffff
55 #else
56 #error Unsupported width of 'short' (not 16 bit).
57 #endif
58 #define _PDCLIB_USHRT_MIN 0
59
60 #if _PDCLIB_INT_BYTES < _PDCLIB_SHRT_BYTES
61 #error Bogus setting: short > int? Check _PDCLIB_config.h.
62 #endif
63
64 /* Setting 'int' limits                                                       */
65 #if     _PDCLIB_INT_BYTES == 2
66 #define _PDCLIB_INT_MAX   0x7fff
67 #define _PDCLIB_INT_MIN   (-0x7fff - 1)
68 #define _PDCLIB_UINT_MAX  0xffffU
69 #elif   _PDCLIB_INT_BYTES == 4
70 #define _PDCLIB_INT_MAX   0x7fffffff
71 #define _PDCLIB_INT_MIN   (-0x7fffffff - 1)
72 #define _PDCLIB_UINT_MAX  0xffffffffU
73 #elif _PDCLIB_INT_BYTES   == 8
74 #define _PDCLIB_INT_MAX   0x7fffffffffffffff
75 #define _PDCLIB_INT_MIN   (-0x7fffffffffffffff - 1)
76 #define _PDCLIB_UINT_MAX  0xffffffffffffffff
77 #else
78 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
79 #endif
80 #define _PDCLIB_UINT_MIN 0
81
82 /* Setting 'long' limits                                                      */
83 #if   _PDCLIB_LONG_BYTES   == 4
84 #define _PDCLIB_LONG_MAX   0x7fffffffL
85 #define _PDCLIB_LONG_MIN   (-0x7fffffffL - 1L)
86 #define _PDCLIB_ULONG_MAX  0xffffffffUL
87 #elif   _PDCLIB_LONG_BYTES == 8
88 #define _PDCLIB_LONG_MAX   0x7fffffffffffffffL
89 #define _PDCLIB_LONG_MIN   (-0x7fffffffffffffffL - 1L)
90 #define _PDCLIB_ULONG_MAX  0xffffffffffffffffUL
91 #else
92 #error Unsupported width of 'long' (neither 32 nor 64 bit).
93 #endif
94 #define _PDCLIB_ULONG_MIN 0
95
96 /* Setting 'long long' limits                                                 */
97 #if _PDCLIB_LLONG_BYTES    == 8
98 #define _PDCLIB_LLONG_MAX  0x7fffffffffffffffLL
99 #define _PDCLIB_LLONG_MIN  (-0x7fffffffffffffffLL - 1LL)
100 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffULL
101 #elif _PDCLIB_LLONG_BYTES  == 16
102 #define _PDCLIB_LLONG_MAX  0x7fffffffffffffffffffffffffffffffLL
103 #define _PDCLIB_LLONG_MIN  (-0x7fffffffffffffffffffffffffffffffLL - 1LL)
104 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffffffffffffffffffLL
105 #else
106 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
107 #endif
108 #define _PDCLIB_ULLONG_MIN 0
109
110 /* -------------------------------------------------------------------------- */
111 /* <stdint.h> exact-width types and their limits                              */
112 /* -------------------------------------------------------------------------- */
113
114 /* Setting 'int8_t', its limits, and its literal.                             */
115 #if     _PDCLIB_CHAR_BIT == 8
116 typedef signed char        _PDCLIB_int8_t;
117 typedef unsigned char      _PDCLIB_uint8_t;
118 #define _PDCLIB_INT8_MAX   _PDCLIB_CHAR_MAX
119 #define _PDCLIB_INT8_MIN   _PDCLIB_CHAR_MIN
120 #define _PDCLIB_UINT8_MAX  _PDCLIB_UCHAR_MAX
121 #else
122 #error Unsupported width of char (not 8 bits).
123 #endif
124
125 /* Setting 'int16_t', its limits, and its literal                             */
126 #if     _PDCLIB_INT_BYTES  == 2
127 typedef signed int         _PDCLIB_int16_t;
128 typedef unsigned int       _PDCLIB_uint16_t;
129 #define _PDCLIB_INT16_MAX  _PDCLIB_INT_MAX
130 #define _PDCLIB_INT16_MIN  _PDCLIB_INT_MIN
131 #define _PDCLIB_UINT16_MAX _PDCLIB_UINT_MAX
132 #elif   _PDCLIB_SHRT_BYTES == 2
133 typedef signed short       _PDCLIB_int16_t;
134 typedef unsigned short     _PDCLIB_uint16_t;
135 #define _PDCLIB_INT16_MAX  _PDCLIB_SHRT_MAX
136 #define _PDCLIB_INT16_MIN  _PDCLIB_SHRT_MIN
137 #define _PDCLIB_UINT16_MAX _PDCLIB_USHRT_MAX
138 #else
139 #error Neither 'short' nor 'int' are 16-bit.
140 #endif
141
142 /* Setting 'int32_t', its limits, and its literal                             */
143 #if     _PDCLIB_INT_BYTES  == 4
144 typedef signed int         _PDCLIB_int32_t;
145 typedef unsigned int       _PDCLIB_uint32_t;
146 #define _PDCLIB_INT32_MAX  _PDCLIB_INT_MAX
147 #define _PDCLIB_INT32_MIN  _PDCLIB_INT_MIN
148 #define _PDCLIB_UINT32_MAX _PDCLIB_UINT_MAX
149 #define _PDCLIB_INT32_LITERAL
150 #define _PDCLIB_UINT32_LITERAL
151 #elif   _PDCLIB_LONG_BYTES == 4
152 typedef signed long        _PDCLIB_int32_t;
153 typedef unsigned long      _PDCLIB_uint32_t;
154 #define _PDCLIB_INT32_MAX  _PDCLIB_LONG_MAX
155 #define _PDCLIB_INT32_MIN  _PDCLIB_LONG_MIN
156 #define _PDCLIB_UINT32_MAX _PDCLIB_LONG_MAX
157 #define _PDCLIB_INT32_LITERAL  l
158 #define _PDCLIB_UINT32_LITERAL ul
159 #else
160 #error Neither 'int' nor 'long' are 32-bit.
161 #endif
162
163 #if     _PDCLIB_LONG_BYTES == 8
164 typedef signed long        _PDCLIB_int64_t;
165 typedef unsigned long      _PDCLIB_uint64_t;
166 #define _PDCLIB_INT64_MAX  _PDCLIB_LONG_MAX
167 #define _PDCLIB_INT64_MIN  _PDCLIB_LONG_MIN
168 #define _PDCLIB_UINT64_MAX  _PDCLIB_ULONG_MAX
169 #define _PDCLIB_INT64_LITERAL  l
170 #define _PDCLIB_UINT64_LITERAL ul
171 #elif _PDCLIB_LLONG_BYTES  == 8
172 typedef signed long long   _PDCLIB_int64_t;
173 typedef unsigned long long _PDCLIB_uint64_t;
174 #define _PDCLIB_INT64_MAX  _PDCLIB_LLONG_MAX
175 #define _PDCLIB_INT64_MIN  _PDCLIB_LLONG_MIN
176 #define _PDCLIB_UINT64_MAX  _PDCLIB_ULLONG_MAX
177 #define _PDCLIB_INT64_LITERAL  ll
178 #define _PDCLIB_UINT64_LITERAL ull
179 #else
180 #error Neither 'long' nor 'long long' are 64-bit.
181 #endif
182
183 /* -------------------------------------------------------------------------- */
184 /* <stdint.h> "fastest" types and their limits                                */
185 /* -------------------------------------------------------------------------- */
186 /* This is, admittedly, butt-ugly. But at least it's ugly where the average   */
187 /* user of PDCLib will never see it, and makes <_PDCLIB_config.h> much        */
188 /* cleaner.                                                                   */
189 /* -------------------------------------------------------------------------- */
190
191 typedef _PDCLIB_fast8          _PDCLIB_int_fast8_t;
192 typedef unsigned _PDCLIB_fast8 _PDCLIB_uint_fast8_t;
193 #define _PDCLIB_INT_FAST8_MIN  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST8 ), _MIN )
194 #define _PDCLIB_INT_FAST8_MAX  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST8 ), _MAX )
195 #define _PDCLIB_UINT_FAST8_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST8 ), _MAX )
196
197 typedef _PDCLIB_fast16          _PDCLIB_int_fast16_t;
198 typedef unsigned _PDCLIB_fast16 _PDCLIB_uint_fast16_t;
199 #define _PDCLIB_INT_FAST16_MIN  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST16 ), _MIN )
200 #define _PDCLIB_INT_FAST16_MAX  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST16 ), _MAX )
201 #define _PDCLIB_UINT_FAST16_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST16 ), _MAX )
202
203 typedef _PDCLIB_fast32          _PDCLIB_int_fast32_t;
204 typedef unsigned _PDCLIB_fast32 _PDCLIB_uint_fast32_t;
205 #define _PDCLIB_INT_FAST32_MIN  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST32 ), _MIN )
206 #define _PDCLIB_INT_FAST32_MAX  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST32 ), _MAX )
207 #define _PDCLIB_UINT_FAST32_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST32 ), _MAX )
208
209 typedef _PDCLIB_fast64          _PDCLIB_int_fast64_t;
210 typedef unsigned _PDCLIB_fast64 _PDCLIB_uint_fast64_t;
211 #define _PDCLIB_INT_FAST64_MIN  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST64 ), _MIN )
212 #define _PDCLIB_INT_FAST64_MAX  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST64 ), _MAX )
213 #define _PDCLIB_UINT_FAST64_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST64 ), _MAX )
214
215 /* -------------------------------------------------------------------------- */
216 /* Various <stddef.h> typedefs and limits                                     */
217 /* -------------------------------------------------------------------------- */
218
219 typedef _PDCLIB_ptrdiff     _PDCLIB_ptrdiff_t;
220 #define _PDCLIB_PTRDIFF_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MIN )
221 #define _PDCLIB_PTRDIFF_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MAX )
222
223 #define _PDCLIB_SIG_ATOMIC_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MIN )
224 #define _PDCLIB_SIG_ATOMIC_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MAX )
225
226 typedef _PDCLIB_size     _PDCLIB_size_t;
227 #define _PDCLIB_SIZE_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_SIZE ), _MAX )
228
229 typedef _PDCLIB_wchar     _PDCLIB_wchar_t;
230 #define _PDCLIB_WCHAR_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_WCHAR ), _MIN )
231 #define _PDCLIB_WCHAR_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_WCHAR ), _MAX )
232
233 typedef _PDCLIB_intptr          _PDCLIB_intptr_t;
234 typedef unsigned _PDCLIB_intptr _PDCLIB_uintptr_t;
235 #define _PDCLIB_INTPTR_MIN  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTPTR ), _MIN )
236 #define _PDCLIB_INTPTR_MAX  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTPTR ), _MAX )
237 #define _PDCLIB_UINTPTR_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_INTPTR ), _MAX )
238
239 typedef _PDCLIB_intmax          _PDCLIB_intmax_t;
240 typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t;
241 #define _PDCLIB_INTMAX_MIN  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTMAX ), _MIN )
242 #define _PDCLIB_INTMAX_MAX  _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTMAX ), _MAX )
243 #define _PDCLIB_UINTMAX_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_INTMAX ), _MAX )
244 #define _PDCLIB_INTMAX_C( value )  _PDCLIB_concat( value, _PDCLIB_INTMAX_LITERAL )
245 #define _PDCLIB_UINTMAX_C( value ) _PDCLIB_concat( value, _PDCLIB_concat( u, _PDCLIB_INTMAX_LITERAL ) )
246
247 /* -------------------------------------------------------------------------- */
248 /* Various <stdio.h> internals                                                */
249 /* -------------------------------------------------------------------------- */
250
251 /* Flags for representing mode (see fopen()). Note these must fit the same
252    status field as the _IO?BF flags in <stdio.h> and the internal flags below.
253 */
254 #define _PDCLIB_FREAD    8u
255 #define _PDCLIB_FWRITE   16u
256 #define _PDCLIB_FAPPEND  32u 
257 #define _PDCLIB_FRW      64u
258 #define _PDCLIB_FBIN    128u
259
260 /* Internal flags, made to fit the same status field as the flags above. */
261 #define _PDCLIB_WROTELAST  256u
262 #define _PDCLIB_LIBBUFFER  512u
263 #define _PDCLIB_VIRGINSTR 1024u
264 #define _PDCLIB_ERRORFLAG 2048u
265 #define _PDCLIB_EOFFLAG   4096u
266
267 struct _PDCLIB_file_t
268 {
269     _PDCLIB_fd_t            handle;   /* OS-specific file descriptor */
270     _PDCLIB_fpos_t          position; /* file position indicator */
271     char *                  buffer;   /* file buffer */
272     _PDCLIB_size_t          bufsize;  /* size of buffer */
273     _PDCLIB_size_t          bufidx;   /* index to point of action in buffer */
274     _PDCLIB_size_t          bufend;   /* index to end of pre-read buffer */
275     unsigned int            status;   /* misc. status bits */
276     struct _PDCLIB_file_t * next;     /* provisions for linked list handling */
277 };
278
279 /* -------------------------------------------------------------------------- */
280 /* Internal data types                                                        */
281 /* -------------------------------------------------------------------------- */
282
283 /* Structure required by both atexit() and exit() for handling atexit functions */
284 struct _PDCLIB_exitfunc_t
285 {
286     struct _PDCLIB_exitfunc_t * next;
287     void (*func)( void );
288 };
289
290 /* Structures required by malloc(), realloc(), and free(). */
291 struct _PDCLIB_headnode_t
292 {
293     struct _PDCLIB_memnode_t * first;
294     struct _PDCLIB_memnode_t * last;
295 };
296
297 struct _PDCLIB_memnode_t
298 {
299     _PDCLIB_size_t size;
300     struct _PDCLIB_memnode_t * next;
301 };
302
303 /* Status structure required by _PDCLIB_print(). */
304 struct _PDCLIB_status_t
305 {
306     int              base;  /* base to which the value shall be converted    */
307     _PDCLIB_int_fast32_t flags; /* flags and length modifiers                */
308     _PDCLIB_size_t   n;     /* maximum number of characters to be written    */
309     _PDCLIB_size_t   i;     /* number of characters already written          */
310     _PDCLIB_size_t   this;  /* output chars in the current conversion        */
311     char *           s;     /* target buffer                                 */
312     _PDCLIB_size_t   width; /* width of current field                        */
313     _PDCLIB_size_t   prec;  /* precision of current field                    */
314     struct _PDCLIB_file_t * stream;/* for to-stream output                   */
315     _PDCLIB_va_list  arg;   /* argument stack passed to the printf function  */
316 };
317
318 /* -------------------------------------------------------------------------- */
319 /* Declaration of helper functions (implemented in functions/_PDCLIB).        */
320 /* -------------------------------------------------------------------------- */
321
322 /* This is the main function called by atoi(), atol() and atoll().            */
323 _PDCLIB_intmax_t _PDCLIB_atomax( const char * s );
324
325 /* Two helper functions used by strtol(), strtoul() and long long variants.   */
326 const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base );
327 _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, _PDCLIB_uintmax_t error, _PDCLIB_uintmax_t limval, _PDCLIB_uintmax_t limdigit, char * sign );
328
329 /* Digits arrays used by various integer conversion functions */
330 extern char _PDCLIB_digits[];
331 extern char _PDCLIB_Xdigits[];
332
333 /* The worker for all printf() type of functions. The pointer spec should point
334    to the introducing '%' of a conversion specifier. The status structure is to
335    be that of the current printf() function, of which the members n, s, stream
336    and arg will be preserved, i will be updated, and all others will be trashed
337    by the function.
338    Returns a pointer to the first character not parsed as conversion specifier.
339 */
340 const char * _PDCLIB_print( const char * spec, struct _PDCLIB_status_t * status );
341
342 /* Parsing any fopen() style filemode string into a number of flags. */
343 unsigned int _PDCLIB_filemode( const char * mode );