Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS oks source for the DUNE DAQ project.
3 : // Fork baseline commit: oks-08-03-04 (2022-04-14).
4 : // Renamed since fork: no.
5 :
6 : #define _OksBuildDll_
7 :
8 : #include "oks/defs.hpp"
9 : #include "oks_utils.hpp"
10 :
11 : #include <stdlib.h>
12 : #include <time.h>
13 :
14 : #include <iomanip>
15 : #include <sstream>
16 :
17 : namespace dunedaq {
18 : namespace oks {
19 :
20 : //
21 : // The method converts string 's' to time
22 : // It is expected that the 's' is in the
23 : // following format: dd/mm/[yy]yy hh:mm[:ss]
24 : //
25 :
26 : void
27 0 : Date::set(const char* s)
28 : {
29 0 : p_tm.tm_sec = p_tm.tm_min = p_tm.tm_hour = p_tm.tm_mday = p_tm.tm_mon = p_tm.tm_year = 0;
30 :
31 0 : if (s == 0)
32 0 : return;
33 :
34 : // skip leading spaces
35 :
36 0 : while (*s == ' ')
37 0 : ++s;
38 :
39 0 : const char* s2 = s; // original time used in error reports
40 0 : char* nds = 0; // non-digit symbol returned by strtol
41 :
42 : // find the day
43 :
44 0 : p_tm.tm_mday = strtol(s, &nds, 10);
45 :
46 0 : if (*nds != '/') {
47 0 : Oks::error_msg("oks::Time::Time()") << "failed to find the day in \'" << s2 << "\'\n";
48 0 : return;
49 : } else
50 0 : s = nds + 1;
51 :
52 0 : if (p_tm.tm_mday > 31 || p_tm.tm_mday == 0) {
53 0 : Oks::error_msg("oks::Time::Time()") << "bad day " << p_tm.tm_mday << " in \'" << s2 << "\'\n";
54 0 : return;
55 : }
56 :
57 : // find the month
58 :
59 0 : p_tm.tm_mon = strtol(s, &nds, 10);
60 :
61 0 : if (*nds != '/') {
62 0 : Oks::error_msg("oks::Time::Time()") << "failed to find the month in \'" << s2 << "\'\n";
63 0 : return;
64 : } else
65 0 : s = nds + 1;
66 :
67 0 : if (p_tm.tm_mon > 12 || p_tm.tm_mon == 0) {
68 0 : Oks::error_msg("oks::Time::Time()") << "bad month " << p_tm.tm_mon << " in \'" << s2 << "\'\n";
69 0 : return;
70 : }
71 :
72 0 : p_tm.tm_mon--;
73 :
74 : // find the year
75 :
76 0 : p_tm.tm_year = strtol(s, &nds, 10);
77 :
78 0 : if (p_tm.tm_year >= 0) {
79 0 : if (p_tm.tm_year < 70)
80 0 : p_tm.tm_year += 2000;
81 0 : else if (p_tm.tm_year < 100)
82 0 : p_tm.tm_year += 1900;
83 : }
84 :
85 : // check if there is no data anymore
86 :
87 0 : if (*nds == 0)
88 : return;
89 :
90 : s = nds;
91 :
92 : // skip spaces
93 :
94 0 : while (*s == ' ')
95 0 : ++s;
96 0 : if (*s == 0)
97 : return;
98 :
99 : // find the hour
100 :
101 0 : p_tm.tm_hour = strtol(s, &nds, 10);
102 :
103 0 : if (*nds != ':') {
104 0 : Oks::error_msg("oks::Time::Time()") << "failed to find the hour in \'" << s2 << "\'\n";
105 0 : return;
106 : } else
107 0 : s = nds + 1;
108 :
109 0 : if (p_tm.tm_hour > 23) {
110 0 : Oks::error_msg("oks::Time::Time()") << "bad hour " << p_tm.tm_hour << " in \'" << s2 << "\'\n";
111 0 : return;
112 : }
113 :
114 : // find the minute
115 :
116 0 : p_tm.tm_min = strtol(s, &nds, 10);
117 :
118 0 : if (p_tm.tm_min > 59) {
119 0 : Oks::error_msg("oks::Time::Time()") << "bad minute " << p_tm.tm_min << " in \'" << s2 << "\'\n";
120 0 : return;
121 : }
122 :
123 0 : if (*nds != ':')
124 : return;
125 :
126 : // find the second
127 :
128 0 : p_tm.tm_sec = strtol(nds + 1, &nds, 10);
129 :
130 0 : if (p_tm.tm_sec > 59) {
131 0 : Oks::error_msg("oks::Time::Time()") << "bad second " << p_tm.tm_sec << " in \'" << s2 << "\'\n";
132 0 : return;
133 : }
134 : }
135 :
136 : std::string
137 0 : Date::str() const
138 : {
139 0 : std::ostringstream s;
140 :
141 0 : s << p_tm.tm_mday << '/' << (p_tm.tm_mon + 1) << '/' << std::setfill('0') << std::setw(2)
142 0 : << ((p_tm.tm_year < 1970 || p_tm.tm_year >= 2070)
143 : ? p_tm.tm_year
144 0 : : (p_tm.tm_year < 2000 ? (p_tm.tm_year - 1900) : (p_tm.tm_year - 2000)));
145 :
146 0 : return s.str();
147 0 : }
148 :
149 : std::string
150 0 : Time::str() const
151 : {
152 0 : std::string ds = Date::str();
153 :
154 0 : std::ostringstream s;
155 :
156 0 : s << ds << std::setfill('0') << ' ' << std::setw(2) << p_tm.tm_hour << ':' << std::setw(2) << p_tm.tm_min << ':'
157 0 : << std::setw(2) << p_tm.tm_sec;
158 :
159 0 : return s.str();
160 0 : }
161 :
162 : std::ostream&
163 0 : operator<<(std::ostream& s, const Date& d)
164 : {
165 0 : std::string str = d.str();
166 0 : s << str;
167 0 : return s;
168 0 : }
169 :
170 : std::ostream&
171 0 : operator<<(std::ostream& s, const Time& t)
172 : {
173 0 : std::string str = t.str();
174 0 : s << str;
175 0 : return s;
176 0 : }
177 :
178 : ////////////////////////////////////////////////////////////////////////////////
179 :
180 : std::string Oks::Tokenizer::s_empty;
181 :
182 2240 : Oks::Tokenizer::Tokenizer(const std::string& s, const char* d)
183 2240 : : p_string(s)
184 2240 : , p_delimeters(d)
185 2240 : , p_idx(p_string.find_first_not_of(p_delimeters))
186 : {
187 2240 : }
188 :
189 : const std::string
190 360 : Oks::Tokenizer::next()
191 : {
192 360 : if (p_idx == std::string::npos)
193 180 : return s_empty;
194 180 : std::string::size_type end_idx = p_string.find_first_of(p_delimeters, p_idx);
195 180 : if (end_idx == std::string::npos)
196 180 : end_idx = p_string.length();
197 180 : std::string token = p_string.substr(p_idx, end_idx - p_idx);
198 180 : p_idx = p_string.find_first_not_of(p_delimeters, end_idx);
199 180 : return token;
200 180 : }
201 :
202 : bool
203 15531 : Oks::Tokenizer::next(std::string& token)
204 : {
205 15531 : if (p_idx == std::string::npos) {
206 2060 : token.clear();
207 2060 : return false;
208 : }
209 :
210 13471 : std::string::size_type end_idx = p_string.find_first_of(p_delimeters, p_idx);
211 13471 : if (end_idx == std::string::npos)
212 2060 : end_idx = p_string.length();
213 13471 : token.assign(p_string, p_idx, end_idx - p_idx);
214 13471 : p_idx = p_string.find_first_not_of(p_delimeters, end_idx);
215 13471 : return true;
216 : }
217 :
218 : } // namespace oks
219 : } // namespace dunedaq
|