summaryrefslogtreecommitdiff
path: root/src/fileops.c
blob: e43a455d1b361d335d5737cc7125ddfddaa787a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "include/fileops.h"
#include "include/keytab.h"

uint8_t *readdb(char *filename)
{
  FILE *fp = fopen(filename, "rb");
  if (fp == NULL)
    return NULL;

  int len = 10;
  uint8_t *buf = calloc(len, sizeof(uint8_t));
  int c, i = 0;
  while ((c = fgetc(fp)) != EOF) {
    if (i >= len) {
      len *= 2;
      buf = realloc(buf, len * sizeof(uint8_t));
      memset(buf + i, 0, (len - i) * sizeof(uint8_t));
    }
    buf[i++] = c;
  }

  fclose(fp);
  return buf;
}

void writedb(char *filename, struct keytablist *list, int len)
{
  FILE *fp = fopen(filename, "wb");
  for (int i = 0; i < len; ++i) {
    fprintf(fp, "\xfb");
    int *indexes = getkeys(list, i);
    for (int j = 0; indexes[j]; ++j) {
      fprintf(fp, "\xfa%s:", list[i].tab[indexes[j]].key);
      switch (list[i].tab[indexes[j]].flag) {
        case 1:
          fprintf(fp, "%.2lf", list[i].tab[indexes[j]].v.num);
          break;
        case 2:
          fprintf(fp, "%s", list[i].tab[indexes[j]].v.b == 1 ? "true" : "false");
          break;
        case 3:
          fprintf(fp, "%s", list[i].tab[indexes[j]].v.str);
          break;
      }
    }
    free(indexes);
    fprintf(fp, "\xfe");
  }
}