summaryrefslogtreecommitdiff
path: root/plugins/yaml/pair.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2023-04-27 21:27:52 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2023-04-27 21:27:52 (GMT)
commit25bac01127581767639a5bd9024c41eb803388fa (patch)
treea67cd5cdecbf001e788935e756fc722b7c552b9b /plugins/yaml/pair.c
parenta1ff7646ce6f829dd192e2e2246def2ea583e93b (diff)
Replace the old YAML parser by another one relying on the external libyaml library.
Diffstat (limited to 'plugins/yaml/pair.c')
-rw-r--r--plugins/yaml/pair.c308
1 files changed, 150 insertions, 158 deletions
diff --git a/plugins/yaml/pair.c b/plugins/yaml/pair.c
index df29f6f..1057a68 100644
--- a/plugins/yaml/pair.c
+++ b/plugins/yaml/pair.c
@@ -1,8 +1,8 @@
/* Chrysalide - Outil d'analyse de fichiers binaires
- * pair.c - noeud Yaml de paire clef/valeur
+ * pair.c - noeud YAML de paire clef/valeur
*
- * Copyright (C) 2020 Cyrille Bagard
+ * Copyright (C) 2020-2023 Cyrille Bagard
*
* This file is part of Chrysalide.
*
@@ -24,38 +24,22 @@
#include "pair.h"
+#include <assert.h>
#include <malloc.h>
#include <string.h>
-#include "node-int.h"
+#include "pair-int.h"
-/* Noeud d'une arborescence au format Yaml (instance) */
-struct _GYamlPair
-{
- GYamlNode parent; /* A laisser en premier */
-
- char *key; /* Clef présente dans le noeud */
- char *value; /* Valeur associée */
-
- GYamlCollection *collection; /* Collection de noeuds */
-
-};
-
-/* Noeud d'une arborescence au format Yaml (classe) */
-struct _GYamlPairClass
-{
- GYamlNodeClass parent; /* A laisser en premier */
-
-};
+/* -------------------- DEFINITIONS PROPRES POUR LE SUPPORT YAML -------------------- */
-/* Initialise la classe des noeuds d'arborescence Yaml. */
+/* Initialise la classe des noeuds d'arborescence YAML. */
static void g_yaml_pair_class_init(GYamlPairClass *);
-/* Initialise une instance de noeud d'arborescence Yaml. */
+/* Initialise une instance de noeud d'arborescence YAML. */
static void g_yaml_pair_init(GYamlPair *);
/* Supprime toutes les références externes. */
@@ -64,12 +48,22 @@ static void g_yaml_pair_dispose(GYamlPair *);
/* Procède à la libération totale de la mémoire. */
static void g_yaml_pair_finalize(GYamlPair *);
-/* Recherche les noeuds correspondant à un chemin. */
-static void g_yaml_pair_find_by_path(const GYamlPair *, const char *, bool, GYamlNode ***, size_t *);
+/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */
-/* Indique le type défini pour un noeud d'arborescence Yaml. */
+
+/* Recherche le premier noeud correspondant à un chemin. */
+static GYamlNode *g_yaml_pair_find_first_by_path(GYamlPair *, const char *);
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* DEFINITIONS PROPRES POUR LE SUPPORT YAML */
+/* ---------------------------------------------------------------------------------- */
+
+
+/* Indique le type défini pour un noeud d'arborescence YAML. */
G_DEFINE_TYPE(GYamlPair, g_yaml_pair, G_TYPE_YAML_NODE);
@@ -77,7 +71,7 @@ G_DEFINE_TYPE(GYamlPair, g_yaml_pair, G_TYPE_YAML_NODE);
* *
* Paramètres : klass = classe à initialiser. *
* *
-* Description : Initialise la classe des noeuds d'arborescence Yaml. *
+* Description : Initialise la classe des noeuds d'arborescence YAML. *
* *
* Retour : - *
* *
@@ -97,16 +91,16 @@ static void g_yaml_pair_class_init(GYamlPairClass *klass)
node = G_YAML_NODE_CLASS(klass);
- node->find = (find_yaml_node_fc)g_yaml_pair_find_by_path;
+ node->find = (find_first_yaml_node_fc)g_yaml_pair_find_first_by_path;
}
/******************************************************************************
* *
-* Paramètres : node = instance à initialiser. *
+* Paramètres : pair = instance à initialiser. *
* *
-* Description : Initialise une instance de noeud d'arborescence Yaml. *
+* Description : Initialise une instance de noeud d'arborescence YAML. *
* *
* Retour : - *
* *
@@ -114,19 +108,19 @@ static void g_yaml_pair_class_init(GYamlPairClass *klass)
* *
******************************************************************************/
-static void g_yaml_pair_init(GYamlPair *node)
+static void g_yaml_pair_init(GYamlPair *pair)
{
- node->key = NULL;
- node->value = NULL;
+ pair->key = NULL;
+ pair->value = NULL;
- node->collection = NULL;
+ pair->children = NULL;
}
/******************************************************************************
* *
-* Paramètres : node = instance d'objet GLib à traiter. *
+* Paramètres : pair = instance d'objet GLib à traiter. *
* *
* Description : Supprime toutes les références externes. *
* *
@@ -136,18 +130,18 @@ static void g_yaml_pair_init(GYamlPair *node)
* *
******************************************************************************/
-static void g_yaml_pair_dispose(GYamlPair *node)
+static void g_yaml_pair_dispose(GYamlPair *pair)
{
- g_clear_object(&node->collection);
+ g_clear_object(&pair->children);
- G_OBJECT_CLASS(g_yaml_pair_parent_class)->dispose(G_OBJECT(node));
+ G_OBJECT_CLASS(g_yaml_pair_parent_class)->dispose(G_OBJECT(pair));
}
/******************************************************************************
* *
-* Paramètres : node = instance d'objet GLib à traiter. *
+* Paramètres : pair = instance d'objet GLib à traiter. *
* *
* Description : Procède à la libération totale de la mémoire. *
* *
@@ -157,24 +151,25 @@ static void g_yaml_pair_dispose(GYamlPair *node)
* *
******************************************************************************/
-static void g_yaml_pair_finalize(GYamlPair *node)
+static void g_yaml_pair_finalize(GYamlPair *pair)
{
- if (node->key != NULL)
- free(node->key);
+ if (pair->key != NULL)
+ free(pair->key);
- if (node->value != NULL)
- free(node->value);
+ if (pair->value != NULL)
+ free(pair->value);
- G_OBJECT_CLASS(g_yaml_pair_parent_class)->finalize(G_OBJECT(node));
+ G_OBJECT_CLASS(g_yaml_pair_parent_class)->finalize(G_OBJECT(pair));
}
/******************************************************************************
* *
-* Paramètres : line = ligne Yaml à l'origine du futur noeud. *
+* Paramètres : key = désignation pour le noeud YAML. *
+* value = éventuelle valeur directe portée par le noeud. *
* *
-* Description : Construit un noeud d'arborescence Yaml. *
+* Description : Construit un noeud d'arborescence YAML. *
* *
* Retour : Instance mise en place ou NULL en cas d'échec. *
* *
@@ -182,36 +177,14 @@ static void g_yaml_pair_finalize(GYamlPair *node)
* *
******************************************************************************/
-GYamlPair *g_yaml_pair_new(GYamlLine *line)
+GYamlPair *g_yaml_pair_new(const char *key, const char *value)
{
GYamlPair *result; /* Structure à retourner */
- const char *key; /* Clef associée au noeud */
- const char *value; /* Eventuelle valeur associée */
- key = g_yaml_line_get_key(line);
- value = g_yaml_line_get_value(line);
+ result = g_object_new(G_TYPE_YAML_PAIR, NULL);
- if (key == NULL)
- key = g_yaml_line_get_payload(line);
-
- if (key == NULL)
- result = NULL;
-
- else
- {
- result = g_object_new(G_TYPE_YAML_PAIR, NULL);
-
- G_YAML_NODE(result)->line = line;
- g_object_ref(G_OBJECT(line));
-
- result->key = strdup(key);
-
- if (value == NULL)
- result->value = NULL;
- else
- result->value = strdup(value);
-
- }
+ if (!g_yaml_pair_create(result, key, value))
+ g_clear_object(&result);
return result;
@@ -220,93 +193,39 @@ GYamlPair *g_yaml_pair_new(GYamlLine *line)
/******************************************************************************
* *
-* Paramètres : node = noeud d'arborescence Yaml à consulter. *
-* path = chemin d'accès à parcourir. *
-* prepare = indication sur une préparation d'un prochain appel.*
-* nodes = liste de noeuds avec correspondance établie. [OUT] *
-* count = quantité de ces noeuds. [OUT] *
+* Paramètres : pair = paire YAML à initialiser pleinement. *
+* key = désignation pour le noeud YAML. *
+* value = éventuelle valeur directe portée par le noeud. *
* *
-* Description : Recherche les noeuds correspondant à un chemin. *
+* Description : Met en place une pair clef/valeur YAML. *
* *
-* Retour : - *
+* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
-static void g_yaml_pair_find_by_path(const GYamlPair *node, const char *path, bool prepare, GYamlNode ***nodes, size_t *count)
+bool g_yaml_pair_create(GYamlPair *pair, const char *key, const char *value)
{
- char *next; /* Prochaine partie du chemin */
- size_t cmplen; /* Etendue de la comparaison */
- int ret; /* Bilan d'une comparaison */
-
- if (path[0] == '\0')
- goto exit;
-
- /* Correspondance au niveau du noeud ? */
-
- if (path[0] == '/')
- {
- path++;
-
- if (path[0] == '\0')
- goto matched;
-
- }
-
- next = strchr(path, '/');
-
- if (next == NULL)
- ret = strcmp(path, node->key);
-
- else
- {
- cmplen = next - path;
-
- if (cmplen == 0)
- goto cont;
-
- ret = strncmp(path, node->key, cmplen);
-
- }
-
- if (ret != 0)
- goto done;
-
- else if (next != NULL)
- {
- path += cmplen;
- goto cont;
- }
-
- matched:
+ bool result; /* Bilan à retourner */
- *nodes = realloc(*nodes, ++(*count) * sizeof(GYamlNode **));
+ result = true;
- g_object_ref(G_OBJECT(node));
- (*nodes)[*count - 1] = G_YAML_NODE(node);
+ pair->key = strdup(key);
- goto done;
+ if (value != NULL)
+ pair->value = strdup(value);
- cont:
-
- if (node->collection != NULL)
- _g_yaml_node_find_by_path(G_YAML_NODE(node->collection), path, prepare, nodes, count);
-
- done:
-
- exit:
-
- ;
+ return result;
}
/******************************************************************************
* *
-* Paramètres : node = noeud d'arborescence Yaml à consulter. *
+* Paramètres : pair = noeud d'arborescence YAML à consulter. *
* *
-* Description : Fournit la clef représentée dans une paire en Yaml. *
+* Description : Fournit la clef représentée dans une paire en YAML. *
* *
* Retour : Clef sous forme de chaîne de caractères. *
* *
@@ -314,11 +233,11 @@ static void g_yaml_pair_find_by_path(const GYamlPair *node, const char *path, bo
* *
******************************************************************************/
-const char *g_yaml_pair_get_key(const GYamlPair *node)
+const char *g_yaml_pair_get_key(const GYamlPair *pair)
{
char *result; /* Valeur à retourner */
- result = node->key;
+ result = pair->key;
return result;
@@ -327,9 +246,9 @@ const char *g_yaml_pair_get_key(const GYamlPair *node)
/******************************************************************************
* *
-* Paramètres : node = noeud d'arborescence Yaml à consulter. *
+* Paramètres : pair = noeud d'arborescence YAML à consulter. *
* *
-* Description : Fournit l'éventuelle valeur d'une paire en Yaml. *
+* Description : Fournit l'éventuelle valeur d'une paire en YAML. *
* *
* Retour : Valeur sous forme de chaîne de caractères ou NULL. *
* *
@@ -337,11 +256,11 @@ const char *g_yaml_pair_get_key(const GYamlPair *node)
* *
******************************************************************************/
-const char *g_yaml_pair_get_value(const GYamlPair *node)
+const char *g_yaml_pair_get_value(const GYamlPair *pair)
{
char *result; /* Valeur à retourner */
- result = node->value;
+ result = pair->value;
return result;
@@ -350,10 +269,10 @@ const char *g_yaml_pair_get_value(const GYamlPair *node)
/******************************************************************************
* *
-* Paramètres : node = noeud d'arborescence Yaml à compléter. *
-* collec = collection de noeuds Yaml. *
+* Paramètres : pair = noeud d'arborescence YAML à compléter. *
+* children = collection de noeuds YAML. *
* *
-* Description : Attache une collection de noeuds Yaml à un noeud. *
+* Description : Attache une collection de noeuds YAML à un noeud. *
* *
* Retour : - *
* *
@@ -361,33 +280,33 @@ const char *g_yaml_pair_get_value(const GYamlPair *node)
* *
******************************************************************************/
-void g_yaml_pair_set_collection(GYamlPair *node, GYamlCollection *collec)
+void g_yaml_pair_set_children(GYamlPair *pair, GYamlCollection *children)
{
- g_clear_object(&node->collection);
+ g_clear_object(&pair->children);
- g_object_ref_sink(G_OBJECT(collec));
- node->collection = collec;
+ g_object_ref_sink(G_OBJECT(children));
+ pair->children = children;
}
/******************************************************************************
* *
-* Paramètres : node = noeud d'arborescence Yaml à consulter. *
+* Paramètres : pair = noeud d'arborescence YAML à consulter. *
* *
* Description : Fournit une éventuelle collection rattachée à un noeud. *
* *
-* Retour : Collection de noeuds Yaml ou NULL. *
+* Retour : Collection de noeuds YAML ou NULL. *
* *
* Remarques : - *
* *
******************************************************************************/
-GYamlCollection *g_yaml_pair_get_collection(const GYamlPair *node)
+GYamlCollection *g_yaml_pair_get_children(const GYamlPair *pair)
{
GYamlCollection *result; /* Collection à renvoyer */
- result = node->collection;
+ result = pair->children;
if (result != NULL)
g_object_ref(G_OBJECT(result));
@@ -395,3 +314,76 @@ GYamlCollection *g_yaml_pair_get_collection(const GYamlPair *node)
return result;
}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* IMPLEMENTATION DES FONCTIONS DE CLASSE */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : pair = noeud d'arborescence YAML à consulter. *
+* path = chemin d'accès à parcourir. *
+* *
+* Description : Recherche le premier noeud correspondant à un chemin. *
+* *
+* Retour : Noeud avec la correspondance établie ou NULL si non trouvé. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static GYamlNode *g_yaml_pair_find_first_by_path(GYamlPair *pair, const char *path)
+{
+ GYamlNode *result; /* Trouvaille à retourner */
+ char *next; /* Prochaine partie du chemin */
+ size_t cmplen; /* Etendue de la comparaison */
+ int ret; /* Bilan d'une comparaison */
+
+ assert(path[0] != '/' && path[0] != '\0');
+
+ /* Correspondance au niveau du noeud ? */
+
+ next = strchr(path, '/');
+
+ if (next == NULL)
+ ret = strcmp(path, pair->key);
+
+ else
+ {
+ cmplen = next - path;
+ assert(cmplen > 0);
+
+ ret = strncmp(path, pair->key, cmplen);
+
+ }
+
+ /* Si correspondance il y a... */
+
+ if (ret == 0)
+ {
+ /* ... et que la recherche se trouve en bout de parcours */
+ if (next == NULL)
+ {
+ result = G_YAML_NODE(pair);
+ g_object_ref(G_OBJECT(result));
+ }
+
+ /* Recherche supplémentaire dans les sous-noeuds ? */
+
+ else if (pair->children != NULL)
+ result = g_yaml_node_find_first_by_path(G_YAML_NODE(pair->children), path + cmplen);
+
+ else
+ result = NULL;
+
+ }
+
+ else
+ result = NULL;
+
+ return result;
+
+}