view src/dmfile.c @ 2576:812b16ee49db

I had been living under apparent false impression that "realfft.c" on which the FFT implementation in DMLIB was basically copied from was released in public domain at some point, but it could very well be that it never was. Correct license is (or seems to be) GNU GPL. Thus I removing the code from DMLIB, and profusely apologize to the author, Philip Van Baren. It was never my intention to distribute code based on his original work under a more liberal license than originally intended.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 Mar 2022 16:32:50 +0200
parents 36edd316184a
children 9807ae37ad69
line wrap: on
line source

/*
 * DMLib
 * -- Standard I/O (stdio) file write/read endianess helpers
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2011 Tecnic Software productions (TNSP)
 */
#include "dmfile.h"


BOOL dm_fread_str(FILE *f, void *buf, const size_t len)
{
    return fread(buf, len, 1, f) == 1;
}


BOOL dm_fread_byte(FILE *f, Uint8 *val)
{
    int tmp = fgetc(f);
    *val = tmp;
    return tmp != EOF;
}


#define DM_DEFINE_FFUNC(xname, xtype, xmacro)         \
BOOL dm_fread_ ## xname (FILE *f, xtype *v) {         \
    xtype result;                                     \
    if (fread(&result, sizeof( xtype ), 1, f) != 1)   \
        return FALSE;                                 \
    *v = DM_ ## xmacro ## _TO_NATIVE (result);        \
    return TRUE;                                      \
}

#include "dmfiletmpl.h"

#undef DM_DEFINE_FFUNC


BOOL dm_fwrite_str(FILE *f, const void *buf, const size_t len)
{
    return fwrite(buf, len, 1, f) == 1;
}


BOOL dm_fwrite_byte(FILE *f, const Uint8 val)
{
    return fputc(val, f) == val;
}


#define DM_DEFINE_FFUNC(xname, xtype, xmacro)         \
BOOL dm_fwrite_ ## xname (FILE *f, const xtype v) {   \
    xtype result = DM_NATIVE_TO_ ## xmacro (v);       \
    return fwrite(&result, sizeof( xtype ), 1, f) == 1;  \
}

#include "dmfiletmpl.h"

#undef DM_DEFINE_FFUNC


int dmWriteDataFile(FILE *fh, const char *filename, const Uint8 *buf, const size_t bufSize)
{
    int res = DMERR_OK;

    if (fh == NULL && filename != NULL &&
        (fh = fopen(filename, "wb")) == NULL)
    {
        res = dmGetErrno();
        return dmError(res,
            "Could not open '%s' for writing: %s.\n",
            filename, dmErrorStr(res));
    }

    if (fh == NULL)
    {
        res = dmError(DMERR_NULLPTR,
            "NULL filename and stream pointers.\n");
        goto error;
    }

    if (fwrite(buf, 1, bufSize, fh) != bufSize)
    {
        res = dmGetErrno();
        dmError(res,
            "Error writing data to '%s': %s\n",
            filename, dmErrorStr(res));
    }

error:
    if (fh != NULL)
        fclose(fh);

    return res;
}


#define BUF_SIZE_INITIAL   (16*1024)
#define BUF_SIZE_GROW      (4*1024)

int dmReadDataFile(FILE *ffh, const char *filename, Uint8 **pbuf, size_t *pbufSize)
{
    FILE *fh = NULL;
    int res = DMERR_OK;
    Uint8 *dataBuf = NULL;
    size_t readSize, dataSize, dataRead, dataPos;

    if (ffh != NULL)
        fh = ffh;
    else
    if (filename != NULL)
    {
        if ((fh = fopen(filename, "rb")) == NULL)
            return DMERR_FOPEN;
    }

    if (fh == NULL)
    {
        return dmError(DMERR_NULLPTR,
            "NULL filename and stream pointers.\n");
    }

    // Allocate initial data buffer
    readSize = dataSize = BUF_SIZE_INITIAL;
    if ((dataBuf = dmMalloc(dataSize)) == NULL)
    {
        res = dmError(DMERR_MALLOC,
            "Error allocating memory for data, %" DM_PRIu_SIZE_T " bytes.\n",
            dataSize);
        goto error;
    }

    // Read the data
    dataPos = dataRead = 0;
    while (!feof(fh) && !ferror(fh))
    {
        size_t read = fread(dataBuf + dataPos, 1, readSize, fh);
        dataPos += read;
        dataRead += read;

        if (dataRead >= dataSize)
        {
            readSize = BUF_SIZE_GROW;
            dataSize += BUF_SIZE_GROW;
            if ((dataBuf = dmRealloc(dataBuf, dataSize)) == NULL)
            {
                res = dmError(DMERR_MALLOC,
                    "Error reallocating memory for data, %" DM_PRIu_SIZE_T " bytes.\n",
                    dataSize);
                goto error;
            }
        }
        else
            break;
    }

    *pbufSize = dataRead;
    *pbuf = dataBuf;

error:
    if (ffh != fh)
        fclose(fh);

    return res;
}