Strings.cpp 5.81 KB
Newer Older
legoc's avatar
legoc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright 2015 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include "Strings.h"
#include "BadFormatException.h"
legoc's avatar
legoc committed
19
#include <regex>
legoc's avatar
legoc committed
20
21
22
23
24

using namespace std;

namespace cameo {

legoc's avatar
legoc committed
25
26
27
28
29
30
31
32
bool Name::check(const std::string& str) {

	regex nameRegex("[a-zA-Z0-9\\-_]+");
	std::cmatch m;

	return regex_match(str.c_str(), m, nameRegex);
}

legoc's avatar
legoc committed
33
34
35
36
std::vector<std::string> split(const std::string& str, char c) {

	vector<string> result;

legoc's avatar
legoc committed
37
38
	string::size_type lastIndex = 0;
	string::size_type index = str.find(c);
legoc's avatar
legoc committed
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
	while (index != string::npos) {
		result.push_back(str.substr(lastIndex, index - lastIndex));
		lastIndex = index + 1;
		index = str.find(c, lastIndex);
	}
	result.push_back(str.substr(lastIndex));

	return result;
}

Endpoint::Endpoint(const std::string& address, int port) {
	m_address = address;
	m_port = port;
}

const std::string& Endpoint::getAddress() const {
	return m_address;
}

int Endpoint::getPort() const {
	return m_port;
}

Endpoint Endpoint::parse(const std::string& str) {

	if (str.substr(0, 6) != "tcp://") {
		throw new BadFormatException("Bad format for endpoint " + str);
	}

legoc's avatar
legoc committed
68
	string substr = str.substr(6);
legoc's avatar
legoc committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	vector<string> tokens = split(substr, ':');

	if (tokens.size() != 2) {
		throw new BadFormatException("Bad format for endpoint " + str);
	}

	string address = tokens[0];
	int port = 0;

	try {
		port = stoi(tokens[1]);
	}
	catch (...) {
		throw new BadFormatException("Bad format for endpoint " + str);
	}

	return Endpoint(address, port);
}

std::string Endpoint::toString() const {
	return string("tcp://") + m_address + ":" + to_string(m_port);
}

92
93
94
95
NameId::NameId(const std::string& name) :
	m_name(name) {
}

legoc's avatar
legoc committed
96
97
98
99
100
101
102
103
104
NameId::NameId(const std::string& name, int id) :
	m_name(name),
	m_id(id) {
}

const std::string& NameId::getName() const {
	return m_name;
}

105
const std::optional<int>& NameId::getId() const {
legoc's avatar
legoc committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	return m_id;
}

NameId NameId::parse(const std::string& str) {

	vector<string> tokens = split(str, '.');

	if (tokens.size() > 2) {
		throw BadFormatException("Bad format for nameid " + str);
	}

	string name = tokens[0];

	if (tokens.size() == 2) {
		try {
121
122
			int id = stoi(tokens[1]);
			return NameId(name, id);
legoc's avatar
legoc committed
123
124
125
126
127
		}
		catch (...) {
			throw new BadFormatException("Bad format for endpoint " + str);
		}
	}
128
	return NameId(name);
legoc's avatar
legoc committed
129
130
131
132
133
}

std::string NameId::toString() const {

	if (m_id > 0) {
134
		return m_name + "." + to_string(m_id.value());
legoc's avatar
legoc committed
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
	}
	return m_name;
}

ApplicationIdentity::ApplicationIdentity(const NameId& nameId, const Endpoint& endpoint) :
	m_nameId(nameId),
	m_endpoint(endpoint) {
}

const NameId& ApplicationIdentity::getNameId() const {
	return m_nameId;
}

const Endpoint& ApplicationIdentity::getEndpoint() const {
	return m_endpoint;
}

ApplicationIdentity ApplicationIdentity::parse(const std::string& str) {

	vector<string> tokens = split(str, '@');

	if (tokens.size() != 2) {
		throw BadFormatException("Bad format for application identity " + str);
	}

	return ApplicationIdentity(NameId::parse(tokens[0]), Endpoint::parse(tokens[1]));
}

std::string ApplicationIdentity::toString() const {
	return m_nameId.toString() + "@" + m_endpoint.toString();
}

167
168
169
170
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application) :
	m_application(application) {
}

legoc's avatar
legoc committed
171
172
173
174
175
176
177
178
179
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter) :
	m_application(application),
	m_starter(starter) {
}

const ApplicationIdentity& ApplicationAndStarterIdentities::getApplication() const {
	return m_application;
}

180
const std::optional<ApplicationIdentity>& ApplicationAndStarterIdentities::getStarter() const {
legoc's avatar
legoc committed
181
	return m_starter;
legoc's avatar
legoc committed
182
}
legoc's avatar
legoc committed
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

ApplicationAndStarterIdentities ApplicationAndStarterIdentities::parse(const std::string& str) {

	// The string is either <name>@<endpoint>:<name>@<endpoint> or <name>@<endpoint>:
	// To separate the two identities, we search for the last : before the last @.

	string::size_type firstIndex = str.find_first_of('@');

	if (firstIndex == string::npos) {
		throw new BadFormatException("Bad format for application and starter identities " + str);
	}

	string::size_type index = str.find_last_of('@');

	if (index == firstIndex) {

		// Format <name>@<endpoint>:
		if (str[str.length() - 1] != ':') {
			throw BadFormatException("Bad format for application and starter identities " + str);
		}

		string applicationString = str.substr(0, str.length() - 1);

		ApplicationIdentity application = ApplicationIdentity::parse(applicationString);

208
		return ApplicationAndStarterIdentities(application);
legoc's avatar
legoc committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	}
	else {
		// Format <name>@<endpoint>:<name>@<endpoint>
		string substring = str.substr(0, index);
		index = substring.find_last_of(':');

		if (index == string::npos) {
			throw BadFormatException("Bad format for application and starter identities " + str);
		}

		string applicationString = str.substr(0, index);
		string starterString = str.substr(index + 1, str.length() - index);

		ApplicationIdentity application = ApplicationIdentity::parse(applicationString);
		ApplicationIdentity starter = ApplicationIdentity::parse(starterString);

		return ApplicationAndStarterIdentities(application, starter);
	}
}

std::string ApplicationAndStarterIdentities::toString() const {
230
231
232
233
	if (m_starter.has_value()) {
		return m_application.toString() + ":" + m_starter.value().toString();
	}
	return m_application.toString() + ":";
legoc's avatar
legoc committed
234
235
236
237
}

}