00001 #ifndef STREAMPARSER_H
00002 #define STREAMPARSER_H
00003
00004 #include <string>
00005 #include <sstream>
00006 #include <iostream>
00007 #include <vector>
00008 #include <typeinfo>
00009 #include <map>
00010 #include "parametermap.h"
00011 #include "any.h"
00012 #include "convert.h"
00013
00014 #define COMMENT_STRING "#"
00015 #define BEGIN_BLOCK "{"
00016 #define END_BLOCK "}"
00017
00018 using namespace std;
00019
00020
00021
00022
00023
00025 string get_prepped_line( istream& s, const string& comment_string=COMMENT_STRING );
00026
00028 template< typename Target >
00029 Target simple_lexical_cast( const string& argu ){
00030 stringstream interpreter;
00031 Target result;
00032 interpreter << argu << boolalpha;
00033 interpreter >> result;
00034 return result;
00035 }
00036
00038 template< typename Target >
00039 vector< Target > simple_vector_lexical_cast( const string& argu ){
00040 stringstream interpreter;
00041 vector< Target > result;
00042 Target buff;
00043 interpreter << argu << boolalpha;
00044 while ( interpreter >> buff ) result.push_back( buff );
00045 return result;
00046 }
00047
00048
00049
00050
00051 string get_type_string( const type_info& tid );
00052
00053 boost::any stoany( const string& type, const string& instring );
00054
00055
00056
00057
00059 istream& operator>>( istream& i, vector< string > V );
00060
00062 ostream& operator<<(ostream& o, vector< string > v);
00063
00065 template< typename ValueType >
00066 ostream& operator<<(ostream& o, vector< ValueType > v){
00067 typename vector< ValueType >::iterator it;
00068 for ( it=v.begin(); it!=v.end(); ++it ){
00069 o << (*it);
00070 ++it; if (it!=v.end()) o << endl; --it;
00071 }
00072 return o;
00073 }
00074
00076 template< typename ValueType >
00077 istream& operator>>( istream& i, vector< ValueType > V ){
00078 string line;
00079 while (i.good()) {
00080 getline(i,line);
00081 if (line.find(END_BLOCK) == string::npos) {
00082 V.push_back( simple_lexical_cast< ValueType >(line) );
00083 }
00084 else return i;
00085 }
00086 return i;
00087 }
00088
00090 template< typename ValueType >
00091 ostream& operator<<(ostream& o, vector< vector< ValueType > > v){
00092 typename vector< vector< ValueType > >::iterator it;
00093 typename vector< ValueType >::iterator rit;
00094 for ( it=v.begin(); it!=v.end(); ++it ){
00095 for ( rit=it->begin(); rit!=it->end(); ++rit ){
00096 o << (*rit) << " ";
00097 }
00098 ++it; if (it!=v.end()) o << endl; --it;
00099 }
00100 return o;
00101 }
00102
00104 template< typename ValueType >
00105 istream& operator>>( istream& i, vector< vector< ValueType > > V ){
00106 string line;
00107 V.clear();
00108 while (i.good()) {
00109 getline(i,line);
00110 if (line.find(END_BLOCK) == string::npos) {
00111 vector< string > srow = split(line);
00112 vector< double > row;
00113 for (
00114 vector< string >::iterator buff = srow.begin();
00115 buff != srow.end();
00116 ++buff
00117 ) row.push_back( simple_lexical_cast< double >(*buff) );
00118 V.push_back(row);
00119 }
00120 else return i;
00121 }
00122 return i;
00123 }
00124
00125
00126
00127
00128
00129
00131 class CParserObject{
00132 public:
00133 string type;
00134 string key;
00135 boost::any value;
00136 CParserObject( const string& instring );
00137 CParserObject( const string& intype, const string& inkey, const string& inblock):
00138 type(intype), key(inkey), value( stoany( intype, inblock ) ){}
00139 CParserObject( const string& inkey, const boost::any& initem):
00140 type(get_type_string(initem.type())), key(inkey), value(initem){}
00141 };
00142
00143 #endif