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 #include <cstdio>
32 #include <iosfwd>
33 
34 namespace ept {
35 namespace debtags {
36 namespace voc {
37 
39 std::string getfacet(const std::string& tagname);
40 
42 struct Data : public std::map<std::string, std::string>
43 {
44 protected:
45  // Cache the parsed short description
46  mutable std::string m_desc;
47 
48 public:
49  std::string name;
50 
55  std::string shortDescription() const;
56 
62  std::string longDescription() const;
63 };
64 
88 struct TagData : public Data
89 {
90  TagData() {}
91 
92  // Facet facet() const;
93 };
94 
118 class FacetData : public Data
119 {
120 public:
121  std::map<std::string, TagData> m_tags;
122 
124 
125  TagData& obtainTag(const std::string& fullname);
126 
130  bool hasTag(const std::string& name) const;
131 
135  const TagData* tagData(const std::string& name) const;
136 
140  std::set<std::string> tags() const;
141 };
142 
143 }
144 
146 {
147 protected:
148  std::map<std::string, voc::FacetData> m_facets;
149 
150  time_t m_timestamp;
151 
152  // Empty parsed data to return when data is asked for IDs == -1
153  std::map<std::string, std::string> emptyData;
154 
155  void parseVocBuf(std::map<std::string, std::string>& res, size_t ofs, size_t len) const;
156 
157  voc::FacetData& obtainFacet(const std::string& name);
158  voc::TagData& obtainTag(const std::string& fullname);
159 
163  void write(std::ostream& out);
164 
165 public:
173  Vocabulary(bool empty=false);
174  ~Vocabulary();
175 
177  time_t timestamp() const { return m_timestamp; }
178 
180  bool hasData() const { return m_timestamp != 0; }
181 
185  bool empty() const { return m_facets.empty(); }
186 
190  bool hasFacet(const std::string& name) const;
191 
195  bool hasTag(const std::string& name) const;
196 
200  const voc::FacetData* facetData(const std::string& name) const;
201 
205  const voc::TagData* tagData(const std::string& fullname) const;
206 
210  std::set<std::string> facets() const;
211 
215  std::set<std::string> tags() const;
216 
220  std::set<std::string> tags(const std::string& facet) const;
221 
222 #if 0
223  const DerivedTagList& getEquations() const throw () { return equations; }
225 
227  FacetSet facets(const FacetMatcher& filter) const throw () { return getFiltered(filter); }
228 #endif
229 
231  void load(const std::string& pathname);
232 
237  void read(FILE* input, const std::string& pathname);
238 
242  void write();
243 
247  void write(const std::string& fname);
248 
255  static std::string pathname();
256 };
257 
258 }
259 }
260 
261 // vim:set ts=4 sw=4:
262 #endif
sys.h
ept::debtags::coll::utils::set_contains
bool set_contains(const std::set< T > &set1, const std::set< T > &set2)
Definition: set.h:65
ept::debtags::Vocabulary::tagData
const voc::TagData * tagData(const std::string &fullname) const
Return the tag with the given full name.
Definition: vocabulary.cc:183
ept::sys::write_file_atomically
void write_file_atomically(const std::string &file, const std::string &data, mode_t mode)
Write data to file, replacing existing contents if it already exists.
Definition: sys.cc:619
ept::debtags::Vocabulary::timestamp
time_t timestamp() const
Get the timestamp of when the index was last updated.
Definition: vocabulary.h:177
ept::debtags::voc::FacetData::tags
std::set< std::string > tags() const
Return the list of tags in this facet.
Definition: vocabulary.cc:83
ept::debtags::Vocabulary::hasTag
bool hasTag(const std::string &name) const
Check if the vocabulary contains the tag ‘fullname’.
Definition: vocabulary.cc:168
debdbparser.h
Parser for debian database files.
ept::debtags::voc::TagData
Representation of a tag.
Definition: vocabulary.h:89
ept::debtags::Vocabulary::m_facets
std::map< std::string, voc::FacetData > m_facets
Definition: vocabulary.h:148
ept::debtags::coll::utils
Definition: set.h:32
ept::debtags
Definition: fast.cc:29
ept::debtags::Vocabulary::hasData
bool hasData() const
Return true if this data source has data, false if it's empty.
Definition: vocabulary.h:180
ept::debtags::voc::FacetData::tagData
const TagData * tagData(const std::string &name) const
Return the tag data for the given tag, or 0 if not found.
Definition: vocabulary.cc:76
ept::debtags::voc::FacetData::m_tags
std::map< std::string, TagData > m_tags
Definition: vocabulary.h:121
ept::tests
Definition: tests.cc:23
ept::debtags::voc::FacetData::hasTag
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:71
ept::debtags::Vocabulary::obtainFacet
voc::FacetData & obtainFacet(const std::string &name)
Definition: vocabulary.cc:143
ept::debtags::voc::getfacet
std::string getfacet(const std::string &tagname)
Extract the facet name from a tag name.
Definition: vocabulary.cc:40
ept::debtags::voc::Data::shortDescription
std::string shortDescription() const
Return the short description of the tag.
Definition: vocabulary.cc:49
ept::debtags::Vocabulary::Vocabulary
Vocabulary(bool empty=false)
Instantiate the Debtags vocabulary.
Definition: vocabulary.cc:94
ept::debtags::DebDBParser
Parse Debian records from a parser input.
Definition: debdbparser.h:44
EnvOverride
Definition: test.h:17
ept
String functions.
Definition: apt.cc:38
res
set< string > & res
Definition: packagerecord.cc:73
vocabulary.h
ept::debtags::voc::Data
Base class for facet and tag data.
Definition: vocabulary.h:43
ept::debtags::Vocabulary::facets
std::set< std::string > facets() const
Return all the facets in the vocabulary.
Definition: vocabulary.cc:191
ept::debtags::voc::TagData::TagData
TagData()
Definition: vocabulary.h:90
ept::sys::exists
bool exists(const std::string &file)
Same as access(s, F_OK);.
Definition: sys.cc:148
ept::debtags::Vocabulary::parseVocBuf
void parseVocBuf(std::map< std::string, std::string > &res, size_t ofs, size_t len) const
ept::tests::actual
Actual< A > actual(const A &actual)
Definition: tests.h:320
ept::debtags::voc::FacetData::obtainTag
TagData & obtainTag(const std::string &fullname)
Definition: vocabulary.cc:130
ept::debtags::Vocabulary::empty
bool empty() const
Check if there is any data in the merged vocabulary.
Definition: vocabulary.h:185
test.h
ept::debtags::Vocabulary::load
void load(const std::string &pathname)
Load vocabulary data from the given file.
Definition: vocabulary.cc:112
ept::tests::TestCase
Test case collecting several test methods, and self-registering with the singleton instance of TestRe...
Definition: tests.h:615
ept::debtags::DebDBParser::fileName
const std::string & fileName() const
Definition: debdbparser.h:70
std
Definition: packagerecord-test.cc:4
ept::debtags::DebDBParser::Record
std::map< std::string, std::string > Record
Definition: debdbparser.h:66
ept::debtags::Vocabulary
Definition: vocabulary.h:146
register_tests
void register_tests() override
Definition: vocabulary-test.cc:2
ept::debtags::voc::FacetData
Representation of a facet.
Definition: vocabulary.h:119
ept::debtags::voc::Data::longDescription
std::string longDescription() const
Return the long description of the tag.
Definition: vocabulary.cc:64
ept::debtags::Vocabulary::m_timestamp
time_t m_timestamp
Definition: vocabulary.h:150
ept::debtags::Vocabulary::obtainTag
voc::TagData & obtainTag(const std::string &fullname)
Definition: vocabulary.cc:156
ept::debtags::DebDBParser::nextRecord
bool nextRecord(Record &rec)
Definition: debdbparser.cc:146
ept::debtags::Vocabulary::tags
std::set< std::string > tags() const
Return all the tags in the vocabulary.
Definition: vocabulary.cc:200
ept::debtags::Vocabulary::~Vocabulary
~Vocabulary()
Definition: vocabulary.cc:101
testfile
#define testfile
Definition: vocabulary-test.cc:11
ept::debtags::Vocabulary::hasFacet
bool hasFacet(const std::string &name) const
Check if the vocabulary contains the facet ‘name’.
Definition: vocabulary.cc:163
ept::tests::assert_true
void assert_true(std::nullptr_t actual)
Definition: tests.cc:194
wassert
#define wassert(...)
Run the given command, raising TestFailed with the appropriate backtrace information if it threw an e...
Definition: tests.h:343
ept::debtags::Vocabulary::write
void write()
Atomically update the system vocabulary.
Definition: vocabulary.cc:260
ept::debtags::voc::Data::name
std::string name
Definition: vocabulary.h:49
ept::debtags::Vocabulary::pathname
static std::string pathname()
Return the default pathname for the vocabulary.
Definition: vocabulary.cc:105
ept::debtags::Vocabulary::emptyData
std::map< std::string, std::string > emptyData
Definition: vocabulary.h:153
set.h
Extra useful set operations.
ept::debtags::voc::FacetData::FacetData
FacetData()
Definition: vocabulary.h:123
ept::sys::timestamp
time_t timestamp(const std::string &file)
File mtime.
Definition: sys.cc:103
ept::debtags::Vocabulary::read
void read(FILE *input, const std::string &pathname)
Parse and import the vocabulary from ‘input’, merging the data with the previously imported ones.
Definition: vocabulary.cc:218
ept::debtags::Vocabulary::facetData
const voc::FacetData * facetData(const std::string &name) const
Return the facet with the given name.
Definition: vocabulary.cc:175
ept::debtags::voc::Data::m_desc
std::string m_desc
Definition: vocabulary.h:46