inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_sto 00005 #define inlib_sto 00006 00007 #include <string> 00008 00009 namespace inlib { 00010 00011 inline std::string to(bool a_value){return a_value?"true":"false";} 00012 00013 inline bool to(const std::string& a_string,bool& a_value){ 00014 if( (a_string=="1") 00015 ||(a_string=="true")||(a_string=="TRUE")||(a_string=="True") 00016 ||(a_string=="yes")||(a_string=="YES")||(a_string=="Yes") 00017 ||(a_string=="on")||(a_string=="ON")||(a_string=="On") 00018 ){ 00019 a_value = true; 00020 return true; 00021 } else if((a_string=="0") 00022 ||(a_string=="false")||(a_string=="FALSE")||(a_string=="False") 00023 ||(a_string=="no")||(a_string=="NO")||(a_string=="No") 00024 ||(a_string=="off")||(a_string=="OFF")||(a_string=="Off") 00025 ){ 00026 a_value = false; 00027 return true; 00028 } else { 00029 a_value = false; 00030 return false; 00031 } 00032 } 00033 00034 } 00035 00036 #include <sstream> 00037 00038 namespace inlib { 00039 00040 template <class T> 00041 inline bool to(const std::string& a_s,T& a_v) { 00042 std::istringstream strm(a_s.c_str()); 00043 std::streampos pb = strm.tellg(); 00044 strm >> a_v; 00045 std::streampos pe = strm.tellg(); 00046 std::string::size_type sz = pe-pb; 00047 if(sz!=a_s.size()) {a_v = T();return false;} 00048 return true; 00049 } 00050 00051 template <class T> 00052 inline std::string to(const T& a_v) { 00053 std::ostringstream strm; 00054 strm << a_v; 00055 return strm.str(); 00056 } 00057 00058 inline std::string d2s(double a_value){ 00059 std::ostringstream strm; 00060 strm.precision(25); 00061 strm << a_value; 00062 return strm.str(); 00063 } 00064 00065 inline std::string soutd(double a_value) { 00066 return std::string("\"")+d2s(a_value)+"\""; 00067 } 00068 00069 // for BatchLab/XML 00070 template <class T> 00071 inline std::string sout(const T& a_value) { 00072 return std::string("\"")+to<T>(a_value)+"\""; 00073 } 00074 00075 } 00076 00077 #endif