1 : #ifndef EPT_APT_RECORDPARSER_H
2 : #define EPT_APT_RECORDPARSER_H
3 :
4 : /** \file
5 : * Parser for APT records
6 : */
7 :
8 : /*
9 : * Copyright (C) 2007 Enrico Zini <enrico@enricozini.org>
10 : *
11 : * This library is free software; you can redistribute it and/or
12 : * modify it under the terms of the GNU Lesser General Public
13 : * License as published by the Free Software Foundation; either
14 : * version 2.1 of the License, or (at your option) any later version.
15 : *
16 : * This library is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 : * Lesser General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU Lesser General Public
22 : * License along with this library; if not, write to the Free Software
23 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 : */
25 :
26 : #include <vector>
27 : #include <string>
28 :
29 : namespace ept {
30 : namespace apt {
31 :
32 : /**
33 : * Access the fields of a package record contained inside a std::string.
34 : *
35 : * Implementation note: this implementation should take advantage of
36 : * std::string sharing buffer space among them.
37 : */
38 : class RecordParser
39 19 : {
40 : /// Buffer containing the whole record
41 : std::string buffer;
42 :
43 : /// End offsets of the various fields in the record
44 : std::vector<size_t> ends;
45 :
46 : /// Indexes on the ends vector, sorted by field name
47 : std::vector<size_t> sorted;
48 :
49 : public:
50 6 : RecordParser() {}
51 13 : RecordParser(const std::string& str) { scan(str); }
52 :
53 : /// Index a new record
54 : void scan(const std::string& str);
55 :
56 : /**
57 : * Get the index of the field with the given name.
58 : *
59 : * size() is returned if not found
60 : */
61 : size_t index(const std::string& str) const;
62 :
63 : /// Return the field by its index
64 : std::string field(size_t idx) const;
65 :
66 : /// Return the name of a field by its index
67 : std::string name(size_t idx) const;
68 :
69 : /// Return the content of a field by its index
70 : std::string lookup(size_t idx) const;
71 :
72 : /// Return the content of a field by its name
73 18273 : std::string lookup(const std::string& name) const { return lookup(index(name)); }
74 :
75 : /// Return the content of a field by its index
76 6 : std::string operator[](size_t idx) const { return lookup(idx); }
77 :
78 : /// Return the content of a field by its name
79 19 : std::string operator[](const std::string& name) const { return lookup(name); }
80 :
81 : /// Return the entire record
82 : const std::string& record() const { return buffer; }
83 :
84 : /// Return the entire record
85 1 : std::string record() { return buffer; }
86 :
87 : /// Return the number of fields in the record
88 20972 : size_t size() const { return ends.size(); }
89 : };
90 :
91 : }
92 : }
93 :
94 : // vim:set ts=4 sw=4:
95 : #endif
|