SimpleDemarshaller.hpp
00001 '/***************************************************************************
00002 tag: Peter Soetens Mon Jan 19 14:11:20 CET 2004 SimpleDemarshaller.hpp
00003
00004 SimpleDemarshaller.hpp - description
00005 -------------------
00006 begin : Mon January 19 2004
00007 copyright : (C) 2004 Peter Soetens
00008 email : peter.soetens@mech.kuleuven.ac.be
00009
00010 ***************************************************************************
00011 * This library is free software; you can redistribute it and/or *
00012 * modify it under the terms of the GNU General Public *
00013 * License as published by the Free Software Foundation; *
00014 * version 2 of the License. *
00015 * *
00016 * As a special exception, you may use this file as part of a free *
00017 * software library without restriction. Specifically, if other files *
00018 * instantiate templates or use macros or inline functions from this *
00019 * file, or you compile this file and link it with other files to *
00020 * produce an executable, this file does not by itself cause the *
00021 * resulting executable to be covered by the GNU General Public *
00022 * License. This exception does not however invalidate any other *
00023 * reasons why the executable file might be covered by the GNU General *
00024 * Public License. *
00025 * *
00026 * This library is distributed in the hope that it will be useful, *
00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
00029 * Lesser General Public License for more details. *
00030 * *
00031 * You should have received a copy of the GNU General Public *
00032 * License along with this library; if not, write to the Free Software *
00033 * Foundation, Inc., 59 Temple Place, *
00034 * Suite 330, Boston, MA 02111-1307 USA *
00035 * *
00036 ***************************************************************************/
00037
00038 #ifndef PI_PROPERTIES_SIMPLE_DEMARSHALLER
00039 #define PI_PROPERTIES_SIMPLE_DEMARSHALLER
00040
00041 #include "../Property.hpp"
00042
00043 #include <vector>
00044 #include <map>
00045 #include <iostream>
00046 #include <fstream>
00047
00048 namespace RTT
00049 {
00050
00056 class RTT_API SimpleDemarshaller : public Demarshaller
00057 {
00058 static const char TYPECODE_BOOL = 'B';
00059 static const char TYPECODE_CHAR = 'C';
00060 static const char TYPECODE_INT = 'I';
00061 static const char TYPECODE_DOUBLE = 'D';
00062 static const char TYPECODE_STRING = 'S';
00063
00064 public:
00065 typedef std::istream input_stream;
00066
00067 SimpleDemarshaller(const std::string& filename)
00068 {
00069 // FIXME first check if the target file exists...
00070 _is = new std::ifstream( filename.c_str() );
00071 }
00072
00073 virtual bool deserialize(PropertyBag &v)
00074 {
00075 using namespace std;
00076 #if 0
00077 char begin_token, end_token;
00078 *_is >> begin_token;
00079 if(begin_token == '{')
00080 cerr << "read begin";
00081 #endif
00082
00083 char c;
00084 while( _is->read(&c,1) )
00085 {
00086 unsigned char name_length, value_length;
00087 char name[256];
00088 char value[256];
00089 int intvalue;
00090 double doublevalue;
00091 _is->read(reinterpret_cast<char*>(&name_length),1);
00092 _is->read(name,name_length);
00093 name[name_length] = 0;
00094 _is->ignore();
00095 _is->read(reinterpret_cast<char*>(&value_length),1);
00096 _is->read(value,value_length);
00097 switch(c)
00098 {
00099 case TYPECODE_BOOL:
00100 if(value[0])
00101 v.add(new Property<bool>(name,"",false));
00102 else
00103 v.add(new Property<bool>(name,"",true));
00104 cerr << "bool: ";
00105 _is->ignore();
00106 break;
00107 case TYPECODE_CHAR:
00108 v.add(new Property<char>(name,"",value[0]));
00109 cerr << "char: ";
00110 _is->ignore();
00111 break;
00112 case TYPECODE_INT:
00113 value[value_length]=0;
00114 sscanf(value, "%d", &intvalue);
00115 v.add(new Property<int>(name,"",intvalue));
00116 cerr << "int: ";
00117 _is->ignore();
00118 break;
00119 case TYPECODE_DOUBLE:
00120 value[value_length]=0;
00121 sscanf(value, "%lf", &doublevalue);
00122 v.add(new Property<double>(name,"",doublevalue));
00123 cerr << "double: ";
00124 _is->ignore();
00125 break;
00126 case TYPECODE_STRING:
00127 value[value_length]=0;
00128 cerr << value<<"\n";
00129 v.add(new Property<string>(name,"",value));
00130 _is->ignore();
00131 cerr << "string: ";
00132 break;
00133 default:
00134 cerr << "lost synchro" << endl;
00135 }
00136 cerr << "\n"<<(int)name_length<<"\n"<<name<<"\n"<<(int)value_length<<endl;
00137 }
00138 #if 0
00139 for (
00140 vector<PropertyBase*>::iterator i = v._properties.begin();
00141 i != v._properties.end();
00142 i++ )
00143 {
00144 (*i)->serialize(*this);
00145 }
00146 #endif
00147 // _os <<"</Bag>\n";
00148
00149 #if 0
00150 *_is >> end_token;
00151 if(end_token == '}')
00152 cerr << "read end";
00153 #endif
00154 return true;
00155 }
00156
00157 protected:
00158 // Input stream
00159 input_stream * _is;
00160
00161
00162 };
00163 }
00164 #endif