summaryrefslogtreecommitdiff
path: root/database.c
blob: 07f3f4a4939a4d8f8cfc8c920adc15b703389234 (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
#include "database.h"

#include <stdlib.h>
#include <stdio.h>

void database_prefixPath(const char *path, char *out) {
  sprintf(out, ".rolex/%s", path);
}

enum DatabaseError_t database_pushEntry(const char *path, struct TimeEnty_t *entry) {
  char filePath[1024];
  FILE *f;
  database_prefixPath(path, filePath);
  mkdir(".rolex", 0777);
  f = fopen(filePath, "a");
  fprintf(f, "%d %ld\n", entry->direction, entry->datetime);
  fclose(f);
  return DB_OK;
}

enum DatabaseError_t database_getEntries(const char *path, struct TimeEnty_t *out, int *size) {
  char filePath[1024], line[2048];
  bool lineDirection;
  long lineTime;
  FILE *f;
  int i = 0;
  database_prefixPath(path, filePath);
  f = fopen(filePath, "r");
  if(f == NULL) goto done;
  while(fgets(line, 2048, f)) {
    sscanf(line, "%d %ld", &lineDirection, &lineTime);
    out[i].direction = lineDirection;
    out[i].datetime = lineTime;
    i++;
  }

done:
  fclose(f);
  *size = i;
  return DB_OK;
}

enum DatabaseError_t database_nuke(const char *path) {
  char *filePath[1024];
  database_prefixPath(path, filePath);
  remove(filePath);
}