From 76ba833da455be3e184006a60e25077e65ed607f Mon Sep 17 00:00:00 2001 From: Meep-Eep Date: Wed, 12 Mar 2008 22:38:13 +0000 Subject: [PATCH] Added endian-aware integer read functions to uio git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2952 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/ChangeLog | 1 + sc2/src/msvc++/UrQuanMasters.dsp | 4 + sc2/src/sc2code/libs/uio/getint.h | 141 ++++++++++++++++++++++++++++++ sc2/src/sc2code/libs/uio/io.h | 1 + 4 files changed, 147 insertions(+) create mode 100644 sc2/src/sc2code/libs/uio/getint.h diff --git a/sc2/ChangeLog b/sc2/ChangeLog index fa6b9d287..dac83790d 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.7: +- Added endian-aware integer read functions to uio - SvdB - Introduce the concept of an "InputContext" - SvdB - Don't use alloca() in uio. - SvdB - Replace PlayerOne/PlayerTwo by PlayerControls[0]/PlayerControls[1] - SvdB diff --git a/sc2/src/msvc++/UrQuanMasters.dsp b/sc2/src/msvc++/UrQuanMasters.dsp index 43d4c8cfd..d993a7a6a 100644 --- a/sc2/src/msvc++/UrQuanMasters.dsp +++ b/sc2/src/msvc++/UrQuanMasters.dsp @@ -994,6 +994,10 @@ SOURCE=..\sc2code\libs\task\tasklib.c # Begin Group "uio" # PROP Default_Filter "" +# Begin Source File + +SOURCE=..\sc2code\libs\uio\getint.h +# End Source File # Begin Group "stdio" # PROP Default_Filter "" diff --git a/sc2/src/sc2code/libs/uio/getint.h b/sc2/src/sc2code/libs/uio/getint.h new file mode 100644 index 000000000..5759f0e98 --- /dev/null +++ b/sc2/src/sc2code/libs/uio/getint.h @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2007 Serge van den Boom + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * Nota bene: later versions of the GNU General Public License do not apply + * to this program. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef _GETINT_H +#define _GETINT_H + +/* All these functions return true on success, or false on failure */ + +#include "types.h" +#include "uioport.h" + +static inline uio_bool +uio_getU8(uio_Stream *stream, uio_uint8 *result) { + int val = uio_getc(stream); + if (val == EOF) + return false; + + *result = (uio_uint8) val; + return true; +} + +static inline uio_bool +uio_getS8(uio_Stream *stream, uio_sint8 *result) { + int val = uio_getc(stream); + if (val == EOF) + return false; + + *result = (uio_sint8) val; + return true; +} + +static inline uio_bool +uio_getU16LE(uio_Stream *stream, uio_uint16 *result) { + uio_uint8 buf[2]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (buf[1] << 8) | buf[0]; + return true; +} + +static inline uio_bool +uio_getU16BE(uio_Stream *stream, uio_uint16 *result) { + uio_uint8 buf[2]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (buf[0] << 8) | buf[1]; + return true; +} + +static inline uio_bool +uio_getS16LE(uio_Stream *stream, uio_sint16 *result) { + uio_uint8 buf[2]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (uio_sint16) ((buf[1] << 8) | buf[0]); + return true; +} + +static inline uio_bool +uio_getS16BE(uio_Stream *stream, uio_sint16 *result) { + uio_uint8 buf[2]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (uio_sint16) ((buf[0] << 8) | buf[1]); + return true; +} + +static inline uio_bool +uio_getU32LE(uio_Stream *stream, uio_uint32 *result) { + uio_uint8 buf[4]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; + return true; +} + +static inline uio_bool +uio_getU32BE(uio_Stream *stream, uio_uint32 *result) { + uio_uint8 buf[4]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + return true; +} + +static inline uio_bool +uio_getS32LE(uio_Stream *stream, uio_sint32 *result) { + uio_uint8 buf[4]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (uio_sint32) ((buf[3] << 24) | (buf[2] << 16) | + (buf[1] << 8) | buf[0]); + return true; +} + +static inline uio_bool +uio_getS32BE(uio_Stream *stream, uio_sint32 *result) { + uio_uint8 buf[4]; + + if (uio_fread(buf, sizeof buf, 1, stream) != 1) + return false; + + *result = (uio_sint32) ((buf[0] << 24) | (buf[1] << 16) | + (buf[2] << 8) | buf[3]); + return true; +} + + +#endif /* _GETINT_H */ + diff --git a/sc2/src/sc2code/libs/uio/io.h b/sc2/src/sc2code/libs/uio/io.h index 9269071d8..5bd8ea186 100644 --- a/sc2/src/sc2code/libs/uio/io.h +++ b/sc2/src/sc2code/libs/uio/io.h @@ -41,6 +41,7 @@ typedef enum { #include "mount.h" #include "mounttree.h" #include "uiostream.h" +#include "getint.h" #include "debug.h" struct uio_AutoMount {