31 char parent_name[255];
38 key_value_pairs *keys;
51 int get_nkeys_binding(
int *file_id,
int *block_id)
57 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
59 if(my_files.files[j].keys[i].parent_key == *block_id && !strcmp(my_files.files[j].keys[i].parent_name,
"") ) nkeys = nkeys + 1;
67 void get_key_ids_binding(
int *file_id,
int *block_id,
int *key_ids)
73 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
75 if(my_files.files[j].keys[i].parent_key == *block_id && !strcmp(my_files.files[j].keys[i].parent_name,
"") ){
76 key_count = key_count + 1;
77 key_ids[key_count] = i;
86 char *get_key(
int *file_id,
int *key_id)
89 return my_files.files[j].keys[*key_id].key;
94 char *get_value(
int *file_id,
int *key_id)
97 return my_files.files[j].keys[*key_id].value;
102 char *get_block(
int *file_id,
int *block_id)
105 return my_files.files[j].keys[*block_id].parent_name;
110 char *get_value_from_key_wrap(
int *file_id,
int *block_id,
char *key_name,
int *sucess)
117 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
119 if (my_files.files[j].keys[i].parent_key == *block_id)
121 if( strcmp(my_files.files[j].keys[i].key, key_name) == 0)
128 if (*sucess == 1) {
return my_files.files[j].keys[i].value;}
else {
return "";}
133 int get_num_blocks_all(
int *file_id,
char *block_name)
139 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
141 if(strcmp(my_files.files[j].keys[i].parent_name, block_name) == 0) nblocks = nblocks + 1;
149 int get_num_unique_blocks_bind(
int *file_id,
int *parent_block_id)
154 char block_names[my_files.files[j].nkeys][255];
158 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
160 if (my_files.files[j].keys[i].parent_key == *parent_block_id )
162 if (strcmp(my_files.files[j].keys[i].parent_name,
"") == 0){
166 for (k = 1; k <= nblocks; k++)
168 if (strcmp(block_names[k], my_files.files[j].keys[i].parent_name) == 0)
177 nblocks = nblocks + 1;
178 strcpy(block_names[nblocks], my_files.files[j].keys[i].parent_name);
187 void get_unique_block_ids_bind(
int *file_id,
int *block_ids,
int *parent_block_id)
192 char block_names[my_files.files[j].nkeys][255];
196 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
198 if (my_files.files[j].keys[i].parent_key == *parent_block_id )
200 if (strcmp(my_files.files[j].keys[i].parent_name,
"") == 0){
204 for (k = 1; k <= nblocks; k++)
206 if (strcmp(block_names[k], my_files.files[j].keys[i].parent_name) == 0)
215 nblocks = nblocks + 1;
216 block_ids[nblocks - 1] = my_files.files[j].keys[i].key_number;
217 strcpy(block_names[nblocks], my_files.files[j].keys[i].parent_name);
226 int get_num_blocks_child(
int *file_id,
char *block_name,
int *parent_block_id)
232 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
234 if(strcmp(my_files.files[j].keys[i].parent_name, block_name) == 0 && my_files.files[j].keys[i].parent_key == *parent_block_id) nblocks = nblocks + 1;
242 void get_block_ids_all(
int *file_id,
char *block_name,
int *block_ids)
248 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
250 if(strcmp(my_files.files[j].keys[i].parent_name, block_name) == 0) {
251 nblocks = nblocks + 1;
252 block_ids[nblocks] = my_files.files[j].keys[i].key_number;
260 void get_block_ids_child(
int *file_id,
char *block_name,
int *block_ids,
int *parent_key_id )
266 for ( i = 1; i <= my_files.files[j].nkeys; i++ )
268 if(strcmp(my_files.files[j].keys[i].parent_name, block_name) == 0 && my_files.files[j].keys[i].parent_key == *parent_key_id) {
269 nblocks = nblocks + 1;
270 block_ids[nblocks] = my_files.files[j].keys[i].key_number;
277 bool is_valid_block_id(
int *file_id,
int *block_id)
280 if (*block_id <= -1 || *block_id > my_files.files[*file_id].nkeys) {
return false;}
283 if (*block_id != 0 && strcmp(my_files.files[*file_id].keys[*block_id].parent_name,
"") == 0) {
return false;}
288 bool is_valid_key_id(
int *file_id,
int *key_id)
290 if (*key_id > -1 && *key_id <= my_files.files[*file_id].nkeys) {
return true;}
291 else {
return false;}
295 bool is_valid_file_id(
int *file_id)
297 if (*file_id > -1 && *file_id < nfiles) {
return true;}
298 else {
return false;}
303 int open_and_parse_file_wrap(
char *filename,
int *file_id)
305 yaml_parser_t parser;
315 char layer_name[10][255];
316 char current_layername[255];
322 my_files.files = (yaml_file*)calloc(1,
sizeof(yaml_file));
325 my_files.files = realloc(my_files.files, (nfiles+1)*
sizeof(yaml_file));
332 file = fopen(filename,
"r");
333 if (file == NULL)
return -1;
335 if(!yaml_parser_initialize(&parser))
return -2;
337 my_files.files[j].keys = (key_value_pairs*)calloc(1,
sizeof(key_value_pairs));
340 strcpy(layer_name[0],
"TOP");
342 yaml_parser_set_input_file(&parser, file);
344 if (!yaml_parser_scan(&parser, &token)) {
354 case YAML_VALUE_TOKEN:
359 case YAML_BLOCK_ENTRY_TOKEN:
363 if (strcmp(key_value,
""))
365 strcpy(layer_name[layer], key_value);
367 key_count = key_count + 1;
369 my_files.files[j].keys = realloc(my_files.files[j].keys, (i+1)*
sizeof(key_value_pairs));
370 my_files.files[j].keys[i].key_number=i;
371 my_files.files[j].keys[i].parent_key = parent[layer-1];
372 strcpy(my_files.files[j].keys[i].parent_name, layer_name[layer]);
373 strcpy(my_files.files[j].keys[i].key,
"");
374 strcpy(my_files.files[j].keys[i].value,
"");
375 parent[layer]=key_count;
380 case YAML_BLOCK_END_TOKEN:
385 case YAML_SCALAR_TOKEN:
389 current_parent = parent[layer];
390 strcpy(current_layername,
"");
391 key_count = key_count + 1;
393 my_files.files[j].keys = realloc(my_files.files[j].keys, (i+1)*
sizeof(key_value_pairs));
394 my_files.files[j].keys[i].key_number=i;
395 my_files.files[j].keys[i].parent_key = current_parent;
396 strcpy(my_files.files[j].keys[i].parent_name, current_layername);
397 strcpy(my_files.files[j].keys[i].key, key_value);
398 strcpy(my_files.files[j].keys[i].value, token.data.scalar.value);
399 my_files.files[j].nkeys = key_count;
401 strcpy(key_value,
"");
404 {strcpy(key_value,token.data.scalar.value);}
408 if(token.type != YAML_STREAM_END_TOKEN)
409 yaml_token_delete(&token);
410 }
while(token.type != YAML_STREAM_END_TOKEN);
411 yaml_token_delete(&token);
412 yaml_parser_delete(&parser);