summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 1aaaaa72cb4862b768b25ed2279ff26bd8d05552 (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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

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

int main(int argc, char **argv)
{
  struct keytab tab[TABLEN] = {{ NULL, 0, { 0 } }};
  if (argc != 2)
    exit(1);
  char *filename = argv[1];
  
  uint8_t *buf = readdb(filename);
  if (buf == NULL)
    exit(0);
  struct byte *bytes = parse(buf);
  free(buf);

  for (int i = 0; i < blen; ++i) {
    switch (bytes[i].type) {
      case BEGIN:
        break;
      case END:
        break;
      case PAIR:
        setkey(tab, bytes[i].value);
        free(bytes[i].value);
        break;
      case ERROR:
        fprintf(stderr, "%s\n", bytes[i].value);
        exit(1);
    }
  }

  int *indexes = getkeys(tab);
  for (int i = 0; indexes[i] != 0; ++i) {
    printf("%s: ", tab[indexes[i]].key);
    if (tab[indexes[i]].flag == 1) {
      printf("%.2lf\n", tab[indexes[i]].v.num);
    } else if (tab[indexes[i]].flag == 2) {
      printf("%d\n", tab[indexes[i]].v.b);
    } else if (tab[indexes[i]].flag == 3) {
      printf("%s\n", tab[indexes[i]].v.str);
    }
    delkey(tab, tab[indexes[i]].key);
  }
  free(indexes);
  free(bytes);
  exit(0);
}