libept
vocabulary.h
Go to the documentation of this file.
1 #ifndef EPT_DEBTAGS_VOCABULARY_H
2 #define EPT_DEBTAGS_VOCABULARY_H
3 
9 /*
10  * Copyright (C) 2003,2004,2005,2006,2007 Enrico Zini <enrico@debian.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  */
26 
27 #include <string>
28 #include <vector>
29 #include <set>
30 #include <map>
31 
32 namespace tagcoll {
33 namespace input {
34 struct Input;
35 }
36 }
37 
38 namespace ept {
39 namespace debtags {
40 namespace voc {
41 
43 std::string getfacet(const std::string& tagname);
44 
46 struct Data : public std::map<std::string, std::string>
47 {
48 protected:
49  // Cache the parsed short description
50  mutable std::string m_desc;
51 
52 public:
53  std::string name;
54 
59  std::string shortDescription() const;
60 
66  std::string longDescription() const;
67 };
68 
92 struct TagData : public Data
93 {
94  TagData() {}
95 
96  // Facet facet() const;
97 };
98 
122 class FacetData : public Data
123 {
124 public:
125  std::map<std::string, TagData> m_tags;
126 
128 
129  TagData& obtainTag(const std::string& fullname);
130 
134  bool hasTag(const std::string& name) const;
135 
139  const TagData* tagData(const std::string& name) const;
140 
144  std::set<std::string> tags() const;
145 };
146 
147 }
148 
150 {
151 protected:
152  std::map<std::string, voc::FacetData> m_facets;
153 
154  time_t m_timestamp;
155 
156  // Empty parsed data to return when data is asked for IDs == -1
157  std::map<std::string, std::string> emptyData;
158 
159  void parseVocBuf(std::map<std::string, std::string>& res, size_t ofs, size_t len) const;
160 
161  voc::FacetData& obtainFacet(const std::string& name);
162  voc::TagData& obtainTag(const std::string& fullname);
163 
164 public:
172  Vocabulary(bool empty=false);
173  ~Vocabulary();
174 
176  time_t timestamp() const { return m_timestamp; }
177 
179  bool hasData() const { return m_timestamp != 0; }
180 
184  bool empty() const { return m_facets.empty(); }
185 
189  bool hasFacet(const std::string& name) const;
190 
194  bool hasTag(const std::string& name) const;
195 
199  const voc::FacetData* facetData(const std::string& name) const;
200 
204  const voc::TagData* tagData(const std::string& fullname) const;
205 
209  std::set<std::string> facets() const;
210 
214  std::set<std::string> tags() const;
215 
219  std::set<std::string> tags(const std::string& facet) const;
220 
221 #if 0
222  const DerivedTagList& getEquations() const throw () { return equations; }
224 
226  FacetSet facets(const FacetMatcher& filter) const throw () { return getFiltered(filter); }
227 #endif
228 
233  void read(tagcoll::input::Input& input);
234 
238  void write();
239 
243  void write(const std::string& fname);
244 
248  void write(FILE* out);
249 };
250 
251 }
252 }
253 
254 // vim:set ts=4 sw=4:
255 #endif
std::map< std::string, std::string > emptyData
Definition: vocabulary.h:157
FacetData()
Definition: vocabulary.h:127
std::string getfacet(const std::string &tagname)
Extract the facet name from a tag name.
Definition: vocabulary.cc:43
std::string name
Definition: vocabulary.h:53
Representation of a tag.
Definition: vocabulary.h:92
std::map< std::string, voc::FacetData > m_facets
Definition: vocabulary.h:152
void read(tagcoll::input::Input &input)
Parse and import the vocabulary from `input', merging the data with the previously imported ones...
Definition: vocabulary.cc:205
std::set< std::string > tags() const
Return the list of tags in this facet.
Definition: vocabulary.cc:86
std::string m_desc
Definition: vocabulary.h:50
std::set< std::string > tags() const
Return all the tags in the vocabulary.
Definition: vocabulary.cc:187
TagData()
Definition: vocabulary.h:94
voc::FacetData & obtainFacet(const std::string &name)
Definition: vocabulary.cc:130
void parseVocBuf(std::map< std::string, std::string > &res, size_t ofs, size_t len) const
Vocabulary(bool empty=false)
Instantiate the Debtags vocabulary.
Definition: vocabulary.cc:97
std::string shortDescription() const
Return the short description of the tag.
Definition: vocabulary.cc:52
std::map< std::string, TagData > m_tags
Definition: vocabulary.h:125
bool empty() const
Check if there is any data in the merged vocabulary.
Definition: vocabulary.h:184
Base class for facet and tag data.
Definition: vocabulary.h:46
const TagData * tagData(const std::string &name) const
Return the tag data for the given tag, or 0 if not found.
Definition: vocabulary.cc:79
bool hasTag(const std::string &name) const
Check if the vocabulary contains the tag `fullname'.
Definition: vocabulary.cc:155
TagData & obtainTag(const std::string &fullname)
Definition: vocabulary.cc:117
const voc::TagData * tagData(const std::string &fullname) const
Return the tag with the given full name.
Definition: vocabulary.cc:170
Definition: vocabulary.h:149
std::set< std::string > facets() const
Return all the facets in the vocabulary.
Definition: vocabulary.cc:178
bool hasTag(const std::string &name) const
Return true if the facet has a tag with the given name (name, not fullname)
Definition: vocabulary.cc:74
bool hasFacet(const std::string &name) const
Check if the vocabulary contains the facet `name'.
Definition: vocabulary.cc:150
void write()
Atomically update the system vocabulary.
Definition: vocabulary.cc:248
bool hasData() const
Return true if this data source has data, false if it's empty.
Definition: vocabulary.h:179
~Vocabulary()
Definition: vocabulary.cc:113
time_t m_timestamp
Definition: vocabulary.h:154
std::string longDescription() const
Return the long description of the tag.
Definition: vocabulary.cc:67
Representation of a facet.
Definition: vocabulary.h:122
const voc::FacetData * facetData(const std::string &name) const
Return the facet with the given name.
Definition: vocabulary.cc:162
time_t timestamp() const
Get the timestamp of when the index was last updated.
Definition: vocabulary.h:176
voc::TagData & obtainTag(const std::string &fullname)
Definition: vocabulary.cc:143