00001 #include "cheezyparser.h"
00002 #include "message.h"
00003
00004
00005
00006
00007 string get_prepped_line( istream& s, const string& comment_string ){
00008 string line;
00009 getline(s,line);
00010 return remove_trailing_blanks( remove_comments( line, comment_string ) );
00011 }
00012
00013
00014
00015
00016
00017 vector<int> __ivec__(1,1);
00018 vector<float> __fvec__(1,1.0);
00019 vector<double> __dvec__(1,1.0);
00020 vector<bool> __bvec__(1,0);
00021 vector<string> __svec__(1,"");
00022 vector< vector< double > > __dmat__(1,__dvec__);
00023 parameterMap __pmap__;
00024
00025 string get_type_string( const type_info& tid ){
00026 if ( tid == typeid(int(1)) ) return "int" ;
00027 else if ( tid == typeid(float(1)) ) return "float" ;
00028 else if ( tid == typeid(double(1)) ) return "double" ;
00029 else if ( tid == typeid(bool(1)) ) return "bool" ;
00030 else if ( tid == typeid(string("")) ) return "string" ;
00031 else if ( tid == typeid("") ) return "string" ;
00032 else if ( tid == typeid(__ivec__) ) return "vector_int" ;
00033 else if ( tid == typeid(__fvec__) ) return "vector_float" ;
00034 else if ( tid == typeid(__dvec__) ) return "vector_double";
00035 else if ( tid == typeid(__bvec__) ) return "vector_bool" ;
00036 else if ( tid == typeid(__svec__) ) return "free_text" ;
00037 else if ( tid == typeid(__pmap__) ) return "instance" ;
00038 else if ( tid == typeid(__pmap__) ) return "parameter_map";
00039 else if ( tid == typeid(__svec__) ) return "vector_string";
00040 else if ( tid == typeid(__dmat__) ) return "matrix_double";
00041 cerr << "Type "<<tid.name()<<" is not supported!" << endl;
00042 return "";
00043 }
00044
00045 boost::any stoany( const string& type, const string& instring ){
00046 if ( type == "int" ) return boost::any(simple_lexical_cast<int>(instring));
00047 else if ( type == "float" ) return boost::any(simple_lexical_cast<float>(instring));
00048 else if ( type == "double" ) return boost::any(simple_lexical_cast<double>(instring));
00049 else if ( type == "bool" ) return boost::any(simple_lexical_cast<bool>(instring));
00050 else if ( type == "string" ) return boost::any( instring );
00051 else if ( type == "vector_int" ) return boost::any(simple_vector_lexical_cast< int >(instring));
00052 else if ( type == "vector_float" ) return boost::any(simple_vector_lexical_cast< float >(instring));
00053 else if ( type == "vector_double" ) return boost::any(simple_vector_lexical_cast< double >(instring));
00054 else if ( type == "vector_bool" ) return boost::any(simple_vector_lexical_cast< bool >(instring));
00055 else if ( type == "free_text" ) return boost::any( split(instring,"\n") );
00056 else if ( type == "vector_string" ) return boost::any(simple_vector_lexical_cast< string >(instring));
00057 else if ( type == "instance" ) return boost::any(simple_lexical_cast< parameterMap >(instring));
00058 else if ( type == "parameter_map" ) return boost::any(simple_lexical_cast< parameterMap >(instring));
00059 else if ( type == "matrix_double" ) {
00060 vector< string > rows = split( instring, "\n" );
00061 vector< vector< double > > result;
00062 for ( vector< string >::iterator it = rows.begin(); it!=rows.end(); ++it )
00063 result.push_back( simple_vector_lexical_cast< double >(*it) );
00064 return boost::any(result);
00065 }
00066 cerr << "Making a boost::any out of type "+type+" is not supported!" << endl;
00067 return boost::any(0);
00068 }
00069
00070
00071
00072
00074 istream& operator>>( istream& i, vector< string > V ){
00075 string line;
00076 while (i.good()) {
00077 getline(i,line);
00078 if (line.find(END_BLOCK) == string::npos)
00079 V.push_back( line );
00080 else return i;
00081 }
00082 return i;
00083 }
00084
00086 ostream& operator<<(ostream& o, vector< string > v){
00087 vector< string >::iterator it;
00088 for ( it=v.begin(); it!=v.end(); ++it ){
00089 o << (*it);
00090 ++it; if (it!=v.end()) o << endl; --it;
00091 }
00092 return o;
00093 }
00094
00096 istream& operator>>( istream& in_stream, parameterMap& p ){
00097 while ( in_stream ){
00098
00099 string line = get_prepped_line(in_stream,COMMENT_STRING);
00100 if ( line != "" ){
00101
00102 if ( line.find(END_BLOCK) != string::npos ) {return in_stream;}
00103
00104 if ( line.find(BEGIN_BLOCK) == string::npos ) {
00105 CParserObject parsed_line(line);
00106 if (p.hasKey(parsed_line.key)) p.erase(parsed_line.key);
00107 p.insert( make_pair( parsed_line.key, parsed_line.value ) );
00108
00109 } else {
00110 CParserObject parsed_first_line(line);
00111 vector< string > block;
00112 bool found_end(false);
00113 while ( in_stream.good() && !found_end ) {
00114 line = get_prepped_line( in_stream, COMMENT_STRING );
00115 found_end = ( line.find(END_BLOCK) != string::npos );
00116 if ( (line!="") && !found_end ) block.push_back(line);
00117 }
00118 CParserObject parsed_block( parsed_first_line.type, parsed_first_line.key, join(block,"\n") );
00119 if (p.hasKey(parsed_block.key)) p.erase(parsed_block.key);
00120 p.insert( make_pair( parsed_block.key, parsed_block.value ) );
00121 }
00122 }
00123 }
00124 return in_stream;
00125 }
00126
00128 ostream& operator<<( ostream& out_stream, const parameterMap& p ){
00129 parameterMap::const_iterator itr;
00130 for (itr=p.begin();itr!=p.end();++itr){
00131 string type = get_type_string(itr->second.type());
00132 string key = itr->first;
00133 boost::any value = itr->second;
00134 out_stream << "\n"<< type << " " << key << " " << flush;
00135
00136 if ( type == "int" ) out_stream << boost::any_cast<int>(value);
00137 else if ( type == "float" ) out_stream << boost::any_cast<float>(value);
00138 else if ( type == "double" ) out_stream << boost::any_cast<double>(value);
00139 else if ( type == "bool" ) out_stream << boolalpha << boost::any_cast<bool>(value);
00140 else if ( type == "string" ) out_stream << boost::any_cast<string>(value);
00141
00142 else {
00143 out_stream << BEGIN_BLOCK << flush;
00144 if ( type == "vector_int" ) out_stream << "\n" << boost::any_cast< vector< int > >(value);
00145 else if ( type == "vector_float" ) out_stream << "\n" << boost::any_cast< vector< float > >(value);
00146 else if ( type == "vector_double" ) out_stream << "\n" << boost::any_cast< vector< double > >(value);
00147 else if ( type == "vector_bool" ) out_stream << "\n" << boolalpha << boost::any_cast< vector< bool > >(value);
00148 else if ( type == "free_text" ) out_stream << "\n" << boost::any_cast< vector< string > >(value);
00149 else if ( type == "vector_string" ) out_stream << "\n" << boost::any_cast< vector< string > >(value);
00150 else if ( type == "matrix_double" ) out_stream << "\n" << boost::any_cast< vector< vector< double > > >(value);
00151 else if ( type == "instance" ) out_stream << boost::any_cast< parameterMap >(value);
00152 else if ( type == "parameter_map" ) out_stream << boost::any_cast< parameterMap >(value);
00153 out_stream << "\n" << END_BLOCK << flush;
00154 }
00155 }
00156 return out_stream;
00157 }
00158
00159
00160
00161
00162 CParserObject::CParserObject( const string& instring ){
00163 vector< string > split_line = split(instring," ");
00164 type = split_line[0];
00165 key = split_line[1];
00166 string rest = instring.substr(instring.find(key)+key.size());
00167 if ( remove_extra_blanks(rest) == BEGIN_BLOCK ) {
00168 value = boost::any(0);
00169 return;
00170 }
00171 value = boost::any(stoany(type,remove_extra_blanks(rest)));
00172 }
00173