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