]> icculus.org git repositories - btb/d2x.git/blob - misc/fileutil.c
comments/formatting/dist problems
[btb/d2x.git] / misc / fileutil.c
1 /* $Id: fileutil.c,v 1.7 2003-04-12 00:11:46 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * utilities for file manipulation
18  *
19  * Old Log:
20  * Revision 1.6  1995/10/30  11:09:51  allender
21  * use FILE, not CFILE on the write* routines
22  *
23  * Revision 1.5  1995/05/11  13:00:34  allender
24  * added write functions which swap bytes
25  *
26  * Revision 1.4  1995/05/04  20:10:38  allender
27  * remove include for fcntl
28  *
29  * Revision 1.3  1995/04/26  10:14:39  allender
30  * added byteswap header file
31  *
32  * Revision 1.2  1995/04/26  10:13:21  allender
33  *
34  * Revision 1.1  1995/03/30  15:02:34  allender
35  * Initial revision
36  *
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include <conf.h>
41 #endif
42
43 #include <stdio.h>
44
45 #include "fileutil.h"
46 #include "fix.h"
47 #include "byteswap.h"
48 #include "error.h"
49
50 byte file_read_byte(FILE *fp)
51 {
52         byte b;
53
54         if (fread(&b, 1, 1, fp) != 1)
55                 Error("Error reading byte in file_read_byte()");
56         return b;
57 }
58
59 short file_read_short(FILE *fp)
60 {
61         short s;
62
63         if (fread(&s, 2, 1, fp) != 1)
64                 Error("Error reading short in file_read_short()");
65         return INTEL_SHORT(s);
66 }
67
68 int file_read_int(FILE *fp)
69 {
70         uint i;
71
72         if (fread(&i, 4, 1, fp) != 1)
73                 Error("Error reading int in file_read_int()");
74         return INTEL_INT(i);
75 }
76
77 fix file_read_fix(FILE *fp)
78 {
79         fix f;
80
81         if (fread(&f, 4, 1, fp) != 1)
82                 Error("Error reading fix in file_read_fix()");
83         return INTEL_INT(f);
84 }
85
86 void file_read_string(char *s, FILE *f)
87 {
88         if (feof(f))
89                 *s = 0;
90         else
91                 do
92                         *s = fgetc(f);
93                 while (!feof(f) && *s++!=0);
94 }
95
96 int file_write_byte(byte b, FILE *fp)
97 {
98         return (fwrite(&b, 1, 1, fp));
99 }
100
101 int file_write_short(short s, FILE *fp)
102 {
103         s = INTEL_SHORT(s);
104         return (fwrite(&s, 2, 1, fp));
105 }
106
107 int file_write_int(int i, FILE *fp)
108 {
109         i = INTEL_INT(i);
110         return (fwrite(&i, 4, 1, fp));
111 }
112
113 int file_write_fix(fix f, FILE *fp)
114 {
115         f = (fix)INTEL_INT((int)f);
116         return (fwrite(&f, 4, 1, fp));
117 }
118
119 void file_write_string(char *s, FILE *f)
120 {
121         do
122                 fputc(*s,f);
123         while (*s++!=0);
124 }