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 printf("\33[?25l"); // hide cursor
53
54 data = new_data(":memory:");
55 f = fopen(txt, "r");
56
57 bootstrap(data);
58
59 total = count_file_lines(f);
60 fseek(f, 0, SEEK_SET);
61
62 while ((read = getline(&line, &len, f)) != -1) {
63 if (line[0] == '#' || line[0] == '\n')
64 continue;
65
66 insert(data, line, read-1);
67 count++;
68
69 if ((count % 321) == 0){
70 float t = ((float)count/(float)total)*100;
71 printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
72 }
73 }
74
75 float t = ((float)count/(float)total)*100;
76 printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
77 int r = load_or_save_db(data->db, db, 1);
78
79 printf("\rDONE!");
80 printf("\33[?25h"); // reenable cursor
81
82 return r;
83}