Update of /cvsroot/pure-data/externals/PDContainer/include In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11891/include
Modified Files: GlobalStuff.h MapBase.h SequBase.h SimpleBase.h Log Message: added new xml parser
Index: GlobalStuff.h =================================================================== RCS file: /cvsroot/pure-data/externals/PDContainer/include/GlobalStuff.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** GlobalStuff.h 29 Oct 2004 11:56:13 -0000 1.3 --- GlobalStuff.h 5 May 2005 23:00:42 -0000 1.4 *************** *** 33,37 ****
// current version ! #define PDC_VERSION "0.0.1pre"
--- 33,42 ----
// current version ! #define PDC_VERSION "0.1" ! ! ! // TinyXML ! //#define TIXML_USE_STL ! #include "tinyxml/tinyxml.h"
Index: SequBase.h =================================================================== RCS file: /cvsroot/pure-data/externals/PDContainer/include/SequBase.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** SequBase.h 29 Oct 2004 11:56:13 -0000 1.2 --- SequBase.h 5 May 2005 23:00:55 -0000 1.3 *************** *** 108,111 **** --- 108,118 ---- */ virtual bool readFromFile2(string filename, int index); + + /* reads from a XML input file and adds the data to + * the current namespace + * index: inserts the data starting with this index + * returns true on success + */ + virtual bool readFromFile2XML(string filename, int index); };
*************** *** 145,149 **** { post("%s, read: wrong index !!",dataname_.c_str()); ! return true; }
--- 152,156 ---- { post("%s, read: wrong index !!",dataname_.c_str()); ! return false; }
*************** *** 216,219 **** --- 223,341 ---- }
+ //---------------------------------------------------- + /* reads the data from the XML file into the current + * namespace + * index: inserts the data starting with this index + * returns true on success + */ + template<class ContainerType, class ContTypeIterator> + bool SequBase<ContainerType,ContTypeIterator>::readFromFile2XML(string filename, int index) + { + int max_index = data_[h_namespace_].size(); + if(index < 0 || index >= max_index) + { + post("%s, read: wrong index !!",dataname_.c_str()); + return false; + } + + + TiXmlDocument doc( filename.c_str() ); + + if( !doc.LoadFile() ) return false; + + TiXmlNode *parent = 0; + TiXmlElement *child1 = 0; + TiXmlElement *child2 = 0; + + t_atom *el_atom = 0; + Element el; + t_float f; + bool parsed=false; + + + // Get the <PDContainer> tag and check type + parent = doc.FirstChild( "PDContainer" ); + if(!parent) return false; + + if(!parent->ToElement()) return false; + if(!parent->ToElement()->Attribute("type")) + { + post("readXML: you must specify an attribute type in <PDContainer> !"); + return false; + } + + string type(parent->ToElement()->Attribute("type")); + + if( type != "h_vector" && type != "h_list" && type != "h_deque" && + type != "h_set" && type != "h_multiset") + { + post("readXML: wrong container type (attribute type in <PDContainer>) !"); + return false; + } + + if( type != dataname_ ) + post("readXML: importing data from %s!", type.c_str() ); + + // iterate through all the <element> tags + for( child1 = parent->FirstChildElement("element"); child1; + child1 = child1->NextSiblingElement("element") ) + { + // get nr of atoms and allocate mem for them + // (if its a pd list) + int atoms = 0; + for( child2 = child1->FirstChildElement(); child2; + child2 = child2->NextSiblingElement() ) + atoms++; + + el_atom = new t_atom[atoms]; + if(el_atom == NULL) + { + post("Fatal Error Out Of Memory (%s-readFromFile)",dataname_.c_str()); + return false; + } + + // iterate through all the atoms of one <element> + atoms = 0; + for( child2 = child1->FirstChildElement(); child2; + child2 = child2->NextSiblingElement() ) + { + string tag(child2->Value()); + + if(!child2->FirstChild()) continue; + istringstream in(child2->FirstChild()->Value()); + + if(tag == "f" || tag == "float") + { + in >> f; + SETFLOAT(&el_atom[atoms], f); + } + if(tag == "s" || tag == "symbol") + { + SETSYMBOL(&el_atom[atoms], + gensym(const_cast<char*>(in.str().c_str()))); + } + + atoms++; + } + + if(!atoms) continue; + + + // add the element to the container + el.length = atoms; + el.atom = (t_atom*)copybytes(el_atom, atoms * sizeof(t_atom)); + + // insert the data + data_[h_namespace_][index] = el; + index++; + + delete[] el_atom; + + parsed = true; + } + return parsed; + + } +
Index: MapBase.h =================================================================== RCS file: /cvsroot/pure-data/externals/PDContainer/include/MapBase.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** MapBase.h 25 Oct 2004 19:02:30 -0000 1.1.1.1 --- MapBase.h 5 May 2005 23:00:55 -0000 1.2 *************** *** 14,18 **** #define _map_base_h__
- #include "include/ContainerBase.h"
--- 14,17 ---- *************** *** 84,87 **** --- 83,93 ---- */ virtual bool saveToFile(string filename); + + /* saves all the data of the current namespace to a XML file + * ATTENTION: if the file exists, all the old data of + * the file is lost + * returns true on success + */ + virtual bool saveToFileXML(string filename);
/* reads from an input file and adds the data to *************** *** 91,94 **** --- 97,106 ---- */ virtual bool readFromFile(string filename); + + /* reads from an XML input file and adds the data to + * the current namespace + * returns true on success + */ + virtual bool readFromFileXML(string filename); };
*************** *** 222,225 **** --- 234,308 ----
//---------------------------------------------------- + /* saves all the data of the current namespace to a XML file + * ATTENTION: if the file exists, all the old data of + * the file is lost + * returns true on success + */ + template<class ContainerType, class ContTypeIterator> + bool MapBase<ContainerType,ContTypeIterator>::saveToFileXML(string filename) + { + ostringstream output(""); + ContTypeIterator iter = data_[h_namespace_].begin(); + + // add XML Header: + output << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" + << "<!DOCTYPE PDContainer SYSTEM " + << "\"http://grh.mur.at/software/pdcontainer_multi.dtd\">\n" + << "<PDContainer type="" << dataname_ << "">\n"; + + while(iter != data_[h_namespace_].end()) + { + output << "<element>\n"; + + // add Key: + Element key((*iter).first); + + output << "<key>\n"; + + for (int i=0; i < key.length; i++) + { + if (key.atom[i].a_type == A_FLOAT) + output << "<f> " << key.atom[i].a_w.w_float << " </f>\n"; + if (key.atom[i].a_type == A_SYMBOL) + output << "<s>" << key.atom[i].a_w.w_symbol->s_name << " </s>\n"; + } + + output << "</key>\n"; + + + // add Value: + Element el((*iter).second); + + output << "<value>\n"; + + for (int i=0; i < el.length; i++) + { + if (el.atom[i].a_type == A_FLOAT) + output << "<f> " << el.atom[i].a_w.w_float << " </f>\n"; + if (el.atom[i].a_type == A_SYMBOL) + output << "<s>" << el.atom[i].a_w.w_symbol->s_name << " </s>\n"; + } + + output << "</value>\n"; + + output << "</element>\n"; + + iter++; + } + + output << "</PDContainer>\n"; + + // now write to file: + TiXmlDocument outfile( filename.c_str() ); + outfile.Parse( output.str().c_str() ); + + if ( outfile.Error() ) return false; + + outfile.SaveFile(); + + return true; + } + + //---------------------------------------------------- /* reads the data from the file into the current * namespace *************** *** 366,369 **** --- 449,608 ---- }
+ //---------------------------------------------------- + /* reads the data from the XML file into the current + * namespace + * returns true on success + */ + template<class ContainerType, class ContTypeIterator> + bool MapBase<ContainerType,ContTypeIterator>::readFromFileXML(string filename) + { + TiXmlDocument doc( filename.c_str() ); + + if( !doc.LoadFile() ) return false; + + TiXmlNode *parent = 0; + TiXmlElement *child1 = 0; + TiXmlElement *child2 = 0; + TiXmlElement *child3 = 0; + + t_atom *key_atom = 0; + t_atom *val_atom = 0; + Element key; + Element el; + t_float f; + bool parsed=false; + + + // Get the <PDContainer> tag and check type + parent = doc.FirstChild( "PDContainer" ); + if(!parent) return false; + + if(!parent->ToElement()) return false; + if(!parent->ToElement()->Attribute("type")) + { + post("readXML: you must specify an attribute type in <PDContainer> !"); + return false; + } + + string type(parent->ToElement()->Attribute("type")); + if( type != "h_map" && type != "h_multimap" ) + { + post("readXML: wrong container type (attribute type in <PDContainer>) !"); + return false; + } + + if( type != dataname_ ) + post("readXML: importing data from %s!", type.c_str() ); + + // iterate through all the <element> tags + for( child1 = parent->FirstChildElement("element"); child1; + child1 = child1->NextSiblingElement("element") ) + { + // get the <key> tag + child2 = child1->FirstChildElement( "key" ); + if(!child2) return false; + + // get nr of keys and allocate mem for them + // (if its a pd list) + int key_count = 0; + for( child3 = child2->FirstChildElement(); child3; + child3 = child3->NextSiblingElement() ) + key_count++; + + key_atom = new t_atom[key_count]; + if(key_atom == NULL) + { + post("Fatal Error Out Of Memory (%s-readFromFile)",dataname_.c_str()); + return false; + } + + // iterate through all the atoms of <key> + key_count = 0; + for( child3 = child2->FirstChildElement(); child3; + child3 = child3->NextSiblingElement() ) + { + string tag(child3->Value()); + + if(!child3->FirstChild()) continue; + istringstream in(child3->FirstChild()->Value()); + + if(tag == "f" || tag == "float") + { + in >> f; + SETFLOAT(&key_atom[key_count], f); + } + if(tag == "s" || tag == "symbol") + { + SETSYMBOL(&key_atom[key_count], + gensym(const_cast<char*>(in.str().c_str()))); + } + + key_count++; + } + + if(!key_count) continue; + + //---------------------- + + // get the <value> tag + child2 = child1->FirstChildElement( "value" ); + if(!child2) return false; + + // get nr of values and allocate mem for them + // (if its a pd list) + int val_count = 0; + for( child3 = child2->FirstChildElement(); child3; + child3 = child3->NextSiblingElement() ) + val_count++; + + val_atom = new t_atom[val_count]; + if(val_atom == NULL) + { + post("Fatal Error Out Of Memory (%s-readFromFile)",dataname_.c_str()); + return false; + } + + // iterate through all the atoms of <value> + val_count = 0; + for( child3 = child2->FirstChildElement(); child3; + child3 = child3->NextSiblingElement() ) + { + string tag(child3->Value()); + + if(!child3->FirstChild()) continue; + istringstream in(child3->FirstChild()->Value()); + + if(tag == "f" || tag == "float") + { + in >> f; + SETFLOAT(&val_atom[val_count],f); + } + if(tag == "s" || tag == "symbol") + { + SETSYMBOL(&val_atom[val_count], + gensym(const_cast<char*>(in.str().c_str()))); + } + + val_count++; + } + + if(!val_count) continue; + + // add the element to the container + key.length = key_count; + key.atom = (t_atom*)copybytes(key_atom, key_count * sizeof(t_atom)); + + el.length = val_count; + el.atom = (t_atom*)copybytes(val_atom, val_count * sizeof(t_atom)); + + data_[h_namespace_].insert(std::pair<Element,Element>(key,el)); + + delete[] key_atom; + delete[] val_atom; + + parsed = true; + } + return parsed; + }
Index: SimpleBase.h =================================================================== RCS file: /cvsroot/pure-data/externals/PDContainer/include/SimpleBase.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** SimpleBase.h 25 Oct 2004 19:02:30 -0000 1.1.1.1 --- SimpleBase.h 5 May 2005 23:00:55 -0000 1.2 *************** *** 71,74 **** --- 71,81 ---- */ virtual bool saveToFile(string filename); + + /* saves all the data of the current namespace to a XML file + * ATTENTION: if the file exists, all the old data of + * the file is lost + * returns true on success + */ + virtual bool saveToFileXML(string filename);
/* reads from an input file and adds the data to *************** *** 78,81 **** --- 85,94 ---- */ virtual bool readFromFile(string filename); + + /* reads from an input XML file and adds the data to + * the current namespace + * returns true on success + */ + virtual bool readFromFileXML(string filename); };
*************** *** 105,109 ****
if (key.length > 1) // list ! { output << "list "; for (int i=0; i < key.length; i++) --- 118,122 ----
if (key.length > 1) // list ! { output << "list "; for (int i=0; i < key.length; i++) *************** *** 233,236 **** --- 246,301 ----
//---------------------------------------------------- + /* saves all the data of the current namespace to a XML file + * ATTENTION: if the file exists, all the old data of + * the file is lost + * returns true on success + */ + template<class ContainerType, class ContTypeIterator> + bool SimpleBase<ContainerType,ContTypeIterator>::saveToFileXML(string filename) + { + ostringstream output(""); + ContTypeIterator iter = data_[h_namespace_].begin(); + + // add XML Header: + output << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" + << "<!DOCTYPE PDContainer SYSTEM " + << "\"http://grh.mur.at/software/pdcontainer_simple.dtd\">\n" + << "<PDContainer type="" << dataname_ << "">\n"; + + + while(iter != data_[h_namespace_].end()) + { + output << "<element>\n"; + + // add Element: + Element el((*iter)); + + for (int i=0; i < el.length; i++) + { + if (el.atom[i].a_type == A_FLOAT) + output << "<f> " << el.atom[i].a_w.w_float << " </f>\n"; + if (el.atom[i].a_type == A_SYMBOL) + output << "<s>" << el.atom[i].a_w.w_symbol->s_name << " </s>\n"; + } + + output << "</element>\n"; + + iter++; + } + + output << "</PDContainer>\n"; + + // now write to file: + TiXmlDocument outfile( filename.c_str() ); + outfile.Parse( output.str().c_str() ); + + if ( outfile.Error() ) return false; + + outfile.SaveFile(); + + return true; + } + + //---------------------------------------------------- /* reads the data from the file into the current * namespace *************** *** 328,331 **** --- 393,500 ---- }
+ //---------------------------------------------------- + /* reads the data from th XML file into the current + * namespace + * returns true on success + */ + template<class ContainerType, class ContTypeIterator> + bool SimpleBase<ContainerType,ContTypeIterator>::readFromFileXML(string filename) + { + TiXmlDocument doc( filename.c_str() ); + + if( !doc.LoadFile() ) return false; + + TiXmlNode *parent = 0; + TiXmlElement *child1 = 0; + TiXmlElement *child2 = 0; + + t_atom *el_atom = 0; + Element el; + t_float f; + bool parsed=false; + + + // Get the <PDContainer> tag and check type + parent = doc.FirstChild( "PDContainer" ); + if(!parent) return false; + + if(!parent->ToElement()) return false; + if(!parent->ToElement()->Attribute("type")) + { + post("readXML: you must specify an attribute type in <PDContainer> !"); + return false; + } + + string type(parent->ToElement()->Attribute("type")); + + if( type != "h_vector" && type != "h_list" && type != "h_deque" && + type != "h_set" && type != "h_multiset") + { + post("readXML: wrong container type (attribute type in <PDContainer>) !"); + return false; + } + + if( type != dataname_ ) + post("readXML: importing data from %s!", type.c_str() ); + + // iterate through all the <element> tags + for( child1 = parent->FirstChildElement("element"); child1; + child1 = child1->NextSiblingElement("element") ) + { + // get nr of atoms and allocate mem for them + // (if its a pd list) + int atoms = 0; + for( child2 = child1->FirstChildElement(); child2; + child2 = child2->NextSiblingElement() ) + atoms++; + + el_atom = new t_atom[atoms]; + if(el_atom == NULL) + { + post("Fatal Error Out Of Memory (%s-readFromFile)",dataname_.c_str()); + return false; + } + + // iterate through all the atoms of one <element> + atoms = 0; + for( child2 = child1->FirstChildElement(); child2; + child2 = child2->NextSiblingElement() ) + { + string tag(child2->Value()); + + if(!child2->FirstChild()) continue; + istringstream in(child2->FirstChild()->Value()); + + if(tag == "f" || tag == "float") + { + in >> f; + SETFLOAT(&el_atom[atoms], f); + } + if(tag == "s" || tag == "symbol") + { + SETSYMBOL(&el_atom[atoms], + gensym(const_cast<char*>(in.str().c_str()))); + } + + atoms++; + } + + if(!atoms) continue; + + + // add the element to the container + el.length = atoms; + el.atom = (t_atom*)copybytes(el_atom, atoms * sizeof(t_atom)); + + // insert it in the container + data_[h_namespace_].insert(data_[h_namespace_].end(),el); + + delete[] el_atom; + + parsed = true; + } + return parsed; + } +