forked from wanleung/libcangjie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbbuilder.cpp
More file actions
115 lines (97 loc) · 3.01 KB
/
Copy pathdbbuilder.cpp
File metadata and controls
115 lines (97 loc) · 3.01 KB
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
/*
* Copyright (C) 2012 Wan Leung Wong <me at wanleung dot com>
*
* This file is part of libcangjie.
*
* libcangjie 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 3 of the License, or
* (at your option) any later version.
*
* libcangjie 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 libcangjie. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <db_cxx.h>
#include <fstream>
#include "util.h"
using namespace std;
void readfile(Db* pdb, const char* filename) {
string line;
ifstream myfile (filename);
bool datamode = false;
if (myfile.is_open()) {
while ( myfile.good() ) {
getline (myfile,line);
// Ignore empty and commented lines
if ((line.length() == 0) || (startswith(line, std::string("#")))) {
continue;
}
// Ignore the preamble
if (!datamode) {
if ("[DATA]" == line ) {
datamode = true;
}
continue;
}
// Get the key code
int index = 0;
while (32 != (char)line[index]) {
index++;
}
string cjkey = line.substr(0, index);
while (32 == line[index]) {
index++;
}
Dbt key(const_cast<char*>(cjkey.data()), cjkey.size());
// Get the character
string cjdata = line.substr(index, (line.size()-index));
Dbt value(const_cast<char*>(cjdata.data()), cjdata.size());
pdb->put(NULL, &key, &value, 0);
}
myfile.close();
} else {
cerr << "Unable to open file";
}
}
int main(int argc, const char* argv[]) {
if (argc !=3) {
cout << "usage: "<< argv[0] << " <filename> <db filename>" << endl;
return 1;
}
const char* kDatabaseName = argv[2];
const char* filename = argv[1];
DbEnv env(0);
Db* pdb;
try {
env.set_error_stream(&cerr);
env.open("./", DB_CREATE | DB_INIT_MPOOL, 0);
pdb = new Db(&env, 0);
pdb->set_flags(DB_DUP);
// Create (or clear its content if it exists) the database
pdb->open(NULL, kDatabaseName, NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0);
readfile(pdb, filename);
readfile(pdb, "tables/jp.txt");
// Clean up
if (pdb != NULL) {
pdb->close(0);
delete pdb;
}
env.close(0);
} catch (DbException& e) {
cerr << "DbException: " << e.what() << endl;
return -1;
} catch (std::exception& e) {
cerr << e.what() << endl;
return -1;
}
return 0;
}