forked from incrediblesound/Graph-Reply
/
input.c
92 lines (89 loc) · 2.6 KB
/
input.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
#include "input.h"
#include <stdio.h> /* for printf(3) and sscanf(3) */
#include <stdlib.h> /* for free(3) and strtol(3) */
#include <string.h> /* for strcmp(3) */
#include "disk_utility.h" /* for read_from_disk() and save_to_disk() */
#include "link_utility.h" /* for a bunch of things */
#include "node_utility.h" /* for a bunch of things */
#include "node_math.h" /* for {add,subtract,multiply,divide}_nodes() */
void process_input(char *input, struct Graph *g){
int i = 0;
char name_a[50];
char name_b[50];
char type[5];
sscanf(input, "%s %s %s", type, name_a, name_b);
if(!strcmp(type,"new")){
add_new_node(name_a, g);
}
else if(!strcmp(type,"link")){
add_new_link(name_a, name_b, g);
}
else if(!strcmp(type,"out")){
print_outgoing_nodes(name_a, g);
}
else if(!strcmp(type,"in")){
print_incoming_nodes(name_a, g);
}
else if(!strcmp(type,"wn")){
write_data_prompt(name_a, g);
}
else if(!strcmp(type,"rn")){
read_data(name_a, g);
}
else if(!strcmp(type, "all")){
print_graph(g);
}
else if(!strcmp(type,"wl")){
write_link_data_prompt(name_a, name_b, g);
}
else if(!strcmp(type,"rl")){
read_link_data(name_a, name_b, g);
}
else if(!strcmp(type, "path")){
// printf("Hey");
test_for_path(name_a, name_b, g);
}
else if(!strcmp(type, "save")){
save_to_disk(g, name_a);
}
else if(!strcmp(type, "load")){
read_from_disk(name_a, g);
}
else if(!strcmp(type, "getl")){
get_links(g);
// all links (node pairs) with X data
}
else if(!strcmp(type, "getn")){
get_nodes(g);
// all nodes with X data
}
else if(!strcmp(type, "add")){
add_nodes(name_a, name_b, g);
}
else if(!strcmp(type, "div")){
divide_nodes(name_a, name_b, g);
}
else if(!strcmp(type, "sub")){
subtract_nodes(name_a, name_b, g);
}
else if(!strcmp(type, "mult")){
multiply_nodes(name_a, name_b, g);
}
else if(!strcmp(type, "cmd")){
run_command(name_a, g);
}
else if(!strcmp(type, "id")){
long long_val;
long_val = strtol(name_a, NULL, 10);
int id = (int) long_val;
struct Node *node;
node = get_node_by_id(id, g);
if(!strcmp(node->name, "NULL")){
printf("{ \"error\": \"No node with id %d\" }\n", id);
free(node->name);
free(node);
} else {
printf("{ \"id: %d, \"name\": \"%s\", \"data\": \"%s\" }\n", id, node->name, node->data);
}
}
}