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