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.

204 lines
4.6 KiB

  1. // Copyright 1997-1997 Microsoft Corporation. All Rights Reserved.
  2. //*********************************************************************************************************************************************
  3. //
  4. // File: Parser.cpp
  5. // Author: Donald Drake
  6. // Purpose: Implements classes to support parsing tokens from a xml file
  7. #include "header.h"
  8. #include "TCHAR.h"
  9. #include "string.h"
  10. #include <winnls.h>
  11. #include "parserhh.h"
  12. DWORD CParseXML::Start(const TCHAR *szFile)
  13. {
  14. ASSERT(!m_pin);
  15. m_pin = new CInput(szFile);
  16. if (!m_pin->isInitialized())
  17. return F_NOFILE;
  18. if (SetError(Read()) != F_OK)
  19. return GetError();
  20. return F_OK;
  21. }
  22. void CParseXML::End()
  23. {
  24. if (m_pin) {
  25. delete m_pin;
  26. m_pin = NULL;
  27. }
  28. }
  29. DWORD CParseXML::Read()
  30. {
  31. if (!m_pin->getline(&m_cszLine))
  32. return F_EOF;
  33. _tcscpy(m_cCurBuffer, m_cszLine);
  34. m_pCurrentIndex = m_cCurBuffer;
  35. return F_OK;
  36. }
  37. TCHAR * CParseXML::GetFirstWord(const TCHAR *sz)
  38. {
  39. if (sz == NULL)
  40. return NULL;
  41. // ignore starting white space
  42. for (const TCHAR *pChar = sz; *pChar && *pChar== ' '; pChar = CharNext(pChar));
  43. memset(m_cCurWord, 0, MAX_LINE_LEN);
  44. TCHAR *pWord = m_cCurWord;
  45. for (;*pChar && *pChar != ' ' && *pChar != '=' && *pChar != '\n' && *pChar != '\t' ;pChar = CharNext(pChar), pWord = CharNext(pWord))
  46. {
  47. *pWord = *pChar;
  48. if (IsDBCSLeadByte(*pChar))
  49. {
  50. *(pWord+1) = *(pChar+1);
  51. }
  52. }
  53. *pWord = NULL;
  54. return m_cCurWord;}
  55. static const char txtValue[] = "value";
  56. TCHAR * CParseXML::GetValue(const TCHAR *sz)
  57. {
  58. // get location of word value then find = sign
  59. TCHAR *pChar;
  60. pChar = stristr(sz, txtValue);
  61. // did not find the value tag
  62. if (pChar == NULL)
  63. return NULL;
  64. for (; *pChar && *pChar != '='; pChar = CharNext(pChar));
  65. if (*pChar == '=')
  66. {
  67. memset(m_cCurWord, 0, MAX_LINE_LEN);
  68. TCHAR *pWord = m_cCurWord;
  69. // ignore white space
  70. pChar = CharNext(pChar);
  71. for (; *pChar && *pChar == ' '; pChar = CharNext(pChar));
  72. for ( ; *pChar ; pChar = CharNext(pChar))
  73. {
  74. if (*pChar == '/' && *(pChar+1) == NULL)
  75. break;
  76. if (*pChar != 34)
  77. {
  78. *pWord = *pChar;
  79. if (IsDBCSLeadByte(*pChar))
  80. {
  81. *(pWord+1) = *(pChar+1);
  82. }
  83. pWord = CharNext(pWord);
  84. }
  85. }
  86. *pWord = NULL;
  87. return m_cCurWord;
  88. }
  89. return NULL; // did not find the = sign
  90. }
  91. TCHAR *CParseXML::GetToken()
  92. {
  93. // start looking for <
  94. while (TRUE)
  95. {
  96. if (*m_pCurrentIndex == NULL)
  97. {
  98. if (SetError(Read()) != F_OK)
  99. return NULL;
  100. }
  101. if (*m_pCurrentIndex != '<')
  102. {
  103. m_pCurrentIndex = CharNext(m_pCurrentIndex);
  104. continue;
  105. }
  106. // found a < skip it and start building the token
  107. memset(m_cCurToken, 0, MAX_LINE_LEN);
  108. TCHAR *pWord = m_cCurToken;
  109. while (TRUE)
  110. {
  111. m_pCurrentIndex = CharNext(m_pCurrentIndex);
  112. if (*m_pCurrentIndex == NULL)
  113. if (SetError(Read()) != F_OK)
  114. return NULL;
  115. if (*m_pCurrentIndex == '>')
  116. {
  117. m_pCurrentIndex = CharNext(m_pCurrentIndex);
  118. return m_cCurToken;
  119. }
  120. *pWord = *m_pCurrentIndex;
  121. if (IsDBCSLeadByte(*m_pCurrentIndex))
  122. {
  123. *(pWord+1) = *(m_pCurrentIndex+1);
  124. }
  125. pWord = CharNext(pWord);
  126. }
  127. }
  128. }
  129. // class to support a queue of strings
  130. void CFIFOString::RemoveAll()
  131. {
  132. FIFO *prev;
  133. while (m_fifoTail)
  134. {
  135. prev = m_fifoTail->prev;
  136. delete [] m_fifoTail->string;
  137. delete m_fifoTail;
  138. m_fifoTail = prev;
  139. }
  140. }
  141. CFIFOString::~CFIFOString()
  142. {
  143. RemoveAll();
  144. }
  145. DWORD CFIFOString::AddTail(PCSTR psz)
  146. {
  147. FIFO *entry;
  148. entry = new FIFO; // won't return if out of memory
  149. entry->string = new CHAR[strlen(psz) + 1];
  150. strcpy(entry->string, psz);
  151. entry->prev = NULL;
  152. if (m_fifoTail)
  153. {
  154. entry->prev = m_fifoTail;
  155. }
  156. m_fifoTail = entry;
  157. return F_OK;
  158. }
  159. DWORD CFIFOString::GetTail(CHAR **sz)
  160. {
  161. FIFO *entry;
  162. if (m_fifoTail == NULL)
  163. return F_END;
  164. *sz = new CHAR[strlen(m_fifoTail->string) + 1]; // doesn't return if out of memory
  165. strcpy(*sz, m_fifoTail->string);
  166. entry = m_fifoTail->prev;
  167. delete [] m_fifoTail->string;
  168. delete m_fifoTail;
  169. m_fifoTail = entry;
  170. return F_OK;
  171. }