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) return;
32 :
33 : // skip leading spaces
34 :
35 0 : while(*s == ' ') ++s;
36 :
37 :
38 0 : const char * s2 = s; // original time used in error reports
39 0 : char * nds = 0; // non-digit symbol returned by strtol
40 :
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 : }
50 : else
51 0 : s = nds + 1;
52 :
53 0 : if(p_tm.tm_mday > 31 || p_tm.tm_mday == 0) {
54 0 : Oks::error_msg("oks::Time::Time()") << "bad day " << p_tm.tm_mday << " in \'" << s2 << "\'\n";
55 0 : return;
56 : }
57 :
58 :
59 : // find the month
60 :
61 0 : p_tm.tm_mon = strtol(s, &nds, 10);
62 :
63 0 : if(*nds != '/') {
64 0 : Oks::error_msg("oks::Time::Time()") << "failed to find the month in \'" << s2 << "\'\n";
65 0 : return;
66 : }
67 : else
68 0 : s = nds + 1;
69 :
70 0 : if(p_tm.tm_mon > 12 || p_tm.tm_mon == 0) {
71 0 : Oks::error_msg("oks::Time::Time()") << "bad month " << p_tm.tm_mon << " in \'" << s2 << "\'\n";
72 0 : return;
73 : }
74 :
75 0 : p_tm.tm_mon--;
76 :
77 :
78 : // find the year
79 :
80 0 : p_tm.tm_year = strtol(s, &nds, 10);
81 :
82 0 : if(p_tm.tm_year >= 0) {
83 0 : if(p_tm.tm_year < 70) p_tm.tm_year += 2000;
84 0 : else if(p_tm.tm_year < 100) p_tm.tm_year += 1900;
85 : }
86 :
87 : // check if there is no data anymore
88 :
89 0 : if(*nds == 0) return;
90 :
91 : s = nds;
92 :
93 :
94 : // skip spaces
95 :
96 0 : while(*s == ' ') ++s;
97 0 : if(*s == 0) return;
98 :
99 :
100 : // find the hour
101 :
102 0 : p_tm.tm_hour = strtol(s, &nds, 10);
103 :
104 0 : if(*nds != ':') {
105 0 : Oks::error_msg("oks::Time::Time()") << "failed to find the hour in \'" << s2 << "\'\n";
106 0 : return;
107 : }
108 : else
109 0 : s = nds + 1;
110 :
111 0 : if(p_tm.tm_hour > 23) {
112 0 : Oks::error_msg("oks::Time::Time()") << "bad hour " << p_tm.tm_hour << " in \'" << s2 << "\'\n";
113 0 : return;
114 : }
115 :
116 :
117 : // find the minute
118 :
119 0 : p_tm.tm_min = strtol(s, &nds, 10);
120 :
121 0 : if(p_tm.tm_min > 59) {
122 0 : Oks::error_msg("oks::Time::Time()") << "bad minute " << p_tm.tm_min << " in \'" << s2 << "\'\n";
123 0 : return;
124 : }
125 :
126 0 : if(*nds != ':') return;
127 :
128 :
129 : // find the second
130 :
131 0 : p_tm.tm_sec = strtol(nds + 1, &nds, 10);
132 :
133 0 : if(p_tm.tm_sec > 59) {
134 0 : Oks::error_msg("oks::Time::Time()") << "bad second " << p_tm.tm_sec << " in \'" << s2 << "\'\n";
135 0 : return;
136 : }
137 : }
138 :
139 :
140 : std::string
141 0 : Date::str() const
142 : {
143 0 : std::ostringstream s;
144 :
145 0 : s << p_tm.tm_mday << '/' << (p_tm.tm_mon + 1) << '/' << std::setfill('0') << std::setw(2)
146 0 : << ((p_tm.tm_year < 1970 || p_tm.tm_year >= 2070) ? p_tm.tm_year : (p_tm.tm_year < 2000 ? (p_tm.tm_year - 1900) : (p_tm.tm_year - 2000)));
147 :
148 0 : return s.str();
149 0 : }
150 :
151 : std::string
152 0 : Time::str() const
153 : {
154 0 : std::string ds = Date::str();
155 :
156 0 : std::ostringstream s;
157 :
158 0 : s << ds << std::setfill('0')
159 0 : << ' ' << std::setw(2) << p_tm.tm_hour
160 0 : << ':' << std::setw(2) << p_tm.tm_min
161 0 : << ':' << std::setw(2) << p_tm.tm_sec;
162 :
163 0 : return s.str();
164 0 : }
165 :
166 :
167 0 : std::ostream& operator<<(std::ostream& s, const Date& d)
168 : {
169 0 : std::string str = d.str();
170 0 : s << str;
171 0 : return s;
172 0 : }
173 :
174 :
175 0 : std::ostream& operator<<(std::ostream& s, const Time& t)
176 : {
177 0 : std::string str = t.str();
178 0 : s << str;
179 0 : return s;
180 0 : }
181 :
182 : ////////////////////////////////////////////////////////////////////////////////
183 :
184 : std::string Oks::Tokenizer::s_empty;
185 :
186 1992 : Oks::Tokenizer::Tokenizer(const std::string& s, const char * d) :
187 1992 : p_string(s),
188 1992 : p_delimeters(d),
189 1992 : p_idx(p_string.find_first_not_of(p_delimeters))
190 : {
191 1992 : }
192 :
193 : const std::string
194 328 : Oks::Tokenizer::next()
195 : {
196 328 : if(p_idx == std::string::npos) return s_empty;
197 164 : std::string::size_type end_idx = p_string.find_first_of(p_delimeters, p_idx);
198 164 : if(end_idx == std::string::npos) end_idx=p_string.length();
199 164 : std::string token = p_string.substr(p_idx, end_idx - p_idx);
200 164 : p_idx = p_string.find_first_not_of(p_delimeters, end_idx);
201 164 : return token;
202 164 : }
203 :
204 : bool
205 14019 : Oks::Tokenizer::next(std::string& token)
206 : {
207 14019 : if(p_idx == std::string::npos) {
208 1828 : token.clear();
209 1828 : return false;
210 : }
211 :
212 12191 : std::string::size_type end_idx = p_string.find_first_of(p_delimeters, p_idx);
213 12191 : if(end_idx == std::string::npos) end_idx=p_string.length();
214 12191 : token.assign(p_string, p_idx, end_idx - p_idx);
215 12191 : p_idx = p_string.find_first_not_of(p_delimeters, end_idx);
216 12191 : return true;
217 : }
218 :
219 : } // namespace oks
220 : } // namespace dunedaq
|