-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_file_creator.c
More file actions
71 lines (52 loc) · 1.5 KB
/
graph_file_creator.c
File metadata and controls
71 lines (52 loc) · 1.5 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
#include "graph_file_creator.h"
static char *get_token(char *str, char *token);
static graph create_empty_graph_read_from_file(FILE *fp);
static void add_edges_in_graph_read_from_file(FILE *fp, graph G);
static char *get_token(char *str, char *token) {
int index = 0;
while (str[index] != ',' && str[index] != '\n') {
token[index] = str[index];
++index;
}
token[index] = '\0';
if (str[index] == '\n') {
return NULL;
}
return (str + index + 1);
}
static graph create_empty_graph_read_from_file(FILE *fp) {
char str[40];
fgets(str, 40, fp);
char token[20];
get_token(str, token);
int vertices = atoi(token);
return GRAPH_create_empty_graph(vertices);
}
static void add_edges_in_graph_read_from_file(FILE *fp, graph G) {
int edge_info[] = {0,0,0};
int i;
char str[40];
while (fgets(str, 40, fp)) {
i = 0;
char token[20];
char *sub_name = get_token(str, token);
edge_info[0] = atoi(token);
while (sub_name) {
sub_name = get_token(sub_name, token); // to go on
edge_info[++i] = atoi(token);
}
GRAPH_add_edge(G, edge_info[0], edge_info[1], edge_info[2]);
}
}
/*********** Public Interfaces ************/
graph GRAPH_FILE_CREATOR_create_graph(FILE *fp) {
if (!fp) {
return NULL;
}
graph G = create_empty_graph_read_from_file(fp);
if (!G) {
return NULL;
}
add_edges_in_graph_read_from_file(fp, G);
return G;
}