Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[Manta] r1733 - in branches/persistent/Core: Color Persistent


Chronological Thread 
  • From: sparker@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [Manta] r1733 - in branches/persistent/Core: Color Persistent
  • Date: Fri, 21 Sep 2007 13:06:58 -0600 (MDT)

Author: sparker
Date: Fri Sep 21 13:06:58 2007
New Revision: 1733

Modified:
   branches/persistent/Core/Color/ColorSpace.h
   branches/persistent/Core/Persistent/ArchiveElement.h
   branches/persistent/Core/Persistent/XMLArchive.cc
   branches/persistent/Core/Persistent/stdClassInfo.h
Log:
Read colors and stl vectors


Modified: branches/persistent/Core/Color/ColorSpace.h
==============================================================================
--- branches/persistent/Core/Color/ColorSpace.h (original)
+++ branches/persistent/Core/Color/ColorSpace.h Fri Sep 21 13:06:58 2007
@@ -11,8 +11,10 @@
 #include <Core/Persistent/ClassInfo.h>
 
 #include <sgi_stl_warnings_off.h>
+#include <sstream>
 #include <string>
 #include <sgi_stl_warnings_on.h>
+#include <ctype.h>
 
 namespace Manta {
   template<typename Traits> class ColorSpace {
@@ -275,10 +277,63 @@
     static std::string getPublicClassname() {
       return "Color";
     }
+    static inline int hexvalue(char c){
+      if(c >= '0' && c <= '9')
+        return c-'0';
+      else if(c >= 'a' && c <= 'f')
+        return c-'a';
+      else if(c >= 'A' && c <= 'F')
+        return c-'A';
+      else
+        return 0;
+    }
     static void readwrite(ArchiveElement* archive, ColorSpace<Traits>& data) 
{
       if(archive->reading()){
         // Read a number of different color representations
-        NOT_FINISHED("ColorSpace::readwrite");
+        std::string str;
+        archive->readwrite("data", str);
+        if(str[0] == '#'){
+          // Parse html style hex string
+          int l = 0;
+          std::string::size_type idx = 1;
+          while(idx < str.size() && ((str[idx] >= '0' && str[idx] <= '9') || 
(str[idx] >= 'a' && str[idx] <= 'f') || (str[idx] >= 'A' && str[idx] <= 'F')))
+            idx++;
+
+          int r, g, b;
+          if(idx == 7){
+            r = (hexvalue(str[1])<<4) + hexvalue(str[2]);
+            g = (hexvalue(str[3])<<4) + hexvalue(str[4]);
+            b = (hexvalue(str[5])<<4) + hexvalue(str[6]);
+          } else if(idx == 4){
+            r = (hexvalue(str[1])<<4) + hexvalue(str[1]);
+            g = (hexvalue(str[2])<<4) + hexvalue(str[2]);
+            b = (hexvalue(str[3])<<4) + hexvalue(str[3]);
+          } else {
+            throw SerializationError("Error parsing rgb triple: " + str);
+          }
+          while(idx < str.size() && !isspace(str[idx]))
+            idx++;
+          if(idx != str.size())
+            throw SerializationError("Error parsing rgb triple: " + str);
+
+          Traits::convertFrom(data.data, RGB(r/255., g/255., b/255.));
+        } else if(str.substr(0, 4) == "rgb:"){
+          // RGB triple
+          std::istringstream in(str.substr(4));
+          float r,g,b;
+          in >> r >> g >> b;
+          if(!in)
+            throw SerializationError("Error parsing rgb value: " + str);
+          Traits::convertFrom(data.data, RGB(r, g, b));
+        } else {
+          // Try to parse as an rgb triple
+          std::istringstream in(str);
+          float r,g,b;
+          in >> r >> g >> b;
+          if(!in)
+            throw SerializationError("Unknown color value: " + str);
+          Traits::convertFrom(data.data, RGB(r, g, b));
+        }
       } else {
         Traits::readwrite(archive, data.data);
       }

Modified: branches/persistent/Core/Persistent/ArchiveElement.h
==============================================================================
--- branches/persistent/Core/Persistent/ArchiveElement.h        (original)
+++ branches/persistent/Core/Persistent/ArchiveElement.h        Fri Sep 21 
13:06:58 2007
@@ -37,6 +37,8 @@
     virtual void readwrite(const std::string& fieldname, std::string& data,
                            Persistent::StorageHint hint = 
Persistent::Default) = 0;
 
+    virtual ArchiveElement* firstChild() = 0;
+    virtual ArchiveElement* nextChild() = 0;
   protected:
     virtual ArchiveElement* findObject(const std::string& fieldname,
                                        std::string& classname,

Modified: branches/persistent/Core/Persistent/XMLArchive.cc
==============================================================================
--- branches/persistent/Core/Persistent/XMLArchive.cc   (original)
+++ branches/persistent/Core/Persistent/XMLArchive.cc   Fri Sep 21 13:06:58 
2007
@@ -107,6 +107,8 @@
                                       const std::string& classname,
                                       const std::string& refname);
 
+    virtual ArchiveElement* firstChild();
+    virtual ArchiveElement* nextChild();
   private:
     XMLArchive* archive;
     xmlNodePtr node;
@@ -253,7 +255,13 @@
 void XMLArchiveElement::readwrite(const std::string& fieldname, bool& data, 
Persistent::StorageHint hint)
 {
   if(reading()){
-    NOT_FINISHED("XMLArchiveElement::readwrite");
+    string str = readProperty(fieldname, hint);
+    if(str == "true" || str == "1" || str == "TRUE" || str == "yes")
+      data = true;
+    else if(str == "false" || str == "0" || str == "FALSE" || str == "no")
+      data = false;
+    else
+      throw SerializationError("Unknown boolean value: " + str);
   } else {
     writeProperty(fieldname, data?"true":"false", hint);
   }
@@ -262,7 +270,10 @@
 void XMLArchiveElement::readwrite(const std::string& fieldname, double& 
data, Persistent::StorageHint hint)
 {
   if(reading()){
-    NOT_FINISHED("XMLArchiveElement::readwrite");
+    istringstream in(readProperty(fieldname, hint));
+    in >> data;
+    if(!in)
+      throw SerializationError("Error parsing double: " + in.str());
   } else {
     ostringstream datastring;
     datastring.precision(17);
@@ -323,10 +334,38 @@
 void XMLArchiveElement::readwrite(const std::string& fieldname, std::string& 
data, Persistent::StorageHint hint)
 {
   if(reading()){
-    NOT_FINISHED("XMLArchiveElement::readwrite");
+    data = readProperty(fieldname, hint);
   } else {
     writeProperty(fieldname, data, hint);
   }
+}
+
+ArchiveElement* XMLArchiveElement::firstChild()
+{
+  if(isTagElement)
+    throw SerializationError("Object cannot be stored in a property");
+
+  xmlNode* child = node->children;
+  while(child && child->type != XML_ELEMENT_NODE)
+    child = child->next;
+  if(child)
+    return new XMLArchiveElement(archive, child);
+  else
+    return 0;
+}
+
+ArchiveElement* XMLArchiveElement::nextChild()
+{
+  if(isTagElement)
+    throw SerializationError("Object cannot be stored in a property");
+
+  xmlNode* child = node->next;
+  while(child && child->type != XML_ELEMENT_NODE)
+    child = child->next;
+  if(child)
+    return new XMLArchiveElement(archive, child);
+  else
+    return 0;
 }
 
 ArchiveElement* XMLArchiveElement::findObject(const std::string& fieldname,

Modified: branches/persistent/Core/Persistent/stdClassInfo.h
==============================================================================
--- branches/persistent/Core/Persistent/stdClassInfo.h  (original)
+++ branches/persistent/Core/Persistent/stdClassInfo.h  Fri Sep 21 13:06:58 
2007
@@ -54,7 +54,12 @@
     static void readwrite(ArchiveElement* archive, std::vector<T*>& data) {
       init.forceinit();
       if(archive->reading()){
-        NOT_FINISHED("vector readwrite");
+        data.resize(0);
+        for(ArchiveElement* child = archive->firstChild(); child != 0; child 
= child->nextChild()){
+          T* ptr;
+          archive->readwrite("element", ptr, Persistent::AnonymousField);
+          data.push_back(ptr);
+        }
       } else {
         for(typename std::vector<T*>::iterator iter = data.begin(); iter != 
data.end(); iter++)
           archive->readwrite("element", *iter, Persistent::AnonymousField);




  • [Manta] r1733 - in branches/persistent/Core: Color Persistent, sparker, 09/21/2007

Archive powered by MHonArc 2.6.16.

Top of page