-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit.H
More file actions
89 lines (85 loc) · 2.17 KB
/
split.H
File metadata and controls
89 lines (85 loc) · 2.17 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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
/*
* split.H
*
* Created on: May 1, 2013
* Author: xaxaxa
*/
#ifndef SPLIT_H_
#define SPLIT_H_
#include <cpoll/basictypes.H>
namespace cppsp
{
struct split
{
const char* s;
const char* end;
CP::String value;
char delim;
split(const char* s, int len, char delim) {
if (len == -1) len = strlen(s);
this->s = s;
this->end = s + len;
this->delim = delim;
}
bool read() {
if (s == nullptr || s == end) return false;
const char* s1 = s;
s = (const char*) memchr(s, delim, end - s);
if (s == nullptr) {
value= {s1, int(end - s1)};
return true;
}
value= {s1, int(s - s1)};
++s;
return true;
}
};
//like split, but allows options containing the delimiter to be enclosed in quotes
struct optionParser
{
const char* s;
const char* end;
CP::String value;
char delim;
optionParser(const char* s, int len, char delim = ' ') {
if (len == -1) len = strlen(s);
this->s = s;
this->end = s + len;
this->delim = delim;
}
bool read() {
if (s == nullptr || s == end) return false;
const char* s1 = s;
if (*s == '"') {
s = (const char*) memchr(s + 1, '"', end - s - 1);
if (s == nullptr) throw invalid_argument("unterminated quote");
value= {s1+1,int(s-s1)-1};
//skip delimiter if present
if ((++s) < end && *s == delim) s++;
return true;
} else {
s = (const char*) memchr(s, delim, end - s);
if (s == nullptr) {
value= {s1, int(end - s1)};
return true;
}
value= {s1, int(s - s1)};
++s;
}
return true;
}
};
}
#endif /* SPLIT_H_ */