1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* Global pointer to line information for the current line */
55 static LineInfo* CurLineInfo = 0;
59 /*****************************************************************************/
61 /*****************************************************************************/
65 static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
66 /* Create and return a new line info. Ref count will be 1. */
68 /* Calculate the length of the line */
69 unsigned Len = strlen (Line);
72 LineInfo* LI = xmalloc (sizeof (LineInfo) + Len);
74 /* Initialize the fields */
77 LI->LineNum = LineNum;
78 memcpy (LI->Line, Line, Len+1);
80 /* Return the new struct */
86 static void FreeLineInfo (LineInfo* LI)
87 /* Free a LineInfo structure */
94 LineInfo* UseLineInfo (LineInfo* LI)
95 /* Increase the reference count of the given line info and return it. */
104 void ReleaseLineInfo (LineInfo* LI)
105 /* Release a reference to the given line info, free the structure if the
106 * reference count drops to zero.
109 CHECK (LI && LI->RefCount > 0);
110 if (--LI->RefCount == 0) {
111 /* No more references, free it */
118 LineInfo* GetCurLineInfo (void)
119 /* Return a pointer to the current line info. The reference count is NOT
120 * increased, use UseLineInfo for that purpose.
128 void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
129 /* Update the line info - called if a new line is read */
131 /* If a current line info exists, release it */
133 ReleaseLineInfo (CurLineInfo);
136 /* Create a new line info */
137 CurLineInfo = NewLineInfo (F, LineNum, Line);
142 const char* GetInputName (const LineInfo* LI)
143 /* Return the file name from a line info */
145 PRECONDITION (LI != 0);
146 return LI->InputFile->Name;
151 unsigned GetInputLine (const LineInfo* LI)
152 /* Return the line number from a line info */
154 PRECONDITION (LI != 0);