-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
171 lines (152 loc) · 5.2 KB
/
config.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "config.h"
#include "action.h"
#include "hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// NOTE TO READER this code only works on the config_examples/basic file. It
// does not work on the example config yet
#define BUFFER_SIZE 1024
#define DEFAULT_TIME 500;
FILE* input_file;
char buffer[BUFFER_SIZE];
bool end_of_file;
const char* delimiter_characters = " \t\n";
// TODO deal with syntax errors
// TODO deal with an default time if there's one at the top of the file
// TODO set a default time if there's on default time at the top
void parse_config(char* fname) {
input_file = fopen(fname, "r");
end_of_file = false;
if (input_file == NULL) {
fprintf(stderr, "Unable to open file %s\n", fname);
}
readline();
while (!end_of_file) {
parent_action_t* parent = build_parent();
put_action(parent);
}
if (ferror(input_file)) {
perror("The following error occurred");
}
fclose(input_file);
}
parent_action_t* build_parent() {
// example: act act1
strtok(buffer, delimiter_characters);
char* parent_name = strtok(NULL, delimiter_characters);
parent_action_t* parent = calloc(1, sizeof(parent_action_t));
parent->name = malloc(strlen(parent_name) + 1);
parent->name = strcpy(parent->name, parent_name);
parent->time_limit = DEFAULT_TIME;
readline();
char* first = get_first_word();
while (strcmp("next", first) != 0) {
read_parent_arguments(parent);
readline();
free(first);
first = get_first_word();
}
int num_children = 0;
int max_num_children = 10;
parent->children = calloc(max_num_children, sizeof(child_action_t));
// the act part is redundant i suppose
while (strcmp(first, "next") == 0 && !end_of_file && strcmp(first, "act") != 0) {
parent->children[num_children] = *build_child();
num_children++;
// resize if there's too many children
if (num_children == max_num_children) {
max_num_children *= 2;
parent->children = realloc(
parent->children, max_num_children * sizeof(child_action_t));
}
first = get_first_word();
}
parent->num_children = num_children;
return parent;
}
child_action_t* build_child() {
child_action_t* child = calloc(1, sizeof(child_action_t));
readline();
char* first = get_first_word();
child->time_limit = DEFAULT_TIME;
while (strcmp(first, "next") != 0 && !end_of_file &&
strcmp(first, "act") != 0) {
read_children_arguments(child);
readline();
free(first);
first = get_first_word();
}
return child;
}
void read_parent_arguments(parent_action_t* parent) {
char* descriptor = strtok(buffer, delimiter_characters);
if (strcmp(descriptor, "command") == 0) {
strtok(NULL, delimiter_characters);
char* command_name = strtok(NULL, "\n");
parent->command = malloc(strlen(command_name) + 1);
parent->command = strcpy(parent->command, command_name);
} else if (strcmp(descriptor, "time") == 0) {
char* time = strtok(NULL, delimiter_characters);
parent->time_limit = atoi(time);
}
}
void read_children_arguments(child_action_t* child) {
char* descriptor = strtok(buffer, delimiter_characters);
if (strcmp(descriptor, "command") == 0) {
// get rid of notify-send
strtok(NULL, delimiter_characters);
char* command_name = strtok(NULL, "");
child->command = malloc(strlen(command_name) + 1);
child->command = strcpy(child->command, command_name);
} else if (strcmp(descriptor, "time") == 0) {
char* time = strtok(NULL, delimiter_characters);
child->time_limit = atoi(time);
} else if (strcmp(descriptor, "undo") == 0) {
size_t num_undoes = 0;
size_t max_undoes = 5;
child->undoes = calloc(max_undoes, sizeof(size_t));
char* action = strtok(NULL, delimiter_characters);
while (action != NULL) {
if (num_undoes == max_undoes) {
max_undoes *= 2;
child->undoes = realloc(child->undoes, max_undoes);
}
child->undoes[num_undoes] = atoi(action);
action = strtok(NULL, delimiter_characters);
num_undoes++;
}
child->num_undos = num_undoes;
}
}
// TODO make sure you copy the buffer like you did in first word
bool ignore_line() {
char* first = get_first_word();
// we want to ignore commented lines and blank lines
if (first == NULL || first[0] == '#') {
return true;
}
free(first);
return false;
}
char* get_first_word() {
char* temp_buffer = malloc(BUFFER_SIZE + 1);
temp_buffer = strcpy(temp_buffer, buffer);
char* c = strtok(temp_buffer, delimiter_characters);
if(c == NULL) {
return NULL;
}
char* first = malloc(strlen(c) + 1);
first = strcpy(first, c);
return first;
}
void readline() {
if(end_of_file) {
return;
}
end_of_file = fgets(buffer, BUFFER_SIZE, input_file) == NULL;
// ignore commented lines and blank lines
while (ignore_line() && !end_of_file) {
end_of_file = fgets(buffer, BUFFER_SIZE, input_file) == NULL;
}
}