-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamParser.h
More file actions
132 lines (119 loc) · 3.78 KB
/
StreamParser.h
File metadata and controls
132 lines (119 loc) · 3.78 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
/**
* @file StreamParser.h
* @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
*
*/
#ifndef __STREAM_HANDLER__
#define __STREAM_HANDLER__
#include "stdint.h"
/**
* @brief Config whether to use double other than float for "f" type.
*/
#define USE_DOUBLE (1)
/**
* @brief Config whether to use string parse function.
* @note String parse may cause addtional memory use through "malloc" in stdlib.
* @warning !!! Remember to free string memory after using it !!!
*/
#define USE_STRING_PARSE (1)
/**
* @brief Just the size of the buffer that you give us.
*
*/
#define STRING_BUFFER_SIZE (64)
#if USE_DOUBLE
typedef double floatTypdef;
#else
typedef float floatTypdef;
#endif
/**
* @brief Define metadata for return value to use.
*
*/
typedef union MetaDataTypedef {
void* strPtr;
floatTypdef flt;
int intenger;
} metaData_t, *pMetaData_t;
typedef struct StreamParser {
char* headStr;
char* divStr;
char* tailStr;
/**
* @note "typeList" should be input as follow:
* " " for ignore
* "s" for string
* "d" for intenger
* "f" for float
*/
char* typeList;
char* typePtr;
char* chPtr[2];
/**
* @brief parser buffer refer as:
* | sign | intenger | decimal | flags |
*/
size_t buff[4];
metaData_t temp;
enum {
FindHead,
Parse
} mState;
uint8_t headStrLen;
uint8_t divStrLen;
uint8_t TailStrLen;
} sParser_t, *pSParser_t;
/**
* @brief Init parser parameters.
*
* @param parser parser handle
* @param headStr The header string before all of the data.
* @param tailStr The tail string after all of the data.
* @param divStr The separator string between each data.
* @param typeList A list of the data type between head and tail.
*
* @note For "headStr", "tailStr" and "divStr":
* You can use not only one character but also a lot of characters
* as the parameter string.But no matter how much the character is,
* it SHOULD be a "string".
*
* @note For "typeList" you can use as follow: \n
* * " " to ignore; \n
* * "s" for string; \n
* * "f" for float; \n
* * "d" for intenger. \n
*
* @example Considering you want to parse data from the following GPS NMEA protocol:
* $GPGGA,092204.999,4250.5589,S,14718.5084,E,1,04,24.4,19.7,M,,,,0000*1F
* You can use "$GP" as the headStr, "," as the divStr and "*" as the tailStr, and
* use the " ffsfsddffs" as the typeList, we will ignore all the data which is not
* included in your typeList. Just look like this:
* SParser_Init(&parser, "$GP", "*", ",?", strList[0]);
*/
void SParser_Init(pSParser_t parser, char* headStr, char* tailStr, char* divStr, char* typeList);
/**
* @brief Parse data
*
* @param parser Parser handle
* @param dataArray A meatData array which used as a return value list for parsed data
* @param ch Each character in the byte stream
* @return int Return whether the parse is finished refer as follow:
* * 0: unfinished;
* * 1: finished.
*
* @note For parameter "dataArray":
* If you set the macro USE_STRING_PARSE as 1 which means you are using the string
* parse function, you should ensure that the element in dataArray which corresponding
* the "s" in typeList is in your control.You should set it as follow: \n
* * NULL : if you do not provide a buffer for string to restore; \n
* * BUFFER ADDRESS : if you can privide a buffer for string to restore. \n
* By the way, your buffer should preferably be greater than or equal to the macro: \n
* STRING_BUFFER_SIZE \n
* which is the buffer size we will creat for your string if you give us NULL.
*/
int SParser_Parse(pSParser_t parser, pMetaData_t dataArray, const char ch);
#endif // __STREAM_HANDLER__