forked from joyieldInc/predixy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfParser.h
86 lines (80 loc) · 1.71 KB
/
ConfParser.h
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
/*
* predixy - A high performance and full features proxy for redis.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
#ifndef _PREDIXY_CONF_PARSER_H_
#define _PREDIXY_CONF_PARSER_H_
#include <string.h>
#include <string>
#include <set>
#include <vector>
#include <fstream>
#include "Exception.h"
#include "Util.h"
class ConfParser
{
public:
DefException(EmptyFileName);
DefException(FileNameTooLong);
DefException(GetCwdFail);
DefException(OpenFileFail);
DefException(IncludeFileDuplicate);
DefException(NodeDepthTooLong);
DefException(FileReadError);
DefException(ParseError);
struct Node
{
const char* file;
int line;
std::string key;
std::string val;
Node* next;
Node* sub;
~Node()
{
if (sub) {
delete sub;
}
Node* n = next;
while (n) {
Node* nn = n->next;
n->next = nullptr;
delete n;
n = nn;
}
}
};
private:
enum Status
{
None,
KeyVal,
BeginScope,
EndScope,
Error
};
enum State
{
KeyReady,
KeyBody,
ValReady,
SValBody,
VValBody,
ScopeReady,
ScopeBody
};
public:
ConfParser(int maxNodeDepth = 10);
~ConfParser();
void clear();
Node* load(const char* file);
private:
static std::string* getFile(const std::string& name, const char* parent);
Status parse(std::string& line, std::string& key, std::string& val);
private:
Node mRoot;
int mMaxNodeDepth;
std::vector<std::string*> mStrings;
};
#endif