1#include <stdio.h>
2#include <sys/stat.h>
3#include <string.h>
4
5#include "utils/Log.h"
6
7#include <vector>
8#include <minikin/Hyphenator.h>
9
10using android::Hyphenator;
11
12Hyphenator* loadHybFile(const char* fn) {
13    struct stat statbuf;
14    int status = stat(fn, &statbuf);
15    if (status < 0) {
16        fprintf(stderr, "error opening %s\n", fn);
17        return nullptr;
18    }
19    size_t size = statbuf.st_size;
20    FILE* f = fopen(fn, "rb");
21    if (f == NULL) {
22        fprintf(stderr, "error opening %s\n", fn);
23        return nullptr;
24    }
25    uint8_t* buf = new uint8_t[size];
26    size_t read_size = fread(buf, 1, size, f);
27    if (read_size < size) {
28        fprintf(stderr, "error reading %s\n", fn);
29        delete[] buf;
30        return nullptr;
31    }
32    return Hyphenator::loadBinary(buf);
33}
34
35int main(int argc, char** argv) {
36    Hyphenator* hyph = loadHybFile("/tmp/en.hyb");  // should also be configurable
37    std::vector<uint8_t> result;
38    std::vector<uint16_t> word;
39    if (argc < 2) {
40        fprintf(stderr, "usage: hyphtool word\n");
41        return 1;
42    }
43    char* asciiword = argv[1];
44    size_t len = strlen(asciiword);
45    for (size_t i = 0; i < len; i++) {
46        uint32_t c = asciiword[i];
47        if (c == '-') {
48            c = 0x00AD;
49        }
50        // ASCII (or possibly ISO Latin 1), but kinda painful to do utf conversion :(
51        word.push_back(c);
52    }
53    hyph->hyphenate(&result, word.data(), word.size());
54    for (size_t i = 0; i < len; i++) {
55        if (result[i] != 0) {
56            printf("-");
57        }
58        printf("%c", word[i]);
59    }
60    printf("\n");
61    return 0;
62}
63