initial version of loading dbscript (without web download)
[aoi.git] / src / main.cxx
blobcada2440f670d16d1ddcad1bc767ef74f4a87910
1 /*
2 Copyright 2013 Karel Matas
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "aoi.hxx"
19 #include "utils.hxx"
20 #include "parsers.hxx"
21 #include "sqlite3.hxx"
22 #include "config.hxx"
24 #include <exception>
25 #include <string>
27 using aoi::ElementKanji;
28 using aoi::ElementReading;
29 using aoi::ElementSense;
30 using aoi::DicWord;
31 using aoi::App;
32 using std::string;
33 using utils::to_string;
35 const char *TEMP_JMDICT = "gztemp.jmdict";
36 const char *TEMP_KANJIDIC = "gztemp.kanjidic";
38 const char *DBSCRIPT_LICENSE =
39 "--This file is based on the JMDICT and KANJIDIC dictionary files. These files \
40 are the property of the Electronic Dictionary Research and Development \
41 Group, and are used in conformance with the Group's licence.\
42 --http://www.csse.monash.edu.au/~jwb/jmdict.html\n\
43 --http://www.csse.monash.edu.au/~jwb/kanjidic2\n\
44 --http://www.edrdg.org/edrdg/licence.html\n\
45 --Decompositions of the kanji to radicals are taken from the KRADFILE-U \
46 - Copyright is held by Jim Rose. (http://www.kanjicafe.com/kradfile_license.htm)\n\
47 --The SKIP (System of Kanji Indexing by Patterns) system for ordering kanji \
48 used in this program was developed by Jack Halpern (Kanji Dictionary \
49 Publishing Society at http://www.kanji.org/, and is used with his \
50 permission.\n\
51 --See http://aoi.souko.cz for more details.\n";
53 /*!
54 * Parses JMDict. File may be GZipped.
55 * \see parsers::JmdictParser
56 * \exception utils::ParsingError
57 * \param fname path to JMDict file
59 void parse_jmdict ( const char *fname )
61 printf("Loading JMdict file '%s'.\n", fname);
62 std::stringstream ss;
63 ss << DBSCRIPT_LICENSE;
64 ss << "BEGIN TRANSACTION;\n";
66 // create parser
67 try {
68 printf("Decompressing file: %s -> %s\n", fname, TEMP_JMDICT);
69 utils::gzip_decompress_file( fname, TEMP_JMDICT);
70 parsers::JmdictParser jmp(TEMP_JMDICT);
71 const char *SEP = aoi::SEPARATOR_SQL;
73 // tables
74 for ( auto &mi: aoi_config::db_tables.at("main") ){
75 if ( strncmp( mi.first, "d_",2 ) !=0 && strcmp( mi.first, "aoi" )!=0 )
76 continue;
77 ss << "DROP TABLE IF EXISTS " << mi.first << ";\n"
78 << "CREATE TABLE " << mi.first << " ( ";
79 for ( size_t i=0; i<mi.second.size(); i++ )
80 ss << mi.second[i].name << " " << mi.second[i].type
81 << ((i==mi.second.size()-1) ? "":",");
82 ss << ");\n";
85 // version
86 ss << "INSERT INTO aoi (key,val) VALUES ('jmdict_version', '"
87 << jmp.get_version() << "');\n";
88 printf("jmdict_version: %s\n", jmp.get_version().c_str());
90 // get entities
91 for ( std::pair<string,string> elt: jmp.get_entities() ){
92 ss << "INSERT INTO d_entities (abbr,desc) VALUES ('" << SQLite3::escape(elt.first)
93 << "','" << SQLite3::escape(elt.second) << "');\n";
96 int n_entries = 1;
97 DicWord entry = jmp.get_entry();
98 while ( entry.did() != -1 ) {
99 for ( ElementReading &rele: entry.r_ele() )
100 ss << rele.sql(entry.did(),SEP);
101 for ( ElementKanji &kele: entry.k_ele() )
102 ss << kele.sql(entry.did(),SEP);
103 for ( ElementSense &sele: entry.s_ele() )
104 ss << sele.sql(entry.did(),SEP);
105 // tbl: header
106 n_entries++;
107 entry = jmp.get_entry();
108 } // while entry
109 printf("%d entries processed.\n", n_entries);
111 catch ( utils::ParsingError &e ){
112 std::string msg = "App::parse_jmdict(): ParsingError: ";
113 msg += e.what();
114 printf("parse_jmdict(): ParsingError: %s\n", e.what() );
115 return;
118 ss << "END TRANSACTION;\n";
119 printf("Writing file 'script.jmdict.sql'...\n");
120 std::ofstream f ("script.jmdict.sql");
121 f << ss.str();
122 f.close();
124 remove(TEMP_JMDICT);
129 * Parses kanjidic2. Works in the same way as parse_jmdict().
130 * \see parsers::KanjidicParser
132 void parse_kanjidic ( const char *fname )
134 printf("Loading kanjidic file: %s\n", fname);
135 const char *SEP = aoi::SEPARATOR_SQL;
137 int n_kanji = 0;
138 try {
139 printf("Decompressing file: %s -> %s\n", fname, TEMP_KANJIDIC);
140 utils::gzip_decompress_file( fname, TEMP_KANJIDIC);
141 parsers::KanjidicParser p(TEMP_KANJIDIC);
142 auto kanji = p.get_entry();
144 std::stringstream ss;
145 ss << "BEGIN TRANSACTION;\n";
147 // tables
148 for ( auto &mi: aoi_config::db_tables.at("main") ){
149 if ( strncmp( mi.first, "k_",2 ) !=0 )
150 continue;
151 ss << "DROP TABLE IF EXISTS " << mi.first << ";\n"
152 << "CREATE TABLE " << mi.first << " ( ";
153 for ( size_t i=0; i<mi.second.size(); i++ )
154 ss << mi.second[i].name << " " << mi.second[i].type
155 << ((i==mi.second.size()-1) ? "":",");
156 ss << ");\n";
159 // version
160 ss << "REPLACE INTO aoi (key,val) VALUES ('kanjidic_version','"
161 << p.get_version() << "');\n";
163 while ( kanji.kanji() != "" ){
164 n_kanji++;
165 ss << "INSERT INTO k_kanji "
166 << "(kanji,ucs,onyomi,kunyomi,meaning,nanori,flags,jlpt,grade,freq,strokes,"
167 << "rad_classic,rad_nelson,components)"
168 << " VALUES('"
169 << kanji.kanji() << "','"
170 << kanji.ucs() << "','"
171 << to_string(kanji.onyomi(),SEP) << "','"
172 << to_string(kanji.kunyomi(),SEP) << "','"
173 << SQLite3::escape(to_string(kanji.meaning(),SEP)) << "','"
174 << to_string(kanji.nanori(),SEP) << "','"
175 << to_string(kanji.flags(),SEP) << "',"
176 << kanji.jlpt() << ","
177 << kanji.grade() << ","
178 << kanji.freq() << ","
179 << kanji.strokes() << ","
180 << kanji.rad_classic() << ","
181 << kanji.rad_nelson() << ","
182 << "''"
183 << ");\n";
184 for ( aoi::SKIP &s: kanji.skip() ) {
185 ss << "INSERT INTO k_skip (kanji,skip1,skip2,skip3,misclass) VALUES('"
186 << kanji.kanji() << "'," << s.s1 << "," << s.s2 << "," << s.s3
187 << ",'" << s.misclass << "');\n";
189 kanji = p.get_entry();
191 ss << "END TRANSACTION\n;";
193 printf("Writing file 'script.kanjidic.sql'...\n");
194 std::ofstream f ("script.kanjidic.sql");
195 f << ss.str();
196 f.close();
198 catch ( utils::ParsingError &e ){
199 printf("parse_kanjidic(): ParsingError: %s\n", e.what());
200 return;
202 remove( TEMP_KANJIDIC );
207 void usage ()
209 printf("USAGE: aoi [OPTIONS]\n");
210 printf("OPTIONS:\n");
211 printf(" -geometry W*H+X+Y \n");
212 printf(" -scheme none|GTK+|plastic \n");
213 printf(" -config config string\n");
214 printf(" -parse jmdict|kanjidic parse either jmdict or kanjidic\n");
218 //////////////////////////////////////////////////////////////////////////
221 int main ( int argc, char **argv )
223 int ret = 0;
224 int fltk_argc = 1;
225 char **fltk_argv = &argv[0];
227 // COmmandline options
228 try {
229 for ( int i=1; i < argc; i++ ){
230 // CONFIG
231 if ( !strcmp( argv[i], "-config") ){
232 string opts = argv[++i];
233 if ( opts == "help" ){
234 for ( auto mi: App::get()->get_config_map() )
235 printf("%s [%s]\n %s\n\n",
236 mi.first.c_str(), mi.second.val.c_str(), mi.second.desc.c_str() );
237 continue;
239 for ( string &s: utils::split_string( opts, ":" ) ){
240 vector<string> kv = utils::split_string(s, "=");
241 App::get()->log("Config override: " + kv[0] + "=" + kv[1]);
242 try {
243 App::get()->config_override( kv[0], kv[1] ); }
244 catch ( std::runtime_error &e ) {
245 App::get()->log_w("Unknown option: " + kv[0] ); }
247 App::get()->apply_config();
248 } // -config
249 // Parse sourcefiles
250 else if ( !strcmp( argv[i], "-parse" ) ) {
251 if ( i == argc-1 ){
252 printf("-parse: Missing parameter: either kanjidic or jmdict\n");
253 return 0;
255 i++;
256 if ( !strcmp( argv[i], "jmdict") ){
257 if ( utils::file_exists("JMdict_e.gz") )
258 parse_jmdict( "JMdict_e.gz" );
259 else if ( utils::file_exists("JMdict_e") )
260 parse_jmdict( "JMdict_e" );
261 else {
262 printf("File not found: JMdict_e or JMdict_e.gz\n");
263 return 0;
266 else if ( !strcmp( argv[i], "kanjidic") ){
267 if ( utils::file_exists("kanjidic2.xml.gz") )
268 parse_kanjidic( "kanjidic2.xml.gz" );
269 else if ( utils::file_exists("kanjidic2.xml") )
270 parse_kanjidic( "kanjidic2.xml" );
271 else {
272 printf("File not found: kanjidic2.xml or kanjidic2.xml.gz\n");
273 return 0;
276 else {
277 printf("-parse: wrong parameter '%s'.\n", argv[i] );
278 printf("Possible parameters: kanjidic or jmdict\n");
280 return 1;
282 else if ( !strncmp( argv[i], "-loaddb", 7) ){
283 if ( i==argc-1 ) {
284 printf("Missing file: -loaddb FILE\n");
285 return 0;
287 if ( !utils::file_exists(argv[i+1]) ){
288 printf("File does not exist: %s\n", argv[i+1]);
289 return 0;
291 App::get()->load_dbscript( argv[i+1] );
292 return 1;
294 // argv[i] not recognized
295 else {
296 printf("argv[%d] : %s -> FLTK\n", i, argv[i]);
297 // pass unparsed arguments to FLTK
298 fltk_argv[fltk_argc++] = argv[i];
301 ret = App::get()->run(fltk_argc,fltk_argv);
303 // EXCEPTIONS
304 catch ( std::exception &e ){
305 // This should never happen ...
306 string msg = std::string("Something went wrong...\n")
307 + std::string(typeid(e).name())
308 + std::string(": ") + std::string(e.what());
309 App::get()->alert(msg);
312 delete App::get();
313 return ret;