forked from wanleung/libcangjie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_classic_builder.cpp
More file actions
136 lines (108 loc) · 3.55 KB
/
Copy pathquick_classic_builder.cpp
File metadata and controls
136 lines (108 loc) · 3.55 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* 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 <vector>
#include <sstream>
#include "util.h"
using namespace std;
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
return split(s, delim, elems);
}
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;
string line;
bool datamode = false;
try {
env.set_error_stream(&cerr);
env.open("./", DB_CREATE | DB_INIT_MPOOL, 0);
pdb = new Db(&env, 0);
// If you want to support duplicated records and make duplicated
// records sorted by data, you need to call:
// pdb->set_flags(DB_DUPSORT);
// Note that only Btree-typed database supports sorted duplicated
// records
// If the database does not exist, create it. If it exists, clear
// its content after openning.
//pdb->set_flags( DB_DUP );// | DB_DUPSORT);
pdb->open(NULL, kDatabaseName, NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0);
ifstream myfile (filename);
if (myfile.is_open()) {
while ( myfile.good() ) {
getline (myfile,line);
// Ignore empty and commented lines
if ((line.length() == 0) || (startswith(line, std::string("#")))) {
continue;
}
if (!datamode) {
if ("BEGIN_TABLE" == line ) {
datamode = true;
}
continue;
}
if ("END_TABLE" == line) {
datamode = false;
continue;
}
std::vector<std::string> cj = split(line, '\t');
int freq = atoi(cj[2].c_str());
Dbt key(const_cast<char*>(cj[1].data()), cj[1].size());
Dbt value(&freq, sizeof(int));
pdb->put(NULL, &key, &value, 0);
}
myfile.close();
} else {
cout << "Unable to open file";
}
// You need to set ulen and flags=DB_DBT_USERMEM to prevent Dbt
// from allocate its own memory but use the memory provided by you.
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;
}