1 | // [Licence]
|
---|
2 | // The Ariba-Underlay Copyright
|
---|
3 | //
|
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
5 | //
|
---|
6 | // Institute of Telematics
|
---|
7 | // UniversitÀt Karlsruhe (TH)
|
---|
8 | // Zirkel 2, 76128 Karlsruhe
|
---|
9 | // Germany
|
---|
10 | //
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are
|
---|
13 | // met:
|
---|
14 | //
|
---|
15 | // 1. Redistributions of source code must retain the above copyright
|
---|
16 | // notice, this list of conditions and the following disclaimer.
|
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | // notice, this list of conditions and the following disclaimer in the
|
---|
19 | // documentation and/or other materials provided with the distribution.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | //
|
---|
33 | // The views and conclusions contained in the software and documentation
|
---|
34 | // are those of the authors and should not be interpreted as representing
|
---|
35 | // official policies, either expressed or implied, of the Institute of
|
---|
36 | // Telematics.
|
---|
37 | // [Licence]
|
---|
38 |
|
---|
39 | #include "Helper.h"
|
---|
40 |
|
---|
41 | namespace ariba {
|
---|
42 | namespace utility {
|
---|
43 |
|
---|
44 | string Helper::ultos(unsigned long val)
|
---|
45 | {
|
---|
46 | char buf[16];
|
---|
47 |
|
---|
48 | #ifdef WIN32
|
---|
49 | _ultoa_s (val, buf, 16, 10);
|
---|
50 | #else
|
---|
51 | sprintf (buf, "%u", val);
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | return buf;
|
---|
55 | }
|
---|
56 |
|
---|
57 | string Helper::ltos(long val)
|
---|
58 | {
|
---|
59 | char buf[16];
|
---|
60 |
|
---|
61 | #ifdef WIN32
|
---|
62 | _ltoa_s (val, buf, 16, 10);
|
---|
63 | #else
|
---|
64 | sprintf (buf, "%i", val);
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | return buf;
|
---|
68 | }
|
---|
69 |
|
---|
70 | string Helper::ultohexs (unsigned long val, bool hexdelimiter)
|
---|
71 | {
|
---|
72 | char buf[16];
|
---|
73 |
|
---|
74 | #ifdef WIN32
|
---|
75 | _ultoa_s (val, buf, 16, 16);
|
---|
76 | #else
|
---|
77 | sprintf (buf, "%x", val);
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | string ret = buf;
|
---|
81 |
|
---|
82 | if (hexdelimiter)
|
---|
83 | ret.insert (0, "0x");
|
---|
84 |
|
---|
85 | return ret;
|
---|
86 | }
|
---|
87 |
|
---|
88 | string Helper::ltohexs (long val, bool hexdelimiter)
|
---|
89 | {
|
---|
90 | char buf[16];
|
---|
91 |
|
---|
92 | #ifdef WIN32
|
---|
93 | _ltoa_s (val, buf, 16, 16);
|
---|
94 | #else
|
---|
95 | sprintf (buf, "%x", val);
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | string ret = buf;
|
---|
99 |
|
---|
100 | if (hexdelimiter)
|
---|
101 | ret.insert (0, "0x");
|
---|
102 |
|
---|
103 | return ret;
|
---|
104 | }
|
---|
105 |
|
---|
106 | string Helper::trim (string str)
|
---|
107 | {
|
---|
108 | string ret = str;
|
---|
109 |
|
---|
110 | ret.erase (0, ret.find_first_not_of( " \n\r\t" ));
|
---|
111 | ret.erase (ret.find_last_not_of( " \n\r\t") + 1 );
|
---|
112 |
|
---|
113 | return ret;
|
---|
114 | }
|
---|
115 |
|
---|
116 | long Helper::stol (string str)
|
---|
117 | {
|
---|
118 | return strtol (str.c_str (), NULL, 10);
|
---|
119 | }
|
---|
120 |
|
---|
121 | int Helper::stoi (string str)
|
---|
122 | {
|
---|
123 | return (int) stol (str);
|
---|
124 | }
|
---|
125 |
|
---|
126 | unsigned int Helper::hstoui (string str)
|
---|
127 | {
|
---|
128 | return (unsigned int) strtol (str.c_str (), NULL, 16);
|
---|
129 | }
|
---|
130 |
|
---|
131 | Helper::STRING_LIST Helper::split (string str, string delimiter)
|
---|
132 | {
|
---|
133 | STRING_LIST ret;
|
---|
134 | string::size_type offset = 0;
|
---|
135 | string::size_type delimIndex = 0;
|
---|
136 | string helpstring = "";
|
---|
137 |
|
---|
138 | if (str.length () <= 0) return ret;
|
---|
139 |
|
---|
140 | while ((delimIndex = str.find (delimiter, offset)) != string::npos) {
|
---|
141 |
|
---|
142 | helpstring = trim (str.substr (offset, delimIndex - offset));
|
---|
143 | if (helpstring.length () > 0) ret.push_back (helpstring);
|
---|
144 |
|
---|
145 | offset = delimIndex + delimiter.length();
|
---|
146 | delimIndex = str.find (delimiter, offset);
|
---|
147 |
|
---|
148 | } // while ((delimIndex = str.find (delimiter, offset)) != string::npos)
|
---|
149 |
|
---|
150 | if (offset < str.length ()) {
|
---|
151 | helpstring = trim (str.substr (offset));
|
---|
152 | if (helpstring.length () > 0) ret.push_back (helpstring);
|
---|
153 | }
|
---|
154 |
|
---|
155 | return ret;
|
---|
156 | }
|
---|
157 |
|
---|
158 | string Helper::replace (string str, string find, string repl)
|
---|
159 | {
|
---|
160 | string ret = str;
|
---|
161 | boost::algorithm::replace_all (ret, find, repl);
|
---|
162 | return ret;
|
---|
163 | }
|
---|
164 |
|
---|
165 | string Helper::getTime (time_t timeval)
|
---|
166 | {
|
---|
167 | time_t rawtime;
|
---|
168 | struct tm* timeinfo;
|
---|
169 |
|
---|
170 | if (timeval == 0)
|
---|
171 | time (&rawtime);
|
---|
172 | else
|
---|
173 | rawtime = timeval;
|
---|
174 |
|
---|
175 | #ifdef WIN32
|
---|
176 | timeinfo = new struct tm ();
|
---|
177 | localtime_s (timeinfo, &rawtime);
|
---|
178 | #else
|
---|
179 | timeinfo = localtime (&rawtime);
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | ostringstream m_formatter;
|
---|
183 |
|
---|
184 | m_formatter.str ("");
|
---|
185 | m_formatter << setw(2) << setfill('0') << timeinfo->tm_hour << ":"
|
---|
186 | << setw(2) << setfill('0') << timeinfo->tm_min << ":"
|
---|
187 | << setw(2) << setfill('0') << timeinfo->tm_sec ;
|
---|
188 | assert (m_formatter.good ());
|
---|
189 |
|
---|
190 | #ifdef WIN32
|
---|
191 | delete timeinfo;
|
---|
192 | #endif
|
---|
193 | return m_formatter.str ();
|
---|
194 | }
|
---|
195 |
|
---|
196 | string Helper::getDate (time_t timeval)
|
---|
197 | {
|
---|
198 | time_t rawtime;
|
---|
199 | struct tm* timeinfo;
|
---|
200 |
|
---|
201 | if (timeval == 0)
|
---|
202 | time (&rawtime);
|
---|
203 | else
|
---|
204 | rawtime = timeval;
|
---|
205 |
|
---|
206 | #ifdef WIN32
|
---|
207 | timeinfo = new struct tm ();
|
---|
208 | localtime_s (timeinfo, &rawtime);
|
---|
209 | #else
|
---|
210 | timeinfo = localtime (&rawtime);
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | //
|
---|
214 | // cache the date because it changes with very low frequency
|
---|
215 | // and formatting using ostringstream or sprintf is more expensive
|
---|
216 | //
|
---|
217 |
|
---|
218 | static struct tm datecache = {0};
|
---|
219 | static string datecachestr = "";
|
---|
220 |
|
---|
221 | if (datecachestr.length() != 0 && ( datecache.tm_mday == timeinfo->tm_mday &&
|
---|
222 | datecache.tm_mon == timeinfo->tm_mon &&
|
---|
223 | datecache.tm_year == timeinfo->tm_year )) {
|
---|
224 | #ifdef WIN32
|
---|
225 | delete timeinfo;
|
---|
226 | #endif
|
---|
227 | return datecachestr;
|
---|
228 | }
|
---|
229 |
|
---|
230 | //
|
---|
231 | // format the date for international use
|
---|
232 | //
|
---|
233 |
|
---|
234 | char buffer [32];
|
---|
235 |
|
---|
236 | setlocale (LC_ALL, "");
|
---|
237 | strftime (buffer, sizeof (buffer), "%x", timeinfo);
|
---|
238 |
|
---|
239 | datecachestr = buffer;
|
---|
240 | datecache = *timeinfo;
|
---|
241 |
|
---|
242 | #ifdef WIN32
|
---|
243 | delete timeinfo;
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | return datecachestr;
|
---|
247 | }
|
---|
248 |
|
---|
249 | unsigned long Helper::getElapsedMillis ()
|
---|
250 | {
|
---|
251 | return static_cast<unsigned long>(((double)clock() / (double)(CLOCKS_PER_SEC)) * 1000.0);
|
---|
252 | }
|
---|
253 |
|
---|
254 | string Helper::dtos (double val)
|
---|
255 | {
|
---|
256 | string ret = boost::lexical_cast<string> (val);
|
---|
257 | return ret;
|
---|
258 | }
|
---|
259 |
|
---|
260 | double Helper::stod (string str)
|
---|
261 | {
|
---|
262 | if (str.length() <= 0) return 0.0;
|
---|
263 | else return boost::lexical_cast<double> (str);
|
---|
264 | }
|
---|
265 |
|
---|
266 | }} // namespace ariba, common
|
---|