-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
121 lines (105 loc) · 2.34 KB
/
common.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
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
#ifndef COMMON_H
#define COMMON_H
#include <memory>
#include <string>
#include <map>
#include <list>
class Variant{
public:
enum Type {EMPTY, STRING, MAP, LIST};
typedef std::map< std::string, Variant> vmap;
typedef std::list< Variant > vlist;
Variant(){}
Variant(const std::string &val): mText(val), mType(STRING){}
Variant(const vmap &val): mMap(val), mType(MAP){}
Variant(const vlist val): mList(val), mType(LIST){}
std::string toText() const{
return mText;
}
vmap toMap() const{
return mMap;
}
vlist toList() const{
return mList;
}
Variant& operator= (const std::string& val){
clear();
mType = STRING;
mText = val;
return *this;
}
Variant& operator= (const char* val){
clear();
mType = STRING;
mText = val;
return *this;
}
Variant& operator= (const vmap& val){
clear();
mType = MAP;
mMap = val;
return *this;
}
Variant& operator= (const vlist& val){
clear();
mType = LIST;
mList = val;
return *this;
}
Type type() const{
return mType;
}
void clear(){
mType = EMPTY;
mMap.clear();
mList.clear();
mText = "";
}
private:
Type mType = EMPTY;
std::string mText;
vmap mMap;
vlist mList;
};
typedef Variant::vmap vmap;
typedef Variant::vlist vlist;
struct Val{
enum Type{NOCHANGE, IN, OUT};
std::string key;
std::string value;
int depth;
Type state;
vmap attrs;
};
#include <algorithm>
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
static inline std::string trimmed(const std::string &val)
{
std::string s = val;
ltrim(s);
rtrim(s);
return s;
}
static inline std::string trim(std::string &s)
{
ltrim(s);
rtrim(s);
return s;
}
template <typename T, typename N>
bool contains(std::map<T, N>& In, const T& key)
{
return In.find(key) != In.end();
}
#endif // COMMON_H