Cleanup
[jmdict-cli.git] / jmdict.cpp
index 2d761100f77eccad2335aef12d0eefbc20aae707..b639756a970cdb0d6ad776d4f1167087dd0a1d95 100644 (file)
@@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <stdexcept>
 #include <exception>
 #include <memory>
+#include <unistd.h>
 #include "sqlite.h"
 #include "kana2romaji.h"
 using namespace std;
@@ -33,6 +34,7 @@ void usage() {
             "  -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"
@@ -50,22 +52,20 @@ namespace options {
     bool fulltext = false;
     bool beginning = false;
     bool ci_search = false;
-
-    class invalid_option : public std::runtime_error {
-           invalid_option(const string& s) : std::runtime_error(s) {}
-    };
+    bool show_romaji = false;
 
     void getFrom(int argc, char** argv) {
         int opt;
-        while ((opt = getopt(argc, argv, "bfijJl:")) != -1)
+        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"));
+                case '?':   throw invalid_argument(string("unrecognized option"));
             }
     }
 }
@@ -102,12 +102,19 @@ int showEntry(void*, int, char** value, char**) {
         sql::query("SELECT kana FROM reading WHERE entry=%s") % *value,
         accumulate, &kana);
 
-    string rom;
-    kana2romaji(kana,rom);
     if (kanji.size())
-        cout << kanji << " (" << kana << ") (" << rom << ')' << endl;
+        cout << kanji << " (" << kana << ')';
     else
-        cout << kana << " (" << rom << ')' << endl;
+        cout << kana;
+
+    if(options::show_romaji) {
+        string rom;
+        kana2romaji(kana,rom);
+
+        cout << " (" << rom << ')';
+    }
+
+    cout << endl;
     
     string sense;
     db->exec(
@@ -154,7 +161,7 @@ void guessLanguage(const std::string& subject) {
     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;
+        options::source = isUTF8 ? options::JAPANESE : options::UNKNOWN;
 }
 
 int main(int argc, char** argv)
@@ -173,14 +180,18 @@ try {
         fromJapanese(subject);
     else if (options::source == options::JAPANESE_ROMAJI)
         fromRomaji(subject);
-    else
+    else if (options::source == options::NOT_JAPANESE)
+        toJapanese(subject);
+    else { /* options::UNKNOWN */
+        fromRomaji(subject);
         toJapanese(subject);
+    }
     cout << entries << " match(es) found." << endl;
 
     return EXIT_SUCCESS;
 }
 catch(const std::exception& e)
 {
-       cerr << e.what() << '\n';
-       return EXIT_FAILURE;
+    cerr << e.what() << '\n';
+    return EXIT_FAILURE;
 }