]> git.sur5r.net Git - cc65/blobdiff - src/od65/fileio.c
Rather stay with OFF_YEAR as it is an "officially" name.
[cc65] / src / od65 / fileio.c
index 1fabded67e745b4a62ae9bf72cbb3acd5c88664e..a1493500689331bc6b8cde996cd6fb1de88b1722 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2000 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* (C) 1998-2011, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -34,6 +34,7 @@
 
 
 #include <string.h>
+#include <errno.h>
 
 /* common */
 #include "xmalloc.h"
 
 
 
+void FileSetPos (FILE* F, unsigned long Pos)
+/* Seek to the given absolute position, fail on errors */
+{                 
+    if (fseek (F, Pos, SEEK_SET) != 0) {
+       Error ("Cannot seek: %s", strerror (errno));
+    }
+}
+
+
+
+unsigned long FileGetPos (FILE* F)
+/* Return the current file position, fail on errors */
+{
+    long Pos = ftell (F);
+    if (Pos < 0) {
+               Error ("Error in ftell: %s", strerror (errno));
+    }
+    return Pos;
+}
+
+
+
 unsigned Read8 (FILE* F)
 /* Read an 8 bit value from the file */
 {
@@ -110,27 +133,35 @@ long Read32Signed (FILE* F)
 
 
 
-char* ReadStr (FILE* F, char* Str)
-/* Read a string from the file. Str must hold 256 chars at max */
+unsigned long ReadVar (FILE* F)
+/* Read a variable size value from the file */
 {
-    /* Read the length byte */
-    unsigned Len = Read8 (F);
-
-    /* Read the string itself */
-    ReadData (F, Str, Len);
-
-    /* Terminate the string and return it */
-    Str [Len] = '\0';
-    return Str;
+    /* 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.
+     */
+    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;
+    } while (C & 0x80);
+
+    /* Return the value read */
+    return V;
 }
 
 
 
-char* ReadMallocedStr (FILE* F)
+char* ReadStr (FILE* F)
 /* Read a string from the file into a malloced area */
 {
-    /* Read the length byte */
-    unsigned Len = Read8 (F);
+    /* Read the length */
+    unsigned Len = ReadVar (F);
 
     /* Allocate memory */
     char* Str = xmalloc (Len + 1);
@@ -148,10 +179,10 @@ char* ReadMallocedStr (FILE* F)
 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
 /* Read a file position from the file */
 {
-    /* The line number is encoded as 24 bit value to save some space */
-    Pos->Line =        Read24 (F);
-    Pos->Col  = Read8 (F);
-    Pos->Name = Read8 (F);
+    /* Read the data fields */
+    Pos->Line =        ReadVar (F);
+    Pos->Col  = ReadVar (F);
+    Pos->Name = ReadVar (F);
     return Pos;
 }
 
@@ -160,8 +191,11 @@ FilePos* ReadFilePos (FILE* F, FilePos* Pos)
 void* ReadData (FILE* F, void* Data, unsigned Size)
 /* Read data from the file */
 {
-    if (fread (Data, 1, Size, F) != Size) {
-       Error ("Read error (file corrupt?)");
+    /* Accept zero sized reads */
+    if (Size > 0) {
+       if (fread (Data, 1, Size, F) != Size) {
+           Error ("Read error (file corrupt?)");
+       }
     }
     return Data;
 }
@@ -187,6 +221,30 @@ void ReadObjHeader (FILE* F, ObjHeader* H)
     H->ExportSize   = Read32 (F);
     H->DbgSymOffs   = Read32 (F);
     H->DbgSymSize   = Read32 (F);
+    H->LineInfoOffs = Read32 (F);
+    H->LineInfoSize = Read32 (F);
+    H->StrPoolOffs  = Read32 (F);
+    H->StrPoolSize  = Read32 (F);
+    H->AssertOffs   = Read32 (F);
+    H->AssertSize   = Read32 (F);
+    H->ScopeOffs    = Read32 (F);
+    H->ScopeSize    = Read32 (F);
+    H->SpanOffs     = Read32 (F);
+    H->SpanSize     = Read32 (F);
+}
+
+
+
+void ReadStrPool (FILE* F, Collection* C)
+/* Read a string pool from the current position into C. */
+{
+    /* The number of strings is the first item */
+    unsigned long Count = ReadVar (F);
+
+    /* Read all the strings into C */
+    while (Count--) {
+        CollAppend (C, ReadStr (F));
+    }
 }