view dmfile.c @ 510:43ea59887c69

Start work on making C64 formats encoding possible by changing DMDecodeOps to DMEncDecOps and adding fields and op enums for custom encode functions, renaming, etc. Split generic op sanity checking into a separate function in preparation for its use in generic encoding function.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Nov 2012 15:06:01 +0200
parents c1f6def0c1da
children b2b461829c61
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);       \
    if (fwrite(&result, sizeof( xtype ), 1, f) != 1)  \
        return FALSE;                                 \
    return TRUE;                                      \
}

#include "dmfiletmpl.h"

#undef DM_DEFINE_FFUNC