1 /*****************************************************************************/
5 /* File I/O for the ld65 linker */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
38 #include "../common/xmalloc.h"
45 /*****************************************************************************/
47 /*****************************************************************************/
51 void Write8 (FILE* F, unsigned Val)
52 /* Write an 8 bit value to the file */
54 if (putc (Val, F) == EOF) {
55 Error ("Write error (disk full?)");
61 void Write16 (FILE* F, unsigned Val)
62 /* Write a 16 bit value to the file */
64 Write8 (F, (unsigned char) Val);
65 Write8 (F, (unsigned char) (Val >> 8));
70 void Write24 (FILE* F, unsigned long Val)
71 /* Write a 24 bit value to the file */
73 Write8 (F, (unsigned char) Val);
74 Write8 (F, (unsigned char) (Val >> 8));
75 Write8 (F, (unsigned char) (Val >> 16));
80 void Write32 (FILE* F, unsigned long Val)
81 /* Write a 32 bit value to the file */
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));
91 void WriteVal (FILE* F, unsigned long Val, unsigned Size)
92 /* Write a value of the given size to the output file */
113 Internal ("WriteVal: Invalid size: %u", Size);
120 void WriteStr (FILE* F, const char* S)
121 /* Write a string to the file */
123 unsigned Len = strlen (S);
125 Internal ("String too long");
127 Write8 (F, (unsigned char) Len);
128 WriteData (F, S, Len);
133 void WriteData (FILE* F, const void* Data, unsigned Size)
134 /* Write data to the file */
136 if (fwrite (Data, 1, Size, F) != Size) {
137 Error ("Write error (disk full?)");
143 void WriteMult (FILE* F, unsigned char Val, unsigned long Count)
144 /* Write one byte several times to the file */
153 unsigned Read8 (FILE* F)
154 /* Read an 8 bit value from the file */
158 Error ("Read error (file corrupt?)");
165 unsigned Read16 (FILE* F)
166 /* Read a 16 bit value from the file */
168 unsigned Lo = Read8 (F);
169 unsigned Hi = Read8 (F);
170 return (Hi << 8) | Lo;
175 unsigned long Read24 (FILE* F)
176 /* Read a 24 bit value from the file */
178 unsigned long Lo = Read16 (F);
179 unsigned long Hi = Read8 (F);
180 return (Hi << 16) | Lo;
185 unsigned long Read32 (FILE* F)
186 /* Read a 32 bit value from the file */
188 unsigned long Lo = Read16 (F);
189 unsigned long Hi = Read16 (F);
190 return (Hi << 16) | Lo;
195 long Read32Signed (FILE* F)
196 /* Read a 32 bit value from the file. Sign extend the value. */
198 /* Read a 32 bit value */
199 unsigned long V = Read32 (F);
201 /* Sign extend the value */
202 if (V & 0x80000000UL) {
207 /* Return it as a long */
213 char* ReadStr (FILE* F, char* Str)
214 /* Read a string from the file. Str must hold 256 chars at max */
216 /* Read the length byte */
217 unsigned Len = Read8 (F);
219 /* Read the string itself */
220 ReadData (F, Str, Len);
222 /* Terminate the string and return it */
229 char* ReadMallocedStr (FILE* F)
230 /* Read a string from the file into a malloced area */
232 /* Read the length byte */
233 unsigned Len = Read8 (F);
235 /* Allocate memory */
236 char* Str = xmalloc (Len + 1);
238 /* Read the string itself */
239 ReadData (F, Str, Len);
241 /* Terminate the string and return it */
248 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
249 /* Read a file position from the file */
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);
260 void* ReadData (FILE* F, void* Data, unsigned Size)
261 /* Read data from the file */
263 if (fread (Data, 1, Size, F) != Size) {
264 Error ("Read error (file corrupt?)");