]> git.sur5r.net Git - cc65/blobdiff - src/cc65/lineinfo.c
Merge branch 'master' into popptr1
[cc65] / src / cc65 / lineinfo.c
index 60ba962efb19e5e210be73b7fbffb700604f71d3..f5c2e2689cdfbacb51b1beb4dbe679636d396166 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                               lineinfo.c                                 */
+/*                                lineinfo.c                                 */
 /*                                                                           */
-/*                     Source file line info structure                      */
+/*                      Source file line info structure                      */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
@@ -48,7 +48,7 @@
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                   Data                                    */
 /*****************************************************************************/
 
 
@@ -59,47 +59,55 @@ static LineInfo* CurLineInfo = 0;
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
 
-static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
+static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const StrBuf* Line)
 /* Create and return a new line info. Ref count will be 1. */
 {
-    unsigned  Len;
-    LineInfo* LI;
-    char*     S;
+    unsigned    Len;
+    LineInfo*   LI;
+    const char* S;
+    char*       T;
+
+    /* Get the length of the line and a pointer to the line buffer */
+    Len = SB_GetLen (Line);
+    S   = SB_GetConstBuf (Line);
 
     /* Skip leading spaces in Line */
-    while (IsBlank (*Line)) {
-       ++Line;
+    while (Len > 0 && IsBlank (*S)) {
+        ++S;
+        --Len;
     }
 
-    /* Calculate the length of the line */
-    Len = strlen (Line);
-
-    /* Allocate memory */
+    /* Allocate memory for the line info and the input line */
     LI = xmalloc (sizeof (LineInfo) + Len);
 
     /* Initialize the fields */
     LI->RefCount  = 1;
     LI->InputFile = F;
     LI->LineNum   = LineNum;
-    memcpy (LI->Line, Line, Len+1);
-
-    /* Replace tabs by spaces in the given line since tabs will give rather
-     * arbitrary results when used in the output later, and if we do it here,
-     * we won't need another copy.
-     */
-    S = LI->Line;
-    while (*S) {
-       if (*S == '\t') {
-           *S = ' ';
-       }
-       ++S;
+
+    /* Copy the line, replacing tabs by spaces in the given line since tabs
+    ** will give rather arbitrary results when used in the output later, and
+    ** if we do it here, we won't need another copy later.
+    */
+    T = LI->Line;
+    while (Len--) {
+        if (*S == '\t') {
+            *T = ' ';
+        } else {
+            *T = *S;
+        }
+        ++S;
+        ++T;
     }
 
+    /* Add the terminator */
+    *T = '\0';
+
     /* Return the new struct */
     return LI;
 }
@@ -126,13 +134,13 @@ LineInfo* UseLineInfo (LineInfo* LI)
 
 void ReleaseLineInfo (LineInfo* LI)
 /* Release a reference to the given line info, free the structure if the
- * reference count drops to zero.
- */
+** reference count drops to zero.
+*/
 {
     CHECK (LI && LI->RefCount > 0);
     if (--LI->RefCount == 0) {
-       /* No more references, free it */
-       FreeLineInfo (LI);
+        /* No more references, free it */
+        FreeLineInfo (LI);
     }
 }
 
@@ -140,27 +148,27 @@ void ReleaseLineInfo (LineInfo* LI)
 
 LineInfo* GetCurLineInfo (void)
 /* Return a pointer to the current line info. The reference count is NOT
- * increased, use UseLineInfo for that purpose.
- */
+** increased, use UseLineInfo for that purpose.
+*/
 {
     return CurLineInfo;
 }
 
 
 
-void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
+void UpdateLineInfo (struct IFile* F, unsigned LineNum, const StrBuf* Line)
 /* Update the line info - called if a new line is read */
 {
     /* If a current line info exists, release it */
     if (CurLineInfo) {
-       ReleaseLineInfo (CurLineInfo);
+        ReleaseLineInfo (CurLineInfo);
     }
 
     /* If we have intermixed assembly switched off, use an empty line instead
-     * of the supplied one to save some memory.
-     */
+    ** of the supplied one to save some memory.
+    */
     if (!AddSource) {
-       Line = "";
+        Line = &EmptyStrBuf;
     }
 
     /* Create a new line info */
@@ -173,7 +181,7 @@ const char* GetInputName (const LineInfo* LI)
 /* Return the file name from a line info */
 {
     PRECONDITION (LI != 0);
-    return LI->InputFile->Name;
+    return GetInputFile (LI->InputFile);
 }
 
 
@@ -184,6 +192,3 @@ unsigned GetInputLine (const LineInfo* LI)
     PRECONDITION (LI != 0);
     return LI->LineNum;
 }
-
-
-