view pack.h @ 161:81d64d4a5473

Complete rewrite of pack.c: o more concise, o for buffer, available in autoallocation and static,
author David Demelier <markand@malikania.fr>
date Mon, 03 Sep 2012 21:21:33 +0200
parents c7ad101ed4a1
children 3e63d94b495f
line wrap: on
line source

/*
 * pack.h -- endian dependant binary file reader
 *
 * Copyright (c) 2011, 2012, David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _PACK_H_
#define _PACK_H_

#include <stdarg.h>

#define PACK_LE		1234
#define PACK_BE		4321

/*
 * Endian detection based on SDL_endian.h
 */

#undef PACK_HOST_BYTEORDER

#if defined(__hppa__) ||						\
    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) ||	\
    (defined(__MIPS__) && defined(__MISPEB__)) ||			\
    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) ||	\
    defined(__sparc__)
#  define PACK_HOST_BYTEORDER	PACK_BE
#else
#  define PACK_HOST_BYTEORDER	PACK_LE
#endif

#define pack_swap16(x)							\
	((((x) >> 8) & 0x00FFL) | (((x) << 8) & 0xFF00L))

#define pack_swap32(x) ((((x) >> 24) & 0x000000FFL)			\
	| (((x) >> 8)  & 0x0000FF00L)					\
	| (((x) << 8)  & 0x00FF0000L)					\
	| (((x) << 24) & 0xFF000000L))

#define pack_swap64(x) (((long)					\
	((int) pack_swap32((int) (((x) << 32) >> 32))) << 32)	\
	    | (int) pack_swap32((int) ((x) >> 32)))

long	pack_write(short, const char *, const char *, ...);
long	pack_vwrite(short, const char *, const char *, va_list);
long	pack_fwrite(short, FILE *, const char *, ...);
long	pack_vfwrite(short, FILE *, const char *, va_list);
long	pack_swrite(short, char *, size_t, const char *, ...);
long	pack_vswrite(short, char *, size_t, const char *, va_list);
long	pack_aswrite(short, char **, const char *, ...);
long	pack_vaswrite(short, char **, const char *, va_list);

long	pack_read(short, const char *, const char *, ...);
long	pack_vread(short, const char *, const char *, va_list);
long	pack_fread(short, FILE *, const char *, ...);
long	pack_vfread(short, FILE *, const char *, va_list);
long	pack_sread(short, const char *, size_t, const char *, ...);
long	pack_vsread(short, const char *, size_t, const char *, va_list);

#endif /* _PACK_H_ */