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{
12 int opt;
13 char* txt = NULL;
14 char* db = NULL;
15
16 while ((opt = getopt(argc, argv, "t:d:h")) != -1) {
17 switch(opt) {
18 case 't':
19 txt = copy_achar(optarg);
20 break;
21 case 'd':
22 db = copy_achar(optarg);
23 break;
24 case 'h':
25 // fall through
26 default:
27 printf("Usage: %s", argv[0]);
28 goto end;
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{
45 char * line = NULL;
46 size_t len = 0;
47 int count = 0;
48 ssize_t read;
49 Data *data;
50 FILE *f;
51 int total;
52
53 printf("\33[?25l"); // hide cursor
54
55 data = new_data(":memory:");
56 f = fopen(txt, "r");
57
58 bootstrap(data);
59
60 total = count_file_lines(f);
61 fseek(f, 0, SEEK_SET);
62
63 while ((read = getline(&line, &len, f)) != -1) {
64 if (line[0] == '#' || line[0] == '\n')
65 continue;
66
67 insert(data, line, read-1);
68 count++;
69
70 if ((count % 321) == 0) {
71 float t = ((float)count/(float)total)*100;
72 printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
73 }
74 }
75
76 float t = ((float)count/(float)total)*100;
77 printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
78 int r = load_or_save_db(data->db, db, 1);
79
80 printf("\rDONE!");
81 printf("\33[?25h"); // reenable cursor
82
83 return r;
84}