summaryrefslogtreecommitdiff
path: root/file-handler.c
blob: 0f9e838e8ea659f40b15c1da84210a1d5224267b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* file-handler.c
 * Created: 2023-08-25
 * Author: Aryadev Chavali
 * Description: Implementations of writing and loading state from files
 */

#include <stdio.h>
#include <string.h>

#include "./lib.h"

#define CHUNK_SIZE 1024

typedef struct Buffer
{
  char *data;
  size_t used, available;
} buffer_t;

void buffer_init(buffer_t *buffer)
{
  *buffer =
      (buffer_t){calloc(CHUNK_SIZE, sizeof(*buffer->data)), 0, CHUNK_SIZE};
}

void buffer_realloc(buffer_t *buffer, size_t new_size)
{
  buffer->data      = realloc(buffer->data, new_size);
  buffer->available = new_size;
}

void buffer_tighten(buffer_t *buffer)
{
  buffer->data               = realloc(buffer->data, buffer->used + 1);
  buffer->data[buffer->used] = '\0';
  buffer->available          = buffer->used;
}

bool load_from_file(state_t *state, const char *filepath)
{
  memset(state->data, 0, state->dwidth * state->dwidth);
  // Read file completely
  FILE *fp = fopen(filepath, "r");
  buffer_t buffer;
  buffer_init(&buffer);
  size_t bytes_read = 0;
  while ((bytes_read =
              fread(buffer.data, sizeof(*buffer.data), CHUNK_SIZE, fp)) != 0)
  {
    buffer.used += bytes_read;
    buffer_realloc(&buffer, buffer.available + CHUNK_SIZE);
  }
  fclose(fp);
  buffer_tighten(&buffer);

  // Now parse it

  free(buffer.data);

  return true;
}

bool write_to_file(state_t *state, const char *filepath)
{
  FILE *fp = fopen(filepath, "w");
  fprintf(fp, "%lu\n", state->dwidth);
  for (size_t i = 0; i < state->dwidth; ++i)
  {
    for (size_t j = 0; j < state->dwidth; ++j)
    {
      fprintf(fp, "%d", state->data[(i * state->dwidth) + j]);
      if (j != state->dwidth - 1)
        fprintf(fp, ",");
    }
    fprintf(fp, "\n");
  }
  fclose(fp);
  return true;
}