libcvd-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libcvd-members] gvars3 gvars3/serialize.h src/serialize.cc


From: Edward Rosten
Subject: [libcvd-members] gvars3 gvars3/serialize.h src/serialize.cc
Date: Mon, 20 Apr 2009 14:35:07 +0000

CVSROOT:        /cvsroot/libcvd
Module name:    gvars3
Changes by:     Edward Rosten <edrosten>        09/04/20 14:35:07

Modified files:
        gvars3         : serialize.h 
        src            : serialize.cc 

Log message:
        Better generic serialization. Works for strings, vector<> and 
        vector<vector<>>. 
        
        vector makes successive calls to from_stream(). from_stream() defaults
        to operator>>(isteram, ...). 
        
        strings can be delinated with "".
        
        vector and vector<vector<>> wil lbe used for Vector and Matrix.
        
        Not yet hooked in properly.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gvars3/gvars3/serialize.h?cvsroot=libcvd&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/gvars3/src/serialize.cc?cvsroot=libcvd&r1=1.12&r2=1.13

Patches:
Index: gvars3/serialize.h
===================================================================
RCS file: /cvsroot/libcvd/gvars3/gvars3/serialize.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- gvars3/serialize.h  20 Apr 2009 12:59:20 -0000      1.10
+++ gvars3/serialize.h  20 Apr 2009 14:35:06 -0000      1.11
@@ -23,6 +23,7 @@
 #define GV3_INC_SERIALIZE_H
 #include <gvars3/config.h>
 #include <string>
+#include <vector>
 #include <sstream>
 
 #ifdef GVARS3_HAVE_TOON
@@ -47,10 +48,108 @@
                        return o.str();
                }
 
+               template<class T> std::istream& from_stream(std::istream& i, T& 
result)
+               {       
+                       i >> result;
+                       return i;
+               }
+               
+               //Special reading of strings
+               std::istream& from_stream(std::istream& in, std::string& s);
+
+               template<class T> std::istream& from_stream(std::istream& in, 
std::vector<T>& v)
+               {
+                       using std::ws;
+                       using std::ios;
+                       v.clear();
+                       in >> ws;
+                       int c;
+
+                       if((c = in.get()) == EOF)
+                               return in;
+
+                       if(c != '[')
+                       {
+                               in.setstate(ios::failbit);
+                               return in;
+                       }
+
+                       for(;;)
+                       {
+                               in >> ws;
+                               
+                               c = in.get();
+                               
+                               if(c == EOF || c == ']') 
+                                       return in;
+
+                               in.unget();
+
+                               T val;
+                               from_stream(in, val);
+
+                               if(!in.fail() && !in.bad())
+                                       v.push_back(val);
+                               else
+                                       return in;
+                       }
+               }
+
+               template<class T> std::istream& from_stream(std::istream& in, 
std::vector<std::vector<T> >& v)
+               {
+                       using std::ws;
+                       using std::ios;
+                       v.clear();
+                       in >> ws;
+                       int c;
+
+                       if((c = in.get()) == EOF)
+                               return in;
+
+                       if(c != '[')
+                       {
+                               in.setstate(ios::failbit);
+                               return in;
+                       }
+
+                       std::vector<T> current;
+
+                       for(;;)
+                       {
+                               in >> ws;
+                               
+                               if((c = in.get()) == EOF || c == ']') 
+                               {
+                                       if(!current.empty())
+                                               v.push_back(current);
+                                       return in;
+                               }
+                               else if(c == ';')
+                               {
+                                       v.push_back(current);
+                                       current.clear();
+                               }
+                               else
+                                       in.unget();
+
+                               T val;
+                               from_stream(in, val);
+
+                               if(!in.fail() && !in.bad())
+                                       current.push_back(val);
+                               else
+                                       return in;
+                       }
+               }
+
+
+
+
                template<class T> int from_string(std::string s, T& result)
                {       
+                       using GVars3::serialize::from_stream;
                        std::istringstream i(s);
-                       i >> result;
+                       from_stream(i, result);
                        return check_stream(i);
                }
                

Index: src/serialize.cc
===================================================================
RCS file: /cvsroot/libcvd/gvars3/src/serialize.cc,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- src/serialize.cc    20 Apr 2009 13:31:21 -0000      1.12
+++ src/serialize.cc    20 Apr 2009 14:35:06 -0000      1.13
@@ -34,7 +34,7 @@
                return s;
        }
 
-       istream& get_string(istream& in, string& s)
+       istream& from_stream(istream& in, string& s)
        {       
                s.clear();
 
@@ -50,60 +50,40 @@
                if(c == '"')
                        quoted=1;
 
+               //This variable holds an escape sequence in progress.
+               //empty means no escaping.
+               string escape;
 
-
-       }
-
-        // For reading strings, if there's double quotes, lift the part inside 
the double quotes.
-        int from_string(std::string s, std::string& so) //Pretty ugly code 
lifted from GVars2, but works.
+               for(;;)
        {
-         unsigned char c;
-         int nPos=0;
-         int nLength = s.length();
+                       c = in.get();
+                       if(c == EOF || (quoted && escape.empty() && c == '"'))
+                               break;
+                       else if(escape.empty() && c == '\\')
+                               escape = "\\";
+                       else if(!escape.empty())
+                               escape += c;
+                       else
+                               s += c;
          
-         // Find Start....
-         bool bFoundOpenQuote = false;
-         while((nPos < nLength) && !bFoundOpenQuote)
+                       //Check escapes
+                       if(escape == "\\\\")
            {
-             c = s[nPos];
-             if(c=='"') bFoundOpenQuote =true;
-             nPos++;
+                               s+="\\"; 
+                               escape.clear();
            }
-         if(!bFoundOpenQuote) // No quotes found - assume that the entire 
thing is good to go...
+                       else if(escape == "\\n")
            {
-             so = s;
-             return 0;
-           }
-         // Found opening ". Copy until end, or closing "
-         
-         std::string sNew("");
-         for (; nPos < nLength; ++nPos) {
-             char c = s[nPos];
-             if (c == '"') {
-                 ++nPos;
-                 break;
+                               s+="\n";
+                               escape.clear();
              }
-             if (nPos+1<nLength && c == '\\') {
-                 char escaped = s[++nPos];
-                 switch (escaped) {
-                 case 'n': c = '\n'; break;
-                 case 'r': c = '\r'; break;
-                 case 't': c = '\t'; break;
-                 default: c = escaped; break;
-                 }
-             }
-             sNew.push_back(c);
-         }
-         if (nPos < nLength) {
-             string rest;
-             from_string(s.substr(nPos), rest);
-             sNew += rest;
          }
 
-         so = sNew;
-         return 0;
-       }
+               //Append any trailing parts of an escape sequence
+               s += escape;
   
+               return in;
+       }
 
        int check_stream(std::istream& i)
        {
@@ -121,7 +101,6 @@
                                return -i.tellg();
                        }
                }
-
                return 0;
        }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]