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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* palm-db-tools: Abstract adaptor for flat-file databases.
* Copyright (C) 1999-2000 by Tom Dyas ([email protected])
*/
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <sstream>
#include <utility>
#include <cctype>
#include <kdebug.h>
#include "Database.h"
PalmLib::FlatFile::Database::Database(std::string p_Type, const PalmLib::Database& pdb)
: m_Type(p_Type)
{
title(pdb.name());
m_backup = pdb.backup();
m_readonly = pdb.readonly();
m_copy_prevention = pdb.copy_prevention();
}
void
PalmLib::FlatFile::Database::outputPDB(PalmLib::Database& pdb) const
{
pdb.name(title());
pdb.backup(m_backup);
pdb.readonly(m_readonly);
pdb.copy_prevention(m_copy_prevention);
}
std::string
PalmLib::FlatFile::Database::title() const
{
return m_title;
}
void
PalmLib::FlatFile::Database::title(const std::string& title)
{
m_title = title;
}
unsigned
PalmLib::FlatFile::Database::getNumOfFields() const
{
return m_fields.size();
}
std::string
PalmLib::FlatFile::Database::field_name(int i) const
{
return m_fields[i].title();
/* return m_fields[i].first;*/
}
PalmLib::FlatFile::Field::FieldType
PalmLib::FlatFile::Database::field_type(int i) const
{
return m_fields[i].type();
/* return m_fields[i].second;*/
}
PalmLib::FlatFile::FType
PalmLib::FlatFile::Database::field(int i) const
{
return m_fields[i];
}
void
PalmLib::FlatFile::Database::appendField(PalmLib::FlatFile::FType field)
{
if (! supportsFieldType(field.type())) {
// throw PalmLib::error("unsupported field type");
kdDebug() << "unsupported field type" << endl;
return;
}
if (getMaxNumOfFields() != 0 && getNumOfFields() + 1 > getMaxNumOfFields()) {
// throw PalmLib::error("maximum number of fields reached");
kdDebug() << "maximum number of fields reached" << endl;
return;
}
m_fields.push_back(field);
}
void
PalmLib::FlatFile::Database::appendField(const std::string& name,
Field::FieldType type, std::string data)
{
if (! supportsFieldType(type)) {
kdDebug() << "PalmLib::FlatFile::Database::appendField() - unsupported field type" << endl;
return;
}
// throw PalmLib::error("unsupported field type");
if (getMaxNumOfFields() != 0 && getNumOfFields() + 1 > getMaxNumOfFields()) {
kdDebug() << "PalmLib::FlatFile::Database::appendField() - maximum number of fields reached" << endl;
return;
}
// throw PalmLib::error("maximum number of fields reached");
/* m_fields.push_back(std::make_pair(name, type));*/
/* m_fields.push_back(PalmLib::FlatFile::make_ftype(name, type));*/
m_fields.push_back(PalmLib::FlatFile::FType(name, type, data));
}
void
PalmLib::FlatFile::Database::insertField(int i, PalmLib::FlatFile::FType field)
{
if (! supportsFieldType(field.type())) {
// throw PalmLib::error("unsupported field type");
kdDebug() << "unsupported field type" << endl;
return;
}
if (getMaxNumOfFields() != 0 && getNumOfFields() + 1 > getMaxNumOfFields()) {
// throw PalmLib::error("maximum number of fields reached");
kdDebug() << "maximum number of fields reached" << endl;
return;
}
/* m_fields.push_back(std::make_pair(name, type));*/
/* m_fields.push_back(PalmLib::FlatFile::make_ftype(name, type));*/
m_fields.insert(m_fields.begin() + i, field);
}
void
PalmLib::FlatFile::Database::insertField(int i, const std::string& name,
Field::FieldType type, std::string data)
{
if (! supportsFieldType(type)) {
// throw PalmLib::error("unsupported field type");
kdDebug() << "unsupported field type" << endl;
return;
}
if (getMaxNumOfFields() != 0 && getNumOfFields() + 1 > getMaxNumOfFields()) {
// throw PalmLib::error("maximum number of fields reached");
kdDebug() << "maximum number of fields reached" << endl;
return;
}
/* m_fields.push_back(std::make_pair(name, type));*/
/* m_fields.push_back(PalmLib::FlatFile::make_ftype(name, type));*/
m_fields.insert(m_fields.begin() + i, PalmLib::FlatFile::FType(name, type, data));
}
void
PalmLib::FlatFile::Database::removeField(int i)
{
m_fields.erase(m_fields.begin() + i);
}
unsigned
PalmLib::FlatFile::Database::getNumRecords() const
{
return m_records.size();
}
PalmLib::FlatFile::Record
PalmLib::FlatFile::Database::getRecord(unsigned index) const
{
if (index >= getNumRecords()) {
kdDebug() << "invalid index" << endl;
//throw std::out_of_range("invalid index");
}
return m_records[index];
}
void
PalmLib::FlatFile::Database::appendRecord(PalmLib::FlatFile::Record rec)
{
if (rec.fields().size() != getNumOfFields()) {
// throw PalmLib::error("the number of fields mismatch");
kdDebug() << "the number of fields mismatch" << endl;
return;
}
for (unsigned int i = 0; i < getNumOfFields(); i++) {
#ifdef HAVE_VECTOR_AT
const Field field = rec.fields().at(i);
#else
const Field field = rec.fields()[i];
#endif
if (field.type != field_type(i)) {
kdDebug() << "field " << i << " type " << field_type(i) << " mismatch: " << field.type << endl;
return;
// throw PalmLib::error(buffer.str());
}
}
m_records.push_back(rec);
}
void
PalmLib::FlatFile::Database::deleteRecord(unsigned index)
{
m_records.erase(m_records.begin() + index);
}
void
PalmLib::FlatFile::Database::clearRecords()
{
m_records.clear();
}
unsigned
PalmLib::FlatFile::Database::getNumOfListViews() const
{
return m_listviews.size();
}
PalmLib::FlatFile::ListView
PalmLib::FlatFile::Database::getListView(unsigned index) const
{
return m_listviews[index];
}
void
PalmLib::FlatFile::Database::setListView(unsigned index,
const PalmLib::FlatFile::ListView& lv)
{
// Ensure that the field numbers are within range.
for (PalmLib::FlatFile::ListView::const_iterator i = lv.begin();
i != lv.end(); ++i) {
if ((*i).field >= getNumOfFields())
return;
}
m_listviews[index] = lv;
}
void
PalmLib::FlatFile::Database::appendListView(const ListView& lv)
{
// Enforce any limit of the maximum number of list views.
if (getMaxNumOfListViews() != 0
&& getNumOfListViews() + 1 > getMaxNumOfListViews())
return;
// throw PalmLib::error("too many list views for this database type");
// Ensure that the field numbers are within range.
for (PalmLib::FlatFile::ListView::const_iterator i = lv.begin();
i != lv.end(); ++i) {
if ((*i).field >= getNumOfFields())
return;
}
m_listviews.push_back(lv);
}
void
PalmLib::FlatFile::Database::removeListView(unsigned index)
{
if (index < getNumOfListViews())
m_listviews.erase( m_listviews.begin()+index);
}
static void
strlower(std::string& str)
{
for (std::string::iterator p = str.begin(); p != str.end(); ++p) {
if (isupper(*p))
*p = tolower(*p);
}
}
static bool
string2boolean(std::string str)
{
strlower(str);
if (str == "on")
return true;
else if (str == "off")
return false;
else if (str == "true")
return true;
else if (str == "t")
return true;
else if (str == "false")
return false;
else if (str == "f")
return false;
else {
int num = 0;
std::istringstream(str.c_str()) >> num;
return num != 0 ? true : false;
}
}
void
PalmLib::FlatFile::Database::setOption(const std::string& name,
const std::string& value)
{
if (name == "backup")
m_backup = string2boolean(value);
else if (name == "inROM")
m_readonly = string2boolean(value);
else if (name == "copy-prevention")
m_copy_prevention = string2boolean(value);
}
PalmLib::FlatFile::Database::options_list_t
PalmLib::FlatFile::Database::getOptions() const
{
PalmLib::FlatFile::Database::options_list_t set;
typedef PalmLib::FlatFile::Database::options_list_t::value_type value;
if (m_backup)
set.push_back(value("backup", "true"));
else
set.push_back(value("backup", "false"));
if (m_readonly)
set.push_back(value("inROM", "true"));
if (m_copy_prevention)
set.push_back(value("copy-prevention", "true"));
return set;
}
void
PalmLib::FlatFile::Database::doneWithSchema()
{
// Ensure that the database has at least one field.
if (getNumOfFields() == 0)
return;
// throw PalmLib::error("at least one field must be specified");
// Ensure that the database has a title.
if (title().empty())
return;
// throw PalmLib::error("a title must be specified");
}
|