view th_endian.c @ 134:4094fcfd4783

Rename functions.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Sep 2014 20:01:13 +0300
parents 34d58b0f2d52
children 51eec969b07a
line wrap: on
line source

/*
 * Endianess handling
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2012 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#include "th_endian.h"


BOOL th_fread_str(FILE *f, uint8_t *s, size_t l)
{
    return (fread(s, sizeof(uint8_t), l, f) == l);
}


BOOL th_fwrite_str(FILE * f, uint8_t * s, size_t l)
{
    return (fwrite(s, sizeof(uint8_t), l, f) == l);
}


BOOL th_fread_byte(FILE *f, uint8_t *v)
{
    return (fread(v, sizeof(uint8_t), 1, f) == 1);
}


BOOL th_fwrite_byte(FILE * f, uint8_t v)
{
    return (fputc(v, f) != EOF);
}


/* File routines for endian-dependant data
 */
#define TH_DEFINE_FUNC(xname, xtype, xmacro)        \
BOOL th_fread_ ## xname (FILE *f, xtype *v) {        \
    xtype result;                                   \
    if (fread(&result, sizeof( xtype ), 1, f) != 1) \
        return FALSE;                               \
    *v = TH_ ## xmacro ## _TO_NATIVE (result);      \
    return TRUE;                                    \
}                                                   \
                                                    \
BOOL th_fwrite_ ## xname (FILE *f, xtype v) {        \
    xtype result = TH_NATIVE_TO_ ## xmacro (v);     \
    if (fwrite(&result, sizeof( xtype ), 1, f) != 1) \
        return FALSE;                               \
    return TRUE;                                    \
}

TH_DEFINE_FUNC(le16, uint16_t, LE16)
TH_DEFINE_FUNC(le32, uint32_t, LE32)

TH_DEFINE_FUNC(be16, uint16_t, BE16)
TH_DEFINE_FUNC(be32, uint32_t, BE32)

#ifdef TH_HAVE_64BIT
TH_DEFINE_FUNC(le64, uint64_t, LE64)
TH_DEFINE_FUNC(be64, uint64_t, BE64)
#endif

#undef TH_DEFINE_FUNC