changeset 567:b2b461829c61

Move utility function dmReadDataFile() to dmfile module instead.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 04 Jan 2013 16:48:15 +0200
parents d400e32b62d9
children c331726f4352
files dmfile.c dmfile.h lib64gfx.c lib64gfx.h
diffstat 4 files changed, 74 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/dmfile.c	Sun Dec 30 13:18:08 2012 +0200
+++ b/dmfile.c	Fri Jan 04 16:48:15 2013 +0200
@@ -59,3 +59,74 @@
 #include "dmfiletmpl.h"
 
 #undef DM_DEFINE_FFUNC
+
+
+#define BUF_SIZE_INITIAL   (16*1024)
+#define BUF_SIZE_GROW      (4*1024)
+
+int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
+{
+    FILE *f;
+    int res = DMERR_OK;
+    Uint8 *dataBuf = NULL;
+    size_t readSize, dataSize, dataRead, dataPos;
+    
+    if (inFile != NULL)
+        f = inFile;
+    else
+    if (filename != NULL)
+    {
+        if ((f = fopen(filename, "rb")) == NULL)
+        {
+            dmError("Could not open '%s' for reading.\n", filename);
+            return DMERR_FOPEN;
+        }
+    }
+    else
+    {
+        dmError("NULL filename and stream pointers.\n");
+        return DMERR_NULLPTR;
+    }
+
+    // Allocate initial data buffer
+    readSize = dataSize = BUF_SIZE_INITIAL;
+    if ((dataBuf = dmMalloc(dataSize)) == NULL)
+    {
+        dmError("Error allocating memory for data, %d bytes.\n", dataSize);
+        res = DMERR_MALLOC;
+        goto error;
+    }
+
+    dataPos = 0;
+    dataRead = 0;
+
+    while (!feof(f) && !ferror(f))
+    {
+        size_t read = fread(dataBuf + dataPos, 1, readSize, f);
+        dataPos += read;
+        dataRead += read;
+
+        if (dataRead >= dataSize)
+        {
+            readSize = BUF_SIZE_GROW;
+            dataSize += BUF_SIZE_GROW;
+            if ((dataBuf = dmRealloc(dataBuf, dataSize)) == NULL)
+            {
+                dmError("Error reallocating memory for data, %d bytes.\n", dataSize);
+                res = DMERR_MALLOC;
+                goto error;
+            }
+        }
+        else
+            break;
+    }
+
+    *pbufSize = dataRead;
+    *pbuf = dataBuf;
+
+error:
+    if (f != inFile)
+        fclose(f);
+    
+    return res;
+}
--- a/dmfile.h	Sun Dec 30 13:18:08 2012 +0200
+++ b/dmfile.h	Fri Jan 04 16:48:15 2013 +0200
@@ -31,6 +31,9 @@
 BOOL    dm_fwrite_byte(FILE *f, const Uint8);
 
 
+int     dmReadDataFile(FILE *inFile, const char *filename, Uint8 **buf, size_t *size);
+
+
 #ifdef __cplusplus
 }
 #endif
--- a/lib64gfx.c	Sun Dec 30 13:18:08 2012 +0200
+++ b/lib64gfx.c	Fri Jan 04 16:48:15 2013 +0200
@@ -821,74 +821,6 @@
 }
 
 
-int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
-{
-    FILE *f;
-    int res = DMERR_OK;
-    Uint8 *dataBuf = NULL;
-    size_t readSize, dataSize, dataRead, dataPos;
-    
-    if (inFile != NULL)
-        f = inFile;
-    else
-    if (filename != NULL)
-    {
-        if ((f = fopen(filename, "rb")) == NULL)
-        {
-            dmError("Could not open '%s' for reading.\n", filename);
-            return DMERR_FOPEN;
-        }
-    }
-    else
-    {
-        dmError("NULL filename and stream pointers.\n");
-        return DMERR_NULLPTR;
-    }
-
-    // Allocate initial data buffer
-    readSize = dataSize = BUF_SIZE_INITIAL;
-    if ((dataBuf = dmMalloc(dataSize)) == NULL)
-    {
-        dmError("Error allocating memory for data, %d bytes.\n", dataSize);
-        res = DMERR_MALLOC;
-        goto error;
-    }
-
-    dataPos = 0;
-    dataRead = 0;
-
-    while (!feof(f) && !ferror(f))
-    {
-        size_t read = fread(dataBuf + dataPos, 1, readSize, f);
-        dataPos += read;
-        dataRead += read;
-
-        if (dataRead >= dataSize)
-        {
-            readSize = BUF_SIZE_GROW;
-            dataSize += BUF_SIZE_GROW;
-            if ((dataBuf = dmRealloc(dataBuf, dataSize)) == NULL)
-            {
-                dmError("Error reallocating memory for data, %d bytes.\n", dataSize);
-                res = DMERR_MALLOC;
-                goto error;
-            }
-        }
-        else
-            break;
-    }
-
-    *pbufSize = dataRead;
-    *pbuf = dataBuf;
-
-error:
-    if (f != inFile)
-        fclose(f);
-    
-    return res;
-}
-
-
 int dmC64DecodeBMP(DMC64Image *img, const Uint8 *buf, const size_t len,
     const size_t probeOffs, const size_t loadOffs,
     const DMC64ImageFormat **fmt, const DMC64ImageFormat *forced)
--- a/lib64gfx.h	Sun Dec 30 13:18:08 2012 +0200
+++ b/lib64gfx.h	Fri Jan 04 16:48:15 2013 +0200
@@ -186,8 +186,6 @@
 int       dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **fmt);
 int       dmC64DecodeBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const size_t probeOffs, const size_t loadOffs, const DMC64ImageFormat **fmt, const DMC64ImageFormat *forced);
 
-int       dmReadDataFile(FILE *inFile, const char *filename, Uint8 **buf, size_t *size);
-
 
 #ifdef __cplusplus
 }