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 namespace {
00037
00038 bool ciCharLess(const char& c1, const char& c2)
00039 {
00040 return std::tolower(static_cast<unsigned char>(c1))<
00041 std::tolower(static_cast<unsigned char>(c2));
00042 }
00043
00044
00045
00046 struct ciStringCompare {
00047 bool operator()(const std::string& s1, const std::string& s2) const
00048 {
00049 return std::lexicographical_compare(s1.begin(), s1.end(),
00050 s2.begin(), s2.end(), ciCharLess);
00051 }
00052 };
00053 }
00054
00055
00056
00057
00058 class utilities::Mutator {
00059 public:
00060 virtual void read (std::istream& in) = 0;
00061 virtual void print(std::ostream& out, const std::string& name) const = 0;
00062 virtual ~Mutator() {}
00063 };
00064
00065
00066 namespace utilities {
00067
00068 template<typename T>
00069 class TypedMutator : public Mutator {
00070 public:
00071 TypedMutator(T& v) : _v(v) {}
00072 ~TypedMutator() {}
00073 void read(std::istream& in) { in >> _v;}
00074 void print(std::ostream& out, const std::string& prefix) const
00075 {
00076 out << prefix << _v;
00077 }
00078
00079 private:
00080 T& _v;
00081 };
00082
00083 }
00084
00085
00086 class utilities::PropertiesParser {
00087 public:
00088
00089 PropertiesParser()
00090 {
00091 _table = new _TableType;
00092 _unrecognized = new _UnrecognizedList;
00093 }
00094
00095 ~PropertiesParser();
00096
00097 void addVariable(const std::string& name, Mutator* m);
00098
00099 void addVariable(const char* name, Mutator* m);
00100
00101 void readVariable(std::istream& is);
00102
00103 void readValues(std::istream& is);
00104
00105 void printValues(std::ostream& out, const std::string& pre,
00106 const std::string& sep) const;
00107
00108 bool hasUnrecognized() const;
00109
00110 void printUnrecognized(std::ostream& out) const;
00111
00112 template<class T>
00113 void registerPropertiesVar(const std::string& name, T& t)
00114 {
00115 utilities::TypedMutator<T>* p = new utilities::TypedMutator<T>(t);
00116 addVariable(name, p);
00117 }
00118
00119 private:
00120 typedef std::map<std::string, Mutator*, ciStringCompare> _TableType;
00121 typedef std::list<std::string> _UnrecognizedList;
00122
00123 _TableType *_table;
00124 _UnrecognizedList *_unrecognized;
00125
00126 };
00127
00128 #endif