// XMLReader.inline.hpp // // (C) 2006-2009 MicroNeil Research Corporation. // // This program is part of the MicroNeil Research Open Library Project. For // more information go to http://www.microneil.com/OpenLibrary/index.html // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for // more details. // // You should have received a copy of the GNU General Public License along with // this program; if not, write to the Free Software Foundation, Inc., 59 Temple // Place, Suite 330, Boston, MA 02111-1307 USA // See XMLReader.hpp for details //// XMLReader Element ///////////////////////////////////////////////////// inline XMLReaderElement::XMLReaderElement(const char* Name) : // Construct with a cstring. myName(std::string(Name)), myParent(NULL), myLine(0), myIndex(0), myLength(0), myCleanFlag(true), myInitOnInterpretFlag(false), myWasProcessed(false), wasProcessed(&myWasProcessed) { } inline XMLReaderElement::XMLReaderElement(const std::string Name) : // Construct with a c++ string. myName(Name), myParent(NULL), myLine(0), myIndex(0), myLength(0), myCleanFlag(true), myInitOnInterpretFlag(false), myWasProcessed(false), wasProcessed(&myWasProcessed) { } inline XMLReaderElement::XMLReaderElement( // Construct sub element w/ cstring. const char* Name, XMLReaderElement& Parent) : myName(std::string(Name)), myParent(&Parent), myLine(0), myIndex(0), myLength(0), myCleanFlag(true), myInitOnInterpretFlag(false), myWasProcessed(false), wasProcessed(&myWasProcessed) { } inline XMLReaderElement::XMLReaderElement( // Construct sub element w/ string. const std::string Name, XMLReaderElement& Parent) : myName(Name), myParent(&Parent), myLine(0), myIndex(0), myLength(0), myCleanFlag(true), myInitOnInterpretFlag(false), myWasProcessed(false), wasProcessed(&myWasProcessed) { } inline std::string XMLReaderElement::Name() { return myName; } // Get the name of this element. inline XMLReaderElement& XMLReaderElement::Parent() { // Get the parrent of this element. if(NULL != myParent) { // If I have a parent return (*myParent); // then I dereference and return it. } // If I don't have a parent return (*this); // then I return myself. } inline XMLReaderElement& XMLReaderElement::Parent( // Set the parrent of this element. XMLReaderElement& Parent) { // Given this parent myParent = &Parent; // I take and store it's address return (*myParent); // then dereference and return it. } inline int XMLReaderElement::Line() { return myLine; } // Get the last line number. inline int XMLReaderElement::Index() { return myIndex; } // Get the last data position. inline int XMLReaderElement::Length() { return myLength; } // Get the last length. inline void XMLReaderElement::notifyDirty() { myCleanFlag = false; } // Attributes do this when they change. inline XMLReaderElement& XMLReaderElement::Element(const char* Name) { // Add a new sub element by c string name. return Element(std::string(Name)); // Use the string name version } inline XMLReaderElement& XMLReaderElement::Element(const std::string Name) { // Add a new sub element by c++ string name. XMLReaderElement* N = new XMLReaderElement( // Create a new Element with the Name, // name provided and (*this)); // myself as the parent. myElements.push_back(N); // Add it to the list. return (*N); // Return the new element. } inline XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, XMLReaderTranslator& newTranslator) { // Add a Translator to this element. return Element(std::string(Name), newTranslator); // Use the string name version } inline XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, std::string& x, std::string init) { // Map to a string. return Element(std::string(Name), x, init); // Use the string name version } inline XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, int& x, int init, int radix) { // Map to an int. return Element(std::string(Name), x, init, radix); // Use the string name version } inline XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, double& x, double init) { // Map to a double. return Element(std::string(Name), x, init); // Use the string name version } inline XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, bool& x, bool init) { // Map to a boolean. return Element(std::string(Name), x, init); // Use the string name version } inline XMLReaderElement& XMLReaderElement::indicator(bool& x) { wasProcessed = &x; return *this; } inline XMLReaderElement& XMLReaderElement::End() { // Return this element's parent. return Parent(); // Borrow Parent() } inline XMLReaderElement& XMLReaderElement::End(const char* Name) { // Check the name and return the parent return End(std::string(Name)); // Borrow End(string) } inline XMLReaderElement& XMLReaderElement::End(const std::string Name) { // if the name is correct - or throw! if(0 != Name.compare(myName)) { // If Name is not myName throw EndNameDoesNotMatch(); // throw an exception! } // If the names match then return Parent(); // return the parent. } inline XMLReaderAttribute& XMLReaderElement::Attribute( // Add an attribute using a cstring. const char* Name) { // Given this cstring name return Attribute(std::string(Name)); // Convert it to a string and borrow } // Attribute(string) inline XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, XMLReaderTranslator& newTranslator) { // Add a Translator to this element. return Attribute(std::string(Name), newTranslator); // Borrow the string name version } inline XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, std::string& x, std::string init) { // Map to a string. return Attribute(std::string(Name), x, init); // Borrow the string name version } inline XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, int& x, int init, int radix) { // Map to an int. return Attribute(std::string(Name), x, init); // Borrow the string name version } inline XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, double& x, double init) { // Map to a double. return Attribute(std::string(Name), x, init); // Borrow the string name version } inline XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, bool& x, bool init) { // Map to a boolean. return Attribute(std::string(Name), x, init); // Borrow the string name version } inline XMLReaderElement& XMLReaderElement::setInitOnInterpret() { // Set the init on interpret flag. myInitOnInterpretFlag = true; // Set the flag. return(*this); // Dereference and return self. } inline XMLReaderElement& XMLReaderElement::atStartCall( // Add an atStart call-back. XMLerator& Functor) { // Given this Functor, myStartXMLerators.push_back(&Functor); // add it to my atStart list then return(*this); // dereference and return myself. } inline XMLReaderElement& XMLReaderElement::atEndCall( // Add an atEnd call-back. XMLerator& Functor) { // Given this Functor, myEndXMLerators.push_back(&Functor); // add it to my atEnd list then return(*this); // dereference and return myself. } inline XMLReaderElement& XMLReaderElement::Mnemonic( // Add a mnemonic using c strings. const char* name, const char* value) { // Given char* and char* return Mnemonic(std::string(name), std::string(value)); // make strings and borrow that method. } inline XMLReaderElement& XMLReaderElement::Mnemonic( // Add a mnemonic using mixed strings. const char* name, const std::string value) { // Given char* and string return Mnemonic(std::string(name), value); // make strings and borrow that method. } inline XMLReaderElement& XMLReaderElement::Mnemonic( // Add a mnemonic using mixed strings. const std::string name, const char* value) { // Given string and char* return Mnemonic(name, std::string(value)); // make strings and borrow that method. } inline XMLReaderElement& XMLReaderElement::Mnemonic( // Add a mnemonic using c++ strings. const std::string name, const std::string value) { // Givent string and string XMLReaderMnemonic* N = // Create a new Mnemonic new XMLReaderMnemonic(name, value); // using the values provided, myMnemonics.push_back(N); // add it to my list, then return(*this); // dereference and return myself. } //// XMLReader Attribute /////////////////////////////////////////////////// inline XMLReaderAttribute::XMLReaderAttribute( // Attributes are constructed with a const char* Name, XMLReaderElement& Parent) : // Name and a Parent. myName(std::string(Name)), // We convert the name to a string. myParent(Parent), // We just grab the parent. myLine(0), // Everything else gets zeroed. myIndex(0), myLength(0), myWasProcessed(false), wasProcessed(&myWasProcessed), attrFunc(nullptr) { } inline XMLReaderAttribute::XMLReaderAttribute( // Attributes are constrictued with a const std::string Name, XMLReaderElement& Parent) : // Name and a Parent. myName(Name), // We grab them and zero the rest. myParent(Parent), myLine(0), myIndex(0), myLength(0), myWasProcessed(false), wasProcessed(&myWasProcessed), attrFunc(nullptr) { } inline std::string XMLReaderAttribute::Name() { // Get the name of this attribute. return myName; } inline XMLReaderElement& XMLReaderAttribute::Parent() { // Get the parent of this attribute. return myParent; } inline int XMLReaderAttribute::Line() { // Get the last line number. return myLine; } inline int XMLReaderAttribute::Index() { // Get the last data position. return myIndex; } inline int XMLReaderAttribute::Length() { // Get the last length. return myLength; } inline XMLReaderElement& XMLReaderAttribute::Element( // Add a new sub element by c string name. const char* Name) { return myParent.Element(Name); } inline XMLReaderElement& XMLReaderAttribute::Element( // Add a new sub element by c++ string name. const std::string Name) { return myParent.Element(Name); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, XMLReaderTranslator& newTranslator) { // Add a Translator to this element. return myParent.Element(Name, newTranslator); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, std::string& x, std::string init) { // Map to a string. return myParent.Element(Name, x, init); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, int& x, int init, int radix) { // Map to an int. return myParent.Element(Name, x, init, radix); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, double& x, double init) { // Map to a double. return myParent.Element(Name, x, init); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const char* Name, // requires a name, of course, bool& x, bool init) { // Map to a boolean. return myParent.Element(Name, x, init); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const std::string Name, // requires a name, of course, XMLReaderTranslator& newTranslator) { // Add a Translator to this element. return myParent.Element(Name, newTranslator); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const std::string Name, // requires a name, of course, std::string& x, std::string init) { // Map to a string. return myParent.Element(Name, x, init); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const std::string Name, // requires a name, of course, int& x, int init, int radix) { // Map to an int. return myParent.Element(Name, x, init, radix); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const std::string Name, // requires a name, of course, double& x, double init) { // Map to a double. return myParent.Element(Name, x, init); } inline XMLReaderElement& XMLReaderAttribute::Element( // Mapping factory for convenience, const std::string Name, // requires a name, of course, bool& x, bool init) { // Map to a boolean. return myParent.Element(Name, x, init); } inline XMLReaderElement& XMLReaderAttribute::End() { // Return this element's parent. return myParent.End(); } inline XMLReaderElement& XMLReaderAttribute::End(const char* Name) { // Check the name and return the parent return myParent.End(Name); } inline XMLReaderElement& XMLReaderAttribute::End(const std::string Name) { // if the name is correct - or throw! return myParent.End(Name); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Add an attribute using a cstring. const char* Name) { return myParent.Attribute(Name); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Add an attribute using a c++ string. const std::string Name) { return myParent.Attribute(Name); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, XMLReaderTranslator& newTranslator) { // Add a Translator to this element. return myParent.Attribute(Name, newTranslator); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, std::string& x, std::string init) { // Map to a string. return myParent.Attribute(Name, x, init); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, int& x, int init, int radix) { // Map to an int. return myParent.Attribute(Name, x, init, radix); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, double& x, double init) { // Map to a double. return myParent.Attribute(Name, x, init); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const char* Name, // requires a name, of course, bool& x, bool init) { // Map to a boolean. return myParent.Attribute(Name, x, init); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const std::string Name, // requires a name, of course, XMLReaderTranslator& newTranslator) { // Add a Translator to this element. return myParent.Attribute(Name, newTranslator); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const std::string Name, // requires a name, of course, std::string& x, std::string init) { // Map to a string. return myParent.Attribute(Name, x, init); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const std::string Name, // requires a name, of course, int& x, int init, int radix) { // Map to an int. return myParent.Attribute(Name, x, init, radix); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const std::string Name, // requires a name, of course, double& x, double init) { // Map to a double. return myParent.Attribute(Name, x, init); } inline XMLReaderAttribute& XMLReaderAttribute::Attribute( // Mapping factory for convenience, const std::string Name, // requires a name, of course, bool& x, bool init) { // Map to a boolean. return myParent.Attribute(Name, x, init); } inline XMLReaderAttribute& XMLReaderAttribute::indicator(bool& x) { wasProcessed = &x; return *this; } inline XMLReaderAttribute& XMLReaderAttribute::indicator(bool& x, AttributeFunctor& f) { wasProcessed = &x; attrFunc = &f; return *this; } inline XMLReaderElement& XMLReaderAttribute::setInitOnInterpret() { // Set the init on interpret flag. return myParent.setInitOnInterpret(); } inline XMLReaderElement& XMLReaderAttribute::atStartCall( // Add an atStart call-back to this element. XMLerator& Functor) { return myParent.atStartCall(Functor); } inline XMLReaderElement& XMLReaderAttribute::atEndCall( // Add an atEnd call-back to this element. XMLerator& Functor) { return myParent.atEndCall(Functor); } inline XMLReaderAttribute& XMLReaderAttribute::Mnemonic( // Add a mnemonic using c strings. const char* name, const char* value) { // Given char* and char* return Mnemonic(std::string(name), std::string(value)); // make strings and borrow that method. } inline XMLReaderAttribute& XMLReaderAttribute::Mnemonic( // Add a mnemonic using mixed strings. const char* name, const std::string value) { // Given char* and string return Mnemonic(std::string(name), value); // make strings and borrow that method. } inline XMLReaderAttribute& XMLReaderAttribute::Mnemonic( // Add a mnemonic using mixed strings. const std::string name, const char* value) { // Given string and char* return Mnemonic(name, std::string(value)); // make strings and borrow that method. } inline XMLReaderAttribute& XMLReaderAttribute::Mnemonic( // Add a mnemonic using c++ strings. const std::string name, const std::string value) { // Givent string and string XMLReaderMnemonic* N = // Create a new Mnemonic new XMLReaderMnemonic(name, value); // using the values provided, myMnemonics.push_back(N); // add it to my list, then return(*this); // dereference and return myself. } //// XMLReader Data //////////////////////////////////////////////////////// inline char XMLReaderData::Data(int Index) { // Returns char from Data[Index] if(0 > Index || Index >= myBufferSize) { // Check that index is in range return 0; // and return 0 if it is not. } // If Index is within range then return myDataBuffer[Index]; // return the byte requested. } inline int XMLReaderData::Index() { // Reads the current Index. return myIndex; } inline int XMLReaderData::Index(int i) { // Changes the current Index. if(0 > i || i >= myBufferSize) { // If i is out of range then return myIndex; // return the current Index unchanged. } // If i is within range then myIndex = i; // change the Index to i and return myIndex; // return the changed Index. } inline int XMLReaderData::Line() { // Reads the current Line number. return myLine; } inline int XMLReaderData::addNewLines(int Count) { // Increments the Line number. myLine += Count; // Add the number of new lines. return myLine; // Return the current Line number. } //// XMLReader Translator ////////////////////////////////////////////////// inline StringTranslator::StringTranslator( // Construct this with std::string& Variable, // the variable to map, std::string Initializer) : // and the default value. myVariable(Variable), myInitializer(Initializer) { } inline void StringTranslator::translate(const char* Value) { // Provide a translation method. myVariable = std::string(Value); // String to String = simple copy. } inline void StringTranslator::initialize() { // Provide an initialization method. myVariable = myInitializer; // Revert to the initializer value. } inline IntegerTranslator::IntegerTranslator( // Construct this with int& Variable, // the variable to map, int Initializer, // and the default value. int Radix) : // For this one we also need a Radix. myVariable(Variable), myInitializer(Initializer), myRadix(Radix) { } inline void IntegerTranslator::translate(const char* Value) { // Provide a translation method. char* dummy; // Throw away ptr for strtol(). myVariable = strtol(Value, &dummy, myRadix); // Convert the string w/ strtol(). } inline void IntegerTranslator::initialize() { // Provide an initialization method. myVariable = myInitializer; // Revert to the initializer value. } inline DoubleTranslator::DoubleTranslator( // Construct this with double& Variable, // the variable to map, double Initializer) : // and the default value. myVariable(Variable), myInitializer(Initializer) { } inline void DoubleTranslator::translate(const char* Value) { // Provide a translation method. char* dummy; // Throw away ptr for strtod(). myVariable = strtod(Value, &dummy); // Convert the string w/ strtod(). } inline void DoubleTranslator::initialize() { // Provide an initialization method. myVariable = myInitializer; // Revert to the initializer value. } inline BoolTranslator::BoolTranslator( // Construct this with bool& Variable, // the variable to map, bool Initializer) : // and the default value. myVariable(Variable), myInitializer(Initializer) { } inline void BoolTranslator::translate(const char* Value) { // Provide a translation method. if( (0 == strcmp(Value,"on")) || (0 == strcmp(Value,"true")) || // on, true, yes, and 1 are (0 == strcmp(Value, "yes")) || // interpreted as a boolean true. (0 == strcmp(Value, "1")) ) { myVariable = true; } else { // Anything else is interpreted as myVariable = false; // boolean false. } } inline void BoolTranslator::initialize() { // Provide an initialization method. myVariable = myInitializer; // Revert to the initializer value. } inline void RawTranslator::setRawDataAssignment(std::string &RawData) { RawDataAssignment = &RawData; } inline void RawTranslator::setIndexAssignment(int &Index, int &Endex) { IndexAssignment = &Index; EndexAssignment = &Endex; } inline void RawTranslator::translate(int Index, int Endex, XMLReaderData &Data) { if (0 != RawDataAssignment) *RawDataAssignment = Data.extract(Index, Endex); if (0 != IndexAssignment) *IndexAssignment = Index; if (0 != EndexAssignment) *EndexAssignment = Endex; } //// XMLReader Mnemonic //////////////////////////////////////////////////// inline XMLReaderMnemonic::XMLReaderMnemonic( // To make one, provide both parts. std::string Name, std::string Value) : myName(Name), myValue(Value) { } inline bool XMLReaderMnemonic::test(std::string Name) { // Test to see if this Mnemonic matches. return (0 == Name.compare(myName)); // Return true if Name and myName match. } inline std::string XMLReaderMnemonic::Value() { // If it does then we will need it's value. return myValue; }