-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.h
More file actions
193 lines (171 loc) · 5.97 KB
/
util.h
File metadata and controls
193 lines (171 loc) · 5.97 KB
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
/*
* util - common utility functions for the JSON parser and tools
*
* "Because specs w/o version numbers are forced to commit to their original design flaws." :-)
*
* Copyright (c) 2022-2026 by Cody Boone Ferguson and Landon Curt Noll. All
* rights reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE OR JSON.
*
* This JSON parser, library and tools were co-developed in 2022-2026 by Cody Boone
* Ferguson and Landon Curt Noll:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* "Share and Enjoy!"
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
#if !defined(INCLUDE_JPARSE_UTIL_H)
# define INCLUDE_JPARSE_UTIL_H
#include <float.h> /* for long doubles */
#include <inttypes.h> /* uintmax_t and intmax_t and perhaps SIZE_MAX */
#include <string.h> /* for strcmp */
#include <sys/types.h> /* various things */
#include <sys/stat.h> /* for stat(2) and others */
#include <fts.h> /* FTS and FTSENT */
#include <limits.h> /* for CHAR_BIT */
/*
* dbg - info, debug, warning, error, and usage message facility
*/
#if defined(INTERNAL_INCLUDE)
#include "../dbg/c_bool.h"
#include "../dbg/c_compat.h"
#include "../dbg/dbg.h"
#elif defined(INTERNAL_INCLUDE_2)
#include "../dbg/c_bool.h"
#include "../dbg/c_compat.h"
#include "../dbg/dbg.h"
#else
#include <c_bool.h>
#include <c_compat.h>
#include <dbg.h>
#endif
/*
* dyn_array - dynamic array facility
*/
#if defined(INTERNAL_INCLUDE)
#include "../dyn_array/dyn_array.h"
#elif defined(INTERNAL_INCLUDE_2)
#include "../dyn_array/dyn_array.h"
#else
#include <dyn_array.h>
#endif
/*
* pr - stdio helper library
*/
#if defined(INTERNAL_INCLUDE)
#include "../pr/pr.h"
#elif defined(INTERNAL_INCLUDE_2)
#include "../pr/pr.h"
#else
#include <pr.h>
#endif
/*
* byte as octet constants
*/
#if !defined(CHAR_BIT)
#define CHAR_BIT (8) /* paranoia - in case limits.h is very old */
#endif
#if !defined(BITS_IN_BYTE)
#define BITS_IN_BYTE (CHAR_BIT) /* assume 8 bit bytes */
#endif
#if !defined(BYTE_VALUES)
#define BYTE_VALUES (1<<CHAR_BIT) /* number of different combinations of bytes */
#endif
#if !defined(MAX_BYTE)
#define MAX_BYTE (BYTE_VALUES-1) /* maximum byte value */
#endif
/*
* off_t MAX and MIN
*/
#if !defined(OFF_MAX)
#define OFF_MAX (~((off_t)1 << (sizeof(off_t) * BITS_IN_BYTE - 1)))
#endif /* OFF_MAX */
#if !defined(OFF_MIN)
#define OFF_MIN (((off_t)1 << (sizeof(off_t) * BITS_IN_BYTE - 1)))
#endif /* OFF_MIN */
/*
* size_t MAX and MIN
*/
#if !defined(SIZE_MAX)
#define SIZE_MAX (~((size_t)0))
#endif /* SIZE_MAX */
#if !defined(SIZE_MIN)
#define SIZE_MIN ((size_t)(0))
#endif /* SIZE_MIN */
/*
* ssize_t MAX and MIN
*/
#if !defined(SSIZE_MAX)
#define SSIZE_MAX (~((ssize_t)1 << (sizeof(ssize_t) * BITS_IN_BYTE - 1)))
#endif /* SSIZE_MAX */
#if !defined(SSIZE_MIN)
#define SSIZE_MIN (((ssize_t)1 << (sizeof(ssize_t) * BITS_IN_BYTE - 1)))
#endif /* SSIZE_MIN */
/*
* definitions
*/
#define LITLEN(x) (sizeof(x)-1) /* length of a literal string w/o the NUL byte */
#define INITIAL_BUF_SIZE (8192) /* initial size of buffer allocated by read_all */
#define READ_ALL_CHUNK (65536) /* grow this read_all by this amount when needed */
#define TBLLEN(x) (sizeof(x)/sizeof((x)[0])) /* number of elements in an initialized table array */
#define UNUSED_ARG(x) (void)(x) /* prevent compiler from complaining about an unused arg */
/*
* fpr() and pr() related macros
*/
#define fprint(stream, fmt, ...) fpr((stream), __func__, (fmt), __VA_ARGS__)
#define fprstr(stream, fmt) fpr((stream), __func__, (fmt))
/**/
#define print(fmt, ...) pr(__func__, (fmt), __VA_ARGS__)
#define prstr(fmt) pr(__func__, (fmt))
/*
* utility macros for find_text functions
*/
#define is_all_text(buf, len) (find_text((buf), (len), NULL) == (len))
#define is_all_text_str(str) (is_all_text((str), strlen(str)))
#define is_all_whitespace(buf, len) (find_text((buf), (len), NULL) == 0)
#define is_all_whitespace_str(str) (is_all_whitespace((str), strlen(str)))
/*
* non-strict floating match to 1 part in MATCH_PRECISION
*/
#define MATCH_PRECISION ((long double)(1<<22))
/*
* external function declarations
*/
extern bool is_string(char const * const ptr, size_t len);
extern char const *strnull(char const * const str);
extern bool string_to_intmax(char const *str, intmax_t *ret);
extern bool string_to_uintmax(char const *str, uintmax_t *ret);
extern bool is_decimal(char const *ptr, size_t len);
extern bool is_decimal_str(char const *str, size_t *retlen);
extern bool is_floating_notation(char const *ptr, size_t len);
extern bool is_floating_notation_str(char const *str, size_t *retlen);
extern bool is_e_notation(char const *str, size_t len);
extern bool is_e_notation_str(char const *str, size_t *retlen);
extern size_t count_char(char const *str, int ch);
/* find non-whitespace text */
extern size_t find_text(char const *ptr, size_t len, char **first);
extern size_t find_text_str(char const *str, char **first);
#endif /* INCLUDE_JPARSE_UTIL_H */