From 1ab3654e4b742816759b0879113b7bd46000999a Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 30 Oct 2018 20:42:55 +0100
Subject: Simplified file creations in the code generator.

---
 tools/d2c/coder.c | 96 +++++++++++++++----------------------------------------
 1 file changed, 26 insertions(+), 70 deletions(-)

diff --git a/tools/d2c/coder.c b/tools/d2c/coder.c
index cd64d88..9066869 100644
--- a/tools/d2c/coder.c
+++ b/tools/d2c/coder.c
@@ -79,13 +79,10 @@ static unsigned int get_bit_width_for_encoding_spec(const rented_coder *, const
 
 
 /* Ouvre un fichier principal en écriture pour y placer du code. */
-static int open_instr_header_file(const rented_coder *, const output_info *, const char *, bool *);
-
-/* Ouvre un fichier principal en écriture pour y placer du code. */
-static int open_instr_code_file(const rented_coder *, const output_info *, const char *, bool *);
+static int open_code_file(const rented_coder *, const char *, const char *, bool *);
 
 /* Ouvre un fichier global en écriture pour y placer du code. */
-static int open_global_header_file(const rented_coder *, const output_info *, const char *, bool *);
+static int open_header_file(const char *, const char *, const char *, bool *);
 
 /* Imprime dans un flux donné un commentaire de propriété. */
 static void write_header_file_license(int, const output_info *, const char *, const char *);
@@ -574,53 +571,7 @@ char *build_coder_main_identifier(const rented_coder *coder, const output_info *
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : coder  = gestion par la machine en remplacement de l'humain. *
-*                info   = précisions quant à la génération.                   *
-*                prefix = type d'encodage à répercuter sur le nom de fichier. *
-*                new    = dit si l'opération a abouti à une création. [OUT]   *
-*                                                                             *
-*  Description : Ouvre un fichier principal en écriture pour y placer du code.*
-*                                                                             *
-*  Retour      : Descripteur du fichier ouvert ou -1 en cas d'échec.          *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-static int open_instr_header_file(const rented_coder *coder, const output_info *info, const char *prefix, bool *new)
-{
-    int result;                             /* Descripteur à retourner     */
-    char *pathname;                         /* Chemin d'accès à constituer */
-    int ret;                                /* Test d'existence du fichier */
-    int flags;                              /* Mode d'accès au fichier     */
-
-    if (prefix != NULL)
-        asprintf(&pathname, "%s%s_opcodes.h", info->directory, prefix);
-    else
-        asprintf(&pathname, "%sopcodes.h", info->directory);
-
-    ret = access(pathname, F_OK);
-
-    *new = (ret != 0);
-
-    if (*new)
-        flags = O_WRONLY | O_CREAT;
-    else
-        flags = O_WRONLY | O_APPEND;
-
-    result = open(pathname, flags, 0644);
-    if (result == -1) perror("open()");
-
-    free(pathname);
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : coder  = gestion par la machine en remplacement de l'humain. *
-*                info   = précisions quant à la génération.                   *
+*                path   = chemin du répertoire de sortie.                     *
 *                prefix = type d'encodage à répercuter sur le nom de fichier. *
 *                new    = dit si l'opération a abouti à une création. [OUT]   *
 *                                                                             *
@@ -632,7 +583,7 @@ static int open_instr_header_file(const rented_coder *coder, const output_info *
 *                                                                             *
 ******************************************************************************/
 
-static int open_instr_code_file(const rented_coder *coder, const output_info *info, const char *prefix, bool *new)
+static int open_code_file(const rented_coder *coder, const char *path, const char *prefix, bool *new)
 {
     int result;                             /* Descripteur à retourner     */
     char *group;                            /* Regroupement des similarités*/
@@ -649,9 +600,9 @@ static int open_instr_code_file(const rented_coder *coder, const output_info *in
         *sep = '\0';
 
     if (prefix != NULL)
-        asprintf(&pathname, "%s%s_%s.c", info->directory, prefix, group);
+        asprintf(&pathname, "%s%s_%s.c", path, prefix, group);
     else
-        asprintf(&pathname, "%s%s.c", info->directory, group);
+        asprintf(&pathname, "%s%s.c", path, group);
 
     free(group);
 
@@ -676,10 +627,10 @@ static int open_instr_code_file(const rented_coder *coder, const output_info *in
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : coder = gestion par la machine en remplacement de l'humain.  *
-*                info  = précisions quant à la génération.                    *
-*                name  = nom du fichier ciblé par l'opération.                *
-*                new   = indique si l'opération a créé le fichier ciblé. [OUT]*
+*  Paramètres  : path   = chemin du répertoire de sortie.                     *
+*                prefix = type d'encodage à répercuter sur le nom de fichier. *
+*                name   = nom du fichier ciblé par l'opération.               *
+*                new    = indique si l'opération a créé le fichier. [OUT]     *
 *                                                                             *
 *  Description : Ouvre un fichier global en écriture pour y placer du code.   *
 *                                                                             *
@@ -689,14 +640,19 @@ static int open_instr_code_file(const rented_coder *coder, const output_info *in
 *                                                                             *
 ******************************************************************************/
 
-static int open_global_header_file(const rented_coder *coder, const output_info *info, const char *name, bool *new)
+static int open_header_file(const char *path, const char *prefix, const char *name, bool *new)
 {
     int result;                             /* Descripteur à retourner     */
     char *pathname;                         /* Chemin d'accès à constituer */
     int ret;                                /* Test d'existence du fichier */
     int flags;                              /* Mode d'accès au fichier     */
 
-    asprintf(&pathname, "%s%s.h", info->directory, name);
+    asprintf(&pathname, "%s%s.h", path, name);
+
+    if (prefix != NULL)
+        asprintf(&pathname, "%s%s_%s.h", path, prefix, name);
+    else
+        asprintf(&pathname, "%s%s.h", path, name);
 
     ret = access(pathname, F_OK);
 
@@ -932,7 +888,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
             if (!has_encoding_spec_prefix(coder->specs[j], enc_name->src))
                 continue;
 
-            header_fd = open_instr_header_file(coder, info, enc_name->dest, &header_new);
+            header_fd = open_header_file(info->directory, enc_name->dest, "opcodes", &header_new);
             if (header_fd == -1)
             {
                 result = false;
@@ -954,7 +910,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
 
             }
 
-            code_fd = open_instr_code_file(coder, info, enc_name->dest, &code_new);
+            code_fd = open_code_file(coder, info->directory, enc_name->dest, &code_new);
             if (code_fd == -1)
             {
                 result = false;
@@ -1009,7 +965,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
             if (!has_encoding_spec_prefix(coder->specs[j], enc_name->src))
                 continue;
 
-            header_fd = open_instr_header_file(coder, info, enc_name->dest, &header_new);
+            header_fd = open_header_file(info->directory, enc_name->dest, "opcodes", &header_new);
             if (header_fd == -1)
             {
                 result = false;
@@ -1018,7 +974,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
 
             assert(!header_new);
 
-            code_fd = open_instr_code_file(coder, info, enc_name->dest, &code_new);
+            code_fd = open_code_file(coder, info->directory, enc_name->dest, &code_new);
             if (code_fd == -1)
             {
                 result = false;
@@ -1557,7 +1513,7 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info)
 
     /* Ouverture de la destination */
 
-    fd = open_global_header_file(coder, info, "identifiers", &created);
+    fd = open_header_file(info->directory, NULL, "identifiers", &created);
     if (fd == -1) goto oci_exit;
 
     if (created)
@@ -1702,7 +1658,7 @@ bool output_coder_sub_identifier(const rented_coder *coder, const output_info *i
 
     /* Ouverture de la destination */
 
-    fd = open_global_header_file(coder, info, "subidentifiers", &created);
+    fd = open_header_file(info->directory, NULL, "subidentifiers", &created);
     if (fd == -1) goto ocsi_exit;
 
     if (created)
@@ -1867,7 +1823,7 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info)
 
     /* Ouverture de la destination */
 
-    fd = open_global_header_file(coder, info, "keywords", &created);
+    fd = open_header_file(info->directory, NULL, "keywords", &created);
     if (fd == -1) goto ock_exit;
 
     if (created)
@@ -2061,7 +2017,7 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info)
 
     /* Ouverture de la destination */
 
-    fd = open_global_header_file(coder, info, "hooks", &created);
+    fd = open_header_file(info->directory, NULL, "hooks", &created);
     if (fd == -1) goto och_exit;
 
     if (created)
@@ -2223,7 +2179,7 @@ bool output_coder_description(const rented_coder *coder, const output_info *info
 
     /* Ouverture de la destination */
 
-    fd = open_global_header_file(coder, info, "descriptions", &created);
+    fd = open_header_file(info->directory, NULL, "descriptions", &created);
     if (fd == -1) goto ocd_exit;
 
     if (created)
-- 
cgit v0.11.2-87-g4458