From: nishi Date: Sat, 20 Apr 2024 00:36:59 +0000 (+0000) Subject: reading db works X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=0aadb3c89a96eb55df582564f12d518da917c30c;p=mandshurica.git reading db works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@45 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- diff --git a/Mandshurica/db.c b/Mandshurica/db.c index 2ec742c..cd0dba6 100644 --- a/Mandshurica/db.c +++ b/Mandshurica/db.c @@ -30,8 +30,11 @@ #include "ms_db.h" +#include "ms_util.h" + #include #include +#include struct ms_db* mandshurica_db_open(const char* path) { FILE* fp = fopen(path, "rb"); @@ -48,4 +51,39 @@ void mandshurica_db_close(struct ms_db* db) { free(db); } -char* mandshurica_db_get(struct ms_db* db, const char* key) {} +/* key:value */ +char* mandshurica_db_get(struct ms_db* db, const char* key) { + fseek(db->fp, 0, SEEK_SET); + char cbuf[2]; + cbuf[1] = 0; + char* line = malloc(1); + line[0] = 0; + while(1) { + fread(cbuf, 1, 1, db->fp); + if(feof(db->fp) || cbuf[0] == '\n') { + if(strlen(line) > 0) { + int i; + for(i = 0; line[i] != 0; i++) { + if(line[i] == ':') { + line[i] = 0; + if(strcmp(line, key) == 0) { + char* ret = mandshurica_strdup(line + i + 1); + free(line); + return ret; + } + } + } + } + free(line); + line = malloc(1); + line[0] = 0; + if(feof(db->fp)) break; + } else if(cbuf[0] != '\r') { + char* tmp = line; + line = mandshurica_strcat(tmp, cbuf); + free(tmp); + } + } + free(line); + return NULL; +} diff --git a/Mandshurica/main.c b/Mandshurica/main.c index e5b1198..2747866 100644 --- a/Mandshurica/main.c +++ b/Mandshurica/main.c @@ -33,6 +33,8 @@ #include "ms_config.h" #include "ms_log.h" +#include "ms_db.h" + #include #include #include