/*****************************************************************************/
/* */
-/* fileio.c */
+/* fileio.c */
/* */
-/* File I/O for the ld65 linker */
+/* File I/O for the ld65 linker */
/* */
/* */
/* */
/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
/* Seek to the given absolute position, fail on errors */
{
if (fseek (F, Pos, SEEK_SET) != 0) {
- Error ("Cannot seek: %s", strerror (errno));
+ Error ("Cannot seek: %s", strerror (errno));
}
}
{
long Pos = ftell (F);
if (Pos < 0) {
- Error ("Error in ftell: %s", strerror (errno));
+ Error ("Error in ftell: %s", strerror (errno));
}
return Pos;
}
/* Write an 8 bit value to the file */
{
if (putc (Val, F) == EOF) {
- Error ("Write error (disk full?)");
+ Error ("Write error (disk full?)");
}
}
{
switch (Size) {
- case 1:
- Write8 (F, Val);
- break;
+ case 1:
+ Write8 (F, Val);
+ break;
- case 2:
- Write16 (F, Val);
- break;
+ case 2:
+ Write16 (F, Val);
+ break;
- case 3:
- Write24 (F, Val);
- break;
+ case 3:
+ Write24 (F, Val);
+ break;
- case 4:
- Write32 (F, Val);
- break;
+ case 4:
+ Write32 (F, Val);
+ break;
- default:
- Internal ("WriteVal: Invalid size: %u", Size);
+ default:
+ Internal ("WriteVal: Invalid size: %u", Size);
}
}
/* Write a variable sized value to the file in special encoding */
{
/* We will write the value to the file in 7 bit chunks. If the 8th bit
- * is clear, we're done, if it is set, another chunk follows. This will
- * allow us to encode smaller values with less bytes, at the expense of
- * needing 5 bytes if a 32 bit value is written to file.
- */
+ ** is clear, we're done, if it is set, another chunk follows. This will
+ ** allow us to encode smaller values with less bytes, at the expense of
+ ** needing 5 bytes if a 32 bit value is written to file.
+ */
do {
- unsigned char C = (V & 0x7F);
- V >>= 7;
- if (V) {
- C |= 0x80;
- }
- Write8 (F, C);
+ unsigned char C = (V & 0x7F);
+ V >>= 7;
+ if (V) {
+ C |= 0x80;
+ }
+ Write8 (F, C);
} while (V != 0);
}
/* Write data to the file */
{
if (fwrite (Data, 1, Size, F) != Size) {
- Error ("Write error (disk full?)");
+ Error ("Write error (disk full?)");
}
}
/* Write one byte several times to the file */
{
while (Count--) {
- Write8 (F, Val);
+ Write8 (F, Val);
}
}
{
int C = getc (F);
if (C == EOF) {
- Error ("Read error (file corrupt?)");
+ long Pos = ftell (F);
+ Error ("Read error at position %ld (file corrupt?)", Pos);
}
return C;
}
/* Sign extend the value */
if (V & 0x80000000UL) {
- /* Signed value */
- V |= ~0xFFFFFFFFUL;
+ /* Signed value */
+ V |= ~0xFFFFFFFFUL;
}
/* Return it as a long */
/* Read a variable size value from the file */
{
/* The value was written to the file in 7 bit chunks LSB first. If there
- * are more bytes, bit 8 is set, otherwise it is clear.
- */
+ ** are more bytes, bit 8 is set, otherwise it is clear.
+ */
unsigned char C;
unsigned long V = 0;
unsigned Shift = 0;
do {
- /* Read one byte */
- C = Read8 (F);
- /* Encode it into the target value */
- V |= ((unsigned long)(C & 0x7F)) << Shift;
- /* Next value */
- Shift += 7;
+ /* Read one byte */
+ C = Read8 (F);
+ /* Encode it into the target value */
+ V |= ((unsigned long)(C & 0x7F)) << Shift;
+ /* Next value */
+ Shift += 7;
} while (C & 0x80);
/* Return the value read */
unsigned ReadStr (FILE* F)
/* Read a string from the file, place it into the global string pool, and
- * return its string id.
- */
+** return its string id.
+*/
{
unsigned Id;
StrBuf Buf = STATIC_STRBUF_INITIALIZER;
/* Read a file position from the file */
{
/* Read the data fields */
- Pos->Line = ReadVar (F);
+ Pos->Line = ReadVar (F);
Pos->Col = ReadVar (F);
Pos->Name = ReadVar (F);
return Pos;
{
/* Explicitly allow reading zero bytes */
if (Size > 0) {
- if (fread (Data, 1, Size, F) != Size) {
- Error ("Read error (file corrupt?)");
- }
+ if (fread (Data, 1, Size, F) != Size) {
+ long Pos = ftell (F);
+ Error ("Read error at position %ld (file corrupt?)", Pos);
+ }
}
return Data;
}
-
-
-
-