-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.hpp
242 lines (214 loc) · 4.6 KB
/
types.hpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* @file
*
* @brief Implementation of data types
*
* @copyright BSD License (see doc/LICENSE.md or http://www.libelektra.org)
*
*/
#ifndef ELEKTRA_TYPES_HPP
#define ELEKTRA_TYPES_HPP
#include <locale>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <key.hpp>
#include <keyset.hpp>
#include <iostream>
namespace elektra
{
using namespace kdb;
using namespace std;
class Type
{
public:
virtual bool check (Key k) = 0;
virtual ~Type ();
};
class AnyType : public Type
{
public:
bool check (Key) override
{
return true;
}
};
class EmptyType : public Type
{
public:
bool check (Key k) override
{
return k.getString ().empty ();
}
};
class StringType : public Type
{
public:
bool check (Key k) override
{
return !k.getString ().empty ();
}
};
template <typename T>
class TType : public Type
{
public:
bool check (Key k) override
{
istringstream i (k.getString ());
i.imbue (locale ("C"));
T n;
i >> n;
if (i.bad ()) return false;
if (i.fail ()) return false;
if (!i.eof ()) return false;
return true;
}
};
/**
* Reversible Type
* This checks even more pedantic, if the type is reversible
* to the same string.
* E.g. -1 might get to highest value (but this is not guaranteed)!
* */
template <typename T>
class RType : public Type
{
public:
bool check (Key k) override
{
istringstream i (k.getString ());
i.imbue (locale ("C"));
T n;
i >> n;
if (i.bad ()) return false;
if (i.fail ()) return false;
if (!i.eof ()) return false;
ostringstream o;
o << n;
if (o.str () != k.getString ()) return false;
return true;
}
};
/**
* Reversible Type with min, max values
* This checks even more pedantic, if the type is reversible
* to the same string.
* E.g. -1 might get to highest value (but this is not guaranteed)!
* */
template <typename T>
class MType : public Type
{
public:
bool check (Key k) override
{
istringstream i (k.getString ());
i.imbue (locale ("C"));
T n;
i >> n;
if (i.bad ()) return false;
if (i.fail ()) return false;
if (!i.eof ()) return false;
ostringstream o;
o.imbue (locale ("C"));
o << n;
if (o.fail ()) return false;
if (o.str () != k.getString ()) return false;
Key const min = k.getMeta<const Key> ("check/type/min");
if (min)
{
istringstream i_min (min.getString ());
i_min.imbue (locale ("C"));
T n_min;
i_min >> n_min;
if (i_min.bad ()) return false;
if (i_min.fail ()) return false;
if (!i_min.eof ()) return false;
if (n < n_min) return false;
}
Key const max = k.getMeta<const Key> ("check/type/max");
if (max)
{
istringstream i_max (max.getString ());
i_max.imbue (locale ("C"));
T n_max;
i_max >> n_max;
if (i_max.bad ()) return false;
if (i_max.fail ()) return false;
if (!i_max.eof ()) return false;
if (n > n_max) return false;
}
return true;
}
};
class FSType : public Type
{
std::set<std::string> choices;
public:
FSType ()
{
choices.insert ("auto");
choices.insert ("swap");
choices.insert ("adfs");
choices.insert ("affs");
choices.insert ("autofs");
choices.insert ("cifs");
choices.insert ("coda");
choices.insert ("coherent");
choices.insert ("cramfs");
choices.insert ("debugfs");
choices.insert ("devpts");
choices.insert ("efs");
choices.insert ("ext");
choices.insert ("ext2");
choices.insert ("ext3");
choices.insert ("ext4");
choices.insert ("hfs");
choices.insert ("hfsplus");
choices.insert ("hpfs");
choices.insert ("iso9660");
choices.insert ("jfs");
choices.insert ("minix");
choices.insert ("msdos");
choices.insert ("ncpfs");
choices.insert ("nfs");
choices.insert ("nfs4");
choices.insert ("ntfs");
choices.insert ("proc");
choices.insert ("qnx4");
choices.insert ("ramfs");
choices.insert ("reiserfs");
choices.insert ("romfs");
choices.insert ("smbfs");
choices.insert ("sysv");
choices.insert ("tmpfs");
choices.insert ("udf");
choices.insert ("ufs");
choices.insert ("umsdos");
choices.insert ("usbfs");
choices.insert ("vfat");
choices.insert ("xenix");
choices.insert ("xfs");
choices.insert ("xiafs");
}
bool check (Key k) override
{
std::string label = k.getString ();
size_t oldpos = 0;
size_t pos = label.find (',');
;
while (pos != string::npos)
{
std::string type = label.substr (oldpos, pos - oldpos);
if (choices.find (type) == choices.end ()) return false;
oldpos = pos + 1;
pos = label.find (',', oldpos);
}
std::string lastType = label.substr (oldpos, string::npos);
if (choices.find (lastType) == choices.end ()) return false;
return true;
}
};
} // end namespace elektra
#endif