From 6dd0c4747aa57227b5898fc639e3f2b643ce013c Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Mon, 15 Apr 2024 22:16:28 +0200 Subject: feat: Remove C implementation --- lib/CMakeLists.txt | 5 --- lib/data.c | 125 ----------------------------------------------------- lib/data.h | 47 -------------------- lib/list.c | 52 ---------------------- lib/list.h | 30 ------------- lib/ui.c | 105 -------------------------------------------- lib/ui.h | 32 -------------- lib/util.c | 62 -------------------------- lib/util.h | 13 ------ 9 files changed, 471 deletions(-) delete mode 100644 lib/CMakeLists.txt delete mode 100644 lib/data.c delete mode 100644 lib/data.h delete mode 100644 lib/list.c delete mode 100644 lib/list.h delete mode 100644 lib/ui.c delete mode 100644 lib/ui.h delete mode 100644 lib/util.c delete mode 100644 lib/util.h (limited to 'lib') diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt deleted file mode 100644 index 023cdf1..0000000 --- a/lib/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -file(GLOB lib CONFIGURE_DEPENDS "*.h" "*.c") - -add_library(lib ${lib}) -target_compile_options(lib PRIVATE -Wall -Wextra -Wpedantic -Werror) -target_link_libraries(lib sqlite3 ncursesw m c) diff --git a/lib/data.c b/lib/data.c deleted file mode 100644 index afbbbb1..0000000 --- a/lib/data.c +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include - -#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 MATCH $VVV LIMIT $NNN;"; -const char *create_table = "CREATE VIRTUAL TABLE IF NOT EXISTS words USING fts4 (ID INTEGER PRIMARY KEY AUTOINCREMENT, LINE TEXT NOT NULL);"; - -Data* new_data(const char* con) -{ - Data* data = (Data*)malloc(sizeof(Data)); - - int v = sqlite3_open(con, &(data->db)); - if (v != SQLITE_OK) { - print_result_code(v); - return NULL; - } - - sqlite3_enable_load_extension(data->db, 1); - v = sqlite3_load_extension(data->db, "ext/libsqlite3ext", "sqlite3_spellfix_init",0); - if (v != SQLITE_OK) { - print_result_code(v); - return NULL; - } - - return data; -} - -void free_data(Data* data) -{ - sqlite3_close(data->db); - free(data); -} - -void insert(Data* data, char* line, int len) -{ - sqlite3_stmt *stmt; - int r = sqlite3_prepare_v2(data->db, insert_into, -1, &stmt, NULL); - - if (r != SQLITE_OK) { - printf("Error executing insert: "); - print_result_code(r); - printf("\n"); - return; - } - - sqlite3_bind_text(stmt, 1, line, len, NULL); - - int c = sqlite3_step(stmt); - if (c != SQLITE_DONE) { - printf("Error executing insert: "); - print_result_code(r); - printf("\n"); - } - - sqlite3_finalize(stmt); -} - -void bootstrap(Data* data) -{ - sqlite3_stmt *stmt; - int r = sqlite3_prepare_v2(data->db, create_table, -1, &stmt, NULL); - - if (r != SQLITE_OK) { - printf("Error preparing bootstrap: "); - print_result_code(r); - printf("\n"); - return; - } - - int c = sqlite3_step(stmt); - if (c != SQLITE_DONE) { - printf("Error executing bootstrap: "); - print_result_code(r); - printf("\n"); - } - - sqlite3_finalize(stmt); -} - -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); - - if (r != SQLITE_OK) { - printf("Error executing select: "); - print_result_code(r); - printf("\n"); - return NULL; - } - - 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) { - Word *word = (Word*)malloc(sizeof(Word)); - - int id = sqlite3_column_int(stmt, 0); - const unsigned char *line = sqlite3_column_text(stmt, 1); - unsigned char *line2 = malloc(sizeof(char*)+strlen((char*)line)); - memcpy(line2, line, strlen((char*)line)); - - word->Id = id; - word->Line = line2; - list = list_add(list, word); - - m = sqlite3_step(stmt); - } - - sqlite3_finalize(stmt); - - return list; -} - -void print_result_code(int code) -{ - printf(sqlite3_errstr(code)); -} diff --git a/lib/data.h b/lib/data.h deleted file mode 100644 index 6c9f30f..0000000 --- a/lib/data.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include -#include "list.h" - -/* - * This word into the dictionary - */ -typedef struct word -{ - int Id; - const unsigned char *Line; -} Word; - -/* - * This is database connection. - */ -typedef struct data -{ - sqlite3 *db; -} Data; - -/* - * create a new data struct from sqlite filename. - */ -Data* new_data(const char*); - -void free_data(Data*); - -/* - * Create the tables. - */ -void bootstrap(Data*); - -/* - * insert line into database. - */ -void insert(Data*, char*, int); - -/* - * Select all words. - */ -LIST* data_select(Data*, char*, int, int); - -/* - * Print result code from sqlite. - */ -void print_result_code(int error); diff --git a/lib/list.c b/lib/list.c deleted file mode 100644 index be1ac61..0000000 --- a/lib/list.c +++ /dev/null @@ -1,52 +0,0 @@ -#include "list.h" -#include - -LIST* list_add(LIST* list, void* item) -{ - - if (list == NULL) { - list = (LIST*)malloc(sizeof(LIST)); - list->size = 0; - list->list = (void**)malloc(sizeof(0)); - - } - - list->size ++; - void** new_list = (void**)reallocarray(list->list, list->size, sizeof(void*)); - - new_list[list->size-1] = item; - list->list = new_list; - - return list; - -} - -LIST* list_remove(LIST* list, unsigned int pos) -{ - for(unsigned int i = pos; i < list->size - 1; i++) - list->list[i] = list->list[i + 1]; - - list->size--; - - void** new_list = reallocarray(list->list, list->size, sizeof(void*)); - list->list = new_list; - - return list; -} - -void list_free(LIST* list) -{ - free(list->list); - free(list); -} - -void *list_get(LIST *list, unsigned int index) -{ - if (list == NULL) - return NULL; - - if (index < list->size) - return list->list[index]; - - return NULL; -} diff --git a/lib/list.h b/lib/list.h deleted file mode 100644 index 18bc423..0000000 --- a/lib/list.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include - -#define LIST_SIZE_FACTOR 1.5 -struct list -{ - unsigned int size; - unsigned int allocated_size; - void** list; -}; - -typedef struct list LIST; - -/** -* Add an item to a list -* @list: array list structure. -* @item: item to be added to the list. -*/ -LIST* list_add(LIST* list, void* item); - -/** -* Remove an item from a given list -* @list: array list structure. -* @pos: position of item to be removed. -*/ -LIST *list_remove(LIST *list, unsigned int pos); - -void list_free(LIST* list); - -void *list_get(LIST *list, unsigned int index); diff --git a/lib/ui.c b/lib/ui.c deleted file mode 100644 index cd54cd4..0000000 --- a/lib/ui.c +++ /dev/null @@ -1,105 +0,0 @@ -#define NCURSES_WIDECHAR 1 - -#include -#include -#include -#include -#include -#include "ui.h" - -const char *uload = "█"; - -PROGRESS_BAR* new_progress_bar(WINDOW* scr, float total) -{ - PROGRESS_BAR *bar = (PROGRESS_BAR*)malloc(sizeof(PROGRESS_BAR)); - bar->scr = scr; - bar->total = total; - bar->current = 0; - return bar; -} - -void bar_step(PROGRESS_BAR* bar, float step) -{ - bar->current += step; - - int x, y; - int hx, hy; - - getmaxyx(bar->scr, y, x); - - hx = x/2; - hy = y/2; - - float total = (bar->current/bar->total); - - wmove(bar->scr, hy-1, 0); - for (int i = 0; i < ((float)x*total); i++) - wprintw(bar->scr, uload); - - wmove(bar->scr, hy, hx-4); - wprintw(bar->scr,"%03.0f%% ", total*100); - - int len = floor(log10(abs((int)bar->total))) + 3; - - wmove(bar->scr, hy+1, hx - len); - wprintw(bar->scr, "%.0f/%.0f", bar->current, bar->total); - - wmove(bar->scr,0,0); - wrefresh(bar->scr); -} - -TEXT_BOX* new_text_box(WINDOW* scr, int length) -{ - TEXT_BOX *text = (TEXT_BOX*)malloc(sizeof(TEXT_BOX)); - text->scr = scr; - text->length = length; - text->current = 0; - text->text = malloc(sizeof(char)*(length+1)); - memset(text->text, '\0', length); - box(scr, 0,0); - return text; -} - -void get_char(TEXT_BOX* text, void (*sch)(char*, int)) -{ - while(1) { - wchar_t c; - get_wch((wint_t*)&c); - - switch(c) { - case KEY_BACKSPACE: - if (text->current > 0) { - text->text[text->current--] = '\0'; - } - break; - default: - if (text->current < (text->length-2)) { - text->text[text->current] = c; - text->text[++text->current] = '\0'; - } - } - - char str[text->length]; - wcstombs(str, text->text, sizeof(text->text)); - sch(str, (int)strlen(str)); - - 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); -} diff --git a/lib/ui.h b/lib/ui.h deleted file mode 100644 index 271105a..0000000 --- a/lib/ui.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include - -typedef struct progress_bar -{ - float total; - float current; - WINDOW *scr; -} PROGRESS_BAR; - -PROGRESS_BAR* new_progress_bar(WINDOW*, float); -void bar_step(PROGRESS_BAR*, float); - -typedef struct text_box -{ - wchar_t *text; - int length; - int current; - WINDOW *scr; -} 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*); diff --git a/lib/util.c b/lib/util.c deleted file mode 100644 index 895ca89..0000000 --- a/lib/util.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include - -#include "util.h" - -#define BUF_SIZE 100 - -char* copy_achar(const char* src) -{ - int len = strlen(src) + 1; - char* dest = (char*)malloc(sizeof(char)*len); - strcpy(dest, src); - - return dest; -} - -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_file_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; -} diff --git a/lib/util.h b/lib/util.h deleted file mode 100644 index 33c61ed..0000000 --- a/lib/util.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include - -/* - * Copy of a char to a newly created string of the same size. - */ -char* copy_achar(const char*); - -int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave); - -unsigned int count_file_lines(FILE *file); -- cgit v1.2.3