00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef ais_resources_h
00027 #define ais_resources_h
00028
00029 #include <string>
00030 #include <iostream>
00031 #include <vector>
00032 #include <map>
00033
00034 #include "GNURegex.h"
00035
00036 #ifndef resource_h
00037 #include "Resource.h"
00038 #endif
00039
00040 #ifndef ais_exceptions_h
00041 #include "AISExceptions.h"
00042 #endif
00043
00044 using namespace std;
00045
00046 typedef vector<Resource> ResourceVector;
00047 typedef ResourceVector::iterator ResourceVectorIter;
00048 typedef ResourceVector::const_iterator ResourceVectorCIter;
00049
00067 class AISResources
00068 {
00069 private:
00070
00071
00072
00073
00074 typedef map<string, ResourceVector> ResourceMap;
00075 typedef ResourceMap::iterator ResourceMapIter;
00076 typedef ResourceMap::const_iterator ResourceMapCIter;
00077
00078 typedef pair<string, ResourceVector> RVPair;
00079 typedef vector<RVPair> ResourceRegexps;
00080 typedef ResourceRegexps::iterator ResourceRegexpsIter;
00081 typedef ResourceRegexps::const_iterator ResourceRegexpsCIter;
00082
00083 ResourceMap d_db;
00084 ResourceRegexps d_re;
00085
00086
00087 struct FindRegexp : public binary_function<RVPair, string, bool>
00088 {
00089 string local_re;
00090 FindRegexp(const string &re) : local_re(re)
00091 {}
00092 bool operator()(const RVPair &p)
00093 {
00094 return p.first == local_re;
00095 }
00096 };
00097
00098
00099
00100
00101 struct MatchRegexp : public binary_function<RVPair, string, bool>
00102 {
00103 string candidate;
00104 MatchRegexp(const string &url) : candidate(url)
00105 {}
00106 bool operator()(const RVPair &p)
00107 {
00108 Regex r(p.first.c_str());
00109 return r.match(candidate.c_str(), candidate.length()) != -1;
00110 }
00111 };
00112
00113 friend class AISResourcesTest;
00114 friend ostream &operator<<(ostream &os, const AISResources &ais_res);
00115
00116 public:
00118 AISResources()
00119 {}
00120 AISResources(const string &database) throw(AISDatabaseReadFailed);
00121
00122 virtual ~AISResources()
00123 {}
00124
00125 virtual void add_url_resource(const string &url,
00126 const Resource &ancillary);
00127 virtual void add_url_resource(const string &url, const ResourceVector &rv);
00128
00129 virtual void add_regexp_resource(const string ®exp,
00130 const Resource &ancillary);
00131 virtual void add_regexp_resource(const string ®exp,
00132 const ResourceVector &rv);
00133
00134 virtual bool has_resource(const string &primary) const;
00135
00136 virtual ResourceVector get_resource(const string &primary);
00137
00138 virtual void read_database(const string &database);
00139
00140 virtual void write_database(const string &filename);
00141 };
00142
00143 #endif // ais_resources_h