|
| 1 | +#include "utils.h" |
| 2 | +#include "graph.h" |
| 3 | + |
| 4 | +void get_values(char str[128], unsigned values[2]); |
| 5 | +struct adjacency_matrix* generate_adjacency_matrix(unsigned* links, unsigned max, unsigned nb_links); |
| 6 | +void print_adjacency_matrix(struct adjacency_matrix* a); |
| 7 | + |
| 8 | + |
| 9 | +struct adjacency_matrix* dot_reader(char* path) { |
| 10 | + |
| 11 | + FILE *file = fopen(path, "r" ); |
| 12 | + size_t nb_links_possible = 300; |
| 13 | + unsigned* links = calloc(nb_links_possible, sizeof(unsigned[2])); |
| 14 | + unsigned max = 0; |
| 15 | + unsigned nb_links = 0; |
| 16 | + |
| 17 | + if (file != NULL) { |
| 18 | + |
| 19 | + char line [ 128 ]; |
| 20 | + unsigned i = 0; |
| 21 | + |
| 22 | + void *trash = fgets(line, sizeof line, file); |
| 23 | + trash = trash; // boring flags |
| 24 | + |
| 25 | + while (fgets(line, sizeof line, file) != NULL && line[0] != '}') { |
| 26 | + unsigned val[2] = {0, 0}; |
| 27 | + get_values(line, val); |
| 28 | + |
| 29 | + unsigned tmp_max = val[0] > val[1] ? val[0] : val[1]; |
| 30 | + max = max > tmp_max ? max : tmp_max; // The idea is to capture the largest id, for adjacency dimensions |
| 31 | + |
| 32 | + if (nb_links == nb_links_possible) { |
| 33 | + nb_links_possible *= 2; |
| 34 | + links = realloc(links, nb_links_possible * sizeof(unsigned[2])); |
| 35 | + } |
| 36 | + |
| 37 | + links[i] = val[0]; |
| 38 | + links[i + 1] = val[1]; |
| 39 | + i += 2; |
| 40 | + nb_links += 1; |
| 41 | + } |
| 42 | + fclose ( file ); |
| 43 | + } |
| 44 | + |
| 45 | + struct adjacency_matrix *a = generate_adjacency_matrix(links, max + 1, nb_links); |
| 46 | + free(links); |
| 47 | + |
| 48 | + print_adjacency_matrix(a); |
| 49 | + |
| 50 | + return a; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +void get_values(char str[128], unsigned values[2]) { |
| 55 | + unsigned i = 0; |
| 56 | + |
| 57 | + // spaces |
| 58 | + while(str[i] == ' ') |
| 59 | + ++i; |
| 60 | + // first val |
| 61 | + while(str[i] != ' ') { |
| 62 | + values[0] = values[0] * 10 + str[i] - 48; |
| 63 | + ++i; |
| 64 | + } |
| 65 | + // sep |
| 66 | + i += 4; |
| 67 | + // second val |
| 68 | + |
| 69 | + while(str[i] != ';') { |
| 70 | + values[1] = values[1] * 10 + (unsigned) str[i] - 48; |
| 71 | + ++i; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +struct adjacency_matrix* generate_adjacency_matrix(unsigned* links, unsigned dim, unsigned nb_links) { // unused nb_links |
| 76 | + |
| 77 | + struct adjacency_matrix* a = malloc(sizeof(struct adjacency_matrix)); |
| 78 | + a->matrix = calloc(sizeof(unsigned*), dim); |
| 79 | + a->dimension = dim; |
| 80 | + // +1 cause id0 is for user ? |
| 81 | + for (unsigned i = 0; i < dim; ++i) { |
| 82 | + a->matrix[i] = calloc(sizeof(unsigned*), dim); |
| 83 | + } |
| 84 | + |
| 85 | + for (unsigned i = 0; i < nb_links; ++i) { |
| 86 | + a->matrix[links[2 * i]][links[2 * i + 1]] = 1; |
| 87 | + a->matrix[links[2 * i + 1]][links[2 * i]] = 1; |
| 88 | + |
| 89 | + i += 1; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + return a; |
| 95 | +} |
| 96 | + |
| 97 | +void free_adjacency_matrix(struct adjacency_matrix* a) { |
| 98 | + for (unsigned i = 0; i < a->dimension; ++i) { |
| 99 | + free(a->matrix[i]); |
| 100 | + } |
| 101 | + free(a->matrix); |
| 102 | + free(a); |
| 103 | +} |
| 104 | + |
| 105 | +void print_adjacency_matrix(struct adjacency_matrix* a) { |
| 106 | + for (unsigned i = 0; i < a->dimension; ++i) { |
| 107 | + unsigned j = 0; |
| 108 | + for (; j < a->dimension - 1; ++j) { |
| 109 | + printf("%u ", a->matrix[i][j]); |
| 110 | + } |
| 111 | + printf("%u\n", a->matrix[i][j]); |
| 112 | + } |
| 113 | +} |
| 114 | + |
0 commit comments