aboutsummaryrefslogtreecommitdiff
path: root/dict/main.c
blob: e5573fea55a25a90f4ad79493f38ec58e3886a3b (plain)
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
104
105
106
107
108
109
110
111
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <sqlite3.h>
#include <ncurses.h>

#include "../lib/data.h"
#include "../lib/ui.h"
#include "../lib/util.h"

Data *data;

void search(char*, int);
int run(const char*, const char*);

int main(int argc, char** argv) {
    int opt;
    char* txt = NULL;
    char* db = NULL;

    while ((opt = getopt(argc, argv, "t:d:h")) != -1) {
        switch(opt) {
            case 't':
                txt = copy_achar(optarg);
                break;
            case 'd':
                db = copy_achar(optarg);
                break;
            case 'h':
                  // fall through
            default:
                printf("Usage: %s", argv[0]);
                goto end;
        }
    }

    int r = run(db, txt);

end:
    if (txt != NULL)
        free(txt);
    if (db != NULL)
        free(db);

    return r;
}

int run(const char *db, const char *txt) {
    data = new_data(db);
    bootstrap(data);

    setlocale(LC_ALL, "");
    initscr();
    noecho();
    cbreak();
    keypad(stdscr, TRUE);

    FILE *f = fopen(txt, "r");
    unsigned int lines = count_file_lines(f);
    fseek(f, 0, SEEK_SET);

    char * line = NULL;
    size_t len = 0;
    ssize_t read;
    PROGRESS_BAR *bar = new_progress_bar(stdscr, lines);
    while ((read = getline(&line, &len, f)) != -1) {
        if (line[0] == '#' || line[0] == '\n')
            continue;

        insert(data, line, read-1);
        bar_step(bar, 1);
    }

    move(2,0);
    printw("Saving db...");
    refresh();
    load_or_save_db(data->db, "backup.db", 1);

    clear();
    refresh();

    TEXT_BOX *box = new_text_box(stdscr, 100);
    get_char(box, search);

    clear();
    refresh();

    endwin();

    free_data(data);
    return 0;
}

void search(char *sch, int len) {
    char s[len+2];

    sprintf(s, "%%%*s%%", len, sch);

    LIST* l = data_select(data, s, len+2);

    for (int y = 1; y < 20; y++) {
        move(y, 0);
        Word *item = (Word*)list_get(l, y);
        if (item != NULL)
            printw("%s", item->Line);
    }
    refresh();
}