diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | dict/main.c | 53 | ||||
-rw-r--r-- | importer/main.c | 6 | ||||
-rw-r--r-- | lib/data.c | 6 | ||||
-rw-r--r-- | lib/data.h | 2 | ||||
-rw-r--r-- | lib/ui.c | 20 | ||||
-rw-r--r-- | lib/ui.h | 9 |
7 files changed, 49 insertions, 48 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a88fc55..63c9d1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,3 +5,4 @@ project(dict VERSION 0.1 LANGUAGES C) add_subdirectory(ext) add_subdirectory(importer) add_subdirectory(lib) +add_subdirectory(dict) diff --git a/dict/main.c b/dict/main.c index b743e07..98608cc 100644 --- a/dict/main.c +++ b/dict/main.c @@ -10,21 +10,19 @@ #include "../lib/util.h" Data *data; +WINDOW* tpanel; +PANEL* panel; void search(char*, int); -int run(const char*, const char*); +int run(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; @@ -36,21 +34,19 @@ int main(int argc, char** argv) } } - int r = run(db, txt); + int r = run(db); end: - if (txt != NULL) - free(txt); if (db != NULL) free(db); return r; } -int run(const char *db, const char *txt) +int run(const char *db) { - data = new_data(db); - bootstrap(data); + data = new_data(":memory:"); + load_or_save_db(data->db, db, 0); setlocale(LC_ALL, ""); initscr(); @@ -58,34 +54,14 @@ int run(const char *db, const char *txt) 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); + WINDOW* tbox = newwin(3,COLS,0,0); + TEXT_BOX *box = new_text_box(tbox, 100); - clear(); - refresh(); + tpanel = newwin(LINES-3, COLS, 3,0); + panel = new_panel(tpanel); - TEXT_BOX *box = new_text_box(stdscr, 100); get_char(box, search); - clear(); refresh(); endwin(); @@ -100,13 +76,12 @@ void search(char *sch, int len) sprintf(s, "%%%*s%%", len, sch); - LIST* l = data_select(data, s, len+2); + LIST* l = data_select(data, s, len+2, LINES-5); - for (int y = 1; y < 20; y++) { + for (int y = 0; y < (LINES-5); y++) { move(y, 0); Word *item = (Word*)list_get(l, y); if (item != NULL) - printw("%s", item->Line); + write_char(panel, y, (char*)item->Line); } - refresh(); } diff --git a/importer/main.c b/importer/main.c index b83e850..087fc48 100644 --- a/importer/main.c +++ b/importer/main.c @@ -67,10 +67,8 @@ int run(const char *db, const char *txt) insert(data, line, read-1); count++; - if ((count % 321) == 0) { - float t = ((float)count/(float)total)*100; - printf("\rLoading data [%03.0f%%] %d/%d", t, count, total); - } + float t = ((float)count/(float)total)*100; + printf("\rLoading data [%03.0f%%] %d/%d", t, count, total); } float t = ((float)count/(float)total)*100; @@ -3,9 +3,10 @@ #include <stdio.h> #include "data.h" +#include "../lib/util.h" const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);"; -const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT 10;"; +const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT $NNN;"; const char *create_table = "CREATE TABLE IF NOT EXISTS words (ID INTEGER PRIMARY KEY AUTOINCREMENT, LINE TEXT NOT NULL);"; Data* new_data(const char* con) @@ -80,7 +81,7 @@ void bootstrap(Data* data) sqlite3_finalize(stmt); } -LIST* data_select(Data* data, char *sch, int len) +LIST* data_select(Data* data, char *sch, int len, int limit) { sqlite3_stmt *stmt; int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL); @@ -95,6 +96,7 @@ LIST* data_select(Data* data, char *sch, int len) LIST *list = NULL; sqlite3_bind_text(stmt, 1, sch, len, NULL); + sqlite3_bind_int(stmt, 2, limit); int m = sqlite3_step(stmt); while(m == SQLITE_ROW) { @@ -39,7 +39,7 @@ void insert(Data*, char*, int); /* * Select all words. */ -LIST* data_select(Data*, char*, int); +LIST* data_select(Data*, char*, int, int); /* * Print result code from sqlite. @@ -56,6 +56,7 @@ TEXT_BOX* new_text_box(WINDOW* scr, int length) text->current = 0; text->text = malloc(sizeof(char)*(length+1)); memset(text->text, '\0', length); + box(scr, 0,0); return text; } @@ -82,8 +83,23 @@ void get_char(TEXT_BOX* text, void (*sch)(char*, int)) wcstombs(str, text->text, sizeof(text->text)); sch(str, (int)strlen(str)); - move(0,0); - wrefresh(text->scr); + wmove(text->scr,1,1); wprintw(text->scr, "%*ls", text->current,text->text); + wrefresh(text->scr); } } + +PANEL* new_panel(WINDOW* scr) +{ + PANEL *panel = (PANEL*)malloc(sizeof(PANEL)); + panel->scr = scr; + box(scr, 0,0); + return panel; +} +void write_char(PANEL* panel, int l, char *text) +{ + int x = getmaxx(panel->scr); + wmove(panel->scr, l+1, 1); + wprintw(panel->scr, "%.*s", x-3, text); + wrefresh(panel->scr); +} @@ -21,3 +21,12 @@ typedef struct text_box TEXT_BOX* new_text_box(WINDOW*, int); void get_char(TEXT_BOX* text, void (*sch)(char*, int)); + +typedef struct panel +{ + WINDOW *scr; +} PANEL; + + +PANEL* new_panel(WINDOW*); +void write_char(PANEL*, int, char*); |