rtcStringTools.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: Autonomous Technologies
00011  * file .......: rtcStringTools.cpp
00012  * authors ....: Benjamin Pitzer
00013  * organization: Robert Bosch LLC
00014  * creation ...: 04/16/2006
00015  * modified ...: $Date: 2009-01-21 18:19:16 -0800 (Wed, 21 Jan 2009) $
00016  * changed by .: $Author: benjaminpitzer $
00017  * revision ...: $Revision: 14 $
00018  */
00019 #ifdef _MSC_VER
00020 #  pragma warning(disable: 4244)
00021 #endif
00022 
00023 //== INCLUDES ==================================================================
00024 #include "rtc/rtcStringTools.h"
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <sstream>
00028 #include <iostream>
00029 #include <boost/lexical_cast.hpp>
00030 
00031 //== NAMESPACES ================================================================
00032 namespace rtc {
00033 
00034 bool lexical_cast_bool(const std::string& str, const bool bDefault)
00035 {
00036   if (strcmp(str.c_str(),"true")==0 ||
00037       strcmp(str.c_str(),"True")==0 ||
00038       strcmp(str.c_str(),"TRUE")==0 ||
00039       strcmp(str.c_str(),"1")==0 ||
00040       strcmp(str.c_str(),"-1")==0)
00041     return true;
00042   else if (
00043       strcmp(str.c_str(),"false")==0 ||
00044       strcmp(str.c_str(),"False")==0 ||
00045       strcmp(str.c_str(),"FALSE")==0 ||
00046       strcmp(str.c_str(),"0")==0)
00047     return false;
00048   else
00049     return bDefault;
00050 }
00051 
00052 int lexical_cast_int(const std::string& str, const int nDefault)
00053 {
00054   int value;
00055   int res;
00056   if(str.length() == 0)
00057     return nDefault;
00058   res=sscanf(str.c_str(),"%i",&value);
00059   if (res==1)
00060     return value;
00061   return 0;
00062 }
00063 
00064 float lexical_cast_float(const std::string& str, const float fDefault)
00065 {
00066   double value;
00067   try { value = boost::lexical_cast<float>(str); }
00068   catch(boost::bad_lexical_cast &) { value = fDefault; }
00069   return value;
00070 }
00071 
00072 double lexical_cast_double(const std::string& str, const double dDefault )
00073 {
00074   double value;
00075   try { value = boost::lexical_cast<double>(str); }
00076   catch(boost::bad_lexical_cast &) { value = dDefault; }
00077   return value;
00078 }
00079 
00080 long lexical_cas_long(const std::string& str, const long lDefault )
00081 {
00082   int value;
00083   int res;
00084   if(str.length() == 0)
00085     return lDefault;
00086   res=sscanf(str.c_str(),"%i",&value);
00087   if (res!=1)
00088     return lDefault;
00089   return (long)value;
00090 }
00091 
00092 std::string lexical_cast_string(const double& var)
00093 {
00094   std::stringstream ss;
00095   ss << var;
00096   return ss.str();
00097 }
00098 
00099 std::string lexical_cast_string(const float& var)
00100 {
00101   std::stringstream ss;
00102   ss << var;
00103   return ss.str();
00104 }
00105 
00106 std::string lexical_cast_string(const bool& var)
00107 {
00108   if(var)
00109     return std::string("true");
00110   else
00111     return std::string("false");
00112 }
00113 
00114 std::string lexical_cast_string(const int& var){
00115   std::stringstream ss;
00116   ss << var;
00117   return ss.str();
00118 }
00119 
00120 // taken from http://code.activestate.com/recipes/577849-volume-slicer/
00121 // MIT license
00122 void split_string(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
00123 {
00124   // Skip delimiters at beginning.
00125   std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00126   // Find first "non-delimiter".
00127   std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
00128 
00129   while (std::string::npos != pos || std::string::npos != lastPos)
00130   {
00131     // Found a token, add it to the vector.
00132     tokens.push_back(str.substr(lastPos, pos - lastPos));
00133     // Skip delimiters.  Note the "not_of"
00134     lastPos = str.find_first_not_of(delimiters, pos);
00135     // Find next "non-delimiter"
00136     pos = str.find_first_of(delimiters, lastPos);
00137   }
00138 }
00139 
00140 // taken from http://code.activestate.com/recipes/577849-volume-slicer/
00141 // MIT license
00142 std::string& trim(std::string& str)
00143 {
00144   // Trim Both leading and trailing spaces
00145   size_t startpos = str.find_first_not_of(" \t"); // Find the first character position after excluding leading blank spaces
00146   size_t endpos = str.find_last_not_of(" \t"); // Find the first character position from reverse af
00147   // if all spaces or empty return an empty string
00148   if(( std::string::npos == startpos ) || ( std::string::npos == endpos))
00149     str = "";
00150   else
00151     str = str.substr(startpos, endpos-startpos+1);
00152   return str;
00153 }
00154 
00155 //==============================================================================
00156 } // NAMESPACE rtc
00157 //==============================================================================


rtc
Author(s): Benjamin Pitzer
autogenerated on Thu Jan 2 2014 11:04:53