]> pd.if.org Git - pdclib/blob - includes/stdlib.h
e724bf764102a5f6c1037a4b39474471458728f1
[pdclib] / includes / stdlib.h
1 /* General utilities <stdlib.h>
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #ifndef _PDCLIB_STDLIB_H
8 #define _PDCLIB_STDLIB_H _PDCLIB_STDLIB_H
9 #include <_PDCLIB_int.h>
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 #ifndef _PDCLIB_SIZE_T_DEFINED
16 #define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED
17 typedef _PDCLIB_size_t size_t;
18 #endif
19
20 #ifndef _PDCLIB_NULL_DEFINED
21 #define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED
22 #define NULL _PDCLIB_NULL
23 #endif
24
25 #ifndef _PDCLIB_MB_CUR_MAX_DEFINED
26 #define _PDCLIB_MB_CUR_MAX_DEFINED
27 #define MB_CUR_MAX (_PDCLIB_mb_cur_max())
28 #endif
29
30 /* Numeric conversion functions */
31
32 /* TODO: atof(), strtof(), strtod(), strtold() */
33
34 double atof( const char * nptr ) _PDCLIB_nothrow;
35 double strtod( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr ) _PDCLIB_nothrow;
36 float strtof( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr ) _PDCLIB_nothrow;
37 long double strtold( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr ) _PDCLIB_nothrow;
38
39 /* Seperate the character array nptr into three parts: A (possibly empty)
40    sequence of whitespace characters, a character representation of an integer
41    to the given base, and trailing invalid characters (including the terminating
42    null character). If base is 0, assume it to be 10, unless the integer
43    representation starts with 0x / 0X (setting base to 16) or 0 (setting base to
44    8). If given, base can be anything from 0 to 36, using the 26 letters of the
45    base alphabet (both lowercase and uppercase) as digits 10 through 35.
46    The integer representation is then converted into the return type of the
47    function. It can start with a '+' or '-' sign. If the sign is '-', the result
48    of the conversion is negated.
49    If the conversion is successful, the converted value is returned. If endptr
50    is not a NULL pointer, a pointer to the first trailing invalid character is
51    returned in *endptr.
52    If no conversion could be performed, zero is returned (and nptr in *endptr,
53    if endptr is not a NULL pointer). If the converted value does not fit into
54    the return type, the functions return LONG_MIN, LONG_MAX, ULONG_MAX,
55    LLONG_MIN, LLONG_MAX, or ULLONG_MAX respectively, depending on the sign of
56    the integer representation and the return type, and errno is set to ERANGE.
57 */
58 /* There is strtoimax() and strtoumax() in <inttypes.h> operating on intmax_t /
59    uintmax_t, if the long long versions do not suit your needs.
60 */
61 long int strtol( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;
62 long long int strtoll( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;
63 unsigned long int strtoul( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;
64 unsigned long long int strtoull( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;
65
66 /* These functions are the equivalent of (int)strtol( nptr, NULL, 10 ),
67    strtol( nptr, NULL, 10 ) and strtoll(nptr, NULL, 10 ) respectively, with the
68    exception that they do not have to handle overflow situations in any defined
69    way.
70    (PDCLib does not simply forward these to their strtox() equivalents, but
71    provides a simpler atox() function that saves a couple of tests and simply
72    continues with the conversion in case of overflow.)
73 */
74 int atoi( const char * nptr ) _PDCLIB_nothrow;
75 long int atol( const char * nptr ) _PDCLIB_nothrow;
76 long long int atoll( const char * nptr ) _PDCLIB_nothrow;
77
78 /* Pseudo-random sequence generation functions */
79
80 extern unsigned long int _PDCLIB_seed;
81
82 #define RAND_MAX 32767
83
84 /* Returns the next number in a pseudo-random sequence, which is between 0 and
85    RAND_MAX.
86    (PDCLib uses the implementation suggested by the standard document, which is
87    next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768;)
88 */
89 int rand( void ) _PDCLIB_nothrow;
90
91 /* Initialize a new pseudo-random sequence with the starting seed. Same seeds
92    result in the same pseudo-random sequence. The default seed is 1.
93 */
94 void srand( unsigned int seed ) _PDCLIB_nothrow;
95
96 /* Memory management functions */
97
98 /* Allocate a chunk of memory of given size. If request could not be
99    satisfied, return NULL. Otherwise, return a pointer to the allocated
100    memory. Memory contents are undefined.
101 */
102 void * malloc( size_t size ) _PDCLIB_nothrow;
103
104 /* Allocate a chunk of memory that is large enough to hold nmemb elements of
105    the given size, and zero-initialize that memory. If request could not be
106    satisfied, return NULL. Otherwise, return a pointer to the allocated
107    memory.
108 */
109 void * calloc( size_t nmemb, size_t size ) _PDCLIB_nothrow;
110
111 /* Allocate a chunk of memory of given size, with specified alignment (which
112    must be a power of two; if it is not, the next greater power of two is
113    used). If request could not be satisfied, return NULL. Otherwise, return
114    a pointer to the allocated memory.
115 */
116 void * aligned_alloc( size_t alignment, size_t size ) _PDCLIB_nothrow;
117
118 /* De-allocate a chunk of heap memory previously allocated using malloc(),
119    calloc(), or realloc(), and pointed to by ptr. If ptr does not match a
120    pointer previously returned by the mentioned allocation functions, or
121    free() has already been called for this ptr, behaviour is undefined.
122 */
123 void free( void * ptr ) _PDCLIB_nothrow;
124
125 /* Resize a chunk of memory previously allocated with malloc() and pointed to
126    by ptr to the given size (which might be larger or smaller than the original
127    size). Returns a pointer to the reallocated memory, or NULL if the request
128    could not be satisfied. Note that the resizing might include a memcpy()
129    from the original location to a different one, so the return value might or
130    might not equal ptr. If size is larger than the original size, the value of
131    memory beyond the original size is undefined. If ptr is NULL, realloc()
132    behaves like malloc().
133 */
134 void * realloc( void * ptr, size_t size ) _PDCLIB_nothrow;
135
136 /* Communication with the environment */
137
138 /* These two can be passed to exit() or _Exit() as status values, to signal
139    successful and unsuccessful program termination, respectively. EXIT_SUCCESS
140    can be replaced by 0. How successful or unsuccessful program termination are
141    signaled to the environment, and what happens if exit() or _Exit() are being
142    called with a value that is neither of the three, is defined by the hosting
143    OS and its glue function.
144 */
145 #define EXIT_SUCCESS _PDCLIB_SUCCESS
146 #define EXIT_FAILURE _PDCLIB_FAILURE
147
148 /* Initiate abnormal process termination, unless programm catches SIGABRT and
149    does not return from the signal handler.
150    This implementantion flushes all streams, closes all files, and removes any
151    temporary files before exiting with EXIT_FAILURE.
152    abort() does not return.
153 */
154 _PDCLIB_noreturn void abort( void ) _PDCLIB_nothrow;
155
156 /* Register a function that will be called on exit(), or when main() returns.
157    At least 32 functions can be registered this way, and will be called in
158    reverse order of registration (last-in, first-out).
159    Returns zero if registration is successfull, nonzero if it failed.
160 */
161 int atexit( void (*func)( void ) ) _PDCLIB_nothrow;
162
163 /* Register a function that will be called on quick_exit(), or when main() returns.
164    At least 32 functions can be registered this way, and will be called in
165    reverse order of registration (last-in, first-out).
166    Returns zero if registration is successfull, nonzero if it failed.
167 */
168 int at_quick_exit( void (*func)( void ) ) _PDCLIB_nothrow;
169
170 /* Normal process termination. Functions registered by atexit() (see above) are
171    called, streams flushed, files closed and temporary files removed before the
172    program is terminated with the given status. (See comment for EXIT_SUCCESS
173    and EXIT_FAILURE above.)
174    exit() does not return.
175 */
176 _PDCLIB_noreturn void exit( int status ) _PDCLIB_nothrow;
177
178 /* Normal process termination. Functions registered by atexit() (see above) are
179    NOT CALLED. This implementation DOES flush streams, close files and removes
180    temporary files before the program is teminated with the given status. (See
181    comment for EXIT_SUCCESS and EXIT_FAILURE above.)
182    _Exit() does not return.
183 */
184 _PDCLIB_noreturn void _Exit( int status ) _PDCLIB_nothrow;
185
186 /* Search an environment-provided key-value map for the given key name, and
187    return a pointer to the associated value string (or NULL if key name cannot
188    be found). The value string pointed to might be overwritten by a subsequent
189    call to getenv(). The library never calls getenv() itself.
190    Details on the provided keys and how to set / change them are determined by
191    the hosting OS and its glue function.
192 */
193 char * getenv( const char * name ) _PDCLIB_nothrow;
194
195 /* If string is a NULL pointer, system() returns nonzero if a command processor
196    is available, and zero otherwise. If string is not a NULL pointer, it is
197    passed to the command processor. If system() returns, it does so with a
198    value that is determined by the hosting OS and its glue function.
199 */
200 int system( const char * string ) _PDCLIB_nothrow;
201
202 /* Searching and sorting */
203
204 /* Do a binary search for a given key in the array with a given base pointer,
205    which consists of nmemb elements that are of the given size each. To compare
206    the given key with an element from the array, the given function compar is
207    called (with key as first parameter and a pointer to the array member as
208    second parameter); the function should return a value less than, equal to,
209    or greater than 0 if the key is considered to be less than, equal to, or
210    greater than the array element, respectively.
211    The function returns a pointer to the first matching element found, or NULL
212    if no match is found.
213
214    ** May throw **
215 */
216 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
217
218 /* Do a quicksort on an array with a given base pointer, which consists of
219    nmemb elements that are of the given size each. To compare two elements from
220    the array, the given function compar is called, which should return a value
221    less than, equal to, or greater than 0 if the first argument is considered
222    to be less than, equal to, or greater than the second argument, respectively.
223    If two elements are compared equal, their order in the sorted array is not
224    specified.
225
226    ** May throw **
227 */
228 void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
229
230 /* Integer arithmetic functions */
231
232 /* Return the absolute value of the argument. Note that on machines using two-
233    complement's notation (most modern CPUs), the largest negative value cannot
234    be represented as positive value. In this case, behaviour is unspecified.
235 */
236 int abs( int j ) _PDCLIB_nothrow;
237 long int labs( long int j ) _PDCLIB_nothrow;
238 long long int llabs( long long int j ) _PDCLIB_nothrow;
239
240 /* These structures each have a member quot and a member rem, of type int (for
241    div_t), long int (for ldiv_t) and long long it (for lldiv_t) respectively.
242    The order of the members is platform-defined to allow the div() functions
243    below to be implemented efficiently.
244 */
245 typedef struct _PDCLIB_div_t     div_t;
246 typedef struct _PDCLIB_ldiv_t   ldiv_t;
247 typedef struct _PDCLIB_lldiv_t lldiv_t;
248
249 /* Return quotient (quot) and remainder (rem) of an integer division in one of
250    the structs above.
251 */
252 div_t div( int numer, int denom ) _PDCLIB_nothrow;
253 ldiv_t ldiv( long int numer, long int denom ) _PDCLIB_nothrow;
254 lldiv_t lldiv( long long int numer, long long int denom ) _PDCLIB_nothrow;
255
256 /* Multibyte / wide character conversion functions */
257
258 /* Affected by LC_CTYPE of the current locale. For state-dependent encoding,
259    each function is placed into its initial conversion state at program
260    startup, and can be returned to that state by a call with its character
261    pointer argument s being a null pointer.
262    Changing LC_CTYPE causes the conversion state to become indeterminate.
263 */
264
265 /* If s is not a null pointer, returns the number of bytes contained in the
266    multibyte character pointed to by s (if the next n or fewer bytes form a
267    valid multibyte character); -1, if they don't; or 0, if s points to the
268    null character.
269    If s is a null pointer, returns nonzero if multibyte encodings in the
270    current locale are stateful, and zero otherwise.
271 */
272 int mblen( const char * s, size_t n );
273
274 /* If s is not a null pointer, and the next n bytes (maximum) form a valid
275    multibyte character sequence (possibly including shift sequences), the
276    corresponding wide character is stored in pwc (unless that is a null
277    pointer). If the wide character is the null character, the function is
278    left in the initial conversion state.
279    Returns the number of bytes in the consumed multibyte character sequence;
280    or 0, if the resulting wide character is the null character. If the next
281    n bytes do not form a valid sequence, returns -1.
282    In no case will the returned value be greater than n or MB_CUR_MAX.
283    If s is a null pointer, returns nonzero if multibyte encodings in the
284    current locale are stateful, and zero otherwise.
285 */
286 int mbtowc( wchar_t * _PDCLIB_restrict pwc, const char * _PDCLIB_restrict s, size_t n );
287
288 /* Converts the wide character wc into the corresponding multibyte character
289    sequence (including shift sequences). If s is not a null pointer, the
290    multibyte sequence (at most MB_CUR_MAX characters) is stored at that
291    location. If wc is a null character, a null byte is stored, preceded by
292    any shift sequence needed to restore the initial shift state, and the
293    function is left in the initial conversion state.
294    Returns the number of bytes in the generated multibyte character sequence.
295    If wc does not correspond to a valid multibyte character, returns -1.
296    In no case will the returned value be greater than MB_CUR_MAX.
297    If s is a null pointer, returns nonzero if multibyte encodings in the
298    current locale are stateful, and zero otherwise.
299 */
300 int wctomb( char * s, wchar_t wc );
301
302 /* Convert a sequence of multibyte characters beginning in the initial shift
303    state from the array pointed to by s into the corresponding wide character
304    sequence, storing no more than n wide characters into pwcs. A null
305    character is converted into a null wide character, and marks the end of
306    the multibyte character sequence.
307    If copying takes place between objects that overlap, behaviour is
308    undefined.
309    Returns (size_t)-1 if an invalid multibyte sequence is encountered.
310    Otherwise, returns the number of array elements modified, not including
311    a terminating null wide character, if any. (Target string will not be
312    null terminated if the return value equals n.)
313 */
314 size_t mbstowcs( wchar_t * _PDCLIB_restrict pwcs, const char * _PDCLIB_restrict s, size_t n );
315
316 /* Convert a sequence of wide characters from the array pointed to by pwcs
317    into a sequence of corresponding multibyte characters, beginning in the
318    initial shift state, storing them in the array pointed to by s, stopping
319    if the next multibyte character would exceed the limit of n total bytes
320    or a null character is stored.
321    If copying takes place between objects that overlap, behaviour is
322    undefined.
323    Returns (size_t)-1 if a wide character is encountered that does not
324    correspond to a valid multibyte character. Otherwise, returns the number
325    of array elements modified, not including a terminating null character,
326    if any. (Target string will not be null terminated if the return value
327    equals n.)
328 */
329 size_t wcstombs( char * _PDCLIB_restrict s, const wchar_t * _PDCLIB_restrict pwcs, size_t n );
330
331 #ifdef __cplusplus
332 }
333 #endif
334
335 #endif