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