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