Mercurial > hg > th-libs
view th_endian.c @ 132:11fa2bf90251
Network API changes due to preparation for fixes in proxy support.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 04 Jul 2014 02:18:26 +0300 |
parents | 34d58b0f2d52 |
children | 4094fcfd4783 |
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_read_str(FILE *f, uint8_t *s, size_t l) { return (fread(s, sizeof(uint8_t), l, f) == l); } BOOL th_write_str(FILE * f, uint8_t * s, size_t l) { return (fwrite(s, sizeof(uint8_t), l, f) == l); } BOOL th_read_byte(FILE *f, uint8_t *v) { return (fread(v, sizeof(uint8_t), 1, f) == 1); } BOOL th_write_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_read_ ## 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_write_ ## 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