00001 00019 #ifndef __CONFIGURATION 00020 00021 #define __CONFIGURATION 00022 00023 #include <vector> 00024 #include <string> 00025 #include "tinyxml.h" 00026 #include "utils.h" 00027 00034 class CConfiguration 00035 { 00036 protected: 00037 TiXmlDocument doc; 00038 char rootNode[255]; 00039 00040 void findNodes(const char* name, std::vector<TiXmlElement*> &result, TiXmlElement* start, unsigned int level); 00041 00042 public: 00043 CConfiguration(const char* rootNode); 00044 CConfiguration(); 00045 00046 virtual ~CConfiguration(); 00047 void findNodes(const char* name, std::vector<TiXmlElement*> &result); 00048 void findNodes(const char* name, std::vector<TiXmlElement*> &result, TiXmlElement* start); 00049 TiXmlElement* findNode(const char* name); 00050 TiXmlElement* findNode(const char* name, TiXmlElement* start); 00051 void setRootNode(const char* node); 00052 00053 bool load(const char* cfgFile); 00054 bool parse(const char* str); 00055 void save(const char* cfgFile); 00056 void clear(); 00057 float getFloat(const char* str, float def = 0.0); 00058 double getDouble(const char* str, double def = 0.0); 00059 const char* getString(const char* str, const char* def = ""); 00060 int getInteger(const char* str, int def = 0); 00061 unsigned long getUnsignedLong(const char* str, unsigned long def = 0); 00062 bool getBoolean(const char* str, bool def = false); 00063 00064 static std::string getText(TiXmlElement* node); 00065 00066 float getFloat(const char* str, TiXmlElement* start, float def = 0.0); 00067 double getDouble(const char* str, TiXmlElement* start, double def = 0.0); 00068 const char* getString(const char* str, TiXmlElement* start, const char* def = ""); 00069 int getInteger(const char* str, TiXmlElement* start, int def = 0); 00070 bool getBoolean(const char* str, TiXmlElement* start, bool def = false); 00071 00072 void setFloat(const char* str, float value); 00073 void setDouble(const char* str, double value); 00074 void setString(const char* str, const char* value); 00075 void setInteger(const char* str, int value); 00076 void setBoolean(const char* str, bool value); 00077 00078 static float getAttributeFloat(TiXmlElement* node, const char* str, float def = 0.0); 00079 static unsigned long getAttributeUnsignedLong(TiXmlElement* node, const char* str, unsigned long def = 0); 00080 static double getAttributeDouble(TiXmlElement* node, const char* str, double def = 0.0); 00081 static const char* getAttributeString(TiXmlElement* node, const char* str, const char* def = ""); 00082 static int getAttributeInteger(TiXmlElement* node, const char* str, int def = 0); 00083 static bool getAttributeBoolean(TiXmlElement* node, const char* str, bool def = false); 00084 00085 static void setAttributeFloat(TiXmlElement* node, const char* str, float value); 00086 static void setAttributeDouble(TiXmlElement* node, const char* str, double value); 00087 static void setAttributeString(TiXmlElement* node, const char* str, const char* value); 00088 static void setAttributeInteger(TiXmlElement* node, const char* str, int value); 00089 static void setAttributeBoolean(TiXmlElement* node, const char* str, bool value); 00090 00091 TiXmlDocument &getDocument(); 00092 00093 TiXmlElement* getNode(const char* name); 00094 bool addNode(TiXmlElement* node); 00095 bool addNode(TiXmlElement* node, TiXmlElement* parent); 00096 bool insertNode(TiXmlElement* node, TiXmlElement* afterThis); 00097 bool insertNode(TiXmlElement* node, TiXmlElement* afterThis, TiXmlElement* parent); 00098 00099 bool removeNode(TiXmlElement* element); 00100 bool removeNode(TiXmlElement* element, TiXmlElement* parent); 00101 bool removeAttribute(TiXmlElement* element, const char* name); 00102 }; 00103 00104 #define XML_LOADER_INIT std::vector<std::string> xml_loader_strings; 00105 #define XML_LOADER_ADD(Y,X) xml_loader_strings.push_back(robotLibPbD::printToString("<%s>%s</%s>", Y, X.c_str(), Y)); 00106 #define XML_LOADER_ADD_STRING(Y,X) xml_loader_strings.push_back(robotLibPbD::printToString("<%s>%s</%s>", Y, X.c_str(), Y)); 00107 #define XML_LOADER_ADD_INTEGER(Y,X) xml_loader_strings.push_back(robotLibPbD::printToString("<%s>%d</%s>", Y, X, Y)); 00108 #define XML_LOADER_ADD_DOUBLE(Y,X) xml_loader_strings.push_back(robotLibPbD::printToString("<%s>%g</%s>", Y, X, Y)); 00109 #define XML_LOADER_ADD_FLOAT(Y,X) xml_loader_strings.push_back(robotLibPbD::printToString("<%s>%g</%s>", Y, X, Y)); 00110 #define XML_LOADER_ADD_BOOLEAN(Y,X) xml_loader_strings.push_back(robotLibPbD::printToString("<%s>%s</%s>", Y, X ? "true" : "false", Y)); 00111 #define XML_LOADER_GET(Y) { \ 00112 for (unsigned int i=0; i<xml_loader_strings.size(); i++) \ 00113 { \ 00114 Y += xml_loader_strings[i] + "\n"; \ 00115 } \ 00116 } 00117 00118 class CXmlLoaderInterface 00119 { 00120 public: 00121 virtual bool writeAsXml(std::string &output, bool addBracket = true) { return false; }; 00122 virtual bool readFromXml(CConfiguration &configuration, TiXmlElement* xmlNode) { return false; }; 00123 }; 00124 00125 void tokenize(const std::string& str, 00126 std::vector<std::string>& tokens, 00127 const std::string& delimiters); 00128 00129 #endif