]> icculus.org git repositories - btb/d2x.git/blob - misc/fileutil.c
oops
[btb/d2x.git] / misc / fileutil.c
1 /* $ Id: $ */
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 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include "fileutil.h"
25 #include "fix.h"
26 #include "byteswap.h"
27 #include "error.h"
28
29 int filelength(int fd)
30 {
31         int cur_pos, end_pos;
32
33         cur_pos = lseek(fd, 0, SEEK_CUR);
34         lseek(fd, 0, SEEK_END);
35         end_pos = lseek(fd, 0, SEEK_CUR);
36         lseek(fd, cur_pos, SEEK_SET);
37         return end_pos;
38 }
39
40 byte file_read_byte(FILE *fp)
41 {
42         byte b;
43
44         if (fread(&b, 1, 1, fp) != 1)
45                 Error("Error reading byte in file_read_byte()");
46         return b;
47 }
48
49 short file_read_short(FILE *fp)
50 {
51         short s;
52
53         if (fread(&s, 2, 1, fp) != 1)
54                 Error("Error reading short in file_read_short()");
55         return INTEL_SHORT(s);
56 }
57
58 int file_read_int(FILE *fp)
59 {
60         uint i;
61
62         if (fread(&i, 4, 1, fp) != 1)
63                 Error("Error reading int in file_read_int()");
64         return INTEL_INT(i);
65 }
66
67 fix file_read_fix(FILE *fp)
68 {
69         fix f;
70
71         if (fread(&f, 4, 1, fp) != 1)
72                 Error("Error reading fix in file_read_fix()");
73         return INTEL_INT(f);
74 }
75
76 int file_write_byte(FILE *fp, byte b)
77 {
78         return (fwrite(&b, 1, 1, fp));
79 }
80
81 int file_write_short(FILE *fp, short s)
82 {
83         s = INTEL_SHORT(s);
84         return (fwrite(&s, 2, 1, fp));
85 }
86
87 int file_write_int(FILE *fp, int i)
88 {
89         i = INTEL_INT(i);
90         return (fwrite(&i, 4, 1, fp));
91 }
92
93 int write_fix_swap(FILE *fp, fix f)
94 {
95         f = (fix)INTEL_INT((int)f);
96         return (fwrite(&f, 4, 1, fp));
97 }