void OpenMainFile (const char* Name)
/* Open the main file. Will call Fatal() in case of failures. */
{
+ AFile* MainFile;
+
+
/* Setup a new IFile structure for the main file */
IFile* IF = NewIFile (Name);
}
/* Allocate a new AFile structure for the file */
- (void) NewAFile (IF, F);
+ MainFile = NewAFile (IF, F);
/* Allocate the input line buffer */
Line = NewStrBuf ();
+
+ /* Update the line infos, so we have a valid line info even at start of
+ * the main file before the first line is read.
+ */
+ UpdateLineInfo (MainFile->Input, MainFile->Line, Line);
}
void NextChar (void)
/* Skip the current input character and read the next one from the input
- * stream. CurC and NextC are valid after the call. If end of line is
+ * stream. CurC and NextC are valid after the call. If end of line is
* reached, both are set to NUL, no more lines are read by this function.
*/
{
InitLine (Line);
/* Create line information for this line */
- UpdateLineInfo (Input->Input, Input->Line, SB_GetConstBuf (Line));
+ UpdateLineInfo (Input->Input, Input->Line, Line);
/* Done */
return 1;
-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* T;
+ 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 */
*/
T = LI->Line;
while (Len--) {
- if (*Line == '\t') {
- *T = ' ';
- } else {
- *T = *Line;
- }
- ++Line;
- ++T;
+ if (*S == '\t') {
+ *T = ' ';
+ } else {
+ *T = *S;
+ }
+ ++S;
+ ++T;
}
/* Add the terminator */
-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 */
#define LINEINFO_H
+
+/* common */
+#include "strbuf.h"
+
+
/*****************************************************************************/
/* Forwards */
/* Input file structure */
-struct IFile;
+struct IFile;
* increased, use UseLineInfo for that purpose.
*/
-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 */
const char* GetInputName (const LineInfo* LI);