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
|
/*
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <unistd.h>
#include <exception>
#include <fstream>
#include <iostream>
#include "libime/core/historybigram.h"
void usage(const char *argv0) {
std::cout << "Usage: " << argv0 << " [-c] <source> <dest>" << std::endl
<< "-c: Compile text to binary" << std::endl
<< "-h: Show this help" << std::endl;
}
int main(int argc, char *argv[]) {
bool compile = false;
int c;
while ((c = getopt(argc, argv, "ch")) != -1) {
switch (c) {
case 'c':
compile = true;
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;
HistoryBigram history;
try {
std::ifstream in(argv[optind], std::ios::in | std::ios::binary);
if (compile) {
history.loadText(in);
} else {
history.load(in);
}
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 (compile) {
history.save(*out);
} else {
history.dump(*out);
}
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
|