00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef PROPERTIESPARSER_H
00015 #define PROPERTIESPARSER_H
00016 #include <map>
00017 #include <list>
00018 #include <string>
00019 #include <iostream>
00020 #include <fstream>
00021 #include <algorithm>
00022 #include <cctype>
00023
00024
00025
00026 namespace utilities {
00027 class Mutator;
00028 template <typename T>
00029 class TypedMutator;
00030 class PropertiesParser;
00031 std::istream& skip_comment(std::istream& in);
00032 }
00033
00034
00035
00036 class utilities::Mutator {
00037 public:
00038 virtual void read (std::istream& in) = 0;
00039 virtual void print(std::ostream& out, const std::string& name) const = 0;
00040 virtual ~Mutator() {}
00041 };
00042
00043
00044 namespace utilities {
00045
00046 template<typename T>
00047 class TypedMutator : public Mutator {
00048 public:
00049 TypedMutator(T& v) : _v(v) {}
00050 ~TypedMutator() {}
00051 void read(std::istream& in) { in >> _v;}
00052 void print(std::ostream& out, const std::string& prefix) const
00053 {
00054 out << prefix << _v;
00055 }
00056
00057 private:
00058 T& _v;
00059 };
00060
00061 }
00062
00063
00064 class utilities::PropertiesParser {
00065 public:
00066
00067 PropertiesParser()
00068 {
00069 _table = new _TableType;
00070 _unrecognized = new _UnrecognizedList;
00071 }
00072
00073 ~PropertiesParser();
00074
00075 void addVariable(const std::string& name, Mutator* m);
00076
00077 void addVariable(const char* name, Mutator* m);
00078
00079 void readVariable(std::istream& is);
00080
00081 void readValues(std::istream& is);
00082
00083 void printValues(std::ostream& out, const std::string& pre,
00084 const std::string& sep) const;
00085
00086 bool hasUnrecognized() const;
00087
00088 void printUnrecognized(std::ostream& out) const;
00089
00090 template<class T>
00091 void registerPropertiesVar(const std::string& name, T& t)
00092 {
00093 utilities::TypedMutator<T>* p = new utilities::TypedMutator<T>(t);
00094 addVariable(name, p);
00095 }
00096
00097 private:
00098
00099 struct ciCharCompare {
00100 bool operator()(const char& c1, const char& c2) const
00101 {
00102 return std::tolower(static_cast<unsigned char>(c1))<
00103 std::tolower(static_cast<unsigned char>(c2));
00104 }
00105 };
00106
00107
00108 struct ciStringCompare {
00109 bool operator()(const std::string& s1, const std::string& s2) const
00110 {
00111 return std::lexicographical_compare(s1.begin(), s1.end(),
00112 s2.begin(), s2.end(),
00113 ciCharCompare());
00114 }
00115 };
00116
00117 typedef std::map<std::string, Mutator*, ciStringCompare> _TableType;
00118 typedef std::list<std::string> _UnrecognizedList;
00119
00120 _TableType *_table;
00121 _UnrecognizedList *_unrecognized;
00122
00123 };
00124
00125 #endif