| 81 | } |
| 82 | |
| 83 | tree *read_tree(char *filename) |
| 84 | { |
| 85 | tree t = {0}; |
| 86 | FILE *fp = fopen(filename, "r"); |
| 87 | |
| 88 | char *line; |
| 89 | int last_parent = -1; |
| 90 | int group_size = 0; |
| 91 | int groups = 0; |
| 92 | int n = 0; |
| 93 | while((line=fgetl(fp)) != 0){ |
| 94 | char *id = calloc(256, sizeof(char)); |
| 95 | int parent = -1; |
| 96 | sscanf(line, "%s %d", id, &parent); |
| 97 | t.parent = realloc(t.parent, (n+1)*sizeof(int)); |
| 98 | t.parent[n] = parent; |
| 99 | |
| 100 | t.child = realloc(t.child, (n+1)*sizeof(int)); |
| 101 | t.child[n] = -1; |
| 102 | |
| 103 | t.name = realloc(t.name, (n+1)*sizeof(char *)); |
| 104 | t.name[n] = id; |
| 105 | if(parent != last_parent){ |
| 106 | ++groups; |
| 107 | t.group_offset = realloc(t.group_offset, groups * sizeof(int)); |
| 108 | t.group_offset[groups - 1] = n - group_size; |
| 109 | t.group_size = realloc(t.group_size, groups * sizeof(int)); |
| 110 | t.group_size[groups - 1] = group_size; |
| 111 | group_size = 0; |
| 112 | last_parent = parent; |
| 113 | } |
| 114 | t.group = realloc(t.group, (n+1)*sizeof(int)); |
| 115 | t.group[n] = groups; |
| 116 | if (parent >= 0) { |
| 117 | t.child[parent] = groups; |
| 118 | } |
| 119 | ++n; |
| 120 | ++group_size; |
| 121 | } |
| 122 | ++groups; |
| 123 | t.group_offset = realloc(t.group_offset, groups * sizeof(int)); |
| 124 | t.group_offset[groups - 1] = n - group_size; |
| 125 | t.group_size = realloc(t.group_size, groups * sizeof(int)); |
| 126 | t.group_size[groups - 1] = group_size; |
| 127 | t.n = n; |
| 128 | t.groups = groups; |
| 129 | t.leaf = calloc(n, sizeof(int)); |
| 130 | int i; |
| 131 | for(i = 0; i < n; ++i) t.leaf[i] = 1; |
| 132 | for(i = 0; i < n; ++i) if(t.parent[i] >= 0) t.leaf[t.parent[i]] = 0; |
| 133 | |
| 134 | fclose(fp); |
| 135 | tree *tree_ptr = calloc(1, sizeof(tree)); |
| 136 | *tree_ptr = t; |
| 137 | //error(0); |
| 138 | return tree_ptr; |
| 139 | } |
no test coverage detected