ATLAS Offline Software
Loading...
Searching...
No Matches
dbline.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <type_traits>
8
10#include "GaudiKernel/MsgStream.h"
11
12#include <charconv>
13#include <iostream>
14#include <string_view>
15
19
21 m_data.clear();
22 m_pos = 0;
23 m_backup.clear();
24}
27 m_empty = false;
28}
29void DBline::reset(void) {
30 reset_data();
32}
33
37
41 if (!m_backup.empty()) {
42 m_data = std::move(m_backup);
43 m_backup.clear();
44 }
45}
46
50
52 const size_t pos = m_data.find('#');
53 if (pos != std::string::npos) m_data.erase(pos);
54}
55
56void DBline::GetToken(size_t pos, std::string_view token) {
58 m_data.erase(pos, token.length());
59 m_pos = pos;
60}
61
62void DBline::GetLine(std::istream& input) {
63 if (!check_data()) {
64 MsgStream log(Athena::getMessageSvc(true), "DBline");
65 log << MSG::WARNING << "line " << std::setw(4) << m_line << " -|" << m_data.c_str() << " .. not understood!" << endmsg;
66 }
67 reset();
68 if (input.eof()) {
69 m_fail = true;
70 return;
71 }
72 m_line++;
73 std::getline(input, m_data);
75 check_data();
76 if (input.eof()) m_fail = true;
77}
78
82
83template <class type> void DBline::GetValue(type& value) {
84 if (!m_extraction || m_fail) return;
85 const size_t start = m_data.find_first_not_of(' ', m_pos);
86 if (start == std::string::npos) {
88 return;
89 }
90 size_t stop = m_data.find_first_of(' ', start + 1);
91 if (stop == std::string::npos) stop = m_data.size();
92
93 if constexpr(std::is_unsigned_v<type>) {
94 unsigned long temp = std::stoul(m_data.substr(start, stop - start), nullptr, m_base);
95 value = temp;
96 } else {
97 int temp = std::stoi(m_data.substr(start, stop - start), nullptr, m_base);
98 value = temp;
99 }
100 m_data.erase(m_pos, stop - m_pos);
101 check_data();
102}
103
104void DBline::GetValue(std::string& value) {
105 if (!m_extraction || m_fail) return;
106 const size_t start = m_data.find_first_not_of(' ', m_pos);
107 if (start == std::string::npos) {
109 return;
110 }
111 size_t stop = m_data.find_first_of(' ', start + 1);
112 if (stop == std::string::npos) stop = m_data.size();
113
114 value = m_data.substr(start, stop - start);
115
116 m_data.erase(m_pos, stop - m_pos);
117 check_data();
118}
119
120void DBline::GetStr(std::string& str) {
121 GetValue(str);
122 quote pos = check_quote(str);
123 if (pos == no_quote) { return; }
124 if (pos == begin_quote) {
125 const size_t pos = m_data.find('"', m_pos);
126 if (pos == std::string::npos) {
127 str.clear();
129 return;
130 }
131 str.append(m_data, m_pos, pos);
132 m_data.erase(m_pos, (pos - m_pos) + 1);
133 } else {
134 str.clear();
136 }
137}
138
140 const size_t pos = str.find('"');
141 if (pos == std::string::npos)
142 return no_quote;
143 else if (pos == 0) {
144 str.erase(0, 1);
145 return begin_quote;
146 } else if (pos == str.length() - 1) {
147 str.erase(pos, 1);
148 return end_quote;
149 }
150 return error;
151}
152
156
157void DBline::connect(std::ifstream& file) {
158 if ((&file) != m_file) {
159 m_line = 0;
160 m_fail = 0;
161 m_stream = nullptr;
162 m_file = &file;
163 }
164}
165
166void DBline::connect(std::istream& stream) {
167 if ((&stream) != m_stream) {
168 m_line = 0;
169 m_fail = 0;
170 m_file = nullptr;
171 m_stream = &stream;
172 }
173}
174
178
179DBline& DBline::token(std::string_view token) {
181 if (check_data()) {
183 return *this;
184 }
185 const size_t pos = m_data.find(token);
186
187 if (pos != std::string::npos) {
188 const size_t finalpos = pos + token.length();
189 char prev = (pos) ? m_data[pos - 1] : ' ';
190 char foll = (finalpos < m_data.length()) ? m_data[finalpos] : ' ';
191 if (prev == ' ' && foll == ' ')
192 GetToken(pos, token);
193 else
195 } else
197 return *this;
198}
199
200template <class type> DBline& DBline::token(std::string_view str, type t) {
201 const size_t pos = str.find('#');
202 if (pos != std::string::npos) {
203 const std::string rep = std::to_string(t);
204
205 std::string new_token{str};
206 new_token.replace(pos, rep.length(), rep);
207 token(new_token);
208 } else
209 token(str);
210
211 return *this;
212}
213
214template <class type> DBline& DBline::token(std::string_view str, type t, int /*size*/) {
215 const size_t pos = str.find('#');
216 if (pos != std::string::npos) {
217 std::ostringstream rep;
218 rep << std::setw(2) << std::setfill('0') << t;
219
220 std::string new_token{str};
221 new_token.replace(pos, rep.str().length(), rep.str());
222 token(new_token);
223 } else
224 token(str);
225
226 return *this;
227}
228
229void DBline::go_until(std::string_view token) {
230 if (m_file) do
231 GetLine(*m_file);
232 while (m_data.find(token) == std::string::npos);
233 if (m_stream) do
235 while (m_data.find(token) == std::string::npos);
236}
237
242 m_base = 10;
243 flags(m_default);
244 return *this;
245}
246
250
252 GetStr(str);
253 return *this;
254}
256 GetValue(i);
257 return *this;
258}
260 GetValue(i8);
261 return *this;
262}
263DBline& DBline::operator>>(uint16_t& i16) {
264 GetValue(i16);
265 return *this;
266}
267DBline& DBline::operator>>(uint32_t& i32) {
268 GetValue(i32);
269 return *this;
270}
271DBline& DBline::operator>>(uint64_t& i64) {
272 GetValue(i64);
273 return *this;
274}
275
279
281 flags(f.flags());
282 const std::ios_base::fmtflags fmt = f.flags() & std::ios_base::basefield;
283 switch (fmt) {
284 case std::ios::hex: m_base = 16; break;
285 case std::ios::oct: m_base = 8; break;
286 default: m_base = 10;
287 }
288 return *this;
289}
290
294
295DBline& DBline::operator>>(std::string_view token) {
296 if (!m_extraction || check_data() || m_fail) return *this;
297 const size_t pos = m_data.find(token, m_pos);
298 if (pos != std::string::npos)
299 GetToken(pos, token);
300 else
302 return *this;
303}
304
308
310 if (m_file) GetLine(*m_file);
312 return *this;
313}
314
316 for (int j = -1; j < i; j++) {
317 if (m_file) GetLine(*m_file);
319 }
320 return *this;
321}
322
324 for (int j = 0; j < i; j++) {
325 if (m_file) GetLine(*m_file);
327 }
328 return *this;
329}
330
334
335DBline::operator bool() { return !(m_fail | !static_cast<bool>(m_extraction)); }
336bool DBline::operator!() { return m_fail | !static_cast<bool>(m_extraction); }
337DBline::operator DBstatus() { return m_extraction; }
338
342
343DBline& DBline::operator()(std::string_view str) {
344 token(str);
345 return *this;
346}
347
348DBline& DBline::operator()(std::string_view str, int n) {
349 token(str, n);
350 return *this;
351}
352
353DBline& DBline::operator()(std::string_view str, int n, int s) {
354 token(str, n, s);
355 return *this;
356}
357
359
361 this->unsetf(std::ios::skipws | std::ios::left | std::ios::right | std::ios::internal | std::ios::oct | std::ios::hex |
362 std::ios::showbase | std::ios::showpoint | std::ios::uppercase | std::ios::scientific | std::ios::fixed |
363 std::ios::showpos | std::ios::boolalpha);
364 m_default = flags();
365 m_dbfmt_hex.setf(std::ios::hex, std::ios::basefield);
366 m_dbfmt_oct.setf(std::ios::oct, std::ios::basefield);
367 m_dbfmt_dec.setf(std::ios::dec, std::ios::basefield);
368}
369
370DBline::DBline(std::ifstream& file) :
371 DBline() {
372 m_file = &file;
373}
374
375DBline::DBline(std::istream& stream) :
376 DBline() {
377 m_stream = &stream;
378}
379
381 connect(file);
382 GetLine(file);
383 return *this;
384}
385
386DBline& DBline::operator<<(std::istream& input) {
387 connect(input);
388 GetLine(input);
389 return *this;
390}
391
395
396std::ifstream& operator>>(std::ifstream& file, DBline& db) {
397 db.connect(file);
398 db.GetLine(file);
399 return file;
400}
401
402std::istream& operator>>(std::istream& stream, DBline& db) {
403 db.connect(stream);
404 db.GetLine(stream);
405 return stream;
406}
407
408std::ostream& operator<<(std::ostream& stream, DBline& db) {
409 stream << db.m_data;
410 return stream;
411}
412
414 if (s1 || s2) return extracted;
415 return not_extracted;
416}
417
419 if (s1 && s2) return extracted;
420 return not_extracted;
421}
422
424 this->setf(std::ios::unitbuf | std::ios::dec);
425 this->unsetf(std::ios::skipws | std::ios::left | std::ios::right | std::ios::internal | std::ios::oct | std::ios::hex |
426 std::ios::showbase | std::ios::showpoint | std::ios::uppercase | std::ios::scientific | std::ios::fixed |
427 std::ios::showpos | std::ios::boolalpha);
428}
#define endmsg
const char *const fmt
DBfmt()
Definition dbline.cxx:423
DBline & operator>>(std::string &str)
Definition dbline.cxx:251
bool m_fail
Definition dbline.h:265
enum DBline::exist_quote quote
DBline & operator++()
Definition dbline.cxx:309
DBline & operator+(int i)
Definition dbline.cxx:323
void connect(std::ifstream &file)
Definition dbline.cxx:157
void BadExtraction(void)
Definition dbline.cxx:39
int m_base
Definition dbline.h:268
bool check_data(void)
Definition dbline.h:364
void reset_status(void)
Definition dbline.cxx:25
quote check_quote(std::string &) const
Definition dbline.cxx:139
void GoodExtraction(void)
Definition dbline.cxx:38
@ end_quote
Definition dbline.h:257
@ no_quote
Definition dbline.h:257
@ begin_quote
Definition dbline.h:257
int m_line
Definition dbline.h:264
DBline & reset_fmt(void)
Definition dbline.cxx:241
std::string m_data
Definition dbline.h:262
unsigned long int m_pos
Definition dbline.h:263
DBfmt m_dbfmt_oct
Definition dbline.h:295
DBfmt m_dbfmt_hex
Definition dbline.h:294
DBstatus m_extraction
Definition dbline.h:267
DBfmt m_dbfmt_dec
Definition dbline.h:296
DBline & operator<<(std::istream &input)
Definition dbline.cxx:386
void GetToken(size_t pos, std::string_view token)
Definition dbline.cxx:56
std::istream * m_stream
Definition dbline.h:261
std::ios::fmtflags m_default
Definition dbline.h:259
void GetStr(std::string &)
Definition dbline.cxx:120
bool m_empty
Definition dbline.h:266
void erase_comment(void)
Definition dbline.cxx:51
void GetValue(type &value)
Definition dbline.cxx:83
std::string m_backup
Definition dbline.h:262
void go_until(std::string_view token)
Definition dbline.cxx:229
DBline & operator()(std::string_view)
Definition dbline.cxx:343
std::ifstream * m_file
Definition dbline.h:260
DBline()
Definition dbline.cxx:360
void reset(void)
Definition dbline.cxx:29
bool operator!()
Definition dbline.cxx:336
void reset_data(void)
Definition dbline.cxx:20
DBline & token(std::string_view)
Definition dbline.cxx:179
void GetLine(std::istream &input)
Definition dbline.cxx:62
DBstatus operator&(DBstatus s1, DBstatus s2)
Definition dbline.cxx:418
DBstatus operator|(DBstatus s1, DBstatus s2)
Definition dbline.cxx:413
std::ifstream & operator>>(std::ifstream &file, DBline &db)
Definition dbline.cxx:396
std::ostream & operator<<(std::ostream &stream, DBline &db)
Definition dbline.cxx:408
@ not_extracted
Definition dbline.h:248
@ extracted
Definition dbline.h:248
enum result_extraction DBstatus
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
TFile * file