close Warning: Can't use blame annotator:
No changeset 2259 in the repository

source: source/ariba/utility/misc/Helper.cpp@ 6927

Last change on this file since 6927 was 4063, checked in by Christoph Mayer, 15 years ago
File size: 6.2 KB
RevLine 
1// [License]
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// [License]
38
39#include "Helper.h"
40
41#include <boost/lexical_cast.hpp>
42#include <boost/algorithm/string/replace.hpp>
43
44namespace ariba {
45namespace utility {
46
47string Helper::ultos(unsigned long val)
48{
49 char buf[16];
50
51#ifdef WIN32
52 _ultoa_s (val, buf, 16, 10);
53#else
54 sprintf (buf, "%lu", val);
55#endif
56
57 return buf;
58}
59
60string Helper::ltos(long val)
61{
62 char buf[16];
63
64#ifdef WIN32
65 _ltoa_s (val, buf, 16, 10);
66#else
67 sprintf (buf, "%li", val);
68#endif
69
70 return buf;
71}
72
73string Helper::ultohexs (unsigned long val, bool hexdelimiter)
74{
75 char buf[16];
76
77#ifdef WIN32
78 _ultoa_s (val, buf, 16, 16);
79#else
80 sprintf (buf, "%lx", val);
81#endif
82
83 string ret = buf;
84
85 if (hexdelimiter)
86 ret.insert (0, "0x");
87
88 return ret;
89}
90
91string Helper::ltohexs (long val, bool hexdelimiter)
92{
93 char buf[16];
94
95#ifdef WIN32
96 _ltoa_s (val, buf, 16, 16);
97#else
98 sprintf (buf, "%lx", val);
99#endif
100
101 string ret = buf;
102
103 if (hexdelimiter)
104 ret.insert (0, "0x");
105
106 return ret;
107}
108
109string Helper::trim (string str)
110{
111 string ret = str;
112
113 ret.erase (0, ret.find_first_not_of( " \n\r\t" ));
114 ret.erase (ret.find_last_not_of( " \n\r\t") + 1 );
115
116 return ret;
117}
118
119long Helper::stol (string str)
120{
121 return strtol (str.c_str (), NULL, 10);
122}
123
124int Helper::stoi (string str)
125{
126 return (int) stol (str);
127}
128
129unsigned int Helper::hstoui (string str)
130{
131 return (unsigned int) strtol (str.c_str (), NULL, 16);
132}
133
134Helper::STRING_LIST Helper::split (string str, string delimiter)
135{
136 STRING_LIST ret;
137 string::size_type offset = 0;
138 string::size_type delimIndex = 0;
139 string helpstring = "";
140
141 if (str.length () <= 0) return ret;
142
143 while ((delimIndex = str.find (delimiter, offset)) != string::npos) {
144
145 helpstring = trim (str.substr (offset, delimIndex - offset));
146 if (helpstring.length () > 0) ret.push_back (helpstring);
147
148 offset = delimIndex + delimiter.length();
149 delimIndex = str.find (delimiter, offset);
150
151 } // while ((delimIndex = str.find (delimiter, offset)) != string::npos)
152
153 if (offset < str.length ()) {
154 helpstring = trim (str.substr (offset));
155 if (helpstring.length () > 0) ret.push_back (helpstring);
156 }
157
158 return ret;
159}
160
161string Helper::replace (string str, string find, string repl)
162{
163 string ret = str;
164 boost::algorithm::replace_all (ret, find, repl);
165 return ret;
166}
167
168string Helper::getTime (time_t timeval)
169{
170 time_t rawtime;
171 struct tm* timeinfo;
172
173 if (timeval == 0)
174 time (&rawtime);
175 else
176 rawtime = timeval;
177
178#ifdef WIN32
179 timeinfo = new struct tm ();
180 localtime_s (timeinfo, &rawtime);
181#else
182 timeinfo = localtime (&rawtime);
183#endif
184
185 ostringstream m_formatter;
186
187 m_formatter.str ("");
188 m_formatter << setw(2) << setfill('0') << timeinfo->tm_hour << ":"
189 << setw(2) << setfill('0') << timeinfo->tm_min << ":"
190 << setw(2) << setfill('0') << timeinfo->tm_sec ;
191 assert (m_formatter.good ());
192
193#ifdef WIN32
194 delete timeinfo;
195#endif
196 return m_formatter.str ();
197}
198
199string Helper::getDate (time_t timeval)
200{
201 time_t rawtime;
202 struct tm* timeinfo;
203
204 if (timeval == 0)
205 time (&rawtime);
206 else
207 rawtime = timeval;
208
209#ifdef WIN32
210 timeinfo = new struct tm ();
211 localtime_s (timeinfo, &rawtime);
212#else
213 timeinfo = localtime (&rawtime);
214#endif
215
216 //
217 // cache the date because it changes with very low frequency
218 // and formatting using ostringstream or sprintf is more expensive
219 //
220
221 static struct tm datecache = {0};
222 static string datecachestr = "";
223
224 if (datecachestr.length() != 0 && ( datecache.tm_mday == timeinfo->tm_mday &&
225 datecache.tm_mon == timeinfo->tm_mon &&
226 datecache.tm_year == timeinfo->tm_year )) {
227 #ifdef WIN32
228 delete timeinfo;
229 #endif
230 return datecachestr;
231 }
232
233 //
234 // format the date for international use
235 //
236
237 char buffer [32];
238
239 setlocale (LC_ALL, "");
240 strftime (buffer, sizeof (buffer), "%x", timeinfo);
241
242 datecachestr = buffer;
243 datecache = *timeinfo;
244
245#ifdef WIN32
246 delete timeinfo;
247#endif
248
249 return datecachestr;
250}
251
252unsigned long Helper::getElapsedMillis ()
253{
254 static unsigned long zero = 0;
255
256 struct timeb tp;
257 ftime( &tp );
258
259 unsigned long val = tp.time*1000 + tp.millitm;
260 if( zero == 0 ) zero = val;
261
262 return val-zero;
263}
264
265string Helper::dtos (double val)
266{
267 string ret = boost::lexical_cast<string> (val);
268 return ret;
269}
270
271double Helper::stod (string str)
272{
273 if (str.length() <= 0) return 0.0;
274 else return boost::lexical_cast<double> (str);
275}
276
277}} // namespace ariba, common
Note: See TracBrowser for help on using the repository browser.