#ifndef ATOOLS_Org_MyStrStream_H #define ATOOLS_Org_MyStrStream_H #include "ATOOLS/Org/Exception.H" // Note: new compilers name "stringstream" in // instead of "strstream" in ! #if defined __GNUC__ #if __GNUC__ > 2 // GNU gcc 3.x.x C++ Compiler #include #include typedef std::stringstream MyStrStream; typedef std::ios_base::fmtflags MyFmtFlags; #define IOS_BASE std::ios_base #else // GNU gcc 2.95.x C++ Compiler #include #include #include // namespace std { // typedef strstream stringstream; // } typedef std::strstream MyStrStream; typedef std::ios::fmtflags MyFmtFlags; #define IOS_BASE std::ios #endif #endif #if defined(__sgi) && !defined(__GNUC__) #include #include // SGI IRIX C++ Compiler typedef std::stringstream MyStrStream; typedef std::ios_base::fmtflags MyFmtFlags; #define IOS_BASE std::ios_base #endif #include #include namespace ATOOLS { typedef std::map String_Map; template std::string ToString(const Value_Type &value, const size_t precision=12) { MyStrStream converter; std::string converted; converter.precision(precision); converter<>converted; return converted; } template std::string VectorToString(const std::vector &values, const size_t precision=12) { MyStrStream converter; converter.precision(precision); for (typename std::vector::const_iterator it = values.begin(); it != values.end(); ++it) { if (it != values.begin()) { converter << " "; } converter << ToString(*it); } return converter.str(); } /** * this specialization is necessary, because the template code above will * truncate the input string after the first whitespace */ template<> std::string ToString(const std::string& value, const size_t precision); template std::string MatrixToString(const std::vector > &values, const size_t precision=12) { MyStrStream converter; converter.precision(precision); for (typename std::vector >::const_iterator it = values.begin(); it != values.end(); ++it) { if (it != values.begin()) { converter << "\n"; } converter << VectorToString(*it); } return converter.str(); } template Value_Type ToType(const std::string &value, const size_t precision=12) { MyStrStream converter; Value_Type converted; converter.precision(precision); converter<>converted; if (converter.fail()) THROW(fatal_error, "Failed to parse " + value); return converted; } template std::vector ToVector(const std::string &values, const char separator=' ') { std::vector converted; MyStrStream converter(values); std::string token; while(std::getline(converter, token, separator)) { if (token != "") converted.push_back(ToType(token)); } return converted; } /** * this specialization is necessary, because the template code above will * truncate the input string after the first whitespace */ template<> std::string ToType(const std::string& value, const size_t precision); /// specialisation to handle conversion of strings like "true" etc. template<> bool ToType(const std::string& value, const size_t precision); std::string StringTrim(const std::string&); std::string StringReplace(const std::string &original, const std::string &from, const std::string &to); std::string SubString(const std::string& original, const std::string& begin, const std::string& end); std::string ReplaceUnits(const std::string& v); }// end of namespace ATOOLS; #endif