1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4
5#include "../lib/util.h"
6#include "../lib/data.h"
7
8int run(const char *db, const char *txt);
9
10int main(int argc, char** argv) {
11 int opt;
12 char* txt = NULL;
13 char* db = NULL;
14
15 while ((opt = getopt(argc, argv, "t:d:h")) != -1) {
16 switch(opt) {
17 case 't':
18 txt = copy_achar(optarg);
19 break;
20 case 'd':
21 db = copy_achar(optarg);
22 break;
23 case 'h':
24 // fall through
25 default:
26 printf("Usage: %s", argv[0]);
27 goto end;
28 }
29 }
30
31
32 int r = run(db, txt);
33
34end:
35 if (txt != NULL)
36 free(txt);
37 if (db != NULL)
38 free(db);
39
40 return r;
41}
42
43int run(const char *db, const char *txt) {
44 char * line = NULL;
45 size_t len = 0;
46 int count = 0;
47 ssize_t read;
48 Data *data;
49 FILE *f;
50 int total;
51
52 data = new_data(":memory:");
53 f = fopen(txt, "r");
54
55 bootstrap(data);
56
57 total = count_file_lines(f);
58 fseek(f, 0, SEEK_SET);
59
60 while ((read = getline(&line, &len, f)) != -1) {
61 if (line[0] == '#' || line[0] == '\n')
62 continue;
63
64 insert(data, line, read-1);
65
66 float t = ((float)count/(float)total)*100;
67 printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
68 }
69
70 return load_or_save_db(data->db, db, 1);
71}