File: libime_tabledict.cpp

package info (click to toggle)
libime 1.1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,612 kB
  • sloc: cpp: 40,794; ansic: 952; python: 130; sh: 32; makefile: 11
file content (103 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <unistd.h>
#include <fstream>
#include <iostream>
#include <optional>
#include <string>
#include "libime/table/tablebaseddictionary.h"

void usage(const char *argv0) {
    std::cout << "Usage: " << argv0
              << " [-due] [-m <main dict>] <source> <dest>" << std::endl
              << "-d: Dump binary to text" << std::endl
              << "-u: User dict" << std::endl
              << "-e: Extra dict" << std::endl
              << "-m <path/to/main.dict>: Main dict to be used with extra dict"
              << std::endl
              << "-h: Show this help" << std::endl;
}

int main(int argc, char *argv[]) {

    bool dump = false;
    bool user = false;
    bool extra = false;
    std::optional<std::string> extraMain = std::nullopt;
    int c;
    while ((c = getopt(argc, argv, "dhuem:")) != -1) {
        switch (c) {
        case 'd':
            dump = true;
            break;
        case 'u':
            user = true;
            break;
        case 'e':
            extra = true;
            break;
        case 'm':
            extraMain = std::string(optarg);
            break;
        case 'h':
            usage(argv[0]);
            return 0;
        default:
            usage(argv[0]);
            return 1;
        }
    }

    if (optind + 2 != argc) {
        usage(argv[0]);
        return 1;
    }
    using namespace libime;
    TableBasedDictionary dict;

    std::ifstream fin;
    std::istream *in;
    if (strcmp(argv[optind], "-") == 0) {
        in = &std::cin;
    } else {
        fin.open(argv[optind], std::ios::in | std::ios::binary);
        in = &fin;
    }

    const auto inputFormat = dump ? TableFormat::Binary : TableFormat::Text;
    const auto outputFormat = dump ? TableFormat::Text : TableFormat::Binary;
    size_t extraIndex = 0;
    if (extra && extraMain) {
        dict.load(extraMain->c_str(), libime::TableFormat::Binary);
    }

    if (extra) {
        extraIndex = dict.loadExtra(*in, inputFormat);
    } else if (user) {
        dict.loadUser(*in, inputFormat);
    } else {
        dict.load(*in, inputFormat);
    }

    std::ofstream fout;
    std::ostream *out;
    if (strcmp(argv[optind + 1], "-") == 0) {
        out = &std::cout;
    } else {
        fout.open(argv[optind + 1], std::ios::out | std::ios::binary);
        out = &fout;
    }

    if (extra) {
        dict.saveExtra(extraIndex, *out, outputFormat);
    } else if (user) {
        dict.saveUser(*out, outputFormat);
    } else {
        dict.save(*out, outputFormat);
    }
    return 0;
}