From b6d7af8d3ee8b4e7bd4cd45f1d94cfabd7fa14fd Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Fri, 26 Jul 2002 19:59:44 +0000 Subject: [PATCH] portable file i/o utils --- {misc => include}/fileutil.h | 29 ++++-------- misc/Makefile.am | 6 +-- misc/fileutil.c | 88 ++++++++++++++---------------------- 3 files changed, 47 insertions(+), 76 deletions(-) rename {misc => include}/fileutil.h (67%) diff --git a/misc/fileutil.h b/include/fileutil.h similarity index 67% rename from misc/fileutil.h rename to include/fileutil.h index da8110cc..20e1c6a4 100644 --- a/misc/fileutil.h +++ b/include/fileutil.h @@ -7,28 +7,25 @@ IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS -AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. +AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. */ - + #ifndef _FILEUTIL_ #define _FILEUTIL_ -#include "pstypes.h" -#include "cfile.h" +#include + +#include "pstypes.h" #include "fix.h" - + extern int filelength(int fd); // routines which read basic data types -extern byte read_byte(CFILE *fp); -extern short read_short(CFILE *fp); -extern int read_int(CFILE *fp); -extern fix read_fix(CFILE *fp); - -// versions which swap bytes -#define read_byte_swap(fp) read_byte(fp) -extern short read_short_swap(CFILE *fp); +extern byte read_byte(FILE *fp); +extern short read_short(FILE *fp); +extern int read_int(FILE *fp); +extern fix read_fix(FILE *fp); // routines which write basic data types extern int write_byte(FILE *fp, byte b); @@ -36,10 +33,4 @@ extern int write_short(FILE *fp, short s); extern int write_int(FILE *fp, int i); extern int write_fix(FILE *fp, fix f); -// routines which write swapped bytes -#define write_byte_swap(fp, b) write_byte(fp, b) -extern int write_short_swap(FILE *fp, short s); -extern int write_int_swap(FILE *fp, int i); -extern int write_fix_swap(FILE *fp, fix f); - #endif diff --git a/misc/Makefile.am b/misc/Makefile.am index b9194f11..2664aa39 100644 --- a/misc/Makefile.am +++ b/misc/Makefile.am @@ -3,7 +3,7 @@ INCLUDES = -I $(top_srcdir)/include libmisc_a_SOURCES = \ args.c d_delay.c error.c strio.c \ -d_io.c hash.c strutil.c +d_io.c hash.c strutil.c fileutil.c # checker.c @@ -11,5 +11,5 @@ d_io.c hash.c strutil.c # gcc -c -g $< -o $@ $(INCLUDES) $(CFLAGS) -I/usr/lib/checker/i686-pc-linux-gnu/include -I.. -I. EXTRA_DIST = \ -dos_disk.h fileutil.h parsarg.h \ -byteswap.c errtest.c fileutil.c parsarg.c parstest.c +dos_disk.h parsarg.h \ +byteswap.c errtest.c parsarg.c parstest.c diff --git a/misc/fileutil.c b/misc/fileutil.c index e6c39c75..e92e8199 100644 --- a/misc/fileutil.c +++ b/misc/fileutil.c @@ -7,106 +7,86 @@ IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS -AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. +AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. */ - + #include #include +#include +#include -#include "pstypes.h" #include "fileutil.h" -#include "cfile.h" #include "fix.h" #include "byteswap.h" +#include "error.h" int filelength(int fd) { int cur_pos, end_pos; - + cur_pos = lseek(fd, 0, SEEK_CUR); lseek(fd, 0, SEEK_END); end_pos = lseek(fd, 0, SEEK_CUR); lseek(fd, cur_pos, SEEK_SET); return end_pos; } -#if 0 -byte read_byte(CFILE *fp) + +byte file_read_byte(FILE *fp) { byte b; - - cfread(&b, sizeof(byte), 1, fp); + + if (fread(&b, 1, 1, fp) != 1) + Error("Error reading byte in file_read_byte()"); return b; } -short read_short(CFILE *fp) +short file_read_short(FILE *fp) { short s; - - cfread(&s, sizeof(short), 1, fp); - return (s); -} -short read_short_swap(CFILE *fp) -{ - short s; - - cfread(&s, sizeof(short), 1, fp); - return swapshort(s); + if (fread(&s, 2, 1, fp) != 1) + Error("Error reading short in file_read_short()"); + return INTEL_SHORT(s); } -int read_int(CFILE *fp) +int file_read_int(FILE *fp) { uint i; - - cfread(&i, sizeof(uint), 1, fp); - return i; -} -fix read_fix(CFILE *fp) -{ - fix f; - - cfread(&f, sizeof(fix), 1, fp); - return f; -} - -int write_byte(FILE *fp, byte b) -{ - return (fwrite(&b, sizeof(byte), 1, fp)); + if (fread(&i, 4, 1, fp) != 1) + Error("Error reading int in file_read_int()"); + return INTEL_INT(i); } -int write_short(FILE *fp, short s) +fix file_read_fix(FILE *fp) { - return (fwrite(&s, sizeof(short), 1, fp)); -} + fix f; -int write_short_swap(FILE *fp, short s) -{ - s = swapshort(s); - return (fwrite(&s, sizeof(short), 1, fp)); + if (fread(&f, 4, 1, fp) != 1) + Error("Error reading fix in file_read_fix()"); + return INTEL_INT(f); } -int write_int(FILE *fp, int i) +int file_write_byte(FILE *fp, byte b) { - return (fwrite(&i,sizeof(int), 1, fp)); + return (fwrite(&b, 1, 1, fp)); } -int write_int_swap(FILE *fp, int i) +int file_write_short(FILE *fp, short s) { - i = swapint(i); - return (fwrite(&i,sizeof(int), 1, fp)); + s = INTEL_SHORT(s); + return (fwrite(&s, 2, 1, fp)); } -int write_fix(FILE *fp, fix f) +int file_write_int(FILE *fp, int i) { - return (fwrite(&f, sizeof(fix), 1, fp)); + i = INTEL_INT(i); + return (fwrite(&i, 4, 1, fp)); } int write_fix_swap(FILE *fp, fix f) { - f = (fix)swapint((int)f); - return (fwrite(&f, sizeof(fix), 1, fp)); + f = (fix)INTEL_INT((int)f); + return (fwrite(&f, 4, 1, fp)); } - -#endif -- 2.39.2