-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.hpp
171 lines (150 loc) · 4.3 KB
/
parser.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
/**
* @file parser.hpp
* @author Marcus Edel
*
* Miscellaneous parser routines.
*/
#include <iostream>
#include <string>
#include <stdlib.h>
#include <exception>
namespace parser {
// Info structure used to store the parsed information.
struct info
{
int id;
std::string event;
int eventId;
};
// The event vector contains all events that are parsed by the parser.
std::vector<std::string> events = {"weight",
"gradient",
"input",
"prediction",
"info",
"start",
"state",
"confusion",
"reset",
"step"};
/*
* Cuts off the delimiter (default) and everything that follows.
*
* @param text - The input text.
* @param delimiter - The delimiter that should be cut off.
* @keepDelimiter - Keep the delimiter and cut everything that follows after
* the delimiter.
* @return The parsed text.
*/
static inline std::string TailSlice(std::string& text,
std::string delimiter,
bool keepDelimiter = false)
{
size_t delimiterLocation = text.find(delimiter);
std::string output = "";
if (delimiterLocation < std::string::npos)
{
size_t start = keepDelimiter ?
delimiterLocation : delimiterLocation + delimiter.length();
size_t end = text.length() - start;
output = text.substr(start, end);
text = text.substr(0, delimiterLocation);
}
return output;
}
/*
* Cuts off the delimiter (default) and everything that precedes.
*
* @param text - The input text.
* @param delimiter - The delimiter that should be cut off.
* @return The parsed text.
*/
static inline std::string HeadSlice(std::string &text,
std::string delimiter)
{
auto delimiterLocation = text.find(delimiter);
std::string output = "";
if (delimiterLocation < std::string::npos)
{
output = text.substr(0, delimiterLocation);
text = text.substr(delimiterLocation + delimiter.length(),
text.length() - (delimiterLocation + delimiter.length()));
}
return output;
}
/*
* Function to extract the event id (uri := domain.tld/{event}={event id}.
*
* @param uri - The input uri.
* @param event - The event that should be used to extract the id.
* @return The event id.
*/
static inline int ExtractEventID(std::string uri, std::string event)
{
int id = -1;
std::string idString = HeadSlice(uri, event + "=");
if (idString.length() > 0)
{
TailSlice(uri, "&");
try
{
id = atoi(uri.c_str());
}
catch (std::exception e)
{
id = -1;
}
}
return id;
}
/*
* Function to extract the job id (uri := domain.tld/id={job id} using the
* uri as input.
*
* @param uri - The input uri.
* @return The job id.
*/
static inline int ExtractID(std::string uri)
{
return ExtractEventID(uri, "id");
}
/*
* Function to extract the first event (uri := domain.tld/{event}={job id}
* using the uri as input. All events are defined in the events vector.
*
* @param uri - The input uri.
* @return The event string.
*/
static inline std::string ExtractEvent(std::string uri)
{
for (std::string event : events)
{
std::string eventString = HeadSlice(uri, event + "=");
if (eventString.length() > 0)
{
return event;
}
}
return "";
}
/*
* Extract the job id and the first event and event id from the specified uri.
*
* @param uri - The input uri.
* @param urlInfo - The info object used to store the information.
*/
static inline void parseURI(const std::string& uri, info& urlInfo)
{
urlInfo.id = ExtractID(uri);
urlInfo.event = "";
urlInfo.eventId = -1;
if (urlInfo.id >= 0)
{
urlInfo.event = ExtractEvent(uri);
if (urlInfo.event != "")
{
urlInfo.eventId = ExtractEventID(uri, urlInfo.event);
}
}
}
}