#include "configuration.hpp" | #include "configuration.hpp" | ||||
using namespace std; | |||||
namespace codedweller { | |||||
//// Configuration Element ///////////////////////////////////////////////////// | |||||
ConfigurationElement::ConfigurationElement(const char* Name) : // Construct with a cstring. | |||||
myName(std::string(Name)), | |||||
myParent(NULL), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
ConfigurationElement::ConfigurationElement(const std::string Name) : // Construct with a c++ string. | |||||
myName(Name), | |||||
myParent(NULL), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
ConfigurationElement::ConfigurationElement( // Construct sub element w/ cstring. | |||||
const char* Name, | |||||
ConfigurationElement& Parent) : | |||||
myName(std::string(Name)), | |||||
myParent(&Parent), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
ConfigurationElement::ConfigurationElement( // Construct sub element w/ string. | |||||
const std::string Name, | |||||
ConfigurationElement& Parent) : | |||||
myName(Name), | |||||
myParent(&Parent), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
std::string ConfigurationElement::Name() { return myName; } // Get the name of this element. | |||||
ConfigurationElement& ConfigurationElement::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. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::Parent( // Set the parrent of this element. | |||||
ConfigurationElement& Parent) { // Given this parent | |||||
myParent = &Parent; // I take and store it's address | |||||
return (*myParent); // then dereference and return it. | |||||
} | |||||
int ConfigurationElement::Line() { return myLine; } // Get the last line number. | |||||
int ConfigurationElement::Index() { return myIndex; } // Get the last data position. | |||||
int ConfigurationElement::Length() { return myLength; } // Get the last length. | |||||
void ConfigurationElement::notifyDirty() { myCleanFlag = false; } // Attributes do this when they change. | |||||
ConfigurationElement& ConfigurationElement::Element(const char* Name) { // Add a new sub element by c string name. | |||||
return Element(std::string(Name)); // Use the string name version | |||||
} | |||||
ConfigurationElement& ConfigurationElement::Element(const std::string Name) { // Add a new sub element by c++ string name. | |||||
ConfigurationElement* N = new ConfigurationElement( // 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. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return Element(std::string(Name), newTranslator); // Use the string name version | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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 | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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 | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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 | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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 | |||||
} | |||||
ConfigurationElement& ConfigurationElement::End() { // Return this element's parent. | |||||
return Parent(); // Borrow Parent() | |||||
} | |||||
ConfigurationElement& ConfigurationElement::End(const char* Name) { // Check the name and return the parent | |||||
return End(std::string(Name)); // Borrow End(string) | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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. | |||||
} | |||||
ConfigurationAttribute& ConfigurationElement::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) | |||||
ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return Attribute(std::string(Name), newTranslator); // Borrow the string name version | |||||
} | |||||
ConfigurationAttribute& ConfigurationElement::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 | |||||
} | |||||
ConfigurationAttribute& ConfigurationElement::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 | |||||
} | |||||
ConfigurationAttribute& ConfigurationElement::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 | |||||
} | |||||
ConfigurationAttribute& ConfigurationElement::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 | |||||
} | |||||
ConfigurationElement& ConfigurationElement::setInitOnInterpret() { // Set the init on interpret flag. | |||||
myInitOnInterpretFlag = true; // Set the flag. | |||||
return(*this); // Dereference and return self. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::atStartCall( // Add an atStart call-back. | |||||
Configurator& Functor) { // Given this Functor, | |||||
myStartConfigurators.push_back(&Functor); // add it to my atStart list then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::atEndCall( // Add an atEnd call-back. | |||||
Configurator& Functor) { // Given this Functor, | |||||
myEndConfigurators.push_back(&Functor); // add it to my atEnd list then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::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. | |||||
} | |||||
ConfigurationElement& ConfigurationElement::Mnemonic( // Add a mnemonic using c++ strings. | |||||
const std::string name, const std::string value) { // Givent string and string | |||||
ConfigurationMnemonic* N = // Create a new Mnemonic | |||||
new ConfigurationMnemonic(name, value); // using the values provided, | |||||
myMnemonics.push_back(N); // add it to my list, then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
//// Configuration Attribute /////////////////////////////////////////////////// | |||||
ConfigurationAttribute::ConfigurationAttribute( // Attributes are constructed with a | |||||
const char* Name, ConfigurationElement& 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) { | |||||
} | |||||
ConfigurationAttribute::ConfigurationAttribute( // Attributes are constrictued with a | |||||
const std::string Name, ConfigurationElement& Parent) : // Name and a Parent. | |||||
myName(Name), // We grab them and zero the rest. | |||||
myParent(Parent), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0) { | |||||
} | |||||
std::string ConfigurationAttribute::Name() { // Get the name of this attribute. | |||||
return myName; | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::Parent() { // Get the parent of this attribute. | |||||
return myParent; | |||||
} | |||||
int ConfigurationAttribute::Line() { // Get the last line number. | |||||
return myLine; | |||||
} | |||||
int ConfigurationAttribute::Index() { // Get the last data position. | |||||
return myIndex; | |||||
} | |||||
int ConfigurationAttribute::Length() { // Get the last length. | |||||
return myLength; | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::Element( // Add a new sub element by c string name. | |||||
const char* Name) { | |||||
return myParent.Element(Name); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::Element( // Add a new sub element by c++ string name. | |||||
const std::string Name) { | |||||
return myParent.Element(Name); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Element(Name, newTranslator); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Element(Name, newTranslator); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::End() { // Return this element's parent. | |||||
return myParent.End(); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::End(const char* Name) { // Check the name and return the parent | |||||
return myParent.End(Name); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::End(const std::string Name) { // if the name is correct - or throw! | |||||
return myParent.End(Name); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::Attribute( // Add an attribute using a cstring. | |||||
const char* Name) { | |||||
return myParent.Attribute(Name); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::Attribute( // Add an attribute using a c++ string. | |||||
const std::string Name) { | |||||
return myParent.Attribute(Name); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Attribute(Name, newTranslator); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Attribute(Name, newTranslator); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::setInitOnInterpret() { // Set the init on interpret flag. | |||||
return myParent.setInitOnInterpret(); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::atStartCall( // Add an atStart call-back to this element. | |||||
Configurator& Functor) { | |||||
return myParent.atStartCall(Functor); | |||||
} | |||||
ConfigurationElement& ConfigurationAttribute::atEndCall( // Add an atEnd call-back to this element. | |||||
Configurator& Functor) { | |||||
return myParent.atEndCall(Functor); | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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. | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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. | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::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. | |||||
} | |||||
ConfigurationAttribute& ConfigurationAttribute::Mnemonic( // Add a mnemonic using c++ strings. | |||||
const std::string name, const std::string value) { // Givent string and string | |||||
ConfigurationMnemonic* N = // Create a new Mnemonic | |||||
new ConfigurationMnemonic(name, value); // using the values provided, | |||||
myMnemonics.push_back(N); // add it to my list, then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
//// Configuration Data //////////////////////////////////////////////////////// | |||||
char ConfigurationData::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. | |||||
} | |||||
int ConfigurationData::Index() { // Reads the current Index. | |||||
return myIndex; | |||||
} | |||||
int ConfigurationData::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. | |||||
} | |||||
int ConfigurationData::Line() { // Reads the current Line number. | |||||
return myLine; | |||||
} | |||||
int ConfigurationData::addNewLines(int Count) { // Increments the Line number. | |||||
myLine += Count; // Add the number of new lines. | |||||
return myLine; // Return the current Line number. | |||||
} | |||||
//// Configuration Translator ////////////////////////////////////////////////// | |||||
StringTranslator::StringTranslator( // Construct this with | |||||
std::string& Variable, // the variable to map, | |||||
std::string Initializer) : // and the default value. | |||||
myVariable(Variable), | |||||
myInitializer(Initializer) { | |||||
} | |||||
void StringTranslator::translate(const char* Value) { // Provide a translation method. | |||||
myVariable = std::string(Value); // String to String = simple copy. | |||||
} | |||||
void StringTranslator::initialize() { // Provide an initialization method. | |||||
myVariable = myInitializer; // Revert to the initializer value. | |||||
} | |||||
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) { | |||||
} | |||||
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(). | |||||
} | |||||
void IntegerTranslator::initialize() { // Provide an initialization method. | |||||
myVariable = myInitializer; // Revert to the initializer value. | |||||
} | |||||
DoubleTranslator::DoubleTranslator( // Construct this with | |||||
double& Variable, // the variable to map, | |||||
double Initializer) : // and the default value. | |||||
myVariable(Variable), | |||||
myInitializer(Initializer) { | |||||
} | |||||
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(). | |||||
} | |||||
void DoubleTranslator::initialize() { // Provide an initialization method. | |||||
myVariable = myInitializer; // Revert to the initializer value. | |||||
} | |||||
BoolTranslator::BoolTranslator( // Construct this with | |||||
bool& Variable, // the variable to map, | |||||
bool Initializer) : // and the default value. | |||||
myVariable(Variable), | |||||
myInitializer(Initializer) { | |||||
} | |||||
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. | |||||
} | |||||
} | |||||
void BoolTranslator::initialize() { // Provide an initialization method. | |||||
myVariable = myInitializer; // Revert to the initializer value. | |||||
} | |||||
//// Configuration Mnemonic //////////////////////////////////////////////////// | |||||
ConfigurationMnemonic::ConfigurationMnemonic( // To make one, provide both parts. | |||||
std::string Name, std::string Value) : | |||||
myName(Name), | |||||
myValue(Value) { | |||||
} | |||||
bool ConfigurationMnemonic::test(std::string Name) { // Test to see if this Mnemonic matches. | |||||
return (0 == Name.compare(myName)); // Return true if Name and myName match. | |||||
} | |||||
std::string ConfigurationMnemonic::Value() { // If it does then we will need it's value. | |||||
return myValue; | |||||
} | |||||
//// Helper functions ////////////////////////////////////////////////////////// | //// Helper functions ////////////////////////////////////////////////////////// | ||||
// Delete my attributes | // Delete my attributes | ||||
if(0 < myAttributes.size()) { // If we have attributes... | if(0 < myAttributes.size()) { // If we have attributes... | ||||
list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list. | |||||
std::list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list. | |||||
iAttribute = myAttributes.begin(); // Start at the beginning and | iAttribute = myAttributes.begin(); // Start at the beginning and | ||||
while(iAttribute != myAttributes.end()) { // loop through the whole list. | while(iAttribute != myAttributes.end()) { // loop through the whole list. | ||||
delete (*iAttribute); // Delete each attribute | delete (*iAttribute); // Delete each attribute | ||||
// Delete my sub-elements | // Delete my sub-elements | ||||
if(0 < myElements.size()) { // If we have elements... | if(0 < myElements.size()) { // If we have elements... | ||||
list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list. | |||||
std::list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list. | |||||
iElement = myElements.begin(); // Start at the beginning and | iElement = myElements.begin(); // Start at the beginning and | ||||
while(iElement != myElements.end()) { // loop through the whole list. | while(iElement != myElements.end()) { // loop through the whole list. | ||||
delete (*iElement); // Delete each element | delete (*iElement); // Delete each element | ||||
// Delete my mnemonics | // Delete my mnemonics | ||||
if(0 < myMnemonics.size()) { // If we have mnemonics... | if(0 < myMnemonics.size()) { // If we have mnemonics... | ||||
list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
std::list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
iMnemonic = myMnemonics.begin(); // Start at the beginning and | iMnemonic = myMnemonics.begin(); // Start at the beginning and | ||||
while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | ||||
delete (*iMnemonic); // Delete each mnemonic | delete (*iMnemonic); // Delete each mnemonic | ||||
// Delete my translators | // Delete my translators | ||||
if(0 < myTranslators.size()) { // If we have translators... | if(0 < myTranslators.size()) { // If we have translators... | ||||
list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
std::list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
iTranslator = myTranslators.begin(); // Start at the beginning and | iTranslator = myTranslators.begin(); // Start at the beginning and | ||||
while(iTranslator != myTranslators.end()) { // loop through the whole list. | while(iTranslator != myTranslators.end()) { // loop through the whole list. | ||||
delete (*iTranslator); // Delete each translator | delete (*iTranslator); // Delete each translator | ||||
} | } | ||||
ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | ||||
ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ||||
} | } | ||||
ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
const std::string Name, // requires a name, of course, | |||||
std::string& x, std::string init) { // Map to a string. | |||||
ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ||||
Name, // name provided and | Name, // name provided and | ||||
} | } | ||||
ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
int& x, int init, int radix) { // Map to an int. | int& x, int init, int radix) { // Map to an int. | ||||
ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ||||
} | } | ||||
ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
double& x, double init) { // Map to a double. | double& x, double init) { // Map to a double. | ||||
ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ||||
} | } | ||||
ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
bool& x, bool init) { // Map to a boolean. | bool& x, bool init) { // Map to a boolean. | ||||
ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ConfigurationElement* N = new ConfigurationElement( // Create a new Element with the | ||||
return (*N); // Return the new element. | return (*N); // Return the new element. | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationElement::Attribute(const string Name) { // Add an attribute using a c++ string. | |||||
ConfigurationAttribute* N = // Create a new attribute by name and | |||||
new ConfigurationAttribute(Name, (*this)); // provide myself as the parent. | |||||
ConfigurationAttribute& ConfigurationElement::Attribute(const std::string Name) { // Add an attribute using a c++ string. | |||||
ConfigurationAttribute* N = // Create a new attribute by name and | |||||
new ConfigurationAttribute(Name, (*this)); // provide myself as the parent. | |||||
myCleanFlag = false; // New attributes make us dirty. | myCleanFlag = false; // New attributes make us dirty. | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | ||||
myCleanFlag = false; // New attributes make us dirty. | myCleanFlag = false; // New attributes make us dirty. | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
const std::string Name, // requires a name, of course, | |||||
std::string& x, std::string init) { // Map to a string. | |||||
myCleanFlag = false; // New attributes make us dirty. | myCleanFlag = false; // New attributes make us dirty. | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
int& x, int init, int radix) { // Map to an int. | int& x, int init, int radix) { // Map to an int. | ||||
myCleanFlag = false; // New attributes make us dirty. | myCleanFlag = false; // New attributes make us dirty. | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
double& x, double init) { // Map to a double. | double& x, double init) { // Map to a double. | ||||
myCleanFlag = false; // New attributes make us dirty. | myCleanFlag = false; // New attributes make us dirty. | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
bool& x, bool init) { // Map to a boolean. | bool& x, bool init) { // Map to a boolean. | ||||
myCleanFlag = false; // New attributes make us dirty. | myCleanFlag = false; // New attributes make us dirty. | ||||
} | } | ||||
ConfigurationElement& ConfigurationElement::mapTo( // Map to a string. | ConfigurationElement& ConfigurationElement::mapTo( // Map to a string. | ||||
string& x, string init) { // Given a string and init value, | |||||
std::string& x, std::string init) { // Given a string and init value, | |||||
ConfigurationTranslator* N = // create a new translator for it | ConfigurationTranslator* N = // create a new translator for it | ||||
new StringTranslator(x, init); // with the values i'm given, | new StringTranslator(x, init); // with the values i'm given, | ||||
myTranslators.push_back(N); // push it onto my list, then | myTranslators.push_back(N); // push it onto my list, then | ||||
// Initialize the elements below me | // Initialize the elements below me | ||||
if(0 < myElements.size()) { // If we have elements... | if(0 < myElements.size()) { // If we have elements... | ||||
list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list. | |||||
std::list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list. | |||||
iElement = myElements.begin(); // Start at the beginning and | iElement = myElements.begin(); // Start at the beginning and | ||||
while(iElement != myElements.end()) { // loop through the whole list. | while(iElement != myElements.end()) { // loop through the whole list. | ||||
(*iElement)->initialize(); // Initialize each element | (*iElement)->initialize(); // Initialize each element | ||||
// Initialize my own translators | // Initialize my own translators | ||||
if(0 < myTranslators.size()) { // If we have translators... | if(0 < myTranslators.size()) { // If we have translators... | ||||
list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
std::list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
iTranslator = myTranslators.begin(); // Start at the beginning and | iTranslator = myTranslators.begin(); // Start at the beginning and | ||||
while(iTranslator != myTranslators.end()) { // loop through the whole list. | while(iTranslator != myTranslators.end()) { // loop through the whole list. | ||||
(*iTranslator)->initialize(); // Initialize each translator | (*iTranslator)->initialize(); // Initialize each translator | ||||
// Initialize my own attributes | // Initialize my own attributes | ||||
if(0 < myAttributes.size()) { // If we have attributes... | if(0 < myAttributes.size()) { // If we have attributes... | ||||
list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list. | |||||
std::list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list. | |||||
iAttribute = myAttributes.begin(); // Start at the beginning and | iAttribute = myAttributes.begin(); // Start at the beginning and | ||||
while(iAttribute != myAttributes.end()) { // loop through the whole list. | while(iAttribute != myAttributes.end()) { // loop through the whole list. | ||||
(*iAttribute)->initialize(); // Initialize each attribute | (*iAttribute)->initialize(); // Initialize each attribute | ||||
} | } | ||||
void ConfigurationElement::runStartConfigurators(ConfigurationData& D) { // Does what it says ;-) | void ConfigurationElement::runStartConfigurators(ConfigurationData& D) { // Does what it says ;-) | ||||
list<Configurator*>::iterator iConfigurator; // Iterate through our Configurators list. | |||||
std::list<Configurator*>::iterator iConfigurator; // Iterate through our Configurators list. | |||||
iConfigurator = myStartConfigurators.begin(); // Start at the beginning and | iConfigurator = myStartConfigurators.begin(); // Start at the beginning and | ||||
while(iConfigurator != myStartConfigurators.end()) { // loop through the whole list. | while(iConfigurator != myStartConfigurators.end()) { // loop through the whole list. | ||||
(** iConfigurator)(*this, D); // Launch each configurator with self. | (** iConfigurator)(*this, D); // Launch each configurator with self. | ||||
} | } | ||||
void ConfigurationElement::runEndConfigurators(ConfigurationData& D) { // Does what it says ;-) | void ConfigurationElement::runEndConfigurators(ConfigurationData& D) { // Does what it says ;-) | ||||
list<Configurator*>::iterator iConfigurator; // Iterate through our Configurators list. | |||||
std::list<Configurator*>::iterator iConfigurator; // Iterate through our Configurators list. | |||||
iConfigurator = myEndConfigurators.begin(); // Start at the beginning and | iConfigurator = myEndConfigurators.begin(); // Start at the beginning and | ||||
while(iConfigurator != myEndConfigurators.end()) { // loop through the whole list. | while(iConfigurator != myEndConfigurators.end()) { // loop through the whole list. | ||||
(** iConfigurator)(*this, D); // Launch each configurator with self. | (** iConfigurator)(*this, D); // Launch each configurator with self. | ||||
if(isalpha(Data.Data(Index))) { // If it looks like an attribute... | if(isalpha(Data.Data(Index))) { // If it looks like an attribute... | ||||
bool ParseHappened = false; // Start pessimistically at each pass. | bool ParseHappened = false; // Start pessimistically at each pass. | ||||
list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list. | |||||
std::list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list. | |||||
iAttribute = myAttributes.begin(); // Start at the beginning and | iAttribute = myAttributes.begin(); // Start at the beginning and | ||||
while(iAttribute != myAttributes.end()) { // loop through the whole list. | while(iAttribute != myAttributes.end()) { // loop through the whole list. | ||||
ParseHappened = (* iAttribute)->interpret(Data); // Have each attribute interpret(Data) | ParseHappened = (* iAttribute)->interpret(Data); // Have each attribute interpret(Data) | ||||
NewLines = 0; // Reset our new lines count. | NewLines = 0; // Reset our new lines count. | ||||
if(0 < myElements.size()) { // If we have elements check them. | if(0 < myElements.size()) { // If we have elements check them. | ||||
list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list. | |||||
std::list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list. | |||||
iElement = myElements.begin(); // Start at the beginning and | iElement = myElements.begin(); // Start at the beginning and | ||||
while(iElement != myElements.end()) { // loop through the whole list. | while(iElement != myElements.end()) { // loop through the whole list. | ||||
ConfigurationElement& doNode = **iElement; // Grab the element we're on. | ConfigurationElement& doNode = **iElement; // Grab the element we're on. | ||||
// Create the Content buffer... | // Create the Content buffer... | ||||
int BfrSize = Stopdex - Startdex +1; // How big a buffer do we need? | int BfrSize = Stopdex - Startdex +1; // How big a buffer do we need? | ||||
vector<char> heapBfr(BfrSize,0); // Make one that size. | |||||
std::vector<char> heapBfr(BfrSize,0); // Make one that size. | |||||
char* Bfr = &heapBfr[0]; | char* Bfr = &heapBfr[0]; | ||||
copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and ignore our lines. | copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and ignore our lines. | ||||
// Translate our data by Mnemonic | // Translate our data by Mnemonic | ||||
if(0 < myMnemonics.size()) { // If we have mnemonics... | if(0 < myMnemonics.size()) { // If we have mnemonics... | ||||
list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
std::list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
iMnemonic = myMnemonics.begin(); // Start at the beginning and | iMnemonic = myMnemonics.begin(); // Start at the beginning and | ||||
while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | ||||
if(true == ((*iMnemonic)->test(TranslationData))) { // Check to see if the mnemonic matches. | if(true == ((*iMnemonic)->test(TranslationData))) { // Check to see if the mnemonic matches. | ||||
// Put our TranslationData through each Translator. | // Put our TranslationData through each Translator. | ||||
list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
std::list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
iTranslator = myTranslators.begin(); // Start at the beginning and | iTranslator = myTranslators.begin(); // Start at the beginning and | ||||
while(iTranslator != myTranslators.end()) { // loop through the whole list. | while(iTranslator != myTranslators.end()) { // loop through the whole list. | ||||
(*iTranslator)->translate(TranslationData); // Pass the data to each one then | (*iTranslator)->translate(TranslationData); // Pass the data to each one then | ||||
// Delete my mnemonics | // Delete my mnemonics | ||||
if(0 < myMnemonics.size()) { // If we have mnemonics... | if(0 < myMnemonics.size()) { // If we have mnemonics... | ||||
list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
std::list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
iMnemonic = myMnemonics.begin(); // Start at the beginning and | iMnemonic = myMnemonics.begin(); // Start at the beginning and | ||||
while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | ||||
delete (*iMnemonic); // Delete each mnemonic | delete (*iMnemonic); // Delete each mnemonic | ||||
// Delete my translators | // Delete my translators | ||||
if(0 < myTranslators.size()) { // If we have translators... | if(0 < myTranslators.size()) { // If we have translators... | ||||
list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
std::list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
iTranslator = myTranslators.begin(); // Start at the beginning and | iTranslator = myTranslators.begin(); // Start at the beginning and | ||||
while(iTranslator != myTranslators.end()) { // loop through the whole list. | while(iTranslator != myTranslators.end()) { // loop through the whole list. | ||||
delete (*iTranslator); // Delete each translator | delete (*iTranslator); // Delete each translator | ||||
} | } | ||||
ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a string. | ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a string. | ||||
string& x, string init) { // Given a string and init value, | |||||
std::string& x, std::string init) { // Given a string and init value, | |||||
ConfigurationTranslator* N = // create a new translator for it | ConfigurationTranslator* N = // create a new translator for it | ||||
new StringTranslator(x, init); // with the values i'm given, | new StringTranslator(x, init); // with the values i'm given, | ||||
myTranslators.push_back(N); // push it onto my list, then | myTranslators.push_back(N); // push it onto my list, then | ||||
void ConfigurationAttribute::initialize() { // Reset all translators to defaults. | void ConfigurationAttribute::initialize() { // Reset all translators to defaults. | ||||
if(0 < myTranslators.size()) { // If we have translators... | if(0 < myTranslators.size()) { // If we have translators... | ||||
list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
std::list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
iTranslator = myTranslators.begin(); // Start at the beginning and | iTranslator = myTranslators.begin(); // Start at the beginning and | ||||
while(iTranslator != myTranslators.end()) { // loop through the whole list. | while(iTranslator != myTranslators.end()) { // loop through the whole list. | ||||
(*iTranslator)->initialize(); // initialize each translator | (*iTranslator)->initialize(); // initialize each translator | ||||
// Read our data. | // Read our data. | ||||
int BfrSize = Stopdex - Startdex +1; // How big a buffer do we need? | int BfrSize = Stopdex - Startdex +1; // How big a buffer do we need? | ||||
vector<char> heapBfr(BfrSize,0); // Make one that size. | |||||
std::vector<char> heapBfr(BfrSize,0); // Make one that size. | |||||
char* Bfr = &heapBfr[0]; | char* Bfr = &heapBfr[0]; | ||||
NewLines += copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and count our lines. | NewLines += copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and count our lines. | ||||
// Translate our data by Mnemonic | // Translate our data by Mnemonic | ||||
if(0 < myMnemonics.size()) { // If we have mnemonics... | if(0 < myMnemonics.size()) { // If we have mnemonics... | ||||
list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
std::list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list. | |||||
iMnemonic = myMnemonics.begin(); // Start at the beginning and | iMnemonic = myMnemonics.begin(); // Start at the beginning and | ||||
while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | while(iMnemonic != myMnemonics.end()) { // loop through the whole list. | ||||
if(true == ((*iMnemonic)->test(TranslationData))){ // Check to see if the mnemonic matches. | if(true == ((*iMnemonic)->test(TranslationData))){ // Check to see if the mnemonic matches. | ||||
// Put our TranslationData through each Translator. | // Put our TranslationData through each Translator. | ||||
if(0 < myTranslators.size()) { // We'd better have translators! | if(0 < myTranslators.size()) { // We'd better have translators! | ||||
list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
std::list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list. | |||||
iTranslator = myTranslators.begin(); // Start at the beginning and | iTranslator = myTranslators.begin(); // Start at the beginning and | ||||
while(iTranslator != myTranslators.end()) { // loop through the whole list. | while(iTranslator != myTranslators.end()) { // loop through the whole list. | ||||
(*iTranslator)->translate(TranslationData); // Pass the data to each one and | (*iTranslator)->translate(TranslationData); // Pass the data to each one and | ||||
myIndex(0), // Our Index is zero | myIndex(0), // Our Index is zero | ||||
myLine(1) { // We start on line 1 | myLine(1) { // We start on line 1 | ||||
try { // Capture any throws. | try { // Capture any throws. | ||||
ifstream CFGFile(FileName); // Open the file. | |||||
CFGFile.seekg(0,ios::end); // Seek to the end | |||||
std::ifstream CFGFile(FileName); // Open the file. | |||||
CFGFile.seekg(0,std::ios::end); // Seek to the end | |||||
myBufferSize = CFGFile.tellg(); // to find out what size it is. | myBufferSize = CFGFile.tellg(); // to find out what size it is. | ||||
myDataBuffer = newCStringBuffer(myBufferSize); // Make a new buffer the right size. | myDataBuffer = newCStringBuffer(myBufferSize); // Make a new buffer the right size. | ||||
CFGFile.seekg(0,ios::beg); // Seek to the beginning and | |||||
CFGFile.seekg(0,std::ios::beg); // Seek to the beginning and | |||||
CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer. | CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer. | ||||
if(CFGFile.bad()) { // If the read failed, we're unusable! | if(CFGFile.bad()) { // If the read failed, we're unusable! | ||||
delete[] myDataBuffer; // Delete the buffer | delete[] myDataBuffer; // Delete the buffer | ||||
} // indicating there is no Data. | } // indicating there is no Data. | ||||
} | } | ||||
ConfigurationData::ConfigurationData(const string FileName) : // Raw constructor from file. | |||||
ConfigurationData::ConfigurationData(const std::string FileName) : // Raw constructor from file. | |||||
myDataBuffer(NULL), // No data buffer yet. | myDataBuffer(NULL), // No data buffer yet. | ||||
myBufferSize(0), // No length yet. | myBufferSize(0), // No length yet. | ||||
myIndex(0), // Our Index is zero | myIndex(0), // Our Index is zero | ||||
myLine(1) { // We start on line 1 | myLine(1) { // We start on line 1 | ||||
try { // Capture any throws. | try { // Capture any throws. | ||||
ifstream CFGFile(FileName.c_str()); // Open the file. | |||||
CFGFile.seekg(0,ios::end); // Seek to the end | |||||
std::ifstream CFGFile(FileName.c_str()); // Open the file. | |||||
CFGFile.seekg(0,std::ios::end); // Seek to the end | |||||
myBufferSize = CFGFile.tellg(); // to find out what size it is. | myBufferSize = CFGFile.tellg(); // to find out what size it is. | ||||
myDataBuffer = newCStringBuffer(myBufferSize); // Make a new buffer the right size. | myDataBuffer = newCStringBuffer(myBufferSize); // Make a new buffer the right size. | ||||
CFGFile.seekg(0,ios::beg); // Seek to the beginning and | |||||
CFGFile.seekg(0,std::ios::beg); // Seek to the beginning and | |||||
CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer. | CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer. | ||||
if(CFGFile.bad()) { // If the read failed, we're unusable! | if(CFGFile.bad()) { // If the read failed, we're unusable! | ||||
delete[] myDataBuffer; // Delete the buffer | delete[] myDataBuffer; // Delete the buffer | ||||
} // true. | } // true. | ||||
} | } | ||||
} // End namespace codedweller |
// Include This Header Once Only =============================================== | // Include This Header Once Only =============================================== | ||||
#ifndef configuration_included | |||||
#define configuration_included | |||||
#pragma once | |||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#include <cstdlib> | #include <cstdlib> | ||||
#include <list> | #include <list> | ||||
using namespace std; | |||||
namespace codedweller { | |||||
class ConfigurationElement; // Elements exist | class ConfigurationElement; // Elements exist | ||||
class ConfigurationAttribute; // Attributes exist | class ConfigurationAttribute; // Attributes exist | ||||
private: | private: | ||||
string myName; // Elements have a name. | |||||
std::string myName; // Elements have a name. | |||||
// External important things I remember but don't touch... | // External important things I remember but don't touch... | ||||
ConfigurationElement* myParent; // They may have a parrent. | ConfigurationElement* myParent; // They may have a parrent. | ||||
list<Configurator*> myStartConfigurators; // Call these when we start Interpret() | |||||
list<Configurator*> myEndConfigurators; // Call these when we finish Interpret() | |||||
std::list<Configurator*> myStartConfigurators; // Call these when we start Interpret() | |||||
std::list<Configurator*> myEndConfigurators; // Call these when we finish Interpret() | |||||
// Internal / subordinate things I own and kill... | // Internal / subordinate things I own and kill... | ||||
list<ConfigurationAttribute*> myAttributes; // They may have a list of attributes. | |||||
list<ConfigurationElement*> myElements; // They may have a list of sub-elements. | |||||
list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics. | |||||
list<ConfigurationTranslator*> myTranslators; // They may have a list of translators. | |||||
std::list<ConfigurationAttribute*> myAttributes; // They may have a list of attributes. | |||||
std::list<ConfigurationElement*> myElements; // They may have a list of sub-elements. | |||||
std::list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics. | |||||
std::list<ConfigurationTranslator*> myTranslators; // They may have a list of translators. | |||||
// During Interpret() operations we keep track of where we are seen... | // During Interpret() operations we keep track of where we are seen... | ||||
public: | public: | ||||
ConfigurationElement(const char* Name); // Must be constructed with a name | ConfigurationElement(const char* Name); // Must be constructed with a name | ||||
ConfigurationElement(const string Name); // either c string or c++ string. | |||||
ConfigurationElement(const std::string Name); // either c string or c++ string. | |||||
ConfigurationElement(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a | ConfigurationElement(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a | ||||
ConfigurationElement(const string Name, ConfigurationElement& Parent); // parrent. | |||||
ConfigurationElement(const std::string Name, ConfigurationElement& Parent); // parrent. | |||||
// Upon desctruction an element will delete all subordinate objects: | // Upon desctruction an element will delete all subordinate objects: | ||||
// * All sub element objects. | // * All sub element objects. | ||||
// Elements can be probed for some simple, useful things. | // Elements can be probed for some simple, useful things. | ||||
string Name(); // Get the name of this element. | |||||
std::string Name(); // Get the name of this element. | |||||
ConfigurationElement& Parent(); // Get the parent of this element. | ConfigurationElement& Parent(); // Get the parent of this element. | ||||
ConfigurationElement& Parent(ConfigurationElement& newParent); // Set the parent of this element. | ConfigurationElement& Parent(ConfigurationElement& newParent); // Set the parent of this element. | ||||
// Elements can contain either data or sub-elements. | // Elements can contain either data or sub-elements. | ||||
ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name. | ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name. | ||||
ConfigurationElement& Element(const string Name); // Add a new sub element by c++ string name. | |||||
ConfigurationElement& Element(const std::string Name); // Add a new sub element by c++ string name. | |||||
//// Mapping element factory methods for convenience. | //// Mapping element factory methods for convenience. | ||||
//// Root-Node elements are _usually_ empty and without attributes in xml | //// Root-Node elements are _usually_ empty and without attributes in xml | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
string& x, string init = string("")); // Map to a string. | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
// string versions | // string versions | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
string& x, string init = string("")); // Map to a string. | |||||
const std::string Name, // requires a name, of course, | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
int& x, int init = 0, int radix = 0); // Map to an int. | int& x, int init = 0, int radix = 0); // Map to an int. | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
double& x, double init = 0.0); // Map to a double. | double& x, double init = 0.0); // Map to a double. | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
bool& x, bool init = false); // Map to a boolean. | bool& x, bool init = false); // Map to a boolean. | ||||
// End methods for heading back up the tree at the end of an element. | // End methods for heading back up the tree at the end of an element. | ||||
ConfigurationElement& End(); // Return this element's parent. | ConfigurationElement& End(); // Return this element's parent. | ||||
ConfigurationElement& End(const char* Name); // Check the name and return the parent | ConfigurationElement& End(const char* Name); // Check the name and return the parent | ||||
ConfigurationElement& End(const string Name); // if the name is correct - or throw! | |||||
ConfigurationElement& End(const std::string Name); // if the name is correct - or throw! | |||||
// Elements can have attributes. | // Elements can have attributes. | ||||
ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring. | ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring. | ||||
ConfigurationAttribute& Attribute(const string Name); // Add an attribute using a c++ string. | |||||
ConfigurationAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string. | |||||
//// Mapping Attribute factory methods for convenience. | //// Mapping Attribute factory methods for convenience. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
string& x, string init = string("")); // Map to a string. | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
// string versions | // string versions | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
string& x, string init = string("")); // Map to a string. | |||||
const std::string Name, // requires a name, of course, | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
int& x, int init = 0, int radix = 0); // Map to an int. | int& x, int init = 0, int radix = 0); // Map to an int. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
double& x, double init = 0.0); // Map to a double. | double& x, double init = 0.0); // Map to a double. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
bool& x, bool init = false); // Map to a boolean. | bool& x, bool init = false); // Map to a boolean. | ||||
// Elements can Initialize() at each Interpret() call. | // Elements can Initialize() at each Interpret() call. | ||||
// converts it into the expected type, and sets one or more variables | // converts it into the expected type, and sets one or more variables | ||||
// to the converted value. Usually - just one variable. | // to the converted value. Usually - just one variable. | ||||
ConfigurationElement& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this element. | |||||
ConfigurationElement& mapTo(string& x, string init = string("")); // Map to a string. | |||||
ConfigurationElement& mapTo(int& x, int init = 0, int radix = 0); // Map to an int. | |||||
ConfigurationElement& mapTo(double& x, double init = 0.0); // Map to a double. | |||||
ConfigurationElement& mapTo(bool& x, bool init = false); // Map to a boolean. | |||||
ConfigurationElement& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this element. | |||||
ConfigurationElement& mapTo(std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationElement& mapTo(int& x, int init = 0, int radix = 0); // Map to an int. | |||||
ConfigurationElement& mapTo(double& x, double init = 0.0); // Map to a double. | |||||
ConfigurationElement& mapTo(bool& x, bool init = false); // Map to a boolean. | |||||
// An Element's contents may use some special mnemonics to make a | // An Element's contents may use some special mnemonics to make a | ||||
// configuration easier to understand and less error prone. When the | // configuration easier to understand and less error prone. When the | ||||
// contents match a mnemnoic then the translation of the mnemonic is | // contents match a mnemnoic then the translation of the mnemonic is | ||||
// passed to the Translators instead of the raw contents. | // passed to the Translators instead of the raw contents. | ||||
ConfigurationElement& Mnemonic(const char* name, const char* value); // Add a mnemonic using c strings. | |||||
ConfigurationElement& Mnemonic(const char* name, const string value); // Add a mnemonic using c & c++ strings. | |||||
ConfigurationElement& Mnemonic(const string name, const char* value); // Add a mnemonic using c++ & c strings. | |||||
ConfigurationElement& Mnemonic(const string name, const string value); // Add a mnemonic using c++ strings. | |||||
ConfigurationElement& Mnemonic(const char* name, const char* value); // Add a mnemonic using c strings. | |||||
ConfigurationElement& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings. | |||||
ConfigurationElement& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings. | |||||
ConfigurationElement& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using c++ strings. | |||||
// The way data gets into an element tree is that it is Interpret()ed | // The way data gets into an element tree is that it is Interpret()ed | ||||
// recursively. The data is loaded into a ConfigurationData object which | // recursively. The data is loaded into a ConfigurationData object which | ||||
private: | private: | ||||
string myName; // Elements have a name. | |||||
std::string myName; // Elements have a name. | |||||
ConfigurationElement& myParent; // They may have a parrent. | ConfigurationElement& myParent; // They may have a parrent. | ||||
list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics. | |||||
list<ConfigurationTranslator*> myTranslators; // They may have a list of translators. | |||||
std::list<ConfigurationMnemonic*> myMnemonics; // They may have a list of mnemonics. | |||||
std::list<ConfigurationTranslator*> myTranslators; // They may have a list of translators. | |||||
int myLine; // Last line number I was seen on. | int myLine; // Last line number I was seen on. | ||||
int myIndex; // Last char position I was seen on. | int myIndex; // Last char position I was seen on. | ||||
public: | public: | ||||
ConfigurationAttribute(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a | |||||
ConfigurationAttribute(const string Name, ConfigurationElement& Parent); // parrent. | |||||
ConfigurationAttribute(const char* Name, ConfigurationElement& Parent); // Sub-elements are constructed with a | |||||
ConfigurationAttribute(const std::string Name, ConfigurationElement& Parent); // parrent. | |||||
// Attributes delete their Mnemonics and Translators when they go. | // Attributes delete their Mnemonics and Translators when they go. | ||||
// See Elements for similar warnings about objects provided to | // See Elements for similar warnings about objects provided to | ||||
// Attributes can be probed for some simple, useful things. | // Attributes can be probed for some simple, useful things. | ||||
string Name(); // Get the name of this attribute. | |||||
std::string Name(); // Get the name of this attribute. | |||||
ConfigurationElement& Parent(); // Get the parent of this attribute. | ConfigurationElement& Parent(); // Get the parent of this attribute. | ||||
int Line(); // Get the last line number. | int Line(); // Get the last line number. | ||||
int Index(); // Get the last data position. | int Index(); // Get the last data position. | ||||
//// For switching back to the parent element and adding new sub-elements. | //// For switching back to the parent element and adding new sub-elements. | ||||
ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name. | ConfigurationElement& Element(const char* Name); // Add a new sub element by c string name. | ||||
ConfigurationElement& Element(const string Name); // Add a new sub element by c++ string name. | |||||
ConfigurationElement& Element(const std::string Name); // Add a new sub element by c++ string name. | |||||
//// Mapping element factory methods for convenience. | //// Mapping element factory methods for convenience. | ||||
//// Root-Node elements are _usually_ empty and without attributes in xml | //// Root-Node elements are _usually_ empty and without attributes in xml | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
string& x, string init = string("")); // Map to a string. | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
// string versions | // string versions | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
string& x, string init = string("")); // Map to a string. | |||||
const std::string Name, // requires a name, of course, | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
int& x, int init = 0, int radix = 0); // Map to an int. | int& x, int init = 0, int radix = 0); // Map to an int. | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
double& x, double init = 0.0); // Map to a double. | double& x, double init = 0.0); // Map to a double. | ||||
ConfigurationElement& Element( // Mapping factory for convenience, | ConfigurationElement& Element( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
bool& x, bool init = false); // Map to a boolean. | bool& x, bool init = false); // Map to a boolean. | ||||
// End methods for heading back up the tree at the end of an element. | // End methods for heading back up the tree at the end of an element. | ||||
ConfigurationElement& End(); // Return this element's parent. | ConfigurationElement& End(); // Return this element's parent. | ||||
ConfigurationElement& End(const char* Name); // Check the name and return the parent | ConfigurationElement& End(const char* Name); // Check the name and return the parent | ||||
ConfigurationElement& End(const string Name); // if the name is correct - or throw! | |||||
ConfigurationElement& End(const std::string Name); // if the name is correct - or throw! | |||||
//// For adding new attributes to the parent element. | //// For adding new attributes to the parent element. | ||||
ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring. | ConfigurationAttribute& Attribute(const char* Name); // Add an attribute using a cstring. | ||||
ConfigurationAttribute& Attribute(const string Name); // Add an attribute using a c++ string. | |||||
ConfigurationAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string. | |||||
//// Mapping Attribute factory methods for convenience. | //// Mapping Attribute factory methods for convenience. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
string& x, string init = string("")); // Map to a string. | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const char* Name, // requires a name, of course, | const char* Name, // requires a name, of course, | ||||
// string versions | // string versions | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ConfigurationTranslator& newTranslator); // Add a Translator to this element. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
string& x, string init = string("")); // Map to a string. | |||||
const std::string Name, // requires a name, of course, | |||||
std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
int& x, int init = 0, int radix = 0); // Map to an int. | int& x, int init = 0, int radix = 0); // Map to an int. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
double& x, double init = 0.0); // Map to a double. | double& x, double init = 0.0); // Map to a double. | ||||
ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ConfigurationAttribute& Attribute( // Mapping factory for convenience, | ||||
const string Name, // requires a name, of course, | |||||
const std::string Name, // requires a name, of course, | |||||
bool& x, bool init = false); // Map to a boolean. | bool& x, bool init = false); // Map to a boolean. | ||||
//// Set Init On Interprete for the parent element. | //// Set Init On Interprete for the parent element. | ||||
// apply to the parent element's contents. Here they are for use on this | // apply to the parent element's contents. Here they are for use on this | ||||
// attribute. | // attribute. | ||||
ConfigurationAttribute& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this attribute. | |||||
ConfigurationAttribute& mapTo(string& x, string init = string("")); // Map to a string. | |||||
ConfigurationAttribute& mapTo(int& x, int init, int radix = 0); // Map to an int. | |||||
ConfigurationAttribute& mapTo(double& x, double init = 0.0); // Map to a double. | |||||
ConfigurationAttribute& mapTo(bool& x, bool init = false); // Map to a boolean. | |||||
ConfigurationAttribute& mapTo(ConfigurationTranslator& newTranslator); // Add a Translator to this attribute. | |||||
ConfigurationAttribute& mapTo(std::string& x, std::string init = std::string("")); // Map to a string. | |||||
ConfigurationAttribute& mapTo(int& x, int init, int radix = 0); // Map to an int. | |||||
ConfigurationAttribute& mapTo(double& x, double init = 0.0); // Map to a double. | |||||
ConfigurationAttribute& mapTo(bool& x, bool init = false); // Map to a boolean. | |||||
// Attributes can have mnemonics just like elements. | // Attributes can have mnemonics just like elements. | ||||
ConfigurationAttribute& Mnemonic(const char* name, const char* value); // Add a mnemonic using a c string. | |||||
ConfigurationAttribute& Mnemonic(const char* name, const string value); // Add a mnemonic using c & c++ strings. | |||||
ConfigurationAttribute& Mnemonic(const string name, const char* value); // Add a mnemonic using c++ & c strings. | |||||
ConfigurationAttribute& Mnemonic(const string name, const string value); // Add a mnemonic using a c++ string. | |||||
ConfigurationAttribute& Mnemonic(const char* name, const char* value); // Add a mnemonic using a c string. | |||||
ConfigurationAttribute& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings. | |||||
ConfigurationAttribute& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings. | |||||
ConfigurationAttribute& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using a c++ string. | |||||
// Attributes participate in the Interprete() task just like elements. | // Attributes participate in the Interprete() task just like elements. | ||||
public: | public: | ||||
ConfigurationData(const char* FileName); // Constructor from c string file name. | ConfigurationData(const char* FileName); // Constructor from c string file name. | ||||
ConfigurationData(const string FileName); // Constructor from c++ string file name. | |||||
ConfigurationData(const std::string FileName); // Constructor from c++ string file name. | |||||
ConfigurationData(const char* Data, int Length); // Raw constructor from text buffer. | ConfigurationData(const char* Data, int Length); // Raw constructor from text buffer. | ||||
~ConfigurationData(); // Destroys the internal buffer etc. | ~ConfigurationData(); // Destroys the internal buffer etc. | ||||
int Line(); // Reads the current Line number. | int Line(); // Reads the current Line number. | ||||
int addNewLines(int Count); // Increments the Line number. | int addNewLines(int Count); // Increments the Line number. | ||||
stringstream Log; // Convenient Interpret log. | |||||
std::stringstream Log; // Convenient Interpret log. | |||||
}; | }; | ||||
// collection of the basic translators used for built-in mapTo()s. | // collection of the basic translators used for built-in mapTo()s. | ||||
class ConfigurationTranslator { // Translators exist | class ConfigurationTranslator { // Translators exist | ||||
public: | |||||
public: | |||||
virtual ~ConfigurationTranslator(){}; // Stop No Virt Dtor warnings. | virtual ~ConfigurationTranslator(){}; // Stop No Virt Dtor warnings. | ||||
virtual void translate(const char* Value) = 0; // Pure virtual translator. | virtual void translate(const char* Value) = 0; // Pure virtual translator. | ||||
virtual void initialize() = 0; // Pure virtual initializer. | virtual void initialize() = 0; // Pure virtual initializer. | ||||
class StringTranslator : public ConfigurationTranslator { | class StringTranslator : public ConfigurationTranslator { | ||||
private: | private: | ||||
string& myVariable; // Variable to map. | |||||
string myInitializer; // Initial/Default value. | |||||
std::string& myVariable; // Variable to map. | |||||
std::string myInitializer; // Initial/Default value. | |||||
public: | public: | ||||
StringTranslator( // Construct this with | StringTranslator( // Construct this with | ||||
string& Variable, // the variable to map, | |||||
string Inititializer); // and the default value. | |||||
std::string& Variable, // the variable to map, | |||||
std::string Inititializer); // and the default value. | |||||
void translate(const char* Value); // Provide a translation method. | void translate(const char* Value); // Provide a translation method. | ||||
void initialize(); // Provide an initialization method. | void initialize(); // Provide an initialization method. | ||||
class ConfigurationMnemonic { // Mnemonics | class ConfigurationMnemonic { // Mnemonics | ||||
private: | private: | ||||
string myName; // What is the Mnemonic? | |||||
string myValue; // What is the translation? | |||||
std::string myName; // What is the Mnemonic? | |||||
std::string myValue; // What is the translation? | |||||
public: | public: | ||||
ConfigurationMnemonic(string Name, string Value); // To make one, provide both parts. | |||||
bool test(string Name); // Test to see if this Mnemonic matches. | |||||
string Value(); // If it does then we will need it's value. | |||||
ConfigurationMnemonic(std::string Name, std::string Value); // To make one, provide both parts. | |||||
bool test(std::string Name); // Test to see if this Mnemonic matches. | |||||
std::string Value(); // If it does then we will need it's value. | |||||
}; | }; | ||||
//// Configurator ////////////////////////////////////////////////////////////// | //// Configurator ////////////////////////////////////////////////////////////// | ||||
class Configurator { // Configurators exist | class Configurator { // Configurators exist | ||||
public: | public: | ||||
virtual void operator()(ConfigurationElement& E, ConfigurationData& D) = 0; // Pure virtual configurator. | |||||
virtual void operator()(ConfigurationElement& E, ConfigurationData& D) = 0; // Pure virtual configurator. | |||||
virtual ~Configurator() {} // Virtual dtor keeps warnings away. | virtual ~Configurator() {} // Virtual dtor keeps warnings away. | ||||
}; | }; | ||||
//// Include our inline methods //////////////////////////////////////////////// | |||||
#include "configuration.inline.hpp" | |||||
//// Utilities ///////////////////////////////////////////////////////////////// | //// Utilities ///////////////////////////////////////////////////////////////// | ||||
// SetTrueOnComplete Configurator ////////////////////////////////////////////// | // SetTrueOnComplete Configurator ////////////////////////////////////////////// | ||||
void operator()(ConfigurationElement& E, ConfigurationData& D); // Handle the operation. | void operator()(ConfigurationElement& E, ConfigurationData& D); // Handle the operation. | ||||
}; | }; | ||||
#endif | |||||
// End Of Include Only Once | |||||
} // End namespace codedweller |
// configuration.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 configuration.hpp for details | |||||
//// Configuration Element ///////////////////////////////////////////////////// | |||||
inline ConfigurationElement::ConfigurationElement(const char* Name) : // Construct with a cstring. | |||||
myName(string(Name)), | |||||
myParent(NULL), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
inline ConfigurationElement::ConfigurationElement(const string Name) : // Construct with a c++ string. | |||||
myName(Name), | |||||
myParent(NULL), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
inline ConfigurationElement::ConfigurationElement( // Construct sub element w/ cstring. | |||||
const char* Name, | |||||
ConfigurationElement& Parent) : | |||||
myName(string(Name)), | |||||
myParent(&Parent), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
inline ConfigurationElement::ConfigurationElement( // Construct sub element w/ string. | |||||
const string Name, | |||||
ConfigurationElement& Parent) : | |||||
myName(Name), | |||||
myParent(&Parent), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0), | |||||
myCleanFlag(true), | |||||
myInitOnInterpretFlag(false) { | |||||
} | |||||
inline string ConfigurationElement::Name() { return myName; } // Get the name of this element. | |||||
inline ConfigurationElement& ConfigurationElement::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 ConfigurationElement& ConfigurationElement::Parent( // Set the parrent of this element. | |||||
ConfigurationElement& Parent) { // Given this parent | |||||
myParent = &Parent; // I take and store it's address | |||||
return (*myParent); // then dereference and return it. | |||||
} | |||||
inline int ConfigurationElement::Line() { return myLine; } // Get the last line number. | |||||
inline int ConfigurationElement::Index() { return myIndex; } // Get the last data position. | |||||
inline int ConfigurationElement::Length() { return myLength; } // Get the last length. | |||||
inline void ConfigurationElement::notifyDirty() { myCleanFlag = false; } // Attributes do this when they change. | |||||
inline ConfigurationElement& ConfigurationElement::Element(const char* Name) { // Add a new sub element by c string name. | |||||
return Element(string(Name)); // Use the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Element(const string Name) { // Add a new sub element by c++ string name. | |||||
ConfigurationElement* N = new ConfigurationElement( // 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 ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return Element(string(Name), newTranslator); // Use the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
return Element(string(Name), x, init); // Use the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::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(string(Name), x, init, radix); // Use the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
double& x, double init) { // Map to a double. | |||||
return Element(string(Name), x, init); // Use the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
bool& x, bool init) { // Map to a boolean. | |||||
return Element(string(Name), x, init); // Use the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::End() { // Return this element's parent. | |||||
return Parent(); // Borrow Parent() | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::End(const char* Name) { // Check the name and return the parent | |||||
return End(string(Name)); // Borrow End(string) | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::End(const 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 ConfigurationAttribute& ConfigurationElement::Attribute( // Add an attribute using a cstring. | |||||
const char* Name) { // Given this cstring name | |||||
return Attribute(string(Name)); // Convert it to a string and borrow | |||||
} // Attribute(string) | |||||
inline ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return Attribute(string(Name), newTranslator); // Borrow the string name version | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
return Attribute(string(Name), x, init); // Borrow the string name version | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationElement::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(string(Name), x, init); // Borrow the string name version | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
double& x, double init) { // Map to a double. | |||||
return Attribute(string(Name), x, init); // Borrow the string name version | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationElement::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
bool& x, bool init) { // Map to a boolean. | |||||
return Attribute(string(Name), x, init); // Borrow the string name version | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::setInitOnInterpret() { // Set the init on interpret flag. | |||||
myInitOnInterpretFlag = true; // Set the flag. | |||||
return(*this); // Dereference and return self. | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::atStartCall( // Add an atStart call-back. | |||||
Configurator& Functor) { // Given this Functor, | |||||
myStartConfigurators.push_back(&Functor); // add it to my atStart list then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::atEndCall( // Add an atEnd call-back. | |||||
Configurator& Functor) { // Given this Functor, | |||||
myEndConfigurators.push_back(&Functor); // add it to my atEnd list then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Mnemonic( // Add a mnemonic using c strings. | |||||
const char* name, const char* value) { // Given char* and char* | |||||
return Mnemonic(string(name), string(value)); // make strings and borrow that method. | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Mnemonic( // Add a mnemonic using mixed strings. | |||||
const char* name, const string value) { // Given char* and string | |||||
return Mnemonic(string(name), value); // make strings and borrow that method. | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Mnemonic( // Add a mnemonic using mixed strings. | |||||
const string name, const char* value) { // Given string and char* | |||||
return Mnemonic(name, string(value)); // make strings and borrow that method. | |||||
} | |||||
inline ConfigurationElement& ConfigurationElement::Mnemonic( // Add a mnemonic using c++ strings. | |||||
const string name, const string value) { // Givent string and string | |||||
ConfigurationMnemonic* N = // Create a new Mnemonic | |||||
new ConfigurationMnemonic(name, value); // using the values provided, | |||||
myMnemonics.push_back(N); // add it to my list, then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
//// Configuration Attribute /////////////////////////////////////////////////// | |||||
inline ConfigurationAttribute::ConfigurationAttribute( // Attributes are constructed with a | |||||
const char* Name, ConfigurationElement& Parent) : // Name and a Parent. | |||||
myName(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) { | |||||
} | |||||
inline ConfigurationAttribute::ConfigurationAttribute( // Attributes are constrictued with a | |||||
const string Name, ConfigurationElement& Parent) : // Name and a Parent. | |||||
myName(Name), // We grab them and zero the rest. | |||||
myParent(Parent), | |||||
myLine(0), | |||||
myIndex(0), | |||||
myLength(0) { | |||||
} | |||||
inline string ConfigurationAttribute::Name() { // Get the name of this attribute. | |||||
return myName; | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Parent() { // Get the parent of this attribute. | |||||
return myParent; | |||||
} | |||||
inline int ConfigurationAttribute::Line() { // Get the last line number. | |||||
return myLine; | |||||
} | |||||
inline int ConfigurationAttribute::Index() { // Get the last data position. | |||||
return myIndex; | |||||
} | |||||
inline int ConfigurationAttribute::Length() { // Get the last length. | |||||
return myLength; | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Add a new sub element by c string name. | |||||
const char* Name) { | |||||
return myParent.Element(Name); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Add a new sub element by c++ string name. | |||||
const string Name) { | |||||
return myParent.Element(Name); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Element(Name, newTranslator); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
return myParent.Element(Name, x, init); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::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 ConfigurationElement& ConfigurationAttribute::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 ConfigurationElement& ConfigurationAttribute::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 ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Element(Name, newTranslator); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
return myParent.Element(Name, x, init); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const 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 ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
double& x, double init) { // Map to a double. | |||||
return myParent.Element(Name, x, init); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::Element( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
bool& x, bool init) { // Map to a boolean. | |||||
return myParent.Element(Name, x, init); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::End() { // Return this element's parent. | |||||
return myParent.End(); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::End(const char* Name) { // Check the name and return the parent | |||||
return myParent.End(Name); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::End(const string Name) { // if the name is correct - or throw! | |||||
return myParent.End(Name); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Add an attribute using a cstring. | |||||
const char* Name) { | |||||
return myParent.Attribute(Name); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Add an attribute using a c++ string. | |||||
const string Name) { | |||||
return myParent.Attribute(Name); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Attribute(Name, newTranslator); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const char* Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
return myParent.Attribute(Name, x, init); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::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 ConfigurationAttribute& ConfigurationAttribute::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 ConfigurationAttribute& ConfigurationAttribute::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 ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
ConfigurationTranslator& newTranslator) { // Add a Translator to this element. | |||||
return myParent.Attribute(Name, newTranslator); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
string& x, string init) { // Map to a string. | |||||
return myParent.Attribute(Name, x, init); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const 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 ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
double& x, double init) { // Map to a double. | |||||
return myParent.Attribute(Name, x, init); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Attribute( // Mapping factory for convenience, | |||||
const string Name, // requires a name, of course, | |||||
bool& x, bool init) { // Map to a boolean. | |||||
return myParent.Attribute(Name, x, init); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::setInitOnInterpret() { // Set the init on interpret flag. | |||||
return myParent.setInitOnInterpret(); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::atStartCall( // Add an atStart call-back to this element. | |||||
Configurator& Functor) { | |||||
return myParent.atStartCall(Functor); | |||||
} | |||||
inline ConfigurationElement& ConfigurationAttribute::atEndCall( // Add an atEnd call-back to this element. | |||||
Configurator& Functor) { | |||||
return myParent.atEndCall(Functor); | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Mnemonic( // Add a mnemonic using c strings. | |||||
const char* name, const char* value) { // Given char* and char* | |||||
return Mnemonic(string(name), string(value)); // make strings and borrow that method. | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Mnemonic( // Add a mnemonic using mixed strings. | |||||
const char* name, const string value) { // Given char* and string | |||||
return Mnemonic(string(name), value); // make strings and borrow that method. | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Mnemonic( // Add a mnemonic using mixed strings. | |||||
const string name, const char* value) { // Given string and char* | |||||
return Mnemonic(name, string(value)); // make strings and borrow that method. | |||||
} | |||||
inline ConfigurationAttribute& ConfigurationAttribute::Mnemonic( // Add a mnemonic using c++ strings. | |||||
const string name, const string value) { // Givent string and string | |||||
ConfigurationMnemonic* N = // Create a new Mnemonic | |||||
new ConfigurationMnemonic(name, value); // using the values provided, | |||||
myMnemonics.push_back(N); // add it to my list, then | |||||
return(*this); // dereference and return myself. | |||||
} | |||||
//// Configuration Data //////////////////////////////////////////////////////// | |||||
inline char ConfigurationData::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 ConfigurationData::Index() { // Reads the current Index. | |||||
return myIndex; | |||||
} | |||||
inline int ConfigurationData::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 ConfigurationData::Line() { // Reads the current Line number. | |||||
return myLine; | |||||
} | |||||
inline int ConfigurationData::addNewLines(int Count) { // Increments the Line number. | |||||
myLine += Count; // Add the number of new lines. | |||||
return myLine; // Return the current Line number. | |||||
} | |||||
//// Configuration Translator ////////////////////////////////////////////////// | |||||
inline StringTranslator::StringTranslator( // Construct this with | |||||
string& Variable, // the variable to map, | |||||
string Initializer) : // and the default value. | |||||
myVariable(Variable), | |||||
myInitializer(Initializer) { | |||||
} | |||||
inline void StringTranslator::translate(const char* Value) { // Provide a translation method. | |||||
myVariable = 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. | |||||
} | |||||
//// Configuration Mnemonic //////////////////////////////////////////////////// | |||||
inline ConfigurationMnemonic::ConfigurationMnemonic( // To make one, provide both parts. | |||||
string Name, string Value) : | |||||
myName(Name), | |||||
myValue(Value) { | |||||
} | |||||
inline bool ConfigurationMnemonic::test(string Name) { // Test to see if this Mnemonic matches. | |||||
return (0 == Name.compare(myName)); // Return true if Name and myName match. | |||||
} | |||||
inline string ConfigurationMnemonic::Value() { // If it does then we will need it's value. | |||||
return myValue; | |||||
} |