changeset 2421:1361e3883dd8

Make things more flexible.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Jan 2020 14:08:48 +0200
parents 621d071530ce
children ba8b44cd313b
files tools/data2inc.c
diffstat 1 files changed, 106 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/tools/data2inc.c	Tue Jan 14 04:54:30 2020 +0200
+++ b/tools/data2inc.c	Thu Jan 16 14:08:48 2020 +0200
@@ -26,10 +26,13 @@
 
     char *defDataType;
 
-    int  (*writeHeader) (FILE *fh, const char *name);
-    int  (*writeDecl) (FILE *fh, const size_t len, const char *name);
-    int  (*writeData) (FILE *fh, const Uint8 *buf, const size_t len);
-    int  (*writeFooter) (FILE *fh, const size_t len, const char *name);
+    int  (*initContext) (void **pctx);
+    int  (*closeContext) (void *pctx);
+
+    int  (*writeHeader) (FILE *fh, void *ctx, const char *name);
+    int  (*writeDecl) (FILE *fh, void *ctx, const size_t len, const char *name);
+    int  (*writeData) (FILE *fh, void *ctx, const Uint8 *buf, const size_t len);
+    int  (*writeFooter) (FILE *fh, void *ctx, const size_t len, const char *name);
 } DMOutputFormat;
 
 
@@ -204,11 +207,34 @@
 }
 
 
+static void dmPrintIndentation(FILE *fh)
+{
+    if (optIndentation < 0)
+    {
+        for (int i = 0; i < -optIndentation; i++)
+            fputs("\t", fh);
+    }
+    else
+    if (optIndentation > 0)
+    {
+        for (int i = 0; i < optIndentation; i++)
+            fputs(" ", fh);
+    }
+}
+
+
+static int dmHandleError(const int res)
+{
+    return res >= 0 ? DMERR_OK : dmGetErrno();
+}
+
+
 /* Assembler include data output functions
  */
-int writeHeader_ASM(FILE *fh, const char *name)
+int writeHeader_ASM(FILE *fh, void *ctx, const char *name)
 {
     int res = 0;
+    (void) ctx;
 
     if (name)
         res = fprintf(fh, "; '%s'", name);
@@ -219,13 +245,14 @@
         res = fprintf(fh, " by %s v%s\n",
         dmProgName, dmProgVersion);
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    return dmHandleError(res);
 }
 
 
-int writeDecl_ASM(FILE *fh, const size_t len, const char *name)
+int writeDecl_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
 {
     int res = 0;
+    (void) ctx;
 
     if (optExtraData)
         res = fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
@@ -233,13 +260,17 @@
     if (res >= 0)
         res = fprintf(fh, "%s:\n", name);
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    return dmHandleError(res);
 }
 
 
-int writeData_ASM(FILE *fh, const Uint8 * buf, const size_t len)
+int writeData_ASM(FILE *fh, void *ctx, const Uint8 * buf, const size_t len)
 {
-    int res = fprintf(fh, "%s ", optDataType);
+    int res;
+    (void) ctx;
+
+    dmPrintIndentation(fh);
+    res = fprintf(fh, "%s ", optDataType);
 
     for (size_t i = 0; res >= 0 && i < len; i++)
     {
@@ -262,29 +293,34 @@
             res = fprintf(fh, optFormatting ? ", " : ",");
     }
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    if (res >= 0)
+        res = fprintf(fh, "\n");
+
+    return dmHandleError(res);
 }
 
 
-int writeFooter_ASM(FILE *fh, const size_t len, const char *name)
+int writeFooter_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
 {
     int res;
     (void) len;
+    (void) ctx;
 
     if (optExtraData)
         res = fprintf(fh, "%s_end: \n", name);
     else
         res = fprintf(fh, "\n");
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    return dmHandleError(res);
 }
 
 
 /* ANSI-C include data output functions
  */
-int writeHeader_C(FILE *fh, const char *name)
+int writeHeader_C(FILE *fh, void *ctx, const char *name)
 {
     int res;
+    (void) ctx;
 
     if (name)
         res = fprintf(fh, "/* '%s' generated", name);
@@ -295,13 +331,14 @@
         res = fprintf(fh, " by %s v%s\n" " */\n",
         dmProgName, dmProgVersion);
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    return dmHandleError(res);
 }
 
 
-int writeDecl_C(FILE *fh, const size_t len, const char *name)
+int writeDecl_C(FILE *fh, void *ctx, const size_t len, const char *name)
 {
     int res;
+    (void) ctx;
 
     res = fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
         optDataType, name, len);
@@ -310,13 +347,16 @@
         res = printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
         optDataType, name, len);
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    return dmHandleError(res);
 }
 
 
-int writeData_C(FILE *fh, const Uint8 *buf, const size_t len)
+int writeData_C(FILE *fh, void *ctx, const Uint8 *buf, const size_t len)
 {
     int res = 0;
+    (void) ctx;
+
+    dmPrintIndentation(fh);
 
     for (size_t i = 0; res >= 0 && i < len; i++)
     {
@@ -339,18 +379,22 @@
             res = fprintf(fh, optFormatting ? ", " : ",");
     }
 
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    if (res >= 0)
+        res = fprintf(fh, "\n");
+
+    return dmHandleError(res);
 }
 
 
-int writeFooter_C(FILE *fh, const size_t len, const char *name)
+int writeFooter_C(FILE *fh, void *ctx, const size_t len, const char *name)
 {
     int res;
     (void) len;
     (void) name;
+    (void) ctx;
 
     res = fprintf(fh, "};\n");
-    return res >= 0 ? DMERR_OK : dmGetErrno();
+    return dmHandleError(res);
 }
 
 
@@ -365,6 +409,9 @@
         { "s", "asm", NULL },
         ".byte",
 
+        NULL,
+        NULL,
+
         writeHeader_ASM,
         writeDecl_ASM,
         writeData_ASM,
@@ -375,8 +422,10 @@
         "c",
         "ANSI C array",
         { "c", "h", "cc", "cpp", "hpp", "c++", NULL },
+        "unsigned char",
 
-        "unsigned char",
+        NULL,
+        NULL,
 
         writeHeader_C,
         writeDecl_C,
@@ -424,6 +473,7 @@
 {
     FILE *inFile = NULL, *outFile = NULL;
     Uint8 *dataBuf = NULL;
+    void *ctx = NULL;
     size_t dataSize;
     off_t totalSize;
     int res;
@@ -496,9 +546,19 @@
     // Get sourcefile size
     totalSize = dmGetFileSize(inFile);
 
+    // Call context init
+    if (setFormat->initContext != NULL &&
+        (res = setFormat->initContext(&ctx)) != DMERR_OK)
+    {
+        dmErrorMsg("Error initializing format %s (%s) context: %s\n",
+            setFormat->name, setFormat->desc,
+            dmErrorStr(res));
+        goto exit;
+    }
+
     // Output header
     if (!optQuiet &&
-        (res = setFormat->writeHeader(outFile, optOutFilename)) != DMERR_OK)
+        (res = setFormat->writeHeader(outFile, ctx, optOutFilename)) != DMERR_OK)
     {
         dmErrorMsg("Error writing output header: %s\n",
             dmErrorStr(res));
@@ -509,39 +569,30 @@
         fprintf(outFile, "%s\n", optAddLine);
 
     // Output declaration
-    setFormat->writeDecl(outFile, totalSize, optObjName);
+    if (setFormat->writeDecl != NULL &&
+        (res = setFormat->writeDecl(outFile, ctx, totalSize, optObjName)) != DMERR_OK)
+    {
+        dmErrorMsg("Error writing output declaration: %s\n",
+            dmErrorStr(res));
+        goto exit;
+    }
 
     // Output data
     while (!feof(inFile))
     {
         res = fread(dataBuf, 1, dataSize, inFile);
-        if (res > 0)
+        if (res > 0 &&
+            (res = setFormat->writeData(outFile, ctx, dataBuf, res)) != DMERR_OK)
         {
-            if (optIndentation < 0)
-            {
-                for (int i = 0; i < -optIndentation; i++)
-                    fputs("\t", outFile);
-            }
-            else
-            if (optIndentation > 0)
-            {
-                for (int i = 0; i < optIndentation; i++)
-                    fputs(" ", outFile);
-            }
-
-            if ((res = setFormat->writeData(outFile, dataBuf, res)) != DMERR_OK)
-            {
-                dmErrorMsg("Error writing output data: %s\n",
-                    dmErrorStr(res));
-                goto exit;
-            }
-
-            fprintf(outFile, "\n");
+            dmErrorMsg("Error writing output data: %s\n",
+                dmErrorStr(res));
+            goto exit;
         }
     }
 
     // Output footer
-    if ((res = setFormat->writeFooter(outFile, totalSize, optObjName)) != DMERR_OK)
+    if (setFormat->writeFooter != NULL &&
+        (res = setFormat->writeFooter(outFile, ctx, totalSize, optObjName)) != DMERR_OK)
     {
         dmErrorMsg("Error writing output footer: %s\n",
             dmErrorStr(res));
@@ -558,5 +609,14 @@
 
     dmFree(dataBuf);
 
+    if (setFormat != NULL &&
+        setFormat->closeContext != NULL &&
+        (res = setFormat->closeContext(ctx)) != DMERR_OK)
+    {
+        dmErrorMsg("Error closing format %s (%s) context: %s\n",
+            setFormat->name, setFormat->desc,
+            dmErrorStr(res));
+    }
+
     return 0;
 }