1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
* Parser for debian database files
*
* Copyright (C) 2003--2007 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <ept/debtags/maint/debdbparser.h>
#include <map>
#include <cctype>
#include <system_error>
namespace ept {
namespace debtags {
// Eat spaces and empty lines
// Returns the number of '\n' encountered
int DebDBParser::eatSpacesAndEmptyLines()
{
int res = 0;
int c;
while ((c = getc(in)) != EOF && (isblank(c) || c == '\n'))
if (c == '\n')
{
isBOL = true;
//line++;
res++;
} else
isBOL = false;
if (c == EOF)
{
if (ferror(in))
throw std::system_error(errno, std::system_category(), "cannot read from " + pathname);
isEOF = true;
}
else
ungetc(c, in);
return res;
}
// Get the ^([A-Za-z0-9]+) field name
std::string DebDBParser::getFieldName()
{
if (! isBOL)
throw std::runtime_error("field must start at the beginning of the line");
std::string res;
int c;
while ((c = getc(in)) != EOF && (isalnum(c) || c == '-'))
res += c;
if (c == EOF)
{
if (ferror(in))
throw std::system_error(errno, std::system_category(), "cannot read from " + pathname);
isEOF = true;
if (!res.empty())
throw std::runtime_error("field is truncated at end of file. Last line begins with: \"" + res + "\n");
} else
ungetc(c, in);
return res;
}
// Eat the \s*: characters that divide the field name and the field
// data
void DebDBParser::eatFieldSep()
{
int c;
while ((c = getc(in)) != EOF && isblank(c))
;
if (c != ':')
{
if (c == EOF)
{
if (ferror(in))
throw std::system_error(errno, std::system_category(), "cannot read from " + pathname);
isEOF = true;
throw std::runtime_error("field is truncated at end of file");
} else {
throw std::runtime_error(std::string("invalid character `") + (char)c + "' expecting `:'");
}
}
}
// Get the \s*(.+?)\s*\n of a body line
void DebDBParser::appendFieldBody(std::string& body)
{
int c;
// Skip leading spaces
while ((c = getc(in)) != EOF && isblank(c))
;
// Get the body part
for ( ; c != EOF && c != '\n'; c = getc(in))
body += c;
// Delete trailing spaces
size_t end = body.find_last_not_of(" \t");
if (end != std::string::npos)
body.resize(end + 1);
if (c == EOF)
{
if (ferror(in))
throw std::system_error(errno, std::system_category(), "cannot read from " + pathname);
isEOF = true;
}
else
{
//line++;
isBOL = true;
}
}
DebDBParser::DebDBParser(FILE* input, const std::string& pathname)
: in(input), pathname(pathname), isBOL(true), isEOF(false)
{
// Go at the start of the next record
eatSpacesAndEmptyLines();
}
// Read a record and positions itself at the start of the next one
// Returns false when there are no more records available
bool DebDBParser::nextRecord(Record& rec)
{
if (isEOF)
return false;
rec.clear();
int n;
do {
// Read the field name
std::string field = getFieldName();
std::string body;
//fprintf(stderr, "Got field: %.*s\n", field.size(), field.data());
// Read the colon
eatFieldSep();
// Read the first line of the field body
appendFieldBody(body);
//fprintf(stderr, "Got body: %.*s\n", body.size(), body.data());
// Read the continuation lines of field body
while ((n = eatSpacesAndEmptyLines()) == 0 && ! isBOL)
{
body += '\n';
size_t start_size = body.size();
appendFieldBody(body);
// Check for dot-only lines to be changed to empty lines
if (body.size() - start_size == 1 && body[body.size() - 1] == '.')
body.resize(body.size() - 1);
//fprintf(stderr, "Appended body: %.*s\n", body.size(), body.data());
}
//fprintf(stderr, "Trailing newlines: %d\n", n);
rec.insert(std::pair<std::string,std::string>(field, body));
} while (!isEOF && !n);
return true;
}
}
}
// vim:set ts=4 sw=4:
|