]> git.sur5r.net Git - cc65/blob - src/ar65/fileio.c
Move stuff into the common directory
[cc65] / src / ar65 / fileio.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 fileio.c                                  */
4 /*                                                                           */
5 /*                      File I/O for the ar65 archiver                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 #include "../common/xmalloc.h"
39
40 #include "error.h"
41 #include "fileio.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   Code                                    */
47 /*****************************************************************************/
48
49
50
51 void Write8 (FILE* F, unsigned char Val)
52 /* Write an 8 bit value to the file */
53 {
54     if (putc (Val, F) == EOF) {
55         Error ("Write error (disk full?)");
56     }
57 }
58
59
60
61 void Write16 (FILE* F, unsigned Val)
62 /* Write a 16 bit value to the file */
63 {
64     Write8 (F, (unsigned char) Val);
65     Write8 (F, (unsigned char) (Val >> 8));
66 }
67
68
69
70 void Write32 (FILE* F, unsigned long Val)
71 /* Write a 32 bit value to the file */
72 {
73     Write8 (F, (unsigned char) Val);
74     Write8 (F, (unsigned char) (Val >> 8));
75     Write8 (F, (unsigned char) (Val >> 16));
76     Write8 (F, (unsigned char) (Val >> 24));
77 }
78
79
80
81 void WriteStr (FILE* F, const char* S)
82 /* Write a string to the file */
83 {
84     unsigned Len = strlen (S);
85     if (Len > 255) {
86         Internal ("String too long");
87     }
88     Write8 (F, (unsigned char) Len);
89     WriteData (F, S, Len);
90 }
91
92
93
94 void WriteData (FILE* F, const void* Data, unsigned Size)
95 /* Write data to the file */
96 {
97     if (fwrite (Data, 1, Size, F) != Size) {
98         Error ("Write error (disk full?)");
99     }
100 }
101
102
103
104 unsigned Read8 (FILE* F)
105 /* Read an 8 bit value from the file */
106 {
107     int C = getc (F);
108     if (C == EOF) {
109         Error ("Read error (file corrupt?)");
110     }
111     return C;
112 }
113
114
115
116 unsigned Read16 (FILE* F)
117 /* Read a 16 bit value from the file */
118 {
119     unsigned Lo = Read8 (F);
120     unsigned Hi = Read8 (F);
121     return (Hi << 8) | Lo;
122 }
123
124
125
126 unsigned long Read32 (FILE* F)
127 /* Read a 32 bit value from the file */
128 {
129     unsigned long Lo = Read16 (F);
130     unsigned long Hi = Read16 (F);
131     return (Hi << 16) | Lo;
132 }
133
134
135
136 char* ReadStr (FILE* F)
137 /* Read a string from the file (the memory will be malloc'ed) */
138 {
139     /* Read the length byte */
140     unsigned Len = Read8 (F);
141
142     /* Allocate memory and read the string itself */
143     char* S = xmalloc (Len + 1);
144     ReadData (F, S, Len);
145
146     /* Terminate the string and return it */
147     S [Len] = '\0';
148     return S;
149 }
150
151
152
153 void* ReadData (FILE* F, void* Data, unsigned Size)
154 /* Read data from the file */
155 {
156     if (fread (Data, 1, Size, F) != Size) {
157         Error ("Read error (file corrupt?)");
158     }
159     return Data;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211