Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.1 KiB

  1. /*** line.c - Line stream related functions
  2. *
  3. * Copyright (c) 1996,1997 Microsoft Corporation
  4. * Author: Michael Tsang (MikeTs)
  5. * Created: 09/04/96
  6. *
  7. * This module implements the line stream layer so that it can
  8. * keep track of the information such as line number and line
  9. * position. This information is necessary for the scanner or
  10. * even the parser to accurately pin point the error location
  11. * in case of syntax or semantic errors.
  12. *
  13. * MODIFICATION HISTORY
  14. */
  15. #include "pch.h"
  16. /***EP OpenLine - allocate and initialize line structure
  17. *
  18. * ENTRY
  19. * pfileSrc -> source file
  20. *
  21. * EXIT-SUCCESS
  22. * returns the pointer to the allocated line structure.
  23. * EXIT-FAILURE
  24. * returns NULL.
  25. */
  26. PLINE EXPORT OpenLine(FILE *pfileSrc)
  27. {
  28. PLINE pline;
  29. ENTER((5, "OpenLine(pfileSrc=%p)\n", pfileSrc));
  30. if ((pline = malloc(sizeof(LINE))) == NULL)
  31. MSG(("OpenLine: failed to allocate line structure"))
  32. else
  33. {
  34. memset(pline, 0, sizeof(LINE));
  35. pline->pfileSrc = pfileSrc;
  36. }
  37. EXIT((5, "OpenLine=%p\n", pline));
  38. return pline;
  39. } //OpenLine
  40. /***EP CloseLine - free line structure
  41. *
  42. * ENTRY
  43. * pline->line structure
  44. *
  45. * EXIT
  46. * None
  47. */
  48. VOID EXPORT CloseLine(PLINE pline)
  49. {
  50. ENTER((5, "CloseLine(pline=%p)\n", pline));
  51. free(pline);
  52. EXIT((5, "CloseLine!\n"));
  53. } //CloseLine
  54. /***EP LineGetC - get a character from the line stream
  55. *
  56. * This is equivalent to fgetc() except that it has the line
  57. * stream layer below it instead of directly from the file. It
  58. * is done this way to preserve the line number and line position
  59. * information for accurately pin pointing the error location
  60. * if necessary.
  61. *
  62. * ENTRY
  63. * pline -> line structure
  64. *
  65. * EXIT-SUCCESS
  66. * returns the character
  67. * EXIT-FAILURE
  68. * returns error code - EOF (end-of-file)
  69. */
  70. int EXPORT LineGetC(PLINE pline)
  71. {
  72. int ch = 0;
  73. ENTER((5, "LineGetC(pline=%p)\n", pline));
  74. if (pline->wLinePos >= pline->wLineLen)
  75. {
  76. //
  77. // EOL is encountered
  78. //
  79. if (fgets(pline->szLineBuff, sizeof(pline->szLineBuff), pline->pfileSrc)
  80. != NULL)
  81. {
  82. pline->wLinePos = 0;
  83. if (!(pline->wfLine & LINEF_LONGLINE))
  84. pline->wLineNum++;
  85. pline->wLineLen = (WORD)strlen(pline->szLineBuff);
  86. if (pline->szLineBuff[pline->wLineLen - 1] == '\n')
  87. pline->wfLine &= ~LINEF_LONGLINE;
  88. else
  89. pline->wfLine |= LINEF_LONGLINE;
  90. }
  91. else
  92. ch = EOF;
  93. }
  94. if (ch == 0)
  95. ch = (int)pline->szLineBuff[pline->wLinePos++];
  96. EXIT((5, "LineGetC=%x (ch=%c,Line=%u,NextPos=%u,LineLen=%u)\n",
  97. ch, ch, pline->wLineNum, pline->wLinePos, pline->wLineLen));
  98. return ch;
  99. } //LineGetC
  100. /***EP LineUnGetC - push a character back to the line stream
  101. *
  102. * This is equivalent to fungetc() except that it's source is
  103. * the line stream not the file stream. Refer to LineGetC for
  104. * explanation on this implementation.
  105. *
  106. * ENTRY
  107. * ch - character being pushed back
  108. * pline -> line structure
  109. *
  110. * EXIT-SUCCESS
  111. * returns the character being pushed
  112. * EXIT-FAILURE
  113. * returns -1
  114. */
  115. int EXPORT LineUnGetC(int ch, PLINE pline)
  116. {
  117. ENTER((5, "LineUnGetC(ch=%c,pline=%p)\n", ch, pline));
  118. ASSERT(pline->wLinePos != 0);
  119. if (ch != EOF)
  120. {
  121. pline->wLinePos--;
  122. ASSERT((int)pline->szLineBuff[pline->wLinePos] == ch);
  123. }
  124. EXIT((5, "LineUnGetC=%x (ch=%c)\n", ch, ch));
  125. return ch;
  126. } //LineUnGetC
  127. /***EP LineFlush - flush a line
  128. *
  129. * The scanner may want to discard the rest of the line when it
  130. * detects an in-line comment symbol, for example.
  131. *
  132. * ENTRY
  133. * pline -> line structure
  134. *
  135. * EXIT
  136. * none
  137. */
  138. VOID EXPORT LineFlush(PLINE pline)
  139. {
  140. ENTER((5, "LineFlush(pline=%p)\n", pline));
  141. pline->wLinePos = pline->wLineLen;
  142. EXIT((5, "LineFlush!\n"));
  143. } //LineFlush