Repository navigation
Expand file tree
/
Copy pathparser.h
More file actions
180 lines (145 loc) · 3.91 KB
/
Copy pathparser.h
File metadata and controls
180 lines (145 loc) · 3.91 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
/*
* parser.h
*
* Copyright 2022 Armin Diehl <ad@ardiehl.de>
*
* Simple text file parser used to read config file
*
* License: GPL
*
*/
#ifndef PARSER_H_INCLUDED
#define PARSER_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#define TK_EOF -10
#define TK_EOL -11
#define TK_INTVAL -12
#define TK_FLOATVAL -13
#define TK_STRVAL -14
#define TK_IDENT -15
#define TK_SECTION -16
typedef struct buf_t buf_t;
struct buf_t {
int allocSize;
int len;
char *buf;
char *bufEnd;
};
void buf_init(buf_t * b, int allocSize);
void buf_addChar(buf_t * b, char c);
void buf_clear(buf_t * b);
typedef struct parserToken_t parserToken_t;
struct parserToken_t {
char *name;
int value;
parserToken_t *next;
};
typedef struct parser_t parser_t;
struct parser_t {
char *charTokens;
parserToken_t * tokens;
int col;
char *currPos;
char *currLine;
int currLineNo;
FILE *fh;
char *fileName;
int iVal;
double fVal;
char *strVal;
int firstTokenInLine;
char *buf;
};
/**
parser_getTokenTxt
@param a pointer to an initialized parser struct
@param token value
@return pointer to a string, needs to be freed
*/
char * parserGetTokenTxt(parser_t * pa, int tk);
/**
* Initialize the parser
* @param a string of single character tokens. parserGetToken will return these starting with 0
* @param Tokens, pairs of string and value, the last one have to be NULL
* @return a pointer to parser_t, to be free'd using parserFree
*/
parser_t * parserInit (const char * charTokens, ...);
void parserFree (parser_t * pa);
/**
parserBegin
@param a pointer to an initialized parser struct
@param name of the file to be read
@param 1 to skip lines until the first line that starts with [
@return negative value indicates an error
*/
int parserBegin (parser_t * pa, const char * fileName, int skipToSectionStart);
/**
parserBeginBuf
@param a pointer to an initialized parser struct
@param null terminated text to parse
@param 1 to skip lines until the first line that starts with [
@return negative value indicates an error
*/
int parserBeginBuf (parser_t * pa, const char * buf, int skipToSectionStart);
/**
peek char
@param a pointer to an initialized parser struct
@return current character or less than 0 if no char is available (e.g. TK_EOL, TK_EOF)
*/
int pch (parser_t * pa);
/**
peek next char
@param a pointer to an initialized parser struct
@return next character or less than 0 if no char is available (e.g. TK_EOL, TK_EOF)
*/
int pchnext (parser_t * pa);
/**
get char
@param a pointer to an initialized parser struct
@return current character or less than 0 if no char is available (e.g. TK_EOL, TK_EOF)
*/
int gch (parser_t * pa);
/**
get token
@param a pointer to an initialized parser struct
@return next token and values (iVal,fVal or strVal in pa)
*/
int parserGetToken (parser_t *pa);
/**
* scans for a section start, blank lines will be skipped
* @param a pointer to an initialized parser struct
* @return either TK_SECTION or TK_EOF
*/
int parserExpectSection (parser_t * pa);
/**
shows the filename, the current position in the file as well as txt and exits
@param a pointer to an initialized parser struct
@param error message or format string (without newline)
@
*/
void parserError(parser_t *pa, const char *format, ...);
/**
if next token is an integer, return the value otherwise show error and exit
@param a pointer to an initialized parser struct
@return integer value
*/
int parserExpectInteger(parser_t * pa);
/**
if next token is not the given one show error and exit
@param a pointer to an initialized parser struct
@return expected token value
*/
int parserExpect(parser_t * pa, int tkExpected);
/**
if next token is not the given one or EOL show error and exit
@param a pointer to an initialized parser struct
@return expected token value
*/
int parserExpectOrEOL(parser_t * pa, int tkExpected);
#ifdef __cplusplus
}
#endif
#endif // PARSER_H_INCLUDED