00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TextParse_h
00023 #define TextParse_h
00024
00025 #include <iosfwd>
00026 #include <vector>
00027 #include <list>
00028 #include <set>
00029 #include <string>
00030
00031 #include <y2util/stringutil.h>
00032
00034 namespace TextParse {
00035 ;
00036
00037 struct SingleLine {
00038 enum { SINGLELINE = 1, MULTILINE = !SINGLELINE };
00039 typedef std::string text_type;
00040 static void mustBeSingleLine() {}
00041 };
00042
00043 struct MultiLine {
00044 enum { SINGLELINE = 0, MULTILINE = !SINGLELINE };
00045 typedef std::list<std::string> text_type;
00046 static void mustBeMultiLine() {}
00047 };
00048
00050
00051 template<typename _Vt>
00052 struct TextConvert : public SingleLine {
00053 typedef _Vt value_type;
00054 bool toText( const value_type & value_r, text_type & asText_r );
00055 bool fromText( value_type & value_r, const text_type & asText_r );
00056 };
00057
00059
00061
00062
00063
00065
00067
00069 template<>
00070 bool TextConvert<int>::toText( const value_type & value_r, text_type & asText_r ) {
00071 asText_r = stringutil::numstring( value_r );
00072 return true;
00073 }
00074 template<>
00075 bool TextConvert<int>::fromText( value_type & value_r, const text_type & asText_r ) {
00076 value_r = atoi( asText_r.c_str() );
00077 return true;
00078 }
00079
00081
00083 template<>
00084 bool TextConvert<unsigned>::toText( const value_type & value_r, text_type & asText_r ) {
00085 asText_r = stringutil::numstring( value_r );
00086 return true;
00087 }
00088 template<>
00089 bool TextConvert<unsigned>::fromText( value_type & value_r, const text_type & asText_r ) {
00090 value_r = (value_type)atoi( asText_r.c_str() );
00091 return true;
00092 }
00093
00095
00097 template<>
00098 bool TextConvert<long>::toText( const value_type & value_r, text_type & asText_r ) {
00099 asText_r = stringutil::numstring( value_r );
00100 return true;
00101 }
00102 template<>
00103 bool TextConvert<long>::fromText( value_type & value_r, const text_type & asText_r ) {
00104 value_r = atol( asText_r.c_str() );
00105 return true;
00106 }
00107
00109
00111 template<>
00112 bool TextConvert<unsigned long>::toText( const value_type & value_r, text_type & asText_r ) {
00113 asText_r = stringutil::numstring( value_r );
00114 return true;
00115 }
00116 template<>
00117 bool TextConvert<unsigned long>::fromText( value_type & value_r, const text_type & asText_r ) {
00118 value_r = (value_type)atol( asText_r.c_str() );
00119 return true;
00120 }
00121
00123
00125 template<>
00126 bool TextConvert<long long>::toText( const value_type & value_r, text_type & asText_r ) {
00127 asText_r = stringutil::numstring( value_r );
00128 return true;
00129 }
00130 template<>
00131 bool TextConvert<long long>::fromText( value_type & value_r, const text_type & asText_r ) {
00132 value_r = atoll( asText_r.c_str() );
00133 return true;
00134 }
00135
00137
00139 template<>
00140 bool TextConvert<unsigned long long>::toText( const value_type & value_r, text_type & asText_r ) {
00141 asText_r = stringutil::numstring( value_r );
00142 return true;
00143 }
00144 template<>
00145 bool TextConvert<unsigned long long>::fromText( value_type & value_r, const text_type & asText_r ) {
00146 value_r = (value_type)atoll( asText_r.c_str() );
00147 return true;
00148 }
00149
00151
00153 template<>
00154 bool TextConvert<std::string>::toText( const value_type & value_r, text_type & asText_r ) {
00155 asText_r = value_r;
00156 return true;
00157 }
00158 template<>
00159 bool TextConvert<std::string>::fromText( value_type & value_r, const text_type & asText_r ) {
00160 value_r = asText_r;
00161 return true;
00162 }
00163
00165
00166
00167
00169
00171
00173 template<typename _Ct>
00174 struct TextConvert<std::vector<_Ct> > : public MultiLine {
00175 typedef std::vector<_Ct> value_type;
00176 TextConvert() {
00177 TextConvert<_Ct>::mustBeSingleLine();
00178 }
00179 bool toText( const value_type & value_r, text_type & asText_r ) {
00180 asText_r.clear();
00181 TextConvert<_Ct> lconv;
00182 typename TextConvert<_Ct>::text_type ltext;
00183 for ( typename value_type::const_iterator it = value_r.begin(); it != value_r.end(); ++it ) {
00184 lconv.toText( *it, ltext );
00185 asText_r.push_back( ltext );
00186 }
00187 return true;
00188 }
00189 bool fromText( value_type & value_r, const text_type & asText_r ) {
00190 value_type tmpval;
00191 tmpval.reserve( asText_r.size() );
00192 TextConvert<_Ct> lconv;
00193 typename TextConvert<_Ct>::value_type lval;
00194 for ( text_type::const_iterator it = asText_r.begin(); it != asText_r.end(); ++it ) {
00195 lconv.fromText( lval, *it );
00196 tmpval.push_back( lval );
00197 }
00198 value_r.swap( tmpval );
00199 return true;
00200 }
00201 };
00202
00204
00206 template<typename _Ct>
00207 struct TextConvert<std::list<_Ct> > : public MultiLine {
00208 typedef std::list<_Ct> value_type;
00209 TextConvert() {
00210 TextConvert<_Ct>::mustBeSingleLine();
00211 }
00212 bool toText( const value_type & value_r, text_type & asText_r ) {
00213 asText_r.clear();
00214 TextConvert<_Ct> lconv;
00215 typename TextConvert<_Ct>::text_type ltext;
00216 for ( typename value_type::const_iterator it = value_r.begin(); it != value_r.end(); ++it ) {
00217 lconv.toText( *it, ltext );
00218 asText_r.push_back( ltext );
00219 }
00220 return true;
00221 }
00222 bool fromText( value_type & value_r, const text_type & asText_r ) {
00223 value_r.clear();
00224 TextConvert<_Ct> lconv;
00225 typename TextConvert<_Ct>::value_type lval;
00226 for ( text_type::const_iterator it = asText_r.begin(); it != asText_r.end(); ++it ) {
00227 lconv.fromText( lval, *it );
00228 value_r.push_back( lval );
00229 }
00230 return true;
00231 }
00232 };
00233
00235
00237 template<typename _Ct>
00238 struct TextConvert<std::set<_Ct> > : public MultiLine {
00239 typedef std::set<_Ct> value_type;
00240 TextConvert() {
00241 TextConvert<_Ct>::mustBeSingleLine();
00242 }
00243 bool toText( const value_type & value_r, text_type & asText_r ) {
00244 asText_r.clear();
00245 TextConvert<_Ct> lconv;
00246 typename TextConvert<_Ct>::text_type ltext;
00247 for ( typename value_type::const_iterator it = value_r.begin(); it != value_r.end(); ++it ) {
00248 lconv.toText( *it, ltext );
00249 asText_r.push_back( ltext );
00250 }
00251 return true;
00252 }
00253 bool fromText( value_type & value_r, const text_type & asText_r ) {
00254 value_r.clear();
00255 TextConvert<_Ct> lconv;
00256 typename TextConvert<_Ct>::value_type lval;
00257 for ( text_type::const_iterator it = asText_r.begin(); it != asText_r.end(); ++it ) {
00258 lconv.fromText( lval, *it );
00259 value_r.insert( lval );
00260 }
00261 return true;
00262 }
00263 };
00264
00266 }
00268
00269 #endif // TextParse_h