diff src/dmfile.c @ 1474:6b1f41ca300a

Add dmWriteDataFile() helper function.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 01:59:49 +0300
parents c543f5ae92d5
children 1750498bc746
line wrap: on
line diff
--- a/src/dmfile.c	Fri May 11 01:59:37 2018 +0300
+++ b/src/dmfile.c	Fri May 11 01:59:49 2018 +0300
@@ -58,6 +58,42 @@
 #undef DM_DEFINE_FFUNC
 
 
+int dmWriteDataFile(FILE *fh, const char *filename, const Uint8 *buf, const size_t bufSize)
+{
+    int res;
+
+    if (fh == NULL && filename != NULL)
+    {
+        if ((fh = fopen(filename, "wb")) == NULL)
+        {
+            res = dmGetErrno();
+            return dmError(res,
+                "Could not open '%s' for writing: %s.\n",
+                filename, dmErrorStr(res));
+        }
+    }
+
+    if (fh == NULL)
+    {
+        return dmError(DMERR_NULLPTR,
+            "NULL filename and stream pointers.\n");
+    }
+
+    if (fwrite(buf, bufSize, 1, fh) != 1)
+    {
+        res = dmGetErrno();
+        dmError(res,
+            "Error writing data to '%s': %s\n",
+            filename, dmErrorStr(res));
+    }
+
+    if (fh != NULL)
+        fclose(fh);
+
+    return res;
+}
+
+
 #define BUF_SIZE_INITIAL   (16*1024)
 #define BUF_SIZE_GROW      (4*1024)