[c] bonjour
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "stdbool.h"
- typedef struct {
- char* key;
- char* value;
- } prop_t;
- typedef struct tag_s {
- char* name;
- struct tag_s* children;
- size_t children_count;
- prop_t* props;
- size_t props_count;
- } tag_t;
- static void print_object(char *prefix, tag_t *tag)
- {
- printf("%s{\n", prefix);
- for (size_t i = 0; i < tag->props_count; i++) {
- if (i != 0)
- printf(",\n");
- printf(
- "%s \"%s\": \"%s\"",
- prefix, tag->props[i].key, tag->props[i].value
- );
- }
- printf("\n");
- printf("%s}", prefix);
- }
- static char* find_prop(
- tag_t *tag, char *key, char *value, bool recursive, tag_t **out_holder
- )
- {
- if (tag == NULL)
- return NULL;
- for (size_t i = 0; i < tag->props_count; i++)
- if (
- (key == NULL || strcmp(tag->props[i].key, key) == 0) &&
- (value == NULL || strcmp(tag->props[i].value, value) == 0)
- ) {
- if (out_holder != NULL)
- *out_holder = tag;
- return tag->props[i].value;
- }
- for (size_t i = 0; recursive && i < tag->children_count; i++) {
- char *found = find_prop(
- &tag->children[i], key, value, recursive, out_holder
- );
- if (found != NULL)
- return found;
- }
- return NULL;
- }
- static bool print_childrens_of_child(
- tag_t *root, char* child_name, char *id, char *id_compartment
- )
- {
- tag_t *child = root;
- printf("\"%s\" : [\n", child->name);
- bool had = false;
- for (size_t i = 0; i < child->children_count; i++) {
- if (id && strcmp(child->children[i].name, "speciesReference") &&
- !find_prop(&child->children[i], 0, id, 1, 0) &&
- !find_prop(&child->children[i], "id", id_compartment, 0, 0)) {
- had = false;
- continue;
- }
- had && printf(",\n");
- print_object(" ", &child->children[i]);
- had = true;
- }
- printf("\n]");
- return true;
- }
- static bool find_print_parameters(
- tag_t *root, char *id,
- tag_t **out_to_print, char **out_props,
- tag_t **out_id_holder
- )
- {
- memcpy(out_props, (char*[]){ "listOfCompartments", "listOfSpecies",
- "listOfReactions", NULL }, sizeof(char*) * 4);
- if (id == NULL)
- return true;
- find_prop(root, "id", id, true, out_id_holder);
- if (*out_id_holder == NULL)
- return false;
- if (strcmp((*out_id_holder)->name, "reaction") == 0) {
- *out_to_print = *out_id_holder;
- memcpy(out_props, (char*[]){ "listOfReactants", "listOfProducts",
- NULL }, sizeof(char*) * 4);
- }
- return true;
- }
- /// objects is an array of all objects in the document
- /// objects_len is the number of objects in the objects array
- /// compartment is either null or the name of the compartment to print
- bool to_json(tag_t *root, char *id)
- {
- tag_t *to_print = root, *id_holder = NULL;
- char *props[50];
- find_print_parameters(root, id, &to_print, props, &id_holder);
- char *id_compartment = find_prop(id_holder, "compartment", 0,0,0);
- printf("{\n");
- char **t = props;
- bool printed = false;
- for (char **t = props; *t != NULL; t++) {
- printed && printf(",\n");
- tag_t *child = to_print;
- for (
- size_t i = 0;
- i < root->children_count && strcmp(child->name, *t);
- child = &root->children[i++]
- );
- printed = print_childrens_of_child(child, *t, id, id_compartment);
- }
- printf("\n}\n");
- return true;
- }
Editor
You can edit this paste and save as new: