summaryrefslogtreecommitdiffstats
path: root/src/btparser/utils.h
blob: 680e67e05389a105e7f979c6d0c052ecbe0e6050 (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
/*
    utils.h

    Copyright (C) 2010  Red Hat, Inc.

    This program 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.

    This 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.
*/
#ifndef BTPARSER_UTILS_H
#define BTPARSER_UTILS_H

#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define BTP_lower "abcdefghijklmnopqrstuvwxyz"
#define BTP_upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define BTP_alpha BTP_lower BTP_upper
#define BTP_space " \t\r\n\v\f"
#define BTP_digit "0123456789"
#define BTP_alnum BTP_alpha BTP_digit

/**
 * Debugging output to stdout while parsing.
 * Default value is false.
 */
extern bool
btp_debug_parser;

/**
 * Never returns NULL.
 */
void *
btp_malloc(size_t size);

/**
 * Never returns NULL.
 */
char *
btp_vasprintf(const char *format, va_list p);

/**
 * Never returns NULL.
 */
char *
btp_strdup(const char *s);

/**
 * Never returns NULL.
 */
char *
btp_strndup(const char *s, size_t n);

/**
 * A strcmp() variant that works also with NULL parameters.  NULL is
 * considered to be less than a string.
 */
int
btp_strcmp0(const char *s1, const char *s2);

/**
 * A strchr() variant providing line and column in the string s
 * indicating where the char c was found.
 * @param line
 * Starts from 1.  Its value is valid only when this function does not
 * return NULL.
 * @param column
 * Starts from 0.  Its value is valid only when this function does not
 * return NULL.
 */
char *
btp_strchr_location(const char *s, int c, int *line, int *column);

/**
 * A strstr() variant providing line and column of the haystick
 * indicating where the needle was found.
 * @param line
 * Starts from 1.  Its value is valid only when this function does not
 * return NULL.
 * @param column
 * Starts from 0.  Its value is valid only when this function does not
 * return NULL.
 */
char *
btp_strstr_location(const char *haystack,
                    const char *needle,
                    int *line,
                    int *column);

/**
 * A strspn() variant providing line and column of the string s which
 * corresponds to the returned length.
 * @param line
 * Starts from 1.
 * @param column
 * Starts from 0.
 */
size_t
btp_strspn_location(const char *s,
                    const char *accept,
                    int *line,
                    int *column);

/**
 * Loads file contents to a string.
 * @returns
 * File contents. If file opening/reading fails, NULL is returned.
 */
char *
btp_file_to_string(const char *filename);

/**
 * If the input contains character c in the current positon, move the
 * input pointer after the character, and return true. Otherwise do
 * not modify the input and return false.
 */
bool
btp_skip_char(char **input, char c);

/**
 * If the input contains one of allowed characters, move
 * the input pointer after that character, and return true.
 * Otherwise do not modify the input and return false.
 */
bool
btp_skip_char_limited(char **input, const char *allowed);

/**
 * If the input contains one of allowed characters, store
 * the character to the result, move the input pointer after
 * that character, and return true. Otherwise do not modify
 * the input and return false.
 */
bool
btp_parse_char_limited(char **input, const char *allowed, char *result);

/**
 * If the input contains the character c one or more times, update it
 * so that the characters are skipped. Returns the number of characters
 * skipped, thus zero if **input does not contain c.
 */
int
btp_skip_char_sequence(char **input, char c);

/**
 * If the input contains one or more characters from string chars,
 * move the input pointer after the sequence. Otherwise do not modify
 * the input.
 * @returns
 * The number of characters skipped.
 */
int
btp_skip_char_span(char **input, const char *chars);

/**
 * If the input contains one or more characters from string chars,
 * move the input pointer after the sequence. Otherwise do not modify
 * the input.
 * @param line
 * Starts from 1. Corresponds to the returned number.
 * @param column
 * Starts from 0. Corresponds to the returned number.
 * @returns
 * The number of characters skipped.
 */
int
btp_skip_char_span_location(char **input,
                            const char *chars,
                            int *line,
                            int *column);

/**
 * If the input contains one or more characters from string accept,
 * create a string from this sequence and store it to the result, move
 * the input pointer after the sequence, and return the lenght of the
 * sequence.  Otherwise do not modify the input and return 0.
 *
 * If this function returns nonzero value, the caller is responsible
 * to free the result.
 */
int
btp_parse_char_span(char **input, const char *accept, char **result);

/**
 * If the input contains characters which are not in string reject,
 * create a string from this sequence and store it to the result,
 * move the input pointer after the sequence, and return true.
 * Otherwise do not modify the input and return false.
 *
 * If this function returns true, the caller is responsible to
 * free the result.
 */
bool
btp_parse_char_cspan(char **input, const char *reject, char **result);

/**
 * If the input contains the string, move the input pointer after
 * the sequence. Otherwise do not modify the input.
 * @returns
 * Number of characters skipped. 0 if the input does not contain the
 * string.
 */
int
btp_skip_string(char **input, const char *string);

/**
 * If the input contains the string, copy the string to result,
 * move the input pointer after the string, and return true.
 * Otherwise do not modify the input and return false.
 *
 * If this function returns true, the caller is responsible to free
 * the result.
 */
bool
btp_parse_string(char **input, const char *string, char **result);

/**
 * If the input contains digit 0-9, return it as a character
 * and move the input pointer after it. Otherwise return
 * '\0' and do not modify the input.
 */
char
btp_parse_digit(char **input);

/**
 * If the input contains [0-9]+, move the input pointer
 * after the number.
 * @returns
 * The number of skipped characters. 0 if input does not start with a
 * digit.
 */
int
btp_skip_unsigned_integer(char **input);

/**
 * If the input contains [0-9]+, parse it, move the input pointer
 * after the number.
 * @returns
 * Number of parsed characters. 0 if input does not contain a number.
 */
int
btp_parse_unsigned_integer(char **input, unsigned *result);

/**
 * If the input contains 0x[0-9a-f]+, move the input pointer
 * after that.
 * @returns
 * The number of characters processed from input. 0 if the input does
 * not contain a hexadecimal number.
 */
int
btp_skip_hexadecimal_number(char **input);

/**
 * If the input contains 0x[0-9a-f]+, parse the number, and move the
 * input pointer after it.  Otherwise do not modify the input.
 * @returns
 * The number of characters read from input. 0 if the input does not
 * contain a hexadecimal number.
 */
int
btp_parse_hexadecimal_number(char **input, uint64_t *result);

#ifdef __cplusplus
}
#endif

#endif