summaryrefslogtreecommitdiff
path: root/plugins/yaml/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/yaml/node.c')
-rw-r--r--plugins/yaml/node.c335
1 files changed, 335 insertions, 0 deletions
diff --git a/plugins/yaml/node.c b/plugins/yaml/node.c
new file mode 100644
index 0000000..b0229c0
--- /dev/null
+++ b/plugins/yaml/node.c
@@ -0,0 +1,335 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * node.c - ligne de contenu Yaml
+ *
+ * Copyright (C) 2019 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Chrysalide. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "node.h"
+
+
+#include <malloc.h>
+#include <string.h>
+
+
+
+/* Noeud d'une arborescence au format Yaml (instance) */
+struct _GYamlNode
+{
+ GObject parent; /* A laisser en premier */
+
+ GYamlLine *key; /* Clef principale du noeud */
+
+ GYamlNode **children; /* Sous-noeuds intégrés */
+ size_t count; /* Nombre de ces enfants */
+
+};
+
+/* Noeud d'une arborescence au format Yaml (classe) */
+struct _GYamlNodeClass
+{
+ GObjectClass parent; /* A laisser en premier */
+
+};
+
+
+/* Initialise la classe des noeuds d'arborescence Yaml. */
+static void g_yaml_node_class_init(GYamlNodeClass *);
+
+/* Initialise une instance de noeud d'arborescence Yaml. */
+static void g_yaml_node_init(GYamlNode *);
+
+/* Supprime toutes les références externes. */
+static void g_yaml_node_dispose(GYamlNode *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_yaml_node_finalize(GYamlNode *);
+
+
+
+/* Indique le type défini pour un noeud d'arborescence Yaml. */
+G_DEFINE_TYPE(GYamlNode, g_yaml_node, G_TYPE_OBJECT);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des noeuds d'arborescence Yaml. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_yaml_node_class_init(GYamlNodeClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_yaml_node_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_yaml_node_finalize;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = instance à initialiser. *
+* *
+* Description : Initialise une instance de noeud d'arborescence Yaml. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_yaml_node_init(GYamlNode *node)
+{
+ node->key = NULL;
+
+ node->children = NULL;
+ node->count = 0;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_yaml_node_dispose(GYamlNode *node)
+{
+ size_t i; /* Boucle de parcours */
+
+ g_clear_object(&node->key);
+
+ for (i = 0; i < node->count; i++)
+ g_clear_object(&node->children[i]);
+
+ G_OBJECT_CLASS(g_yaml_node_parent_class)->dispose(G_OBJECT(node));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_yaml_node_finalize(GYamlNode *node)
+{
+ if (node->children != NULL)
+ free(node->children);
+
+ G_OBJECT_CLASS(g_yaml_node_parent_class)->finalize(G_OBJECT(node));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : key = line Yaml représentant la clef du futur noeud. *
+* *
+* Description : Construit un noeud d'arborescence Yaml. *
+* *
+* Retour : Instance mise en place ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GYamlNode *g_yaml_node_new(GYamlLine *key)
+{
+ GYamlNode *result; /* Structure à retourner */
+
+ result = g_object_new(G_TYPE_YAML_NODE, NULL);
+
+ result->key = key;
+ g_object_ref(G_OBJECT(key));
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud d'arborescence Yaml à consulter. *
+* *
+* Description : Fournit la ligne principale associée à un noeud. *
+* *
+* Retour : Ligne Yaml à l'origine du noeud. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GYamlLine *g_yaml_node_get_yaml_line(const GYamlNode *node)
+{
+ GYamlLine *result; /* Ligne d'origine à renvoyer */
+
+ result = node->key;
+
+ g_object_ref(G_OBJECT(result));
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud d'arborescence Yaml à compléter. *
+* child = noeud à rattacher. *
+* *
+* Description : Ajoute un noeud à un noeud d'une arborescence Yaml. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void g_yaml_node_add_child(GYamlNode *node, GYamlNode *child)
+{
+ node->children = realloc(node->children, ++node->count * sizeof(GYamlNode *));
+
+ node->children[node->count - 1] = child;
+ g_object_ref_sink(G_OBJECT(child));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud d'arborescence Yaml à consulter. *
+* count = taille de la liste constituée. [OUT] *
+* *
+* Description : Fournit la liste des noeuds intégrés dans un noeud Yaml. *
+* *
+* Retour : Enfants d'un noeud issu d'une arborescence Yaml. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GYamlNode **g_yaml_node_get_children(const GYamlNode *node, size_t *count)
+{
+ GYamlNode **result; /* Liste à retourner */
+ size_t i; /* Boucle de parcours */
+
+ *count = node->count;
+
+ result = malloc(*count * sizeof(GYamlNode *));
+
+ for (i = 0; i < *count; i++)
+ {
+ result[i] = node->children[i];
+ g_object_ref(G_OBJECT(result[i]));
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : node = noeud d'arborescence Yaml à consulter. *
+* path = chemin d'accès à parcourir. *
+* *
+* Description : Recherche le noeud correspondant à un chemin. *
+* *
+* Retour : Eventuel noeud trouvé ou NULL si aucun. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GYamlNode *g_yaml_node_find_node_by_path(GYamlNode *node, const char *path)
+{
+ GYamlNode *result; /* Trouvaille à retourner */
+ char *next; /* Prochaine partie du chemin */
+ size_t cmplen; /* Etendue de la comparaison */
+ size_t i; /* Boucle de parcours */
+ GYamlLine *line; /* Ligne Yaml d'un enfant */
+ const char *key; /* Clef d'un noeud */
+ int ret; /* Bilan d'une comparaison */
+
+ result = NULL;
+
+ if (path[0] == '\0')
+ result = node;
+
+ else if (path[0] == '/')
+ {
+ next = strchr(path + 1, '/');
+
+ cmplen = (next == NULL ? strlen(path + 1) : next - path - 1);
+
+ if (cmplen == 0)
+ result = node;
+
+ for (i = 0; i < node->count && result == NULL; i++)
+ {
+ line = g_yaml_node_get_yaml_line(node->children[i]);
+
+ key = g_yaml_line_get_key(line);
+
+ ret = strncmp(path + 1, key, cmplen);
+
+ if (ret == 0)
+ {
+ if (next != NULL)
+ result = g_yaml_node_find_node_by_path(node->children[i], next);
+
+ else
+ result = node->children[i];
+
+ }
+
+ }
+
+ }
+
+ if (result != NULL)
+ g_object_ref(G_OBJECT(result));
+
+ return result;
+
+}