File indexing completed on 2025-04-19 09:09:53
0001 #ifndef ATOOLS_Org_MyStrStream_H
0002 #define ATOOLS_Org_MyStrStream_H
0003
0004 #include "ATOOLS/Org/Exception.H"
0005
0006
0007
0008
0009
0010 #include <sstream>
0011 #include <iomanip>
0012 typedef std::stringstream MyStrStream;
0013 typedef std::ios_base::fmtflags MyFmtFlags;
0014 #define IOS_BASE std::ios_base
0015
0016 #include <map>
0017 #include <vector>
0018
0019 namespace ATOOLS {
0020
0021 typedef std::map<std::string,std::string> String_Map;
0022
0023 template <class Value_Type>
0024 std::string ToString(const Value_Type &value,
0025 const size_t precision=12) {
0026 MyStrStream converter;
0027 std::string converted;
0028 converter.precision(precision);
0029 converter<<value;
0030 converter>>converted;
0031 return converted;
0032 }
0033
0034 template <class Value_Type>
0035 std::string VectorToString(const std::vector<Value_Type> &values,
0036 const size_t precision=12,
0037 const std::string& separator=" ") {
0038 MyStrStream converter;
0039 converter.precision(precision);
0040 for (typename std::vector<Value_Type>::const_iterator it = values.begin();
0041 it != values.end();
0042 ++it) {
0043 if (it != values.begin()) {
0044 converter << separator;
0045 }
0046 converter << ToString<Value_Type>(*it);
0047 }
0048 return converter.str();
0049 }
0050
0051
0052
0053
0054
0055 template<> std::string ToString<std::string>(const std::string& value,
0056 const size_t precision);
0057
0058
0059 template <class Value_Type>
0060 std::string MatrixToString(const std::vector<std::vector<Value_Type> > &values,
0061 const size_t precision=12) {
0062 MyStrStream converter;
0063 converter.precision(precision);
0064 for (typename std::vector<std::vector<Value_Type> >::const_iterator it = values.begin();
0065 it != values.end();
0066 ++it) {
0067 if (it != values.begin()) {
0068 converter << "\n";
0069 }
0070 converter << VectorToString<Value_Type>(*it);
0071 }
0072 return converter.str();
0073 }
0074
0075 template <class Value_Type>
0076 Value_Type ToType(const std::string &value,
0077 const size_t precision=12) {
0078 MyStrStream converter;
0079 Value_Type converted;
0080 converter.precision(precision);
0081 converter<<value;
0082 converter>>converted;
0083 if (converter.fail())
0084 THROW(fatal_error, "Failed to parse " + value);
0085 return converted;
0086 }
0087
0088 template <class Value_Type>
0089 std::vector<Value_Type> ToVector(const std::string &values,
0090 const char separator=' ')
0091 {
0092 std::vector<Value_Type> converted;
0093 MyStrStream converter(values);
0094 std::string token;
0095 while(std::getline(converter, token, separator)) {
0096 if (token != "")
0097 converted.push_back(ToType<Value_Type>(token));
0098 }
0099 return converted;
0100 }
0101
0102
0103
0104
0105
0106 template<> std::string ToType<std::string>(const std::string& value,
0107 const size_t precision);
0108
0109
0110 template<> bool ToType<bool>(const std::string& value,
0111 const size_t precision);
0112
0113 template<> double ToType<double>(const std::string& value,
0114 const size_t precision);
0115
0116 std::string StringTrim(const std::string&);
0117
0118 std::string StringReplace(const std::string &original,
0119 const std::string &from, const std::string &to);
0120
0121 std::string SubString(const std::string& original,
0122 const std::string& begin, const std::string& end);
0123
0124
0125 std::string ReplaceUnits(const std::string& v);
0126
0127 }
0128
0129 #endif