]> Nishi Git Mirror - mandshurica.git/commitdiff
reading db works
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 20 Apr 2024 00:36:59 +0000 (00:36 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 20 Apr 2024 00:36:59 +0000 (00:36 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@45 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Mandshurica/db.c
Mandshurica/main.c

index 2ec742c4cb26cf684b9dbb7b0d234303f9946bd3..cd0dba6dba2bf5bbd396b23af487adb41f5c9521 100644 (file)
 
 #include "ms_db.h"
 
+#include "ms_util.h"
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 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;
+}
index e5b1198587be25502e658a7053d1243414320ab4..27478668e1081fd30029d46fb63b3e0472276acb 100644 (file)
@@ -33,6 +33,8 @@
 #include "ms_config.h"
 #include "ms_log.h"
 
+#include "ms_db.h"
+
 #include <dlfcn.h>
 #include <stdbool.h>
 #include <stdio.h>