scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
zid.cpp
Go to the documentation of this file.
1#include "./zid.hpp"
2
3#include "boost/algorithm/string.hpp"
4
5#include <iostream>
6#include <sstream>
7
8using namespace std;
9
10namespace scifir
11{
12 zid::zid() : aid(),regions(),country(),zone(),zone_type(zid::NONE)
13 {}
14
15 zid::zid(const zid& x) : aid(x.aid),regions(x.regions),country(x.country),zone(x.zone),zone_type(x.zone_type)
16 {}
17
18 zid::zid(zid&& x) : aid(std::move(x.aid)),regions(std::move(x.regions)),country(std::move(x.country)),zone(std::move(x.zone)),zone_type(std::move(x.zone_type))
19 {}
20
21 zid::zid(const scifir::aid& new_aid,const zid::type& new_zid_type,const string& new_country) : aid(),country(),regions(),zone(),zone_type(zid::NONE)
22 {
24 {
25 aid = new_aid;
28 }
29 }
30
31 zid::zid(const scifir::aid& new_aid,const string& new_country,const vector<string>& new_regions) : aid(new_aid),country(new_country),regions(new_regions),zone(),zone_type(zid::REGION)
32 {}
33
34 zid::zid(const scifir::aid& new_aid,const zid::type& new_zid_type,const string& new_country,const vector<string>& new_regions,const string& new_zone) : aid(),country(),regions(),zone(),zone_type(zid::NONE)
35 {
37 {
38 aid = new_aid;
41 zone = new_zone;
43 }
44 }
45
46 zid::zid(const scifir::aid& new_aid,const string& init_zid) : aid(new_aid),regions(),country(),zone(),zone_type(zid::NONE)
47 {
48 if (init_zid != "")
49 {
50 vector<string> values;
51 boost::split(values,init_zid,boost::is_any_of(":"));
52 if (values[0].front() == '(' and isalpha(values[0][1]))
53 {
54 zone_type = create_zone_type(values[0].substr(1,1));
55 string new_country;
56 if (values[0][3] != ' ')
57 {
58 new_country = values[0].substr(3);
59 }
60 else
61 {
62 new_country = values[0].substr(4);
63 }
64 if (values.size() == 1 and zone_type == zid::COUNTRY)
65 {
67 }
68 if ((values.size() >= 2 and zone_type == zid::REGION) or (values.size() >= 3 and (zone_type == zid::ZONE or zone_type == zid::SETTLEMENT)))
69 {
73 {
74 total_for_values = int(values.size());
75 }
77 {
78 total_for_values = int(values.size()) - 1;
79 }
80 for (int i = 1; i < total_for_values; i++)
81 {
82 regions.push_back(values[i]);
83 }
84 }
85 if (values.size() >= 3 and (zone_type == zid::ZONE or zone_type == zid::SETTLEMENT))
86 {
88 zone = values.back();
89 }
90 if (country == "")
91 {
93 }
94 }
95 }
96 }
97
98 zid::zid(const string& init_zid_full) : zid()
99 {
101 }
102
104 {
105 aid = x.aid;
106 regions = x.regions;
107 country = x.country;
108 zone = x.zone;
110 return *this;
111 }
112
114 {
115 aid = std::move(x.aid);
116 regions = std::move(x.regions);
117 country = std::move(x.country);
118 zone = std::move(x.zone);
119 zone_type = std::move(x.zone_type);
120 return *this;
121 }
122
124 {
126 return *this;
127 }
128
130 {
131 return (country == "no-country");
132 }
133
135 {
136 return (country == "unknown-country");
137 }
138
139 string zid::display() const
140 {
141 if (country != "")
142 {
144 out << to_string(aid) << " (" << to_string(zone_type) << ") " << country;
145 if (regions.size() > 0 and zone != "")
146 {
147 out << ":";
148 }
149 for (const string& x_region : regions)
150 {
151 out << x_region << ":";
152 }
153 out << zone;
154 return out.str();
155 }
156 else
157 {
158 return "";
159 }
160 }
161
162 string zid::partial_display() const
163 {
164 if (country != "")
165 {
167 out << "(" << to_string(zone_type) << ") " << country << ":";
168 for (const string& x_region : regions)
169 {
170 out << x_region << ":";
171 }
172 out << zone;
173 return out.str();
174 }
175 else
176 {
177 return "";
178 }
179 }
180
182 {
183 if (init_zid_full != "")
184 {
185 if (!(init_zid_full[1] == 'C' or init_zid_full[1] == 'R' or init_zid_full[1] == 'Z'))
186 {
187 std::size_t last_whitespace = init_zid_full.find(" (");
188 string init_aid = init_zid_full.substr(0,last_whitespace);
189 string init_zid = init_zid_full.substr(last_whitespace + 1);
191 vector<string> values;
192 boost::split(values,init_zid,boost::is_any_of(":"));
193 if (values[0].front() == '(' and isalpha(values[0][1]))
194 {
195 zone_type = create_zone_type(values[0].substr(1,1));
196 string new_country;
197 if (values[0][3] != ' ')
198 {
199 new_country = values[0].substr(3);
200 }
201 else
202 {
203 new_country = values[0].substr(4);
204 }
205 if (values.size() == 1 and zone_type == zid::COUNTRY)
206 {
208 }
209 if ((values.size() >= 2 and zone_type == zid::REGION) or (values.size() >= 3 and (zone_type == zid::ZONE or zone_type == zid::SETTLEMENT)))
210 {
213 if (zone_type == zid::REGION)
214 {
215 total_for_values = int(values.size());
216 }
218 {
219 total_for_values = int(values.size()) - 1;
220 }
221 for (int i = 1; i < total_for_values; i++)
222 {
223 regions.push_back(values[i]);
224 }
225 }
226 if (values.size() >= 3 and (zone_type == zid::ZONE or zone_type == zid::SETTLEMENT))
227 {
229 zone = values.back();
230 }
231 if (country == "")
232 {
234 }
235 }
236 }
237 else
238 {
239 vector<string> values;
240 boost::split(values,init_zid_full,boost::is_any_of(":"));
241 if (values[0].front() == '(' and isalpha(values[0][1]))
242 {
243 zone_type = create_zone_type(values[0].substr(1,1));
244 string new_country;
245 if (values[0][3] != ' ')
246 {
247 new_country = values[0].substr(3);
248 }
249 else
250 {
251 new_country = values[0].substr(4);
252 }
253 if (values.size() == 1 and zone_type == zid::COUNTRY)
254 {
256 }
257 if ((values.size() >= 2 and zone_type == zid::REGION) or (values.size() >= 3 and (zone_type == zid::ZONE or zone_type == zid::SETTLEMENT)))
258 {
261 if (zone_type == zid::REGION)
262 {
263 total_for_values = int(values.size());
264 }
266 {
267 total_for_values = int(values.size()) - 1;
268 }
269 for (int i = 1; i < total_for_values; i++)
270 {
271 regions.push_back(values[i]);
272 }
273 }
274 if (values.size() >= 3 and (zone_type == zid::ZONE or zone_type == zid::SETTLEMENT))
275 {
277 zone = values.back();
278 }
279 if (country == "")
280 {
282 }
283 }
284 }
285 }
286 }
287
288 string to_string(const zid& x)
289 {
290 return x.display();
291 }
292
293 string to_string(const zid::type& x)
294 {
295 switch (x)
296 {
297 case zid::NONE:
298 return "";
299 case zid::COUNTRY:
300 return "C";
301 case zid::REGION:
302 return "R";
303 case zid::SETTLEMENT:
304 return "S";
305 case zid::ZONE:
306 return "Z";
307 }
308 return "";
309 }
310
312 {
313 if (zone_type_abbreviation == "C")
314 {
315 return zid::COUNTRY;
316 }
317 else if (zone_type_abbreviation == "R")
318 {
319 return zid::REGION;
320 }
321 else if (zone_type_abbreviation == "S")
322 {
323 return zid::SETTLEMENT;
324 }
325 else if (zone_type_abbreviation == "Z")
326 {
327 return zid::ZONE;
328 }
329 else
330 {
331 return zid::NONE;
332 }
333 }
334}
335
336bool operator ==(const scifir::zid& x, const scifir::zid& y)
337{
338 if (x.aid == y.aid)
339 {
340 if (x.country == y.country)
341 {
342 for (unsigned int i = 0; i < x.regions.size(); i++)
343 {
344 if (x.regions[i] != y.regions[i])
345 {
346 return false;
347 }
348 }
349 if (x.zone == y.zone)
350 {
351 return true;
352 }
353 else
354 {
355 return false;
356 }
357 }
358 else
359 {
360 return false;
361 }
362 }
363 else
364 {
365 return false;
366 }
367}
368
369bool operator !=(const scifir::zid& x, const scifir::zid& y)
370{
371 return !(x == y);
372}
373
374bool operator ==(const scifir::zid& x, const string& init_zid_full)
375{
376 scifir::zid y = scifir::zid(init_zid_full);
377 return (x == y);
378}
379
380bool operator !=(const scifir::zid& x, const string& init_zid_full)
381{
382 return !(x == init_zid_full);
383}
384
385bool operator ==(const string& init_zid_full, const scifir::zid& x)
386{
387 scifir::zid y = scifir::zid(init_zid_full);
388 return (x == y);
389}
390
391bool operator !=(const string& init_zid_full, const scifir::zid& x)
392{
393 return !(init_zid_full == x);
394}
395
396void operator +=(string& x, const scifir::zid& y)
397{
398 ostringstream output;
399 output << y;
400 x += output.str();
401}
402
403string operator +(const string& x, const scifir::zid& y)
404{
405 ostringstream output;
406 output << x;
407 output << y;
408 return output.str();
409}
410
411string operator +(const scifir::zid& y, const string& x)
412{
413 ostringstream output;
414 output << y;
415 output << x;
416 return output.str();
417}
418
419ostream& operator <<(ostream& os, const scifir::zid& x)
420{
421 return os << to_string(x);
422}
423
424istream& operator >>(istream& is, scifir::zid& x)
425{
426 char a[256];
427 is.getline(a, 256);
428 string b(a);
429 boost::trim(b);
430 x = scifir::zid(b);
431 return is;
432}
Class that stores astronomical ids, not as an integer but with an string as identifier....
Definition aid.hpp:14
Class that allows to store information about a zone, including the astronomical object inside which t...
Definition zid.hpp:14
zid::type zone_type
The type of zone. It can be any value of the enum zid::type. The possible values are COUNTRY,...
Definition zid.hpp:41
bool has_unknown_country() const
Returns true if the value of country is the predefined value 'unknown-country'.
Definition zid.cpp:134
void initialize_from_string(const string &init_zid_full)
Internal function. It's used by the string constructor and the string assignment of zid to initialize...
Definition zid.cpp:181
string display() const
Returns a string representation of zid, including the string representation of his aid.
Definition zid.cpp:139
bool has_no_country() const
Returns true if the value of country is the predefined value 'no-country'.
Definition zid.cpp:129
string partial_display() const
Returns a string representation of zid, without the string representation of his aid.
Definition zid.cpp:162
zid & operator=(const zid &x)
Copy assigment. The member-variables are copied from the zid x.
Definition zid.cpp:103
scifir::aid aid
The aid of the astronomical object where the zone is.
Definition zid.hpp:37
zid()
Default constructor. There's no data about any zone, the aid member-variable is also empty.
Definition zid.cpp:12
vector< string > regions
The different administrative regions where the zone is. There can be any number of administrative reg...
Definition zid.hpp:38
string zone
The name of the zone. Personal zones with a personal name are allowed, it's not necessary that the zo...
Definition zid.hpp:40
type
Type of zone.
Definition zid.hpp:16
@ REGION
A region.
Definition zid.hpp:16
@ ZONE
A zone. A zone is any surface inside an astronomical object, of any shape. It's not necessary to be a...
Definition zid.hpp:16
@ COUNTRY
A country.
Definition zid.hpp:16
@ SETTLEMENT
A settlement.
Definition zid.hpp:16
@ NONE
No zone.
Definition zid.hpp:16
string country
The country where the zone is. In extraterrestrial planets, it's considered a country any division of...
Definition zid.hpp:39
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
@ NONE
No predefined astronomical body selected.
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
zid::type create_zone_type(const string &zone_type_abbreviation)
Creates an instance of a zid::type with the given string, which is the reverse as the to_string() fun...
Definition zid.cpp:311
bool operator==(const scifir::zid &x, const scifir::zid &y)
Returns true if the type and all data are the same between x and y.
Definition zid.cpp:336
istream & operator>>(istream &is, scifir::zid &x)
Allows that an istream initializes by string a zid x.
Definition zid.cpp:424
ostream & operator<<(ostream &os, const scifir::zid &x)
Adds the string representation of the zid x to an output stream os.
Definition zid.cpp:419
void operator+=(string &x, const scifir::zid &y)
Concatenates the string representation of zid y to the string x.
Definition zid.cpp:396
string operator+(const string &x, const scifir::zid &y)
Concatenates x with the string representation of zid y.
Definition zid.cpp:403
bool operator!=(const scifir::zid &x, const scifir::zid &y)
Returns true if the type or any data is different between x and y.
Definition zid.cpp:369