#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");
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;
+}