summaryrefslogtreecommitdiff
path: root/jmdict.cpp
blob: a51f61bf3cf9957ff1a52c6fd133a1690ba656f4 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
jmdict, a frontend to the JMdict file. http://mandrill.fuxx0r.net/jmdict.php
Copyright (C) 2004 Florian Bluemel (florian.bluemel@uni-dortmund.de)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <iomanip>
#include <string>
#include <stdexcept>
#include <exception>
#include <memory>
#include "sqlite.h"
#include "kana2romaji.h"
using namespace std;

void usage() {
    cout << "jmdict [options] subject\n"
            "  -b        search for entries beginning with <subject>\n"
            "  -f        perform a fulltext search\n"
            "  -i        case-insensitive search (implied by -b or -f)\n"
            "  -r        also translate kana to romaji\n"
            "\n"
            "  -j        translate from japanese\n"
            "  -J        translate to japanese\n"
            "            if neither -j nor -J is given, source language will be guessed\n"
            "\n"
            "  -l lang   target language is lang, where lang is a three-letter language code\n"
            "            default: eng\n";
}

namespace options {
    enum Language { UNKNOWN, JAPANESE, JAPANESE_ROMAJI, NOT_JAPANESE };

    Language source = UNKNOWN;
    string target("eng");
    bool fulltext = false;
    bool beginning = false;
    bool ci_search = false;
    bool show_romaji = false;

    void getFrom(int argc, char** argv) {
        int opt;
        while ((opt = getopt(argc, argv, "bfirjJl:")) != -1)
            switch (opt) {
                case 'b':   beginning = true;       break;
                case 'f':   fulltext = true;        break;
                case 'i':   ci_search = true;       break;
                case 'r':   show_romaji = true;     break;
                case 'j':   source = JAPANESE;      break;
                case 'J':   source = NOT_JAPANESE;  break;
                case 'l':   target = optarg;        break;
                case '?':   throw invalid_argument(string("unrecognized option"));
            }
    }
}

auto_ptr<sql::db> db;
unsigned entries(0);

int accumulate(void* to, int, char** what, char**) {
    string& app = *static_cast<string*>(to);
    if (app.size())
        app += ", ";
    app += *what;
    return 0;
}

int showGloss(void* s, int, char** value, char**) {
    string& sense = *static_cast<string*>(s);
    if (sense != value[0]) {
        sense = value[0];
        cout << "  " << setw(2) << sense << ")  ";
    }
    else
        cout << "       ";
    cout << value[1] << endl;
    return 0;
}

int showEntry(void*, int, char** value, char**) {
    string kanji, kana;
    db->exec(
        sql::query("SELECT kanji FROM kanji WHERE entry=%s") % *value,
        accumulate, &kanji);
    db->exec(
        sql::query("SELECT kana FROM reading WHERE entry=%s") % *value,
        accumulate, &kana);

    if (kanji.size())
        cout << kanji << " (" << kana << ')';
    else
        cout << kana;

    if(options::show_romaji) {
        string rom;
        kana2romaji(kana,rom);

        cout << " (" << rom << ')';
    }

    cout << endl;
    
    string sense;
    db->exec(
        sql::query("SELECT sense, gloss FROM gloss WHERE lang=%Q AND entry=%s "
                   "ORDER BY sense") % options::target % *value,
        showGloss, &sense);
    ++entries;
    return 0;
}

string compare() {
    if (options::fulltext)
        return " LIKE '%%%q%%'";
    if (options::beginning)
        return " LIKE '%q%%'";
    if (options::ci_search)
        return " LIKE %Q";
    return "=%Q";
}

void fromRomaji(const string& r) {
    db->exec(
        sql::query("SELECT DISTINCT entry FROM reading WHERE romaji" + compare()) % r,
        showEntry);
}

void fromJapanese(const string& j) {
    db->exec(
        sql::query("SELECT DISTINCT entry FROM reading WHERE kana" + compare()) % j,
        showEntry);
    db->exec(
        sql::query("SELECT DISTINCT entry FROM kanji WHERE kanji" + compare()) % j,
        showEntry);
}

void toJapanese(const string& e) {
    sql::query q;
    q = "SELECT DISTINCT entry FROM gloss WHERE lang=%Q AND gloss" + compare();
    db->exec(q % options::target % e, showEntry);
}

void guessLanguage(const std::string& subject) {
    bool isUTF8 = subject[0] & 0x80;
    if (options::source == options::JAPANESE && !isUTF8)
        options::source = options::JAPANESE_ROMAJI;
    else if (options::source == options::UNKNOWN)
        options::source = isUTF8 ? options::JAPANESE : options::NOT_JAPANESE;
}

int main(int argc, char** argv)
try {
    initRomaji();
    options::getFrom(argc, argv);
    if (optind == argc) {
        usage();
        return EXIT_FAILURE;
    }
    string subject = argv[optind];
    db.reset(new sql::db(DICTIONARY_PATH));
    
    guessLanguage(subject);
    if (options::source == options::JAPANESE)
        fromJapanese(subject);
    else if (options::source == options::JAPANESE_ROMAJI)
        fromRomaji(subject);
    else
        toJapanese(subject);
    cout << entries << " match(es) found." << endl;

    return EXIT_SUCCESS;
}
catch(const std::exception& e)
{
    cerr << e.what() << '\n';
    return EXIT_FAILURE;
}