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