-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_utility.c
230 lines (214 loc) · 5.08 KB
/
node_utility.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
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 "node_utility.h"
#include <stdio.h> /* for printf(3) */
#include <stdlib.h> /* for malloc(3) and free(3) */
#include <string.h> /* for strcpy(3), strlen(3), and strcmp(3) */
#include "main_utility.h" /* for get_line() */
#include "input.h" /* for process_input() */
struct Node *get_node_by_id(unsigned int id, struct Graph *g){
for(int i = 0; i < g->num_nodes; i++){
if(g->nodes[i].id == id){
return &g->nodes[i];
}
}
struct Node *target = malloc(sizeof(struct Node));
target->name = malloc(5);
strcpy(target->name, "NULL\0");
return target;
}
int node_exists(char *name, struct Graph *g){
for(int i = 0; i < g->num_nodes; i++){
if(!strcmp(g->nodes[i].name, name)){
return 1;
}
}
return 0;
}
int add_new_node(char *name, struct Graph *g){
g->num_nodes++;
int id = g->num_nodes-1;
struct Node new_node;
new_node.id = id;
new_node.data = "NULL";
g->nodes[id] = new_node;
g->nodes[id].name = malloc(strlen(name)+1);
strcpy(g->nodes[id].name, name);
return id;
}
int new_or_existing_id(char *name, struct Graph *g){
int id = -1;
for(int i = 0; i < g->num_nodes; i++){
if(strcmp(g->nodes[i].name, name) == 0){
id = g->nodes[i].id;
}
}
if(id == -1){
id = add_new_node(name, g);
}
return id;
}
int get_id_by_name(char *name, struct Graph *g){
int id = -1;
for(int i = 0; i < g->num_nodes; i++){
if(strcmp(g->nodes[i].name, name) == 0){
id = g->nodes[i].id;
}
}
return id;
}
char *return_node_data(char *name, struct Graph *g){
int i = 0;
while(strcmp(name, g->nodes[i].name) != 0 && i <= g->num_nodes){
i++;
}
return g->nodes[i].data;
}
void print_outgoing_nodes(char *name, struct Graph *g){
int id = get_id_by_name(name, g);
int first = 0;
printf("[ ");
struct Node node[1];
for(int i = 0; i < g->num_links; i++){
if(g->links[i].source == id){
if(first){
printf(", ");
} else {
first = 1;
}
node[0] = *get_node_by_id(g->links[i].target, g);
printf("{ \"id\": %d, \"name\": \"%s\" }", node[0].id, node[0].name);
}
}
printf(" ]\n");
}
void print_incoming_nodes(char *name, struct Graph *g){
int id = get_id_by_name(name, g);
int first = 0;
printf("[ ");
struct Node node[1];
for(int i = 0; i < g->num_links; i++){
if(g->links[i].target == id){
if(first){
printf(", ");
} else {
first = 1;
}
node[0] = *get_node_by_id(g->links[i].source, g);
printf("{ \"id\": %d, \"name\": \"%s\" }", node[0].id, node[0].name);
}
}
printf(" ]\n");
}
void write_data(char *line, char *name, struct Graph *g){
int len = strlen(line);
if(line[len-1] == '\n'){
line[len-1] = 0;
}
int i = 0;
while(strcmp(name, g->nodes[i].name) != 0 && i <= g->num_nodes){
i++;
}
g->nodes[i].data = malloc(len);
strcpy(g->nodes[i].data, line);
}
void write_data_prompt(char *name, struct Graph *g){
if(!node_exists(name, g)){
printf("{ error: \"No node with name %s.\" }\n", name);
} else {
char *line;
printf("[(%s).data]> ", name);
line = get_line();
write_data(line, name, g);
free(line);
}
}
void read_data(char *name, struct Graph *g){
if(!g->num_nodes){
printf("{ error: \"No nodes in graph.\" }\n");
return;
} else {
int i = 0;
while(i < g->num_nodes && strcmp(name, g->nodes[i].name) != 0){
i++;
}
if(i == g->num_nodes){
printf("{ error: \"No node with name %s.\" }\n", name);
} else {
printf("{ id: %d, \"name\": \"%s\", \"data\": \"%s\" }\n", g->nodes[i].id, name, g->nodes[i].data);
}
}
}
void get_nodes(struct Graph *g){
char *line;
printf("[node.data]> ");
line = get_line();
int len = strlen(line);
if(line[len-1] == '\n'){
line[len-1] = 0;
}
int first = 0;
printf("[ ");
for(int i = 0; i < g->num_nodes; i++){
if(!strcmp(line, g->nodes[i].data)){
if(first){
printf(", ");
} else {
first = 1;
}
printf("{ id: %d, \"name\": \"%s\", \"data\": \"%s\" }", g->nodes[i].id, g->nodes[i].name, g->nodes[i].data);
}
}
printf(" ]\n");
free(line);
}
void run_command(char *name, struct Graph *g){
if(!g->num_nodes){
printf("{ error: \"No nodes in graph.\" }\n");
return;
} else {
int i = 0;
while(i < g->num_nodes && strcmp(name, g->nodes[i].name) != 0){
i++;
}
if(i == g->num_nodes){
printf("{ error: \"No node with name %s.\" }\n", name);
} else {
process_input(g->nodes[i].data, g);
}
}
}
void print_graph(struct Graph *g){
if(g->num_nodes == 0 && g->num_links == 0){
printf("{ error: \"No data in graph.\" }\n");
} else {
printf("{ ");
int has_nodes = 0;
if(g->num_nodes > 0){
has_nodes = 1;
printf("\"nodes\": [");
int i = 0;
while(i < g->num_nodes){
if(i > 0 && i <= g->num_nodes-1){
printf(", ");
}
printf("{ \"id\": %d, \"name\": \"%s\", \"data\": \"%s\" }", g->nodes[i].id, g->nodes[i].name, g->nodes[i].data);
i++;
}
printf("]");
}
if(g->num_links > 0){
if(has_nodes){
printf(", ");
}
printf("\"links\": [");
int l = 0;
while(l < g->num_links){
if(l > 0 && l <= g->num_links-1){
printf(", ");
}
printf("{ \"source\": %d, \"target\": \"%d\", \"data\": \"%s\" }", g->links[l].source, g->links[l].target, g->links[l].data);
l++;
}
printf("] }\n");
}
}
}