Source code of Windows XP (NT5)
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.

192 lines
5.6 KiB

  1. #include <mvopsys.h>
  2. // Global debug variable
  3. #ifdef _DEBUG
  4. static char s_aszModule[] = __FILE__;
  5. #endif
  6. #include <windows.h>
  7. #include <iterror.h>
  8. #include "cistream.h"
  9. #include "orkin.h"
  10. #include "..\svutil.h"
  11. #include "ciutil.h"
  12. CStreamParseLine::CStreamParseLine(void)
  13. {
  14. MEMSET(&m_liNull, 0, sizeof(LARGE_INTEGER));
  15. m_pistmInput = NULL;
  16. #ifdef _DEBUG
  17. MEMSET(m_wstrLine, 0, sizeof(m_wstrLine));
  18. #endif
  19. } // Constructor
  20. STDMETHODIMP CStreamParseLine::Reset(void)
  21. {
  22. return m_pistmInput->Seek(m_liStartOffset, STREAM_SEEK_SET, NULL);
  23. } // Reset
  24. STDMETHODIMP CStreamParseLine::SetStream(IStream *pistm)
  25. {
  26. if (NULL == pistm)
  27. return E_POINTER;
  28. (m_pistmInput = pistm)->AddRef();
  29. // Make sure this stream is Unicode
  30. ULARGE_INTEGER uliStartOffset;
  31. pistm->Seek(m_liNull, STREAM_SEEK_CUR, &uliStartOffset);
  32. WORD wUnicodeWord;
  33. HRESULT hr;
  34. if (SUCCEEDED(hr = pistm->Read (&wUnicodeWord, sizeof (WORD), NULL)))
  35. {
  36. if (wUnicodeWord == 0xFEFF)
  37. m_fASCII = FALSE;
  38. else if (wUnicodeWord == 0xFFEF)
  39. // This requires byte swapping on INTEL
  40. SetErrCode(&hr, E_NOTIMPL);
  41. else
  42. {
  43. LARGE_INTEGER liTemp;
  44. liTemp.QuadPart = uliStartOffset.QuadPart;
  45. pistm->Seek (liTemp, STREAM_SEEK_SET, NULL);
  46. m_fASCII = TRUE;
  47. }
  48. }
  49. pistm->Seek(m_liNull, STREAM_SEEK_CUR, &uliStartOffset);
  50. m_liStartOffset.QuadPart = uliStartOffset.QuadPart;
  51. return hr;
  52. } // SetStream
  53. STDMETHODIMP CStreamParseLine::Close(void)
  54. {
  55. if (m_pistmInput)
  56. {
  57. m_pistmInput->Release();
  58. m_pistmInput = NULL;
  59. }
  60. return S_OK;
  61. } // Close
  62. STDMETHODIMP CStreamParseLine::GetLogicalLine
  63. (LPWSTR *ppwstrLineBuffer, int *piLineCount)
  64. {
  65. ITASSERT(ppwstrLineBuffer);
  66. LPWSTR pwstrCurrent;
  67. BOOL fGotLine, fIgnore = FALSE;
  68. BOOL fCommentStrippingOn = TRUE;
  69. BOOL fCommentedLine;
  70. int iQuoteNesting = 0;
  71. int iParenNesting = 0;
  72. int iLineCount = 0;
  73. for (;;)
  74. {
  75. iLineCount += 1;
  76. fCommentedLine = FALSE;
  77. if (m_fASCII)
  78. fGotLine = StreamGetLineASCII (m_pistmInput,
  79. m_wstrLine, NULL, MAX_MVP_LINE_BYTES * sizeof(WCHAR));
  80. else
  81. fGotLine = StreamGetLine (m_pistmInput,
  82. m_wstrLine, NULL, MAX_MVP_LINE_BYTES * sizeof(WCHAR));
  83. if (fGotLine)
  84. {
  85. StripLeadingBlanks(StripTrailingBlanks(m_wstrLine));
  86. if (!fIgnore)
  87. {
  88. /********************************
  89. SCAN THROUGH LOOKING FOR COMMENTS
  90. (AND NULLING THEM OUT)
  91. *********************************/
  92. pwstrCurrent = m_wstrLine;
  93. while (*pwstrCurrent) {
  94. switch (*pwstrCurrent) {
  95. case '(':
  96. ++iParenNesting;
  97. fCommentStrippingOn=FALSE;
  98. ++pwstrCurrent;
  99. break;
  100. case ')':
  101. if ((iParenNesting == 1) && (iQuoteNesting == 0))
  102. fCommentStrippingOn=TRUE;
  103. if (iParenNesting > 0)
  104. --iParenNesting;
  105. ++pwstrCurrent;
  106. break;
  107. case '"':
  108. if (iQuoteNesting==0) {
  109. iQuoteNesting=1;
  110. fCommentStrippingOn=FALSE;
  111. }
  112. else
  113. iQuoteNesting=0;
  114. if ((iQuoteNesting==0) && (iParenNesting == 0))
  115. fCommentStrippingOn=TRUE;
  116. ++pwstrCurrent;
  117. break;
  118. case '\\':
  119. if (*(pwstrCurrent+1) == ';') {
  120. // slash escapes comment character!
  121. *pwstrCurrent=' '; // just put a space in the buffer!
  122. pwstrCurrent+=2; // increment past ';'
  123. }
  124. else ++pwstrCurrent;
  125. break;
  126. case ';':
  127. if (fCommentStrippingOn)
  128. {
  129. *pwstrCurrent='\0';
  130. fCommentedLine = TRUE;
  131. }
  132. else
  133. ++pwstrCurrent;
  134. break;
  135. default:
  136. ++pwstrCurrent;
  137. break;
  138. }
  139. }
  140. }
  141. StripTrailingBlanks(m_wstrLine);
  142. if (*m_wstrLine)
  143. {
  144. if (!WSTRICMP (m_wstrLine, L"#IGNORE"))
  145. fIgnore = TRUE;
  146. else if (!WSTRICMP (m_wstrLine, L"#ENDIGNORE"))
  147. fIgnore = FALSE;
  148. else if (!fIgnore)
  149. {
  150. *ppwstrLineBuffer = m_wstrLine;
  151. if (piLineCount)
  152. *piLineCount = iLineCount;
  153. return (S_OK);
  154. }
  155. }
  156. }
  157. else
  158. {
  159. if (piLineCount)
  160. *piLineCount = iLineCount;
  161. return (S_FALSE);
  162. }
  163. }
  164. } // GetLogicalLine