break the endian neutral file io out from qendian.c so qfcc can remain

independent of libz
This commit is contained in:
Bill Currie 2001-07-09 20:56:05 +00:00
parent 8000c86e35
commit 0242c0352d
3 changed files with 117 additions and 84 deletions

View file

@ -21,7 +21,7 @@ libasm_la_SOURCES = math.S
libQFutil_la_LDFLAGS = -version-info 1:0:0 $(Z_LIBS) $(DL_LIBS)
libQFutil_la_LIBADD = $(ASM)
libQFutil_la_SOURCES = \
checksum.c cmd.c console.c con_print.c crc.c cvar.c hash.c \
checksum.c cmd.c console.c con_print.c crc.c cvar.c fendian.c hash.c \
info.c link.c \
mathlib.c \
mdfour.c msg.c pcx.c plugin.c qargs.c qendian.c qfplist.c quakefs.c \

116
libs/util/fendian.c Normal file
View file

@ -0,0 +1,116 @@
/*
fendian.c
endian neutral file read/write routines
Copyright (C) 2001 Bill Currie
Author: Bill Currie <bill@taniwha.org>
Date: 2001/7/9
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
#include "QF/qendian.h"
#include "QF/vfile.h"
void
WriteFloat (VFile *file, float f)
{
// a float in C is /defined/ to be 32 bits. byte order, can, of course
// still make a mess.
union {
float f;
byte b[4];
} dat;
dat.f = LittleFloat (f);
Qwrite (file, dat.b, sizeof (dat.b));
}
void
WriteByte (VFile *file, int b)
{
byte dat = b & 0xff;
Qwrite (file, &dat, 1);
}
void
WriteShort (VFile *file, unsigned int s)
{
byte dat[2];
dat[0] = s & 0xff;
dat[1] = (s >> 8) & 0xff;
Qwrite (file, dat, sizeof (dat));
}
void
WriteLong (VFile *file, unsigned int l)
{
byte dat[4];
dat[0] = l & 0xff;
dat[1] = (l >> 8) & 0xff;
dat[2] = (l >> 16) & 0xff;
dat[3] = (l >> 24) & 0xff;
Qwrite (file, dat, sizeof (dat));
}
float
ReadFloat (VFile *file)
{
// a float in C is /defined/ to be 32 bits. byte order, can, of course
// still make a mess.
union {
float f;
byte b[4];
} dat;
Qread (file, dat.b, sizeof (dat.b));
return LittleFloat (dat.f);
}
byte
ReadByte (VFile *file)
{
byte dat;
Qread (file, &dat, 1);
return dat;
}
unsigned short
ReadShort (VFile *file)
{
byte dat[2];
Qread (file, dat, sizeof (dat));
return (dat[1] << 8) | dat[0];
}
unsigned long
ReadLong (VFile *file)
{
byte dat[4];
Qread (file, dat, sizeof (dat));
return (dat[3] << 24) | (dat[2] << 16) | (dat[1] << 8) | dat[0];
}

View file

@ -118,86 +118,3 @@ FloatNoSwap (float f)
{
return f;
}
void
WriteFloat (VFile *file, float f)
{
// a float in C is /defined/ to be 32 bits. byte order, can, of course
// still make a mess.
union {
float f;
byte b[4];
} dat;
dat.f = LittleFloat (f);
Qwrite (file, dat.b, sizeof (dat.b));
}
void
WriteByte (VFile *file, int b)
{
byte dat = b & 0xff;
Qwrite (file, &dat, 1);
}
void
WriteShort (VFile *file, unsigned int s)
{
byte dat[2];
dat[0] = s & 0xff;
dat[1] = (s >> 8) & 0xff;
Qwrite (file, dat, sizeof (dat));
}
void
WriteLong (VFile *file, unsigned int l)
{
byte dat[4];
dat[0] = l & 0xff;
dat[1] = (l >> 8) & 0xff;
dat[2] = (l >> 16) & 0xff;
dat[3] = (l >> 24) & 0xff;
Qwrite (file, dat, sizeof (dat));
}
float
ReadFloat (VFile *file)
{
// a float in C is /defined/ to be 32 bits. byte order, can, of course
// still make a mess.
union {
float f;
byte b[4];
} dat;
Qread (file, dat.b, sizeof (dat.b));
return LittleFloat (dat.f);
}
byte
ReadByte (VFile *file)
{
byte dat;
Qread (file, &dat, 1);
return dat;
}
unsigned short
ReadShort (VFile *file)
{
byte dat[2];
Qread (file, dat, sizeof (dat));
return (dat[1] << 8) | dat[0];
}
unsigned long
ReadLong (VFile *file)
{
byte dat[4];
Qread (file, dat, sizeof (dat));
return (dat[3] << 24) | (dat[2] << 16) | (dat[1] << 8) | dat[0];
}