-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.c
More file actions
230 lines (211 loc) · 6.33 KB
/
io.c
File metadata and controls
230 lines (211 loc) · 6.33 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "io.h"
/*#include "graph.h"*/
/*#include "server.h"*/
void AddCmd(List* list, char* str) {
Cmd* cmd = (Cmd *)malloc(sizeof(Cmd));
memset(cmd, 0, sizeof(Cmd));
strcpy(cmd->val, str);
if (list->front == NULL) {
list->front = cmd;
list->rear = cmd;
} else {
list->rear->next = cmd;
list->rear = cmd;
}
}
int AnalyzeInput(List* cmdList[], char* input) {
/*FILE* file = stdin;*/
char line[128];
char* word;
char* tmp;
char semicolonWord[12];
bzero(line, sizeof(line));
strcpy(line, input);
/*fgets(line, 128, file);*/
if (strlen(line) == 0) {
return -2;
}
if (strcmp(line, "\n") == 0) {
printf("please input the command.\n");
strcat(error, "please input the command.\n");
return -3;
}
/* The input command don't end with ";" */
if (line[strlen(line) - 2] != ';') {
printf("input should end with \";\" \n");
strcat(error, "response from server: input should end with \";\" \n");
return -1;
}
word = strtok(line, " \t\n");
int i = 0;
while (word != NULL) {
if (cmdList[i] == NULL) {
cmdList[i] = (List *)malloc(sizeof(List));
memset(cmdList[i], 0, sizeof(List));
}
tmp = strchr(word, ';');
if (tmp != NULL) {
strncpy(semicolonWord, word, tmp-word);
semicolonWord[tmp-word] = '\0';
AddCmd(cmdList[i], semicolonWord);
i++;
} else {
AddCmd(cmdList[i], word);
}
word = strtok(NULL, " \t\n");
}
/*fclose(file);*/
return i;
}
int IsLegalInsertCmd(Cmd* cmd) {
if (strlen(cmd->val) != 4) {
return 0;
}
if (!isalpha(cmd->val[0]) || !isalpha(cmd->val[3])) {
return 0;
}
if (cmd->val[1] != '-' || cmd->val[2] != '>') {
return 0;
}
return 1;
}
int IsLegalQueryCmd(Cmd* cmd) {
if (strlen(cmd->val) != 1) {
return 0;
}
if (!isalpha(cmd->val[0])) {
return 0;
}
return 1;
}
void InsertNode(List* list, NodeList* nodeList) {
/* skip the fisrt command: insert */
Cmd* cmd = list->front->next;
char startChar[2];
char endChar[2];
bzero(startChar, sizeof(startChar));
bzero(endChar, sizeof(endChar));
/* check if there is any illegal input, if exists, terminate the whole command */
for (; cmd != NULL; cmd = cmd->next) {
if (!IsLegalInsertCmd(cmd)) {
printf("insert command input error.\n");
strcat(error, "response from server: insert command input error.\n");
}
}
/* reset the cmd pointer to the list->front->next; */
cmd = list->front->next;
/*printf("print nodelist backup:\n");*/
/*Traverse(nodeListBackup);*/
for (; cmd != NULL; cmd = cmd->next) {
strncpy(startChar, &cmd->val[0], 1);
strncpy(endChar, &cmd->val[3], 1);
if ((GetNode(nodeList, startChar) != NULL) && (GetNode(nodeList, endChar) != NULL)) {
if (IsConflicted(nodeList, startChar, endChar)) {
printf("CONFLICT DETECTED. INSERT FAILED.\n");
printf("%s->%s and %s->%s cannot be true at the same time!\n", startChar, endChar, endChar, startChar);
strcat(error, "response from server: CONFLICT DETECTED. INSERT FAILED.\n");
strcat(error, startChar);
strcat(error, "->");
strcat(error, endChar);
strcat(error, " and ");
strcat(error, endChar);
strcat(error, "->");
strcat(error, startChar);
strcat(error, " cannot be true at the same time!\n");
return;
}
} else {
if (GetNode(nodeList, startChar) == NULL) {
AddNode(nodeList, startChar);
}
if (GetNode(nodeList, endChar) == NULL) {
AddNode(nodeList, endChar);
}
if (!PathExists(nodeList, startChar, endChar)) {
AddEdge(nodeList, startChar, endChar);
}
}
}
printf("INSERT DONE\n");
strcat(response, "response from server: INSERT DONE\n");
}
void QueryNode(List* list, NodeList* nodeList) {
/* skip the fisrt command: query */
Cmd* cmd = list->front->next;
char startChar[2];
char endChar[2];
bzero(startChar, sizeof(startChar));
bzero(endChar, sizeof(endChar));
for (; cmd != NULL; cmd = cmd->next) {
if (IsLegalQueryCmd(cmd) == 0) {
strcat(error, "response from server: query command input error.\n");
}
}
cmd = list->front->next;
Cmd* cmd2 = cmd->next;
strncpy(startChar, &cmd->val[0], 1);
strncpy(endChar, &cmd2->val[0], 1);
if (GetNode(nodeList, startChar) == NULL) {
printf("Event not found: %s.\n", startChar);
strcat(error, "response from server: Event not found: ");
strcat(error, startChar);
strcat(error, ".\n");
}
if (GetNode(nodeList, endChar) == NULL) {
printf("Event not found: %s.\n", endChar);
strcat(error, "response from server: Event not found: ");
strcat(error, endChar);
strcat(error, ".\n");
return;
}
if (PathExists(nodeList, startChar, endChar)) {
printf("%s happened before %s.\n", startChar, endChar);
strcat(response, "response from server: ");
strcat(response, startChar);
strcat(response, " happened before ");
strcat(response, endChar);
strcat(response, ".\n");
} else if (PathExists(nodeList, endChar, startChar)) {
printf("response from server: %s happened before %s.\n", endChar, startChar);
strcat(response, "response from server: ");
strcat(response, endChar);
strcat(response, " happened before ");
strcat(response, startChar);
strcat(response, ".\n");
} else {
printf("response from server: %s concurrent to %s.\n", startChar, endChar);
strcat(response, "response from server: ");
strcat(response, startChar);
strcat(response, " concurrent to ");
strcat(response, endChar);
strcat(response, ".\n");
}
}
void ResetNodeList(NodeList* nodeList) {
memset(nodeList, 0, sizeof(nodeList));
printf("response from server: RESET DONE.\n");
strcat(response, "RESET DONE.\n");
}
void ExecuteCmd(List* list, NodeList* nodeList, char* clientName) {
printf("request received from %s: ", clientName);
Cmd* cmd = list->front;
for(; cmd != NULL; cmd = cmd->next) {
printf("%s ", cmd->val);
}
printf("\n");
cmd = list->front;
if (strcmp(cmd->val, "insert") == 0) {
InsertNode(list, nodeList);
} else if (strcmp(cmd->val, "query") == 0) {
QueryNode(list, nodeList);
} else if (strcmp(cmd->val, "reset") == 0) {
ResetNodeList(nodeList);
} else {
printf("input error!\n");
strcat(error, "input error!\n");
}
}