test
[jmdict-cli.git] / jmdict.cpp
1 /*
2 jmdict, a frontend to the JMdict file. http://mandrill.fuxx0r.net/jmdict.php
3 Copyright (C) 2004 Florian Bluemel (florian.bluemel@uni-dortmund.de)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19 #include <cstdlib>
20 #include <iostream>
21 #include <ostream>
22 #include <iomanip>
23 #include <string>
24 #include <stdexcept>
25 #include <exception>
26 #include <memory>
27 #include "sqlite.h"
28 #include "kana2romaji.h"
29 using namespace std;
30
31 void usage() {
32     cout << "jmdict [options] subject\n"
33             "  -b        search for entries beginning with <subject>\n"
34             "  -f        perform a fulltext search\n"
35             "  -i        case-insensitive search (implied by -b or -f)\n"
36             "\n"
37             "  -j        translate from japanese\n"
38             "  -J        translate to japanese\n"
39             "            if neither -j nor -J is given, source language will be guessed\n"
40             "\n"
41             "  -l lang   target language is lang, where lang is a three-letter language code\n"
42             "            default: eng\n";
43 }
44
45 namespace options {
46     enum Language { UNKNOWN, JAPANESE, JAPANESE_ROMAJI, NOT_JAPANESE };
47
48     Language source = UNKNOWN;
49     string target("eng");
50     bool fulltext = false;
51     bool beginning = false;
52     bool ci_search = false;
53
54     void getFrom(int argc, char** argv) {
55         int opt;
56         while ((opt = getopt(argc, argv, "bfijJl:")) != -1)
57             switch (opt) {
58                 case 'b':   beginning = true;       break;
59                 case 'f':   fulltext = true;        break;
60                 case 'i':   ci_search = true;       break;
61                 case 'j':   source = JAPANESE;      break;
62                 case 'J':   source = NOT_JAPANESE;  break;
63                 case 'l':   target = optarg;        break;
64                 case '?':   throw invalid_argument(string("unrecognized option"));
65             }
66     }
67 }
68
69 auto_ptr<sql::db> db;
70 unsigned entries(0);
71
72 int accumulate(void* to, int, char** what, char**) {
73     string& app = *static_cast<string*>(to);
74     if (app.size())
75         app += ", ";
76     app += *what;
77     return 0;
78 }
79
80 int showGloss(void* s, int, char** value, char**) {
81     string& sense = *static_cast<string*>(s);
82     if (sense != value[0]) {
83         sense = value[0];
84         cout << "  " << setw(2) << sense << ")  ";
85     }
86     else
87         cout << "       ";
88     cout << value[1] << endl;
89     return 0;
90 }
91
92 int showEntry(void*, int, char** value, char**) {
93     string kanji, kana;
94     db->exec(
95         sql::query("SELECT kanji FROM kanji WHERE entry=%s") % *value,
96         accumulate, &kanji);
97     db->exec(
98         sql::query("SELECT kana FROM reading WHERE entry=%s") % *value,
99         accumulate, &kana);
100
101     string rom;
102     kana2romaji(kana,rom);
103     if (kanji.size())
104         cout << kanji << " (" << kana << ") (" << rom << ')' << endl;
105     else
106         cout << kana << " (" << rom << ')' << endl;
107     
108     string sense;
109     db->exec(
110         sql::query("SELECT sense, gloss FROM gloss WHERE lang=%Q AND entry=%s "
111                    "ORDER BY sense") % options::target % *value,
112         showGloss, &sense);
113     ++entries;
114     return 0;
115 }
116
117 string compare() {
118     if (options::fulltext)
119         return " LIKE '%%%q%%'";
120     if (options::beginning)
121         return " LIKE '%q%%'";
122     if (options::ci_search)
123         return " LIKE %Q";
124     return "=%Q";
125 }
126
127 void fromRomaji(const string& r) {
128     db->exec(
129         sql::query("SELECT DISTINCT entry FROM reading WHERE romaji" + compare()) % r,
130         showEntry);
131 }
132
133 void fromJapanese(const string& j) {
134     db->exec(
135         sql::query("SELECT DISTINCT entry FROM reading WHERE kana" + compare()) % j,
136         showEntry);
137     db->exec(
138         sql::query("SELECT DISTINCT entry FROM kanji WHERE kanji" + compare()) % j,
139         showEntry);
140 }
141
142 void toJapanese(const string& e) {
143     sql::query q;
144     q = "SELECT DISTINCT entry FROM gloss WHERE lang=%Q AND gloss" + compare();
145     db->exec(q % options::target % e, showEntry);
146 }
147
148 void guessLanguage(const std::string& subject) {
149     bool isUTF8 = subject[0] & 0x80;
150     if (options::source == options::JAPANESE && !isUTF8)
151         options::source = options::JAPANESE_ROMAJI;
152     else if (options::source == options::UNKNOWN)
153         options::source = isUTF8 ? options::JAPANESE : options::NOT_JAPANESE;
154 }
155
156 int main(int argc, char** argv)
157 try {
158     initRomaji();
159     options::getFrom(argc, argv);
160     if (optind == argc) {
161         usage();
162         return EXIT_FAILURE;
163     }
164     string subject = argv[optind];
165     db.reset(new sql::db(DICTIONARY_PATH));
166     
167     guessLanguage(subject);
168     if (options::source == options::JAPANESE)
169         fromJapanese(subject);
170     else if (options::source == options::JAPANESE_ROMAJI)
171         fromRomaji(subject);
172     else
173         toJapanese(subject);
174     cout << entries << " match(es) found." << endl;
175
176     return EXIT_SUCCESS;
177 }
178 catch(const std::exception& e)
179 {
180         cerr << e.what() << '\n';
181         return EXIT_FAILURE;
182 }