]> git.sur5r.net Git - cc65/blob - src/ld65/fileio.c
Fixed a bug
[cc65] / src / ld65 / fileio.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 fileio.c                                  */
4 /*                                                                           */
5 /*                       File I/O for the ld65 linker                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 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 /* common */
39 #include "xmalloc.h"
40
41 /* ld65 */
42 #include "error.h"
43 #include "fileio.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 void Write8 (FILE* F, unsigned Val)
54 /* Write an 8 bit value to the file */
55 {
56     if (putc (Val, F) == EOF) {
57         Error ("Write error (disk full?)");
58     }
59 }
60
61
62
63 void Write16 (FILE* F, unsigned Val)
64 /* Write a 16 bit value to the file */
65 {
66     Write8 (F, (unsigned char) Val);
67     Write8 (F, (unsigned char) (Val >> 8));
68 }
69
70
71
72 void Write24 (FILE* F, unsigned long Val)
73 /* Write a 24 bit value to the file */
74 {
75     Write8 (F, (unsigned char) Val);
76     Write8 (F, (unsigned char) (Val >> 8));
77     Write8 (F, (unsigned char) (Val >> 16));
78 }
79
80
81
82 void Write32 (FILE* F, unsigned long Val)
83 /* Write a 32 bit value to the file */
84 {
85     Write8 (F, (unsigned char) Val);
86     Write8 (F, (unsigned char) (Val >> 8));
87     Write8 (F, (unsigned char) (Val >> 16));
88     Write8 (F, (unsigned char) (Val >> 24));
89 }
90
91
92
93 void WriteVal (FILE* F, unsigned long Val, unsigned Size)
94 /* Write a value of the given size to the output file */
95 {
96     switch (Size) {
97
98         case 1:
99             Write8 (F, Val);
100             break;
101
102         case 2:
103             Write16 (F, Val);
104             break;
105
106         case 3:
107             Write24 (F, Val);
108             break;
109
110         case 4:
111             Write32 (F, Val);
112             break;
113
114         default:
115             Internal ("WriteVal: Invalid size: %u", Size);
116
117     }
118 }
119
120
121
122 void WriteVar (FILE* F, unsigned long V)
123 /* Write a variable sized value to the file in special encoding */
124 {
125     /* We will write the value to the file in 7 bit chunks. If the 8th bit
126      * is clear, we're done, if it is set, another chunk follows. This will
127      * allow us to encode smaller values with less bytes, at the expense of
128      * needing 5 bytes if a 32 bit value is written to file.
129      */
130     do {
131         unsigned char C = (V & 0x7F);
132         V >>= 7;
133         if (V) {
134             C |= 0x80;
135         }
136         Write8 (F, C);
137     } while (V != 0);
138 }
139
140
141
142 void WriteStr (FILE* F, const char* S)
143 /* Write a string to the file */
144 {
145     unsigned Len = strlen (S);
146     WriteVar (F, Len);
147     WriteData (F, S, Len);
148 }
149
150
151
152 void WriteData (FILE* F, const void* Data, unsigned Size)
153 /* Write data to the file */
154 {
155     if (fwrite (Data, 1, Size, F) != Size) {
156         Error ("Write error (disk full?)");
157     }
158 }
159
160
161
162 void WriteMult (FILE* F, unsigned char Val, unsigned long Count)
163 /* Write one byte several times to the file */
164 {
165     while (Count--) {
166         Write8 (F, Val);
167     }
168 }
169
170
171
172 unsigned Read8 (FILE* F)
173 /* Read an 8 bit value from the file */
174 {
175     int C = getc (F);
176     if (C == EOF) {
177         Error ("Read error (file corrupt?)");
178     }
179     return C;
180 }
181
182
183
184 unsigned Read16 (FILE* F)
185 /* Read a 16 bit value from the file */
186 {
187     unsigned Lo = Read8 (F);
188     unsigned Hi = Read8 (F);
189     return (Hi << 8) | Lo;
190 }
191
192
193
194 unsigned long Read24 (FILE* F)
195 /* Read a 24 bit value from the file */
196 {
197     unsigned long Lo = Read16 (F);
198     unsigned long Hi = Read8 (F);
199     return (Hi << 16) | Lo;
200 }
201
202
203
204 unsigned long Read32 (FILE* F)
205 /* Read a 32 bit value from the file */
206 {
207     unsigned long Lo = Read16 (F);
208     unsigned long Hi = Read16 (F);
209     return (Hi << 16) | Lo;
210 }
211
212
213
214 long Read32Signed (FILE* F)
215 /* Read a 32 bit value from the file. Sign extend the value. */
216 {
217     /* Read a 32 bit value */
218     unsigned long V = Read32 (F);
219
220     /* Sign extend the value */
221     if (V & 0x80000000UL) {
222         /* Signed value */
223         V |= ~0xFFFFFFFFUL;
224     }
225
226     /* Return it as a long */
227     return (long) V;
228 }
229
230
231
232 unsigned long ReadVar (FILE* F)
233 /* Read a variable size value from the file */
234 {
235     /* The value was written to the file in 7 bit chunks LSB first. If there
236      * are more bytes, bit 8 is set, otherwise it is clear.
237      */
238     unsigned char C;
239     unsigned long V = 0;
240     unsigned Shift = 0;
241     do {
242         /* Read one byte */
243         C = Read8 (F);
244         /* Encode it into the target value */
245         V |= ((unsigned long)(C & 0x7F)) << Shift;
246         /* Next value */
247         Shift += 7;
248     } while (C & 0x80);
249
250     /* Return the value read */
251     return V;
252 }
253
254
255
256 char* ReadStr (FILE* F)
257 /* Read a string from the file (the memory will be malloc'ed) */
258 {
259     /* Read the length */
260     unsigned Len = ReadVar (F);
261
262     /* Allocate memory and read the string itself */
263     char* S = xmalloc (Len + 1);
264     ReadData (F, S, Len);
265
266     /* Terminate the string and return it */
267     S [Len] = '\0';
268     return S;
269 }
270
271
272
273 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
274 /* Read a file position from the file */
275 {
276     /* Read the data fields */
277     Pos->Line = ReadVar (F);
278     Pos->Col  = ReadVar (F);
279     Pos->Name = ReadVar (F);
280     return Pos;
281 }
282
283
284
285 void* ReadData (FILE* F, void* Data, unsigned Size)
286 /* Read data from the file */
287 {     
288     /* Explicitly allow reading zero bytes */
289     if (Size > 0) {
290         if (fread (Data, 1, Size, F) != Size) {
291             Error ("Read error (file corrupt?)");
292         }
293     }
294     return Data;
295 }
296
297
298
299