]> git.sur5r.net Git - cc65/blob - src/ld65/fileio.c
3876c12ea4009d5fbb2b05a172a8598960ea3eaa
[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 #include "error.h"
39 #include "mem.h"
40 #include "fileio.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Code                                    */
46 /*****************************************************************************/
47
48
49
50 void Write8 (FILE* F, unsigned Val)
51 /* Write an 8 bit value to the file */
52 {
53     if (putc (Val, F) == EOF) {
54         Error ("Write error (disk full?)");
55     }
56 }
57
58
59
60 void Write16 (FILE* F, unsigned Val)
61 /* Write a 16 bit value to the file */
62 {
63     Write8 (F, (unsigned char) Val);
64     Write8 (F, (unsigned char) (Val >> 8));
65 }
66
67
68
69 void Write24 (FILE* F, unsigned long Val)
70 /* Write a 24 bit value to the file */
71 {
72     Write8 (F, (unsigned char) Val);
73     Write8 (F, (unsigned char) (Val >> 8));
74     Write8 (F, (unsigned char) (Val >> 16));
75 }
76
77
78
79 void Write32 (FILE* F, unsigned long Val)
80 /* Write a 32 bit value to the file */
81 {
82     Write8 (F, (unsigned char) Val);
83     Write8 (F, (unsigned char) (Val >> 8));
84     Write8 (F, (unsigned char) (Val >> 16));
85     Write8 (F, (unsigned char) (Val >> 24));
86 }
87
88
89
90 void WriteVal (FILE* F, unsigned long Val, unsigned Size)
91 /* Write a value of the given size to the output file */
92 {
93     switch (Size) {
94
95         case 1:
96             Write8 (F, Val);
97             break;
98
99         case 2:
100             Write16 (F, Val);
101             break;
102
103         case 3:
104             Write24 (F, Val);
105             break;
106
107         case 4:
108             Write32 (F, Val);
109             break;
110
111         default:
112             Internal ("WriteVal: Invalid size: %u", Size);
113
114     }
115 }
116
117
118
119 void WriteStr (FILE* F, const char* S)
120 /* Write a string to the file */
121 {
122     unsigned Len = strlen (S);
123     if (Len > 255) {
124         Internal ("String too long");
125     }
126     Write8 (F, (unsigned char) Len);
127     WriteData (F, S, Len);
128 }
129
130
131
132 void WriteData (FILE* F, const void* Data, unsigned Size)
133 /* Write data to the file */
134 {
135     if (fwrite (Data, 1, Size, F) != Size) {
136         Error ("Write error (disk full?)");
137     }
138 }
139
140
141
142 void WriteMult (FILE* F, unsigned char Val, unsigned long Count)
143 /* Write one byte several times to the file */
144 {
145     while (Count--) {
146         Write8 (F, Val);
147     }
148 }
149
150
151
152 unsigned Read8 (FILE* F)
153 /* Read an 8 bit value from the file */
154 {
155     int C = getc (F);
156     if (C == EOF) {
157         Error ("Read error (file corrupt?)");
158     }
159     return C;
160 }
161
162
163
164 unsigned Read16 (FILE* F)
165 /* Read a 16 bit value from the file */
166 {
167     unsigned Lo = Read8 (F);
168     unsigned Hi = Read8 (F);
169     return (Hi << 8) | Lo;
170 }
171
172
173
174 unsigned long Read24 (FILE* F)
175 /* Read a 24 bit value from the file */
176 {
177     unsigned long Lo = Read16 (F);
178     unsigned long Hi = Read8 (F);
179     return (Hi << 16) | Lo;
180 }
181
182
183
184 unsigned long Read32 (FILE* F)
185 /* Read a 32 bit value from the file */
186 {
187     unsigned long Lo = Read16 (F);
188     unsigned long Hi = Read16 (F);
189     return (Hi << 16) | Lo;
190 }
191
192
193
194 long Read32Signed (FILE* F)
195 /* Read a 32 bit value from the file. Sign extend the value. */
196 {
197     /* Read a 32 bit value */
198     unsigned long V = Read32 (F);
199
200     /* Sign extend the value */
201     if (V & 0x80000000UL) {
202         /* Signed value */
203         V |= ~0xFFFFFFFFUL;
204     }
205
206     /* Return it as a long */
207     return (long) V;
208 }
209
210
211
212 char* ReadStr (FILE* F, char* Str)
213 /* Read a string from the file. Str must hold 256 chars at max */
214 {
215     /* Read the length byte */
216     unsigned Len = Read8 (F);
217
218     /* Read the string itself */
219     ReadData (F, Str, Len);
220
221     /* Terminate the string and return it */
222     Str [Len] = '\0';
223     return Str;
224 }
225
226
227
228 char* ReadMallocedStr (FILE* F)
229 /* Read a string from the file into a malloced area */
230 {
231     /* Read the length byte */
232     unsigned Len = Read8 (F);
233
234     /* Allocate memory */
235     char* Str = Xmalloc (Len + 1);
236
237     /* Read the string itself */
238     ReadData (F, Str, Len);
239
240     /* Terminate the string and return it */
241     Str [Len] = '\0';
242     return Str;
243 }
244
245
246
247 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
248 /* Read a file position from the file */
249 {
250     /* The line number is encoded as 24 bit value to save some space */
251     Pos->Line = Read24 (F);
252     Pos->Col  = Read8 (F);
253     Pos->Name = Read8 (F);
254     return Pos;
255 }
256
257
258
259 void* ReadData (FILE* F, void* Data, unsigned Size)
260 /* Read data from the file */
261 {
262     if (fread (Data, 1, Size, F) != Size) {
263         Error ("Read error (file corrupt?)");
264     }
265     return Data;
266 }
267
268
269