From 57e3511bbc5fce4eedfad7b57e775338b3eed56a Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Fri, 11 Dec 2020 15:29:09 +0100
Subject: Updated the Python API to use properly handled constants.

---
 plugins/pychrysalide/analysis/types/constants.c | 100 ++++++++++++++++++++++++
 plugins/pychrysalide/analysis/types/constants.h |   6 ++
 plugins/pychrysalide/analysis/types/encaps.c    |  37 +--------
 plugins/pychrysalide/core/constants.c           |  50 ++++++++++++
 plugins/pychrysalide/core/constants.h           |   3 +
 plugins/pychrysalide/core/params.c              |  39 +--------
 plugins/pychrysalide/gtkext/graph/Makefile.am   |   1 +
 plugins/pychrysalide/gtkext/graph/constants.c   |  75 ++++++++++++++++++
 plugins/pychrysalide/gtkext/graph/constants.h   |  39 +++++++++
 plugins/pychrysalide/gtkext/graph/edge.c        |  36 +--------
 10 files changed, 280 insertions(+), 106 deletions(-)
 create mode 100644 plugins/pychrysalide/gtkext/graph/constants.c
 create mode 100644 plugins/pychrysalide/gtkext/graph/constants.h

diff --git a/plugins/pychrysalide/analysis/types/constants.c b/plugins/pychrysalide/analysis/types/constants.c
index 1112410..76eaaf8 100644
--- a/plugins/pychrysalide/analysis/types/constants.c
+++ b/plugins/pychrysalide/analysis/types/constants.c
@@ -27,6 +27,7 @@
 
 #include <analysis/types/basic.h>
 #include <analysis/types/cse.h>
+#include <analysis/types/encaps.h>
 
 
 #include "../../helpers.h"
@@ -254,3 +255,102 @@ int convert_to_class_enum_type_class_enum_kind(PyObject *arg, void *dst)
     return result;
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : type = type dont le dictionnaire est à compléter.            *
+*                                                                             *
+*  Description : Définit les constantes relatives aux types encapsulés.       *
+*                                                                             *
+*  Retour      : true en cas de succès de l'opération, false sinon.           *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool define_encapsulated_type_constants(PyTypeObject *type)
+{
+    bool result;                            /* Bilan à retourner           */
+    PyObject *values;                       /* Groupe de valeurs à établir */
+
+    values = PyDict_New();
+
+    result = add_const_to_group(values, "POINTER", ECT_POINTER);
+    if (result) result = add_const_to_group(values, "ARRAY", ECT_ARRAY);
+    if (result) result = add_const_to_group(values, "REFERENCE", ECT_REFERENCE);
+    if (result) result = add_const_to_group(values, "RVALUE_REF", ECT_RVALUE_REF);
+    if (result) result = add_const_to_group(values, "COMPLEX", ECT_COMPLEX);
+    if (result) result = add_const_to_group(values, "IMAGINARY", ECT_IMAGINARY);
+    if (result) result = add_const_to_group(values, "COUNT", ECT_COUNT);
+
+    if (!result)
+    {
+        Py_DECREF(values);
+        goto exit;
+    }
+
+    result = attach_constants_group_to_type(type, false, "EncapsulationType", values,
+                                            "Identifiers for basic data types.");
+
+ exit:
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : arg = argument quelconque à tenter de convertir.             *
+*                dst = destination des valeurs récupérées en cas de succès.   *
+*                                                                             *
+*  Description : Tente de convertir en constante EncapsulationType.           *
+*                                                                             *
+*  Retour      : Bilan de l'opération, voire indications supplémentaires.     *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+int convert_to_encapsulation_type(PyObject *arg, void *dst)
+{
+    int result;                             /* Bilan à retourner           */
+    unsigned long value;                    /* Valeur transcrite           */
+
+    result = PyObject_IsInstance(arg, (PyObject *)&PyLong_Type);
+
+    switch (result)
+    {
+        case -1:
+            /* L'exception est déjà fixée par Python */
+            result = 0;
+            break;
+
+        case 0:
+            PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to EncapsulationType");
+            break;
+
+        case 1:
+            value = PyLong_AsUnsignedLong(arg);
+
+            if (value > ECT_COUNT)
+            {
+                PyErr_SetString(PyExc_TypeError, "invalid value for EncapsulationType");
+                result = 0;
+            }
+
+            else
+                *((EncapsulationType *)dst) = value;
+
+            break;
+
+        default:
+            assert(false);
+            break;
+
+    }
+
+    return result;
+
+}
diff --git a/plugins/pychrysalide/analysis/types/constants.h b/plugins/pychrysalide/analysis/types/constants.h
index b695e8d..9245c56 100644
--- a/plugins/pychrysalide/analysis/types/constants.h
+++ b/plugins/pychrysalide/analysis/types/constants.h
@@ -43,6 +43,12 @@ bool define_class_enum_type_constants(PyTypeObject *);
 /* Tente de convertir en constante ClassEnumKind. */
 int convert_to_class_enum_type_class_enum_kind(PyObject *, void *);
 
+/* Définit les constantes relatives aux types encapsulés. */
+bool define_encapsulated_type_constants(PyTypeObject *);
+
+/* Tente de convertir en constante EncapsulationType. */
+int convert_to_encapsulation_type(PyObject *, void *);
+
 
 
 #endif  /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_TYPES_CONSTANTS_H */
diff --git a/plugins/pychrysalide/analysis/types/encaps.c b/plugins/pychrysalide/analysis/types/encaps.c
index b530394..3a5acb5 100644
--- a/plugins/pychrysalide/analysis/types/encaps.c
+++ b/plugins/pychrysalide/analysis/types/encaps.c
@@ -31,6 +31,7 @@
 #include <analysis/types/encaps.h>
 
 
+#include "constants.h"
 #include "../type.h"
 #include "../../access.h"
 #include "../../helpers.h"
@@ -43,9 +44,6 @@ static PyObject *py_encapsulated_type_new(PyTypeObject *, PyObject *, PyObject *
 /* Fournit le type encapsulée dans le type. */
 static PyObject *py_encapsulated_type_get_item(PyObject *, void *);
 
-/* Définit les constantes pour les types d'encapsulation. */
-static bool py_encapsulated_type_define_constants(PyTypeObject *);
-
 
 
 /******************************************************************************
@@ -198,37 +196,6 @@ PyTypeObject *get_python_encapsulated_type_type(void)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
-*                                                                             *
-*  Description : Définit les constantes pour les types d'encapsulation.       *
-*                                                                             *
-*  Retour      : -                                                            *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-static bool py_encapsulated_type_define_constants(PyTypeObject *obj_type)
-{
-    bool result;                            /* Bilan à retourner           */
-
-    result = true;
-
-    result &= PyDict_AddULongMacro(obj_type, ECT_POINTER);
-    result &= PyDict_AddULongMacro(obj_type, ECT_REFERENCE);
-    result &= PyDict_AddULongMacro(obj_type, ECT_RVALUE_REF);
-    result &= PyDict_AddULongMacro(obj_type, ECT_COMPLEX);
-    result &= PyDict_AddULongMacro(obj_type, ECT_IMAGINARY);
-
-    result &= PyDict_AddULongMacro(obj_type, ECT_COUNT);
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
 *  Paramètres  : module = module dont la définition est à compléter.          *
 *                                                                             *
 *  Description : Prend en charge l'objet 'pychrysalide.....EncapsulatedType'. *
@@ -259,7 +226,7 @@ bool ensure_python_encapsulated_type_is_registered(void)
         if (!register_class_for_pygobject(dict, G_TYPE_ENCAPSULATED_TYPE, type, get_python_data_type_type()))
             return false;
 
-        if (!py_encapsulated_type_define_constants(type))
+        if (!define_encapsulated_type_constants(type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/core/constants.c b/plugins/pychrysalide/core/constants.c
index 99abf95..5699f19 100644
--- a/plugins/pychrysalide/core/constants.c
+++ b/plugins/pychrysalide/core/constants.c
@@ -26,6 +26,7 @@
 
 
 #include <core/logs.h>
+#include <core/params.h>
 
 
 #include "../helpers.h"
@@ -129,3 +130,52 @@ int convert_to_log_message_type(PyObject *arg, void *dst)
     return result;
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : module = module dont le dictionnaire est à compléter.        *
+*                                                                             *
+*  Description : Définit les constantes pour les désignations de paramètres.  *
+*                                                                             *
+*  Retour      : true en cas de succès de l'opération, false sinon.           *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool define_core_params_constants(PyObject *module)
+{
+    bool result;                            /* Bilan à retourner           */
+    PyObject *strdict;                      /* Groupe de chaînes constantes*/
+
+    result = create_string_constants_group_to_module(module, "MainParameterKeys",
+                                                   "Keys referring to main configuration parameters.", &strdict);
+
+    if (result) result = extend_string_constants_group(strdict, "FORMAT_NO_NAME", MPK_FORMAT_NO_NAME);
+    if (result) result = extend_string_constants_group(strdict, "INTERNAL_THEME", MPK_INTERNAL_THEME);
+    if (result) result = extend_string_constants_group(strdict, "TITLE_BAR", MPK_TITLE_BAR);
+    if (result) result = extend_string_constants_group(strdict, "LAST_PROJECT", MPK_LAST_PROJECT);
+    if (result) result = extend_string_constants_group(strdict, "SKIP_EXIT_MSG", MPK_SKIP_EXIT_MSG);
+    if (result) result = extend_string_constants_group(strdict, "MAXIMIZED", MPK_MAXIMIZED);
+    if (result) result = extend_string_constants_group(strdict, "ELLIPSIS_HEADER", MPK_ELLIPSIS_HEADER);
+    if (result) result = extend_string_constants_group(strdict, "ELLIPSIS_TAB", MPK_ELLIPSIS_TAB);
+    if (result) result = extend_string_constants_group(strdict, "WELCOME_STARTUP", MPK_WELCOME_STARTUP);
+    if (result) result = extend_string_constants_group(strdict, "WELCOME_CHECK", MPK_WELCOME_CHECK);
+    if (result) result = extend_string_constants_group(strdict, "LABEL_OFFSET", MPK_LABEL_OFFSET);
+    if (result) result = extend_string_constants_group(strdict, "HEX_PADDING", MPK_HEX_PADDING);
+    if (result) result = extend_string_constants_group(strdict, "SELECTION_LINE", MPK_SELECTION_LINE);
+    if (result) result = extend_string_constants_group(strdict, "TOOLTIP_MAX_CALLS", MPK_TOOLTIP_MAX_CALLS);
+    if (result) result = extend_string_constants_group(strdict, "TOOLTIP_MAX_STRINGS", MPK_TOOLTIP_MAX_STRINGS);
+    if (result) result = extend_string_constants_group(strdict, "HEX_UPPER_CASE", MPK_HEX_UPPER_CASE);
+    if (result) result = extend_string_constants_group(strdict, "LINK_DEFAULT", MPK_LINK_DEFAULT);
+    if (result) result = extend_string_constants_group(strdict, "LINK_BRANCH_TRUE", MPK_LINK_BRANCH_TRUE);
+    if (result) result = extend_string_constants_group(strdict, "LINK_BRANCH_FALSE", MPK_LINK_BRANCH_FALSE);
+    if (result) result = extend_string_constants_group(strdict, "LINK_LOOP", MPK_LINK_LOOP);
+    if (result) result = extend_string_constants_group(strdict, "KEYBINDINGS_EDIT", MPK_KEYBINDINGS_EDIT);
+    if (result) result = extend_string_constants_group(strdict, "TMPDIR", MPK_TMPDIR);
+    if (result) result = extend_string_constants_group(strdict, "AUTO_SAVE", MPK_AUTO_SAVE);
+
+    return result;
+
+}
diff --git a/plugins/pychrysalide/core/constants.h b/plugins/pychrysalide/core/constants.h
index 6ed6fbb..783af98 100644
--- a/plugins/pychrysalide/core/constants.h
+++ b/plugins/pychrysalide/core/constants.h
@@ -37,6 +37,9 @@ bool define_core_logs_constants(PyObject *);
 /* Tente de convertir en constante LogMessageType. */
 int convert_to_log_message_type(PyObject *, void *);
 
+/* Définit les constantes pour les désignations de paramètres. */
+bool define_core_params_constants(PyObject *);
+
 
 
 #endif  /* _PLUGINS_PYCHRYSALIDE_CORE_CONSTANTS_H */
diff --git a/plugins/pychrysalide/core/params.c b/plugins/pychrysalide/core/params.c
index cb8c9f1..4a0513d 100644
--- a/plugins/pychrysalide/core/params.c
+++ b/plugins/pychrysalide/core/params.c
@@ -31,6 +31,7 @@
 #include <core/params.h>
 
 
+#include "constants.h"
 #include "../access.h"
 #include "../helpers.h"
 
@@ -39,9 +40,6 @@
 /* Fournit la version du programme global. */
 static PyObject *py_params_get_main_configuration(PyObject *, PyObject *);
 
-/* Définit les constantes pour les paramètres. */
-static bool py_params_define_constants(PyObject *);
-
 
 
 /******************************************************************************
@@ -84,35 +82,6 @@ static PyObject *py_params_get_main_configuration(PyObject *self, PyObject *args
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : dict = dictionnaire de module à compléter.                   *
-*                                                                             *
-*  Description : Définit les constantes pour les paramètres.                  *
-*                                                                             *
-*  Retour      : -                                                            *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-static bool py_params_define_constants(PyObject *dict)
-{
-    bool result;                            /* Bilan à retourner           */
-
-    result = true;
-
-    result &= PyModDict_AddStringMacro(dict, MPK_LAST_PROJECT);
-    result &= PyModDict_AddStringMacro(dict, MPK_ELLIPSIS_HEADER);
-    result &= PyModDict_AddStringMacro(dict, MPK_ELLIPSIS_TAB);
-    result &= PyModDict_AddStringMacro(dict, MPK_KEYBINDINGS_EDIT);
-    result &= PyModDict_AddStringMacro(dict, MPK_AUTO_SAVE);
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
 *  Paramètres  : -                                                            *
 *                                                                             *
 *  Description : Définit une extension du module 'core' à compléter.          *
@@ -127,7 +96,6 @@ bool populate_core_module_with_params(void)
 {
     bool result;                            /* Bilan à retourner           */
     PyObject *module;                       /* Module à recompléter        */
-    PyObject *dict;                         /* Dictionnaire dudit module   */
 
     static PyMethodDef py_params_methods[] = {
         PARAMS_GET_MAIN_CONFIGURATION_METHOD,
@@ -139,10 +107,7 @@ bool populate_core_module_with_params(void)
     result = register_python_module_methods(module, py_params_methods);
 
     if (result)
-    {
-        dict = PyModule_GetDict(module);
-        result = py_params_define_constants(dict);
-    }
+        define_core_params_constants(module);
 
     return result;
 
diff --git a/plugins/pychrysalide/gtkext/graph/Makefile.am b/plugins/pychrysalide/gtkext/graph/Makefile.am
index c30c07c..3b43ffd 100644
--- a/plugins/pychrysalide/gtkext/graph/Makefile.am
+++ b/plugins/pychrysalide/gtkext/graph/Makefile.am
@@ -2,6 +2,7 @@
 noinst_LTLIBRARIES = libpychrysagtkextgraph.la
 
 libpychrysagtkextgraph_la_SOURCES =		\
+	constants.h constants.c				\
 	cluster.h cluster.c					\
 	edge.h edge.c						\
 	module.h module.c
diff --git a/plugins/pychrysalide/gtkext/graph/constants.c b/plugins/pychrysalide/gtkext/graph/constants.c
new file mode 100644
index 0000000..9545354
--- /dev/null
+++ b/plugins/pychrysalide/gtkext/graph/constants.c
@@ -0,0 +1,75 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * constants.c - ajout des constantes de base pour les extensions à la GLib
+ *
+ * Copyright (C) 2020 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include "constants.h"
+
+
+#include <i18n.h>
+#include <gtkext/graph/edge.h>
+
+
+#include "../../helpers.h"
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : type = type dont le dictionnaire est à compléter.            *
+*                                                                             *
+*  Description : Définit les constantes relatives aux liens de graphiques.    *
+*                                                                             *
+*  Retour      : true en cas de succès de l'opération, false sinon.           *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool define_graph_edge_constants(PyTypeObject *type)
+{
+    bool result;                            /* Bilan à retourner           */
+    PyObject *values;                       /* Groupe de valeurs à établir */
+
+    values = PyDict_New();
+
+    result = add_const_to_group(values, "DEFAULT", EGC_DEFAULT);
+    if (result) result = add_const_to_group(values, "GREEN", EGC_GREEN);
+    if (result) result = add_const_to_group(values, "RED", EGC_RED);
+    if (result) result = add_const_to_group(values, "BLUE", EGC_BLUE);
+    if (result) result = add_const_to_group(values, "DASHED_GRAY", EGC_DASHED_GRAY);
+    if (result) result = add_const_to_group(values, "COUNT", EGC_COUNT);
+
+    if (!result)
+    {
+        Py_DECREF(values);
+        goto exit;
+    }
+
+    result = attach_constants_group_to_type(type, false, "EdgeColor", values,
+                                            "Rendering color for graphical edges.");
+
+ exit:
+
+    return result;
+
+}
diff --git a/plugins/pychrysalide/gtkext/graph/constants.h b/plugins/pychrysalide/gtkext/graph/constants.h
new file mode 100644
index 0000000..3c76602
--- /dev/null
+++ b/plugins/pychrysalide/gtkext/graph/constants.h
@@ -0,0 +1,39 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * constants.h - prototypes pour l'ajout des constantes de base pour les graphiques
+ *
+ * Copyright (C) 2020 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _PLUGINS_PYCHRYSALIDE_GTKEXT_GRAPH_CONSTANTS_H
+#define _PLUGINS_PYCHRYSALIDE_GTKEXT_GRAPH_CONSTANTS_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Définit les constantes relatives aux liens de graphiques. */
+bool define_graph_edge_constants(PyTypeObject *);
+
+
+
+#endif  /* _PLUGINS_PYCHRYSALIDE_GTKEXT_GRAPH_CONSTANTS_H */
diff --git a/plugins/pychrysalide/gtkext/graph/edge.c b/plugins/pychrysalide/gtkext/graph/edge.c
index 60d5bb7..ce20ce9 100644
--- a/plugins/pychrysalide/gtkext/graph/edge.c
+++ b/plugins/pychrysalide/gtkext/graph/edge.c
@@ -33,6 +33,7 @@
 #include <plugins/dt.h>
 
 
+#include "constants.h"
 #include "../../access.h"
 #include "../../helpers.h"
 
@@ -47,9 +48,6 @@ static PyObject *py_graph_edge_get_color(PyObject *, void *);
 /* Fournit l'ensemble des points constituant un lien graphique. */
 static PyObject *py_graph_edge_get_points(PyObject *, void *);
 
-/* Définit les constantes pour les liens graphiques. */
-static bool py_graph_edge_define_constants(PyTypeObject *);
-
 
 
 /******************************************************************************
@@ -184,36 +182,6 @@ static PyObject *py_graph_edge_get_points(PyObject *self, void *closure)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
-*                                                                             *
-*  Description : Définit les constantes pour les liens graphiques.            *
-*                                                                             *
-*  Retour      : true en cas de succès de l'opération, false sinon.           *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-static bool py_graph_edge_define_constants(PyTypeObject *obj_type)
-{
-    bool result;                            /* Bilan à retourner           */
-
-    result = true;
-
-    if (result) result = PyDict_AddULongMacro(obj_type, EGC_DEFAULT);
-    if (result) result = PyDict_AddULongMacro(obj_type, EGC_GREEN);
-    if (result) result = PyDict_AddULongMacro(obj_type, EGC_RED);
-    if (result) result = PyDict_AddULongMacro(obj_type, EGC_BLUE);
-    if (result) result = PyDict_AddULongMacro(obj_type, EGC_DASHED_GRAY);
-    if (result) result = PyDict_AddULongMacro(obj_type, EGC_COUNT);
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
 *  Paramètres  : -                                                            *
 *                                                                             *
 *  Description : Fournit un accès à une définition de type à diffuser.        *
@@ -302,7 +270,7 @@ bool ensure_python_graph_edge_is_registered(void)
         if (!register_class_for_pygobject(dict, G_TYPE_GRAPH_EDGE, type, &PyGObject_Type))
             return false;
 
-        if (!py_graph_edge_define_constants(type))
+        if (!define_graph_edge_constants(type))
             return false;
 
     }
-- 
cgit v0.11.2-87-g4458