StringHelper.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2011, SRI International (R)
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <algorithm>
00019 #include <string>
00020 #include <limits>
00021 
00022 #include <OpenKarto/StringHelper.h>
00023 #include <OpenKarto/Geometry.h>
00024 
00025 namespace karto
00026 {
00027 
00031 
00032   String StringHelper::ToString(const char* value)
00033   {
00034     return String(value);
00035   }
00036 
00037   String StringHelper::ToString(kt_bool value)
00038   {
00039     if (value == true)
00040     {
00041       return String("true");
00042     }
00043 
00044     return String("false");
00045   }
00046 
00047   //String StringHelper::ToString(kt_size_t value)
00048   //{
00049   //  std::stringstream converter;
00050   //  converter.precision(std::numeric_limits<double>::digits10);
00051   //  converter << value;
00052   //  return converter.str().c_str();
00053   //}
00054 
00055   String StringHelper::ToString(kt_int16u value)
00056   {
00057     std::stringstream converter;
00058     converter.precision(std::numeric_limits<double>::digits10);
00059     converter << value;
00060     return converter.str().c_str();
00061   }
00062 
00063   String StringHelper::ToString(kt_int16s value)
00064   {
00065     std::stringstream converter;
00066     converter.precision(std::numeric_limits<double>::digits10);
00067     converter << value;
00068     return converter.str().c_str();
00069   }
00070 
00071   String StringHelper::ToString(kt_int32u value)
00072   {
00073     char buffer[64];
00074 #ifdef WIN32
00075     sprintf_s(buffer, 64, "%u", value);
00076 #else
00077     sprintf(buffer, "%u", value);
00078 #endif
00079     return String(buffer);
00080   }
00081 
00082   String StringHelper::ToString(kt_int32s value)
00083   {
00084     char buffer[64];
00085 #ifdef WIN32
00086     sprintf_s(buffer, 64, "%d", value);
00087 #else
00088     sprintf(buffer, "%d", value);
00089 #endif
00090     return String(buffer);
00091   }
00092 
00093   String StringHelper::ToString(kt_int64u value)
00094   {
00095     std::stringstream converter;
00096     converter.precision(std::numeric_limits<double>::digits10);
00097     converter << value;
00098     return converter.str().c_str();
00099   }
00100 
00101   String StringHelper::ToString(kt_int64s value)
00102   {
00103     std::stringstream converter;
00104     converter.precision(std::numeric_limits<double>::digits10);
00105     converter << value;
00106     return converter.str().c_str();
00107   }
00108 
00109 #if defined(__APPLE__) && !defined(__LP64__)
00110   String StringHelper::ToString(kt_size_t value)
00111   {
00112     std::stringstream converter;
00113     converter.precision(std::numeric_limits<double>::digits10);
00114     converter << value;
00115     return converter.str().c_str();
00116   }     
00117 #endif
00118 
00119   String StringHelper::ToString(kt_float value)
00120   {
00121     char buffer[64];
00122 #ifdef WIN32
00123     sprintf_s(buffer, 64, "%.*g", 8, (double) value);
00124 #else
00125     sprintf(buffer, "%.*g", 8, (double) value);
00126 #endif
00127     return String(buffer);
00128   }
00129 
00130   String StringHelper::ToString(kt_double value)
00131   {
00132     char buffer[64];
00133 #ifdef WIN32
00134     sprintf_s(buffer, 64, "%.*g", 16, value);
00135 #else
00136     sprintf(buffer, "%.*g", 16, value);
00137 #endif
00138     return String(buffer);
00139   }
00140 
00141   String StringHelper::ToString(kt_float value, kt_int32u precision)
00142   {
00143     char buffer[64];
00144 #ifdef WIN32
00145     sprintf_s(buffer, 64, "%.*f", (kt_int32s)precision, (double)value);
00146 #else
00147     sprintf(buffer, "%.*f", (kt_int32s)precision, (double)value);
00148 #endif
00149     return String(buffer);
00150   }
00151 
00152   String StringHelper::ToString(kt_double value, kt_int32u precision)
00153   {
00154     char buffer[64];
00155 #ifdef WIN32
00156     sprintf_s(buffer, 64, "%.*f", (kt_int32s)precision, value);
00157 #else
00158     sprintf(buffer, "%.*f", (kt_int32s)precision, value);
00159 #endif
00160     return String(buffer);
00161   }
00162 
00163   karto::String StringHelper::ToString(const String& rValue)
00164   {
00165     return rValue;
00166   }
00167 
00168   karto::String StringHelper::ToString(const Quaternion& rValue)
00169   {
00170     return rValue.ToString();
00171   }
00172 
00173   karto::String StringHelper::ToString(const Color& rValue)
00174   {
00175     return rValue.ToString();
00176   }
00177 
00178   karto::String StringHelper::ToString(const Pose2& rValue)
00179   {
00180     return rValue.ToString();
00181   }
00182 
00183   karto::String StringHelper::ToString(const Pose3& rValue)
00184   {
00185     return rValue.ToString();
00186   }
00187 
00188   kt_bool StringHelper::FromString(const String& rStringValue, kt_bool& rValue)
00189   {
00190     rValue = false;
00191 
00192     if (ToLowerCase(rStringValue) == String("true"))
00193     {
00194       rValue = true;
00195     }
00196 
00197     return true;
00198   }
00199 
00200   kt_bool StringHelper::FromString(const String& rStringValue, kt_int16s& rValue)
00201   {
00202     int precision = std::numeric_limits<double>::digits10;
00203     std::stringstream converter;
00204     converter.precision(precision);
00205 
00206     converter.str(rStringValue.ToCString());
00207 
00208     converter >> rValue;
00209 
00210     return true;
00211   }
00212 
00213   kt_bool StringHelper::FromString(const String& rStringValue, kt_int16u& rValue)
00214   {
00215     int precision = std::numeric_limits<double>::digits10;
00216     std::stringstream converter;
00217     converter.precision(precision);
00218 
00219     converter.str(rStringValue.ToCString());
00220 
00221     converter >> rValue;
00222 
00223     return true;
00224   }
00225 
00226   kt_bool StringHelper::FromString(const String& rStringValue, kt_int32s& rValue)
00227   {
00228     int precision = std::numeric_limits<double>::digits10;
00229     std::stringstream converter;
00230     converter.precision(precision);
00231 
00232     converter.str(rStringValue.ToCString());
00233 
00234     converter >> rValue;
00235 
00236     return true;
00237   }
00238 
00239   kt_bool StringHelper::FromString(const String& rStringValue, kt_int32u& rValue)
00240   {
00241     int precision = std::numeric_limits<double>::digits10;
00242     std::stringstream converter;
00243     converter.precision(precision);
00244 
00245     converter.str(rStringValue.ToCString());
00246 
00247     converter >> rValue;
00248 
00249     return true;
00250   }
00251 
00252   kt_bool StringHelper::FromString(const String& rStringValue, kt_int64s& rValue)
00253   {
00254     int precision = std::numeric_limits<double>::digits10;
00255     std::stringstream converter;
00256     converter.precision(precision);
00257 
00258     converter.str(rStringValue.ToCString());
00259 
00260     converter >> rValue;
00261 
00262     return true;
00263   }
00264 
00265   kt_bool StringHelper::FromString(const String& rStringValue, kt_int64u& rValue)
00266   {
00267     int precision = std::numeric_limits<double>::digits10;
00268     std::stringstream converter;
00269     converter.precision(precision);
00270 
00271     converter.str(rStringValue.ToCString());
00272 
00273     converter >> rValue;
00274 
00275     return true;
00276   }
00277 
00278   kt_bool StringHelper::FromString(const String& rStringValue, kt_float& rValue)
00279   {
00280     int precision = std::numeric_limits<double>::digits10;
00281     std::stringstream converter;
00282     converter.precision(precision);
00283 
00284     converter.str(rStringValue.ToCString());
00285 
00286     converter >> rValue;
00287 
00288     return true;
00289   }
00290 
00291   kt_bool StringHelper::FromString(const String& rStringValue, kt_double& rValue)
00292   {
00293     int precision = std::numeric_limits<double>::digits10;
00294     std::stringstream converter;
00295     converter.precision(precision);
00296 
00297     converter.str(rStringValue.ToCString());
00298 
00299     converter >> rValue;
00300 
00301     return true;
00302   }
00303 
00304   kt_bool StringHelper::FromString(const String& rStringValue, String& rValue)
00305   {
00306     rValue = rStringValue;
00307 
00308     return true;
00309   }
00310 
00311   kt_bool StringHelper::FromString(const String& rStringValue, Quaternion& rValue)
00312   {
00313     kt_size_t index = rStringValue.FindFirstOf(" ");
00314     if (index != -1)
00315     {
00316       std::stringstream converter;
00317       converter.str(rStringValue.ToCString());
00318 
00319       kt_double valueX = 0.0;
00320       kt_double valueY = 0.0;
00321       kt_double valueZ = 0.0;
00322       kt_double valueW = 0.0;
00323 
00324       converter >> valueX;
00325       converter >> valueY;
00326       converter >> valueZ;
00327       converter >> valueW;
00328 
00329       rValue.SetX(valueX);
00330       rValue.SetY(valueY);
00331       rValue.SetZ(valueZ);
00332       rValue.SetW(valueW);
00333 
00334       return true;
00335     }
00336 
00337     return false;
00338   }
00339 
00340   kt_bool StringHelper::FromString(const String& rStringValue, Color& rValue)
00341   {
00342     kt_size_t index = rStringValue.FindFirstOf(" ");
00343     if (index != -1)
00344     {
00345       std::stringstream converter;
00346       converter.str(rStringValue.ToCString());
00347 
00348       kt_double valueRed = 0.0;
00349       kt_double valueGreen = 0.0;
00350       kt_double valueBlue = 0.0;
00351       kt_double valueAlpha = 0.0;
00352 
00353       converter >> valueRed;
00354       converter >> valueGreen;
00355       converter >> valueBlue;
00356       converter >> valueAlpha;
00357 
00358       rValue.SetRed(valueRed);
00359       rValue.SetGreen(valueGreen);
00360       rValue.SetBlue(valueBlue);
00361       rValue.SetAlpha(valueAlpha);
00362 
00363       return true;
00364     }
00365 
00366     return false;
00367   }
00368 
00369   kt_bool StringHelper::FromString(const String& rStringValue, Pose2& rValue)
00370   {
00371     kt_size_t index = rStringValue.FindFirstOf(" ");
00372     if (index != -1)
00373     {
00374       std::stringstream converter;
00375       converter.str(rStringValue.ToCString());
00376 
00377       kt_double valueX = 0.0;
00378       kt_double valueY = 0.0;
00379       kt_double valueHeading = 0.0;
00380 
00381       converter >> valueX;
00382       converter >> valueY;
00383       converter >> valueHeading;
00384 
00385       rValue.SetX(valueX);
00386       rValue.SetY(valueY);
00387       rValue.SetHeading(valueHeading);
00388 
00389       return true;
00390     }
00391 
00392     return false;
00393   }
00394 
00395   kt_bool StringHelper::FromString(const String& rStringValue, Pose3& rValue)
00396   {
00397     kt_size_t index = rStringValue.FindFirstOf(" ");
00398     if (index != -1)
00399     {
00400       std::stringstream converter;
00401       converter.str(rStringValue.ToCString());
00402 
00403       kt_double valueX = 0.0;
00404       kt_double valueY = 0.0;
00405       kt_double valueZ = 0.0;
00406       kt_double valueW = 0.0;
00407 
00408       converter >> valueX;
00409       converter >> valueY;
00410       converter >> valueZ;
00411       rValue.SetPosition(karto::Vector3d(valueX, valueY, valueZ));
00412 
00413       valueX = 0.0;
00414       valueY = 0.0;
00415       valueZ = 0.0;
00416       valueW = 0.0;
00417 
00418       converter >> valueX;
00419       converter >> valueY;
00420       converter >> valueZ;
00421       converter >> valueW;
00422       rValue.SetOrientation(karto::Quaternion(valueX, valueY, valueZ, valueW));
00423 
00424       return true;
00425     }
00426 
00427     return false;
00428   }
00429 
00430   String StringHelper::Trim(const String& rValue)
00431   {
00432     char const* delims = " \t\r\n";
00433 
00434     std::string result(rValue.ToCString());
00435     std::string::size_type index = result.find_last_not_of(delims);
00436     if (index != std::string::npos)
00437       result.erase(++index);
00438 
00439     index = result.find_first_not_of(delims);
00440     if (index != std::string::npos)
00441     {
00442       result.erase(0, index);
00443     }
00444     else
00445     {
00446       result.erase();
00447     }
00448 
00449     return String(result.c_str());
00450   }
00451 
00452   String StringHelper::Replace(const String& rSource, const String& rFind, const String& rReplace)
00453   {
00454     size_t j;
00455 
00456     std::string retStr = rSource.ToCString();
00457 
00458     if (rFind == rReplace)
00459     {
00460       return String(retStr.c_str());
00461     }
00462 
00463     for (; ( j = retStr.find(rFind.ToCString()) ) != std::string::npos; )
00464     {
00465       retStr.replace( j, rFind.Size(), rReplace.ToCString());
00466     }
00467 
00468     return String(retStr.c_str());
00469   }
00470 
00471   kt_bool StringHelper::IsLetter(char ch)
00472   {
00473     return isalpha(ch) != 0;
00474   }  
00475 
00476   String StringHelper::ToLowerCase(const String &rValue)
00477   {
00478     std::string value = rValue.ToCString();
00479     std::string ext = rValue.ToCString();
00480 
00481     std::transform(value.begin(), value.end(), ext.begin(), tolower);
00482 
00483     return String(ext.c_str());
00484   }
00485 
00486   String StringHelper::ToUpperCase(const String &rValue)
00487   {
00488     std::string value = rValue.ToCString();
00489     std::string ext = rValue.ToCString();
00490 
00491     std::transform(value.begin(), value.end(), ext.begin(), toupper);
00492 
00493     return String(ext.c_str());
00494   }
00495 
00499 
00500   void StringBuilder::Clear()
00501   {
00502     m_String = "";
00503   }
00504 
00505   const String& StringBuilder::ToString() const
00506   {
00507     return m_String;
00508   }
00509 
00510   //StringBuilder& StringBuilder::operator << (char value)
00511   //{
00512   //  m_String.Append(value);
00513 
00514   //  return *this;
00515   //}
00516 
00517   StringBuilder& StringBuilder::operator << (kt_int8u value)
00518   {
00519     m_String.Append(karto::StringHelper::ToString(value));
00520 
00521     return *this;
00522   }
00523 
00524   StringBuilder& StringBuilder::operator << (kt_int16s value)
00525   {
00526     m_String.Append(karto::StringHelper::ToString(value));
00527 
00528     return *this;
00529   }
00530 
00531   StringBuilder& StringBuilder::operator << (kt_int16u value)
00532   {
00533     m_String.Append(karto::StringHelper::ToString(value));
00534 
00535     return *this;
00536   }
00537 
00538   StringBuilder& StringBuilder::operator << (kt_int32s value)
00539   {
00540     m_String.Append(karto::StringHelper::ToString(value));
00541 
00542     return *this;
00543   }
00544 
00545   StringBuilder& StringBuilder::operator << (kt_int32u value)
00546   {
00547     m_String.Append(karto::StringHelper::ToString(value));
00548 
00549     return *this;
00550   }
00551 
00552   StringBuilder& StringBuilder::operator << (kt_int64s value)
00553   {
00554     m_String.Append(karto::StringHelper::ToString(value));
00555 
00556     return *this;
00557   }
00558 
00559   StringBuilder& StringBuilder::operator << (kt_int64u value)
00560   {
00561     m_String.Append(karto::StringHelper::ToString(value));
00562 
00563     return *this;
00564   }
00565 
00566 #if defined(__APPLE__) && !defined(__LP64__)
00567   StringBuilder& StringBuilder::operator << (kt_size_t value)
00568   {
00569     m_String.Append(karto::StringHelper::ToString(value));
00570 
00571     return *this;
00572   }
00573 #endif
00574 
00575   StringBuilder& StringBuilder::operator << (kt_float value)
00576   {
00577     m_String.Append(karto::StringHelper::ToString(value));
00578 
00579     return *this;
00580   }
00581 
00582   StringBuilder& StringBuilder::operator << (kt_double value)
00583   {
00584     m_String.Append(karto::StringHelper::ToString(value));
00585 
00586     return *this;
00587   }
00588   
00589         StringBuilder& StringBuilder::operator << (const String& rValue)
00590   {
00591     m_String.Append(rValue);
00592 
00593     return *this;
00594   }
00595 
00596   StringBuilder& StringBuilder::operator << (const StringBuilder& rValue)
00597   {
00598     m_String.Append(rValue.ToString());
00599 
00600     return *this;
00601   }
00602 
00603 }


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Sun Apr 2 2017 04:05:24