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
|
#include <locale.h>
#include <stdio.h>
#include <sqlite3.h>
#include <ncurses.h>
#include "data.h"
#include "ui.h"
#define BUF_SIZE 100
unsigned int count_lines(FILE* file);
int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave);
int main() {
setlocale(LC_ALL, "");
initscr();
noecho();
cbreak();
Data *data = new_data(":memory:");
bootstrap(data);
FILE *f = fopen("dict.txt", "r");
unsigned int lines = count_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();
endwin();
free_data(data);
return 0;
}
int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave){
int rc; /* Function return code */
sqlite3 *pFile; /* Database connection opened on zFilename */
sqlite3_backup *pBackup; /* Backup object used to copy data */
sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */
sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */
rc = sqlite3_open(zFilename, &pFile);
if( rc==SQLITE_OK ){
pFrom = (isSave ? pInMemory : pFile);
pTo = (isSave ? pFile : pInMemory);
pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
if( pBackup ){
(void)sqlite3_backup_step(pBackup, -1);
(void)sqlite3_backup_finish(pBackup);
}
rc = sqlite3_errcode(pTo);
}
(void)sqlite3_close(pFile);
return rc;
}
unsigned int count_lines(FILE* file)
{
char buf[BUF_SIZE];
unsigned int counter = 0;
for(;;)
{
size_t res = fread(buf, 1, BUF_SIZE, file);
if (ferror(file))
return -1;
size_t i;
for(i = 0; i < res; i++)
if (buf[i] == '\n')
counter++;
if (feof(file))
break;
}
return counter;
}
|