-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.
+ /* 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.
*/
- S = LI->Line;
- while (*S) {
- if (*S == '\t') {
- *S = ' ';
- }
- ++S;
+ 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;
}
-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 */
* of the supplied one to save some memory.
*/
if (!AddSource) {
- Line = "";
+ Line = &EmptyStrBuf;
}
/* Create a new line info */
/* Return the file name from a line info */
{
PRECONDITION (LI != 0);
- return LI->InputFile->Name;
+ return GetInputFile (LI->InputFile);
}