-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.c
82 lines (71 loc) · 1.62 KB
/
chart.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
#include "chart.h"
str_ltchart* load_chart(const char* file, str_light* lights, int lightnum) {
str_ltchart* chart = NULL;
if (!lights || lightnum <=0)
return chart;
FILE* fp = fopen(file,"r");
if (!fp)
return chart;
LightStatus **status = NULL;
status = (LightStatus**)malloc(sizeof(LightStatus*));
status[0] = (LightStatus*)malloc(sizeof(LightStatus) * lightnum);
long frame_size = 1;
char ch;
int line_num = 0;
int in_comment = 0;
while ((ch = fgetc(fp)) != EOF) {
switch (ch) {
case '#': // for comment
in_comment = 1;
break;
case '\n':
case '\r':
{ if (in_comment) {
in_comment = 0;
continue;
}
if (line_num < lightnum)
return NULL; // error format;
status = (LightStatus**)realloc(status,sizeof(LightStatus*) * (++frame_size));
status[frame_size - 1] = (LightStatus*)malloc(sizeof(LightStatus) * lightnum);
line_num = 0;
}
break;
case ',':
case '\t':
case ' ':
case ':':
case '-':
continue;
break;
case '1':
case '0':
{
if(in_comment)
continue;
if (line_num > lightnum - 1)
continue; // line content out of lightnum, ignore
status[frame_size - 1][line_num++] = (ch == '1')?1:0;
}
break;
default:
continue;
break;
}
}
#ifdef __TEST_LOAD_CHART
int j,k;
for (j = 0; j < frame_size - 1; ++j) {
for (k = 0; k < lightnum; ++k)
printf("%d\t",status[j][k]);
printf("\n");
}
#endif //__TEST_LOAD_CHART
fclose(fp);
chart = (str_ltchart*)malloc(sizeof(str_ltchart));
chart->light_num = lightnum;
chart->lights = lights;
chart->light_status = status;
chart->frame_count = frame_size;
return chart;
}