00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Helper.h"
00040
00041 #include <boost/lexical_cast.hpp>
00042 #include <boost/algorithm/string/replace.hpp>
00043
00044 namespace ariba {
00045 namespace utility {
00046
00047 string Helper::ultos(unsigned long val)
00048 {
00049 char buf[16];
00050
00051 #ifdef WIN32
00052 _ultoa_s (val, buf, 16, 10);
00053 #else
00054 sprintf (buf, "%lu", val);
00055 #endif
00056
00057 return buf;
00058 }
00059
00060 string Helper::ltos(long val)
00061 {
00062 char buf[16];
00063
00064 #ifdef WIN32
00065 _ltoa_s (val, buf, 16, 10);
00066 #else
00067 sprintf (buf, "%li", val);
00068 #endif
00069
00070 return buf;
00071 }
00072
00073 string Helper::ultohexs (unsigned long val, bool hexdelimiter)
00074 {
00075 char buf[16];
00076
00077 #ifdef WIN32
00078 _ultoa_s (val, buf, 16, 16);
00079 #else
00080 sprintf (buf, "%lx", val);
00081 #endif
00082
00083 string ret = buf;
00084
00085 if (hexdelimiter)
00086 ret.insert (0, "0x");
00087
00088 return ret;
00089 }
00090
00091 string Helper::ltohexs (long val, bool hexdelimiter)
00092 {
00093 char buf[16];
00094
00095 #ifdef WIN32
00096 _ltoa_s (val, buf, 16, 16);
00097 #else
00098 sprintf (buf, "%lx", val);
00099 #endif
00100
00101 string ret = buf;
00102
00103 if (hexdelimiter)
00104 ret.insert (0, "0x");
00105
00106 return ret;
00107 }
00108
00109 string Helper::trim (string str)
00110 {
00111 string ret = str;
00112
00113 ret.erase (0, ret.find_first_not_of( " \n\r\t" ));
00114 ret.erase (ret.find_last_not_of( " \n\r\t") + 1 );
00115
00116 return ret;
00117 }
00118
00119 long Helper::stol (string str)
00120 {
00121 return strtol (str.c_str (), NULL, 10);
00122 }
00123
00124 int Helper::stoi (string str)
00125 {
00126 return (int) stol (str);
00127 }
00128
00129 unsigned int Helper::hstoui (string str)
00130 {
00131 return (unsigned int) strtol (str.c_str (), NULL, 16);
00132 }
00133
00134 Helper::STRING_LIST Helper::split (string str, string delimiter)
00135 {
00136 STRING_LIST ret;
00137 string::size_type offset = 0;
00138 string::size_type delimIndex = 0;
00139 string helpstring = "";
00140
00141 if (str.length () <= 0) return ret;
00142
00143 while ((delimIndex = str.find (delimiter, offset)) != string::npos) {
00144
00145 helpstring = trim (str.substr (offset, delimIndex - offset));
00146 if (helpstring.length () > 0) ret.push_back (helpstring);
00147
00148 offset = delimIndex + delimiter.length();
00149 delimIndex = str.find (delimiter, offset);
00150
00151 }
00152
00153 if (offset < str.length ()) {
00154 helpstring = trim (str.substr (offset));
00155 if (helpstring.length () > 0) ret.push_back (helpstring);
00156 }
00157
00158 return ret;
00159 }
00160
00161 string Helper::replace (string str, string find, string repl)
00162 {
00163 string ret = str;
00164 boost::algorithm::replace_all (ret, find, repl);
00165 return ret;
00166 }
00167
00168 string Helper::getTime (time_t timeval)
00169 {
00170 time_t rawtime;
00171 struct tm* timeinfo;
00172
00173 if (timeval == 0)
00174 time (&rawtime);
00175 else
00176 rawtime = timeval;
00177
00178 #ifdef WIN32
00179 timeinfo = new struct tm ();
00180 localtime_s (timeinfo, &rawtime);
00181 #else
00182 timeinfo = localtime (&rawtime);
00183 #endif
00184
00185 ostringstream m_formatter;
00186
00187 m_formatter.str ("");
00188 m_formatter << setw(2) << setfill('0') << timeinfo->tm_hour << ":"
00189 << setw(2) << setfill('0') << timeinfo->tm_min << ":"
00190 << setw(2) << setfill('0') << timeinfo->tm_sec ;
00191 assert (m_formatter.good ());
00192
00193 #ifdef WIN32
00194 delete timeinfo;
00195 #endif
00196 return m_formatter.str ();
00197 }
00198
00199 string Helper::getDate (time_t timeval)
00200 {
00201 time_t rawtime;
00202 struct tm* timeinfo;
00203
00204 if (timeval == 0)
00205 time (&rawtime);
00206 else
00207 rawtime = timeval;
00208
00209 #ifdef WIN32
00210 timeinfo = new struct tm ();
00211 localtime_s (timeinfo, &rawtime);
00212 #else
00213 timeinfo = localtime (&rawtime);
00214 #endif
00215
00216
00217
00218
00219
00220
00221 static struct tm datecache = {0};
00222 static string datecachestr = "";
00223
00224 if (datecachestr.length() != 0 && ( datecache.tm_mday == timeinfo->tm_mday &&
00225 datecache.tm_mon == timeinfo->tm_mon &&
00226 datecache.tm_year == timeinfo->tm_year )) {
00227 #ifdef WIN32
00228 delete timeinfo;
00229 #endif
00230 return datecachestr;
00231 }
00232
00233
00234
00235
00236
00237 char buffer [32];
00238
00239 setlocale (LC_ALL, "");
00240 strftime (buffer, sizeof (buffer), "%x", timeinfo);
00241
00242 datecachestr = buffer;
00243 datecache = *timeinfo;
00244
00245 #ifdef WIN32
00246 delete timeinfo;
00247 #endif
00248
00249 return datecachestr;
00250 }
00251
00252 unsigned long Helper::getElapsedMillis ()
00253 {
00254 static unsigned long zero = 0;
00255
00256 struct timeb tp;
00257 ftime( &tp );
00258
00259 unsigned long val = tp.time*1000 + tp.millitm;
00260 if( zero == 0 ) zero = val;
00261
00262 return val-zero;
00263 }
00264
00265 string Helper::dtos (double val)
00266 {
00267 string ret = boost::lexical_cast<string> (val);
00268 return ret;
00269 }
00270
00271 double Helper::stod (string str)
00272 {
00273 if (str.length() <= 0) return 0.0;
00274 else return boost::lexical_cast<double> (str);
00275 }
00276
00277 }}