PCMStringConverter.cpp
Go to the documentation of this file.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 #include "../include/PCMStringConverter.h"
00030
00031 namespace ParCompMark
00032 {
00033
00034
00035
00036
00037
00038 const s32 StringConverter::DEFAULTPRECISION = -1;
00039 const s32 StringConverter::DEFAULTFIELDWIDTH = -1;
00040
00041
00042
00043
00044
00045 std::string StringConverter::toString(const u8 & value)
00046 {
00047 std::ostringstream osstr;
00048 osstr << value;
00049 return osstr.str();
00050 }
00051
00052
00053
00054 std::string StringConverter::toString(const u16 & value)
00055 {
00056 std::ostringstream osstr;
00057 osstr << value;
00058 return osstr.str();
00059 }
00060
00061
00062
00063 std::string StringConverter::toString(const u32 & value)
00064 {
00065 std::ostringstream osstr;
00066 osstr << value;
00067 return osstr.str();
00068 }
00069
00070
00071
00072 std::string StringConverter::toString(const s8 & value)
00073 {
00074 std::ostringstream osstr;
00075 osstr << value;
00076 return osstr.str();
00077 }
00078
00079
00080
00081 std::string StringConverter::toString(const s16 & value)
00082 {
00083 std::ostringstream osstr;
00084 osstr << value;
00085 return osstr.str();
00086 }
00087
00088
00089
00090 std::string StringConverter::toString(const s32 & value)
00091 {
00092 std::ostringstream osstr;
00093 osstr << value;
00094 return osstr.str();
00095 }
00096
00097
00098
00099 std::string StringConverter::toString(const Real & value, const s32 & fieldWidth,
00100 const s32 & precision)
00101 {
00102 std::ostringstream osstr;
00103 if(precision == DEFAULTPRECISION)
00104 osstr << value;
00105 else
00106 {
00107 osstr.precision(precision);
00108 osstr << std::fixed << value;
00109 }
00110 return _fitFieldWidth(osstr.str(), fieldWidth);
00111 }
00112
00113
00114
00115 std::string StringConverter::toString(const bool value)
00116 {
00117 std::ostringstream osstr;
00118 osstr << value;
00119 return osstr.str();
00120 }
00121
00122
00123
00124 std::string StringConverter::toString(const void *value)
00125 {
00126 std::ostringstream osstr;
00127 osstr << value;
00128 return osstr.str();
00129 }
00130
00131
00132
00133 u32 StringConverter::toU32(const std::string value)
00134 {
00135 u32 result;
00136
00137 std::istringstream isstr(value);
00138
00139 isstr >> result;
00140 return result;
00141 }
00142
00143
00144
00145 Real StringConverter::toReal(const std::string value)
00146 {
00147 Real result;
00148
00149 std::istringstream isstr(value);
00150
00151 isstr >> result;
00152 return result;
00153 }
00154
00155
00156
00157 void *StringConverter::parsePointer(const std::string value)
00158 {
00159 std::istringstream isstr(value, std::istringstream::in);
00160 void *p;
00161
00162 isstr >> p;
00163 return p;
00164 }
00165
00166
00167
00168 void StringConverter::trim(std::string & str)
00169 {
00170 std::string::size_type pos = str.find_last_not_of(" \t");
00171 if(pos != std::string::npos)
00172 {
00173 str.erase(pos + 1);
00174 pos = str.find_first_not_of(" \t");
00175 if(pos != std::string::npos)
00176 str.erase(0, pos);
00177 } else
00178 str.erase(str.begin(), str.end());
00179 }
00180
00181
00182
00183 std::vector < std::string > StringConverter::tokenize(const std::string & str, const char *delimiters)
00184 {
00185 std::vector < std::string > tokens;
00186
00187 std::string::size_type lastPos = 0, pos = 0;
00188 int count = 0;
00189
00190 if(str.length() < 1)
00191 return tokens;
00192
00193
00194 lastPos = str.find_first_not_of(delimiters, 0);
00195
00196 if((str.substr(0, lastPos - pos).length()) > 0)
00197 {
00198 count = str.substr(0, lastPos - pos).length();
00199
00200 for(int i = 0; i < count; i++)
00201 tokens.push_back("");
00202
00203 if(std::string::npos == lastPos)
00204 tokens.push_back("");
00205 }
00206
00207 pos = str.find_first_of(delimiters, lastPos);
00208
00209 while(std::string::npos != pos || std::string::npos != lastPos)
00210 {
00211
00212 tokens.push_back(str.substr(lastPos, pos - lastPos));
00213
00214
00215 lastPos = str.find_first_not_of(delimiters, pos);
00216
00217 if((std::string::npos != pos) && (str.substr(pos, lastPos - pos).length() > 1))
00218 {
00219 count = str.substr(pos, lastPos - pos).length();
00220
00221 for(int i = 0; i < count; i++)
00222 tokens.push_back("");
00223 }
00224
00225 pos = str.find_first_of(delimiters, lastPos);
00226 }
00227
00228 return tokens;
00229 }
00230
00231
00232
00233 std::string StringConverter::_fitFieldWidth(const std::string & str, const s32 & fieldWidth)
00234 {
00235 s32 padding = fieldWidth - str.size();
00236
00237 if(fieldWidth == DEFAULTFIELDWIDTH || padding <= 0)
00238 return str;
00239 char fitStr[padding + 1];
00240
00241 memset(fitStr, ' ', padding);
00242 fitStr[padding] = '\0';
00243
00244 return fitStr + str;
00245 }
00246
00247
00248
00249 }