-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaton.c
98 lines (82 loc) · 2.02 KB
/
automaton.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "args.h"
#include "automaton.h"
// TODO regarder les return values des fwrite, fread ?
void new_grid(grid* g, size_t n, size_t m, double v)
{
g->n = n;
g->m = m;
g->v = v;
g->data = (cell *) calloc (n*m, sizeof(cell));
}
void copy_grid(grid* src, grid* dst)
{
assert(src->n == dst->n && src->m == dst->m && src->v == dst->v);
memcpy(dst->data, src->data, sizeof(cell)*(src->n)*(src->m));
}
void dump_grid(char* filename, grid *g)
{
FILE *fp;
fp = fopen(filename, "wb");
if(fp == NULL)
{
perror("Error during grid dumping:");
}
size_t n = g->n;
size_t m = g->m;
for(size_t i = 0; i < n; i++)
{
for(size_t j = 0; j < m; j++)
{
fwrite(&(g->data[i*m+j].u), sizeof(double), 1, fp);
}
}
fclose(fp);
}
void parse_file(char* filename, grid *g)
{
FILE *fp;
fp = fopen(filename, "rb");
if(fp == NULL)
{
perror("Error during the input parsing: ");
}
char test;
size_t n;
size_t m;
double v;
if(fread(&test, sizeof(char), 1, fp) != 1 || fread(&n, sizeof(size_t), 1, fp) != 1 || fread(&m, sizeof(size_t), 1, fp) != 1 || fread(&v, sizeof(double), 1, fp) != 1)
exit(EXIT_FAILURE);
char cell_type;
double cell_value;
g->n = n;
g->m = m;
g->v = v;
g->data = (cell *) calloc(n*m, sizeof(cell));
for(size_t i = 0; i < n; i++)
for(size_t j = 0; j < m; j++)
{
if(fread(&cell_type, sizeof(char), 1, fp) != 1 || fread(&cell_value, sizeof(double), 1, fp) != 1)
exit(EXIT_FAILURE);
// if(cell_type == 1)
// printf("%zu %zu\n", i, j);
g->data[i*m+j].type = cell_type;
g->data[i*m+j].u = cell_value;
g->data[i*m+j].v = 0;
}
#ifdef DEBUG
fprintf(stderr, "Initial grid: %d %zd %zd %f\n", test, n, m, v);
for(size_t i = 0; i < n; i++)
{
for(size_t j = 0; j < m; j++)
{
fprintf(stderr, "%f ", g->data[i*m+j].u);
}
fprintf(stderr, "\n");
}
#endif
fclose(fp);
}