-
Notifications
You must be signed in to change notification settings - Fork 2
/
StreamParser.c
215 lines (180 loc) · 5.69 KB
/
StreamParser.c
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
/**
* @file StreamParser.c
* @author ZheWana (zhewana@qq.com)
* @brief A parser that can help to parse specific information from byte stream.
* @date 2023-07-24
*
* @copyright Copyright (c) 2023
*
*/
#include "StreamParser.h"
#include "string.h"
#if USE_STRING_PARSE
#include "stdlib.h"
#endif
#define IS_NUMBER(ch) (ch >= '0' && ch <= '9')
static char* errorMsg[] = {
"Error: malloc Failed.",
"Error: String parser disabled.",
"Error: Type list error."
};
void SParser_Init(pSParser_t parser, char* headStr, char* tailStr, char* divStr, char* typeList)
{
parser->headStr = headStr;
parser->headStrLen = headStr == NULL ? 0 : strlen(headStr);
parser->tailStr = tailStr;
parser->TailStrLen = tailStr == NULL ? 0 : strlen(tailStr);
parser->divStr = divStr;
parser->divStrLen = divStr == NULL ? 0 : strlen(divStr);
parser->typeList = typeList;
parser->chPtr[0] = headStr;
parser->typePtr = parser->typeList;
parser->buff[0] = parser->buff[1] = parser->buff[2] = parser->buff[3] = 0;
parser->mState = FindHead;
}
static metaData_t Parse_Intenger(pSParser_t parser, const char ch)
{
metaData_t temp = { 0 };
if (parser->buff[0] == 0 && ch == '-') {
parser->buff[0] = -1;
} else if (IS_NUMBER(ch)) {
parser->buff[2] = parser->buff[2] * 10 + ch - '0';
} else if (ch == *parser->tailStr || ch == *parser->divStr || ch == '.') {
temp.intenger = parser->buff[0] == 0 ? parser->buff[2] : parser->buff[2] * -1;
parser->buff[0] = parser->buff[2] = 0;
return temp;
}
return temp;
}
static metaData_t Parse_Float(pSParser_t parser, const char ch)
{
metaData_t temp = { 0 };
if ((temp = Parse_Intenger(parser, ch)).intenger) {
parser->buff[1 + parser->buff[3]] = temp.intenger;
}
if (ch == '.') {
parser->buff[3] = 1;
}
if (ch == *parser->tailStr || ch == *parser->divStr) {
temp.flt = parser->buff[2];
while (temp.flt >= 1) {
temp.flt /= 10;
}
temp.flt = (floatTypdef)parser->buff[1] + (parser->buff[1] >= 0 ? temp.flt : -temp.flt);
memset(parser->buff, 0, 4 * sizeof(size_t));
return temp;
}
return (metaData_t)0;
}
#if USE_STRING_PARSE
static char* Parse_String(pSParser_t parser, char* strBuff, const char ch)
{
if (parser->buff[3] == 0) {
if (strBuff == NULL) {
strBuff = malloc(STRING_BUFFER_SIZE * sizeof(char));
if (NULL == strBuff) {
return errorMsg[0];
}
}
parser->buff[3] = 1;
parser->buff[0] = parser->buff[1] = (size_t)strBuff;
}
if (ch == *parser->tailStr || ch == *parser->divStr) {
char* temp = (char*)parser->buff[1];
*(char*)parser->buff[0] = '\0';
parser->buff[0] = parser->buff[3] = parser->buff[1] = 0;
return temp;
}
if (parser->buff[0] == 0 || ((char*)parser->buff[0] - (char*)parser->buff[1]) >= STRING_BUFFER_SIZE)
return NULL;
*(char*)parser->buff[0] = ch;
parser->buff[0] += sizeof(char);
return NULL;
}
#endif
/**
* @return return value as follow:
* 1: compared equal
* 0: compared not equal
*/
static int CompareString(pSParser_t parser, const char* str, const int strLen, const char ch, const int pSelect)
{
if (parser->chPtr[pSelect] == NULL) {
return 1;
} else if (ch == *parser->chPtr[pSelect]) {
EqualHook:
if (strLen == 1) {
return 1;
} else {
parser->chPtr[pSelect] = parser->chPtr[pSelect] + 1;
if (*parser->chPtr[pSelect] == '\0')
return 1;
}
} else {
parser->chPtr[pSelect] = (char*)str;
if (ch == *parser->chPtr[pSelect]) {
goto EqualHook;
}
}
return 0;
}
int SParser_Parse(pSParser_t parser, pMetaData_t dataArray, const char ch)
{
switch (parser->mState) {
case FindHead:
if (CompareString(parser, parser->headStr, parser->headStrLen, ch, 0)) {
parser->chPtr[0] = parser->divStr;
parser->chPtr[1] = parser->tailStr;
parser->mState = Parse;
}
break;
case Parse:
switch (*parser->typePtr) {
metaData_t temp;
case ' ':
case '\0':
break;
case 'd':
if ((temp = Parse_Intenger(parser, ch)).intenger)
parser->temp = temp;
break;
case 'f':
if ((temp = Parse_Float(parser, ch)).flt)
parser->temp = temp;
break;
case 's':
#if USE_STRING_PARSE
parser->temp.strPtr = Parse_String(parser, dataArray[parser->typePtr - parser->typeList].strPtr, ch);
#else
parser->temp.strPtr = errorMsg[1];
#endif
break;
default:
parser->temp.strPtr = errorMsg[2];
break;
}
if (CompareString(parser, parser->divStr, parser->divStrLen, ch, 0)) {
parser->chPtr[0] = parser->divStr;
if (*parser->typePtr != ' ' && *parser->typePtr != '\0') {
dataArray[parser->typePtr - parser->typeList] = parser->temp;
}
if (*(parser->typePtr) == '\0') {
goto EndOfParse;
} else {
parser->typePtr++;
}
}
if (CompareString(parser, parser->tailStr, parser->TailStrLen, ch, 1)) {
if (*parser->typePtr != ' ')
dataArray[parser->typePtr - parser->typeList] = parser->temp;
EndOfParse:
parser->chPtr[0] = parser->headStr;
parser->typePtr = parser->typeList;
parser->mState = FindHead;
return 1;
}
default:
break;
}
return 0;
}