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