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 /*****************************************************************************/
44 /*****************************************************************************/
46 /*****************************************************************************/
50 void Write8 (FILE* F, unsigned Val)
51 /* Write an 8 bit value to the file */
53 if (putc (Val, F) == EOF) {
54 Error ("Write error (disk full?)");
60 void Write16 (FILE* F, unsigned Val)
61 /* Write a 16 bit value to the file */
63 Write8 (F, (unsigned char) Val);
64 Write8 (F, (unsigned char) (Val >> 8));
69 void Write24 (FILE* F, unsigned long Val)
70 /* Write a 24 bit value to the file */
72 Write8 (F, (unsigned char) Val);
73 Write8 (F, (unsigned char) (Val >> 8));
74 Write8 (F, (unsigned char) (Val >> 16));
79 void Write32 (FILE* F, unsigned long Val)
80 /* Write a 32 bit value to the file */
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));
90 void WriteVal (FILE* F, unsigned long Val, unsigned Size)
91 /* Write a value of the given size to the output file */
112 Internal ("WriteVal: Invalid size: %u", Size);
119 void WriteStr (FILE* F, const char* S)
120 /* Write a string to the file */
122 unsigned Len = strlen (S);
124 Internal ("String too long");
126 Write8 (F, (unsigned char) Len);
127 WriteData (F, S, Len);
132 void WriteData (FILE* F, const void* Data, unsigned Size)
133 /* Write data to the file */
135 if (fwrite (Data, 1, Size, F) != Size) {
136 Error ("Write error (disk full?)");
142 void WriteMult (FILE* F, unsigned char Val, unsigned long Count)
143 /* Write one byte several times to the file */
152 unsigned Read8 (FILE* F)
153 /* Read an 8 bit value from the file */
157 Error ("Read error (file corrupt?)");
164 unsigned Read16 (FILE* F)
165 /* Read a 16 bit value from the file */
167 unsigned Lo = Read8 (F);
168 unsigned Hi = Read8 (F);
169 return (Hi << 8) | Lo;
174 unsigned long Read24 (FILE* F)
175 /* Read a 24 bit value from the file */
177 unsigned long Lo = Read16 (F);
178 unsigned long Hi = Read8 (F);
179 return (Hi << 16) | Lo;
184 unsigned long Read32 (FILE* F)
185 /* Read a 32 bit value from the file */
187 unsigned long Lo = Read16 (F);
188 unsigned long Hi = Read16 (F);
189 return (Hi << 16) | Lo;
194 long Read32Signed (FILE* F)
195 /* Read a 32 bit value from the file. Sign extend the value. */
197 /* Read a 32 bit value */
198 unsigned long V = Read32 (F);
200 /* Sign extend the value */
201 if (V & 0x80000000UL) {
206 /* Return it as a long */
212 char* ReadStr (FILE* F, char* Str)
213 /* Read a string from the file. Str must hold 256 chars at max */
215 /* Read the length byte */
216 unsigned Len = Read8 (F);
218 /* Read the string itself */
219 ReadData (F, Str, Len);
221 /* Terminate the string and return it */
228 char* ReadMallocedStr (FILE* F)
229 /* Read a string from the file into a malloced area */
231 /* Read the length byte */
232 unsigned Len = Read8 (F);
234 /* Allocate memory */
235 char* Str = Xmalloc (Len + 1);
237 /* Read the string itself */
238 ReadData (F, Str, Len);
240 /* Terminate the string and return it */
247 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
248 /* Read a file position from the file */
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);
259 void* ReadData (FILE* F, void* Data, unsigned Size)
260 /* Read data from the file */
262 if (fread (Data, 1, Size, F) != Size) {
263 Error ("Read error (file corrupt?)");