proj home

Files   Classes   Functions   Hierarchy  

stringserialization.h

Go to the documentation of this file.
00001 #ifndef STRINGSERIALIZATION_H
00002 #define STRINGSERIALIZATION_H
00003 
00004 #include <string>
00005 #include <sstream>
00006 #include <vector>
00007 using namespace std;
00008 
00009 #include <typedefs.h>
00010 
00011 //  String serialization classes.
00012 //  See http://en.wikipedia.org/wiki/Serialization 
00013 
00017 class vectorstring
00018 {
00019 public:
00020 
00022   template< typename T >
00023   static boolc serialize(string & str, vector<T> const & v);
00025   template< typename T >
00026   static boolc deserialize(vector<T> & v, stringc & str);
00027 
00028   template< typename T >
00029   static void serializeInverse(vector<T> & v, stringc & str)
00030     { deserialize(v,str); };
00031  
00032 
00033 };
00034 
00038 class filestring
00039 {
00040 public:
00041 
00043   static boolc serialize(string & str, stringc & filename);
00045   static boolc deserialize(stringc & filename, stringc & str);
00047   static boolc serializeInverse(stringc & filename, stringc & str);
00048 };
00049 
00053 class vectorfile
00054 {
00055 public:
00056 
00058   template< typename T >
00059   static boolc serialize(string & filename, vector<T> const & v);
00061   template< typename T >
00062   static boolc deserialize(vector<T> & v, stringc & filename);
00063 
00064 };
00065 
00071 class htmlstring
00072 {
00073 public:
00074 
00075   static string lessthan;
00076   static string greaterthan;
00077   static string amp;
00078   static string quot;
00079 
00081   static boolc serialize(string & str, stringc & html );
00082   static boolc serializeInverse(string & html, stringc & str );
00083 
00085   static boolc ishtml(stringc & str);
00086 
00087   
00088 
00089 
00090 };
00091 
00092 
00093 
00094 
00095 //---------------------------------------------------------
00096 //  Implementation.
00097 
00098 template< typename T >
00099 boolc vectorstring::serialize(string & str, vector<T> const & v)
00100 {
00101   uintc vsz=v.size();
00102   str="";
00103   stringc space(" ");
00104   
00105   for (uint i=0; i<vsz; ++i)
00106   {
00107     stringstream targ;
00108     targ << v[i];
00109     if (targ.good()==false)
00110       return false;
00111    
00112     str += (targ.str()+space);
00113   }
00114 
00115   return true;
00116 }
00117 
00118 template< typename T >
00119 boolc vectorstring::deserialize(vector<T> & v, stringc & str)
00120 {
00121   stringstream targ(str);
00122   if (targ.good()==false)
00123     return false;
00124 
00125   bool process=true;
00126   T val;
00127   for (;process; )
00128   {
00129     targ >> val;
00130     // The end of input must fail.
00131     if (targ.fail())
00132     {
00133       // If it is not the end of the data stream return false.
00134       if (!targ.eof())
00135         return false;
00136 
00137       // Do not add the last element twice.
00138       process=false;
00139     }
00140     else
00141       v.push_back(val);
00142   }
00143   
00144   return true;
00145 }
00146 
00147 template< typename T >
00148 boolc vectorfile::serialize(string & filename, vector<T> const & v)
00149 {
00150   bool result;
00151   string str;
00152   result=vectorstring::serialize(str,v);
00153   if (result==false)
00154     return false;
00155 
00156   result=filestring::deserialize(filename,str);
00157   if (result==false)
00158     return false;
00159 
00160   return true;
00161 }
00162 
00163 template< typename T >
00164 boolc vectorfile::deserialize(vector<T> & v, stringc & filename)
00165 {
00166   bool result;
00167   string str;
00168   result=filestring::serialize(str,filename);
00169   if (result==false)
00170     return false;
00171 
00172   result=vectorstring::deserialize(v,str);
00173   if (result==false)
00174     return false;
00175 
00176   return true;
00177 }
00178 
00179 
00180 #endif
00181 

Generated on Fri Mar 4 00:49:29 2011 for Chelton Evans Source by  doxygen 1.5.8