Initial import.
[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     class invalid_option : public std::runtime_error {
55             invalid_option(const string& s) : std::runtime_error(s) {}
56     };
57
58     void getFrom(int argc, char** argv) {
59         int opt;
60         while ((opt = getopt(argc, argv, "bfijJl:")) != -1)
61             switch (opt) {
62                 case 'b':   beginning = true;       break;
63                 case 'f':   fulltext = true;        break;
64                 case 'i':   ci_search = true;       break;
65                 case 'j':   source = JAPANESE;      break;
66                 case 'J':   source = NOT_JAPANESE;  break;
67                 case 'l':   target = optarg;        break;
68                 case '?':   throw invalid_argument(string("unrecognized option"));
69             }
70     }
71 }
72
73 auto_ptr<sql::db> db;
74 unsigned entries(0);
75
76 int accumulate(void* to, int, char** what, char**) {
77     string& app = *static_cast<string*>(to);
78     if (app.size())
79         app += ", ";
80     app += *what;
81     return 0;
82 }
83
84 int showGloss(void* s, int, char** value, char**) {
85     string& sense = *static_cast<string*>(s);
86     if (sense != value[0]) {
87         sense = value[0];
88         cout << "  " << setw(2) << sense << ")  ";
89     }
90     else
91         cout << "       ";
92     cout << value[1] << endl;
93     return 0;
94 }
95
96 int showEntry(void*, int, char** value, char**) {
97     string kanji, kana;
98     db->exec(
99         sql::query("SELECT kanji FROM kanji WHERE entry=%s") % *value,
100         accumulate, &kanji);
101     db->exec(
102         sql::query("SELECT kana FROM reading WHERE entry=%s") % *value,
103         accumulate, &kana);
104
105     string rom;
106     kana2romaji(kana,rom);
107     if (kanji.size())
108         cout << kanji << " (" << kana << ") (" << rom << ')' << endl;
109     else
110         cout << kana << " (" << rom << ')' << endl;
111     
112     string sense;
113     db->exec(
114         sql::query("SELECT sense, gloss FROM gloss WHERE lang=%Q AND entry=%s "
115                    "ORDER BY sense") % options::target % *value,
116         showGloss, &sense);
117     ++entries;
118     return 0;
119 }
120
121 string compare() {
122     if (options::fulltext)
123         return " LIKE '%%%q%%'";
124     if (options::beginning)
125         return " LIKE '%q%%'";
126     if (options::ci_search)
127         return " LIKE %Q";
128     return "=%Q";
129 }
130
131 void fromRomaji(const string& r) {
132     db->exec(
133         sql::query("SELECT DISTINCT entry FROM reading WHERE romaji" + compare()) % r,
134         showEntry);
135 }
136
137 void fromJapanese(const string& j) {
138     db->exec(
139         sql::query("SELECT DISTINCT entry FROM reading WHERE kana" + compare()) % j,
140         showEntry);
141     db->exec(
142         sql::query("SELECT DISTINCT entry FROM kanji WHERE kanji" + compare()) % j,
143         showEntry);
144 }
145
146 void toJapanese(const string& e) {
147     sql::query q;
148     q = "SELECT DISTINCT entry FROM gloss WHERE lang=%Q AND gloss" + compare();
149     db->exec(q % options::target % e, showEntry);
150 }
151
152 void guessLanguage(const std::string& subject) {
153     bool isUTF8 = subject[0] & 0x80;
154     if (options::source == options::JAPANESE && !isUTF8)
155         options::source = options::JAPANESE_ROMAJI;
156     else if (options::source == options::UNKNOWN)
157         options::source = isUTF8 ? options::JAPANESE : options::NOT_JAPANESE;
158 }
159
160 int main(int argc, char** argv)
161 try {
162     initRomaji();
163     options::getFrom(argc, argv);
164     if (optind == argc) {
165         usage();
166         return EXIT_FAILURE;
167     }
168     string subject = argv[optind];
169     db.reset(new sql::db(DICTIONARY_PATH));
170     
171     guessLanguage(subject);
172     if (options::source == options::JAPANESE)
173         fromJapanese(subject);
174     else if (options::source == options::JAPANESE_ROMAJI)
175         fromRomaji(subject);
176     else
177         toJapanese(subject);
178     cout << entries << " match(es) found." << endl;
179
180     return EXIT_SUCCESS;
181 }
182 catch(const std::exception& e)
183 {
184         cerr << e.what() << '\n';
185         return EXIT_FAILURE;
186 }