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.

395 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1996 - 1997, Microsoft Corporation.
  4. //
  5. // File: whtmplat.hxx
  6. //
  7. // Contents: Parser for an webhits template file
  8. //
  9. // History: 96/Sep/8 SrikantsS Adapted from htx.hxx
  10. //
  11. //----------------------------------------------------------------------------
  12. #pragma once
  13. enum ENodeType
  14. {
  15. eNone = 0x0,
  16. eParameter = 0x1,
  17. eString = 0x2,
  18. eEscapeHTML = 0x6,
  19. eEscapeURL = 0x7,
  20. eEscapeRAW = 0x8,
  21. eParamOwnsVariantMemory = 0x00100000
  22. };
  23. const ULONG eJustParamMask = 0x0000ffff;
  24. const ULONG eParamMask = 0x0001FFFF;
  25. //+---------------------------------------------------------------------------
  26. //
  27. // Class: CWTXException
  28. //
  29. // Purpose: Exception class for the template errors.
  30. //
  31. // History: 9-09-96 srikants Created
  32. //
  33. //----------------------------------------------------------------------------
  34. class CWTXException : public CException
  35. {
  36. public:
  37. CWTXException( LONG lerror,
  38. WCHAR const * pwcsFileName,
  39. LONG iLineNum )
  40. : CException( lerror ),
  41. _iLineNum(iLineNum)
  42. {
  43. _wcsFileName[0] = 0;
  44. if ( 0 != pwcsFileName )
  45. {
  46. wcsncpy( _wcsFileName, pwcsFileName, MAX_PATH );
  47. _wcsFileName[MAX_PATH] = 0;
  48. }
  49. }
  50. WCHAR const * GetFileName() const { return _wcsFileName; }
  51. LONG GetLineNumber() const { return _iLineNum; }
  52. private:
  53. WCHAR _wcsFileName[MAX_PATH+1];
  54. LONG _iLineNum;
  55. };
  56. //+---------------------------------------------------------------------------
  57. //
  58. // Class: CWHVarSet
  59. //
  60. // Purpose: Webhits variables sets. This class will resolve the replaceable
  61. // parameters for webhits.
  62. //
  63. // History: 9-09-96 srikants Created
  64. //
  65. // Notes: As the number of repleacable parameters is very small, a
  66. // linear lookup is fine.
  67. //
  68. //----------------------------------------------------------------------------
  69. class CWHVarSet
  70. {
  71. enum { eMaxParams = 15 };
  72. public:
  73. CWHVarSet() : _count(0) {}
  74. BOOL GetStringValueHTML( WCHAR const * wcsName, CVirtualString & str, ULONG ulCodepage );
  75. BOOL GetStringValueURL( WCHAR const * wcsName, CVirtualString & str, ULONG ulCodepage );
  76. WCHAR const * GetStringValueRAW( WCHAR const * wcsName, ULONG & cwc )
  77. {
  78. WCHAR const * pwcsValue = Find( wcsName );
  79. if ( 0 != pwcsValue )
  80. cwc = wcslen( pwcsValue );
  81. return pwcsValue;
  82. }
  83. WCHAR const * Find( WCHAR const * wcsName )
  84. {
  85. for ( ULONG i = 0; i < _count; i++ )
  86. {
  87. if ( 0 == _wcsicmp( _aParamNames[i], wcsName ) )
  88. return _aParamValues[i];
  89. }
  90. return 0;
  91. }
  92. void AddParam( WCHAR const * pwcsName, WCHAR const * pwcsValue )
  93. {
  94. Win4Assert( _count <= eMaxParams );
  95. Win4Assert( 0 != pwcsName );
  96. Win4Assert( 0 != pwcsValue );
  97. if ( _count >= eMaxParams )
  98. THROW( CException( STATUS_INVALID_PARAMETER ) );
  99. _aParamNames[_count] = pwcsName;
  100. _aParamValues[_count++] = pwcsValue;
  101. }
  102. private:
  103. ULONG _count;
  104. WCHAR const * _aParamNames[eMaxParams];
  105. WCHAR const * _aParamValues[eMaxParams];
  106. };
  107. //+---------------------------------------------------------------------------
  108. //
  109. // Class: CWHParamNode
  110. //
  111. // Purpose: A parameter node for the replaceable parameters.
  112. //
  113. // History: 9-09-96 srikants Created
  114. //
  115. //----------------------------------------------------------------------------
  116. class CWHParamNode
  117. {
  118. friend class CWHParamNodeIter;
  119. public:
  120. CWHParamNode( WCHAR * ptr, ENodeType eType = eNone )
  121. :_eType(eType),
  122. _ptr(ptr),
  123. _pNext(0),
  124. _cwcString(0)
  125. {
  126. }
  127. ~CWHParamNode();
  128. WCHAR const * String() const { return _ptr; }
  129. ULONG Type() const { return _eType; }
  130. ULONG Length()
  131. {
  132. if ( 0 == _cwcString )
  133. _cwcString = wcslen( _ptr );
  134. return _cwcString;
  135. }
  136. void SetNextNode( CWHParamNode * pNext ) { _pNext = pNext; }
  137. CWHParamNode * GetNextNode() const { return _pNext; }
  138. CWHParamNode * GetEndNode()
  139. {
  140. CWHParamNode *pEndNode = this;
  141. while ( 0 != pEndNode->GetNextNode() )
  142. {
  143. pEndNode = pEndNode->GetNextNode();
  144. }
  145. return pEndNode;
  146. }
  147. CWHParamNode * QueryNextNode()
  148. {
  149. CWHParamNode * pTemp = _pNext;
  150. _pNext = 0;
  151. return pTemp;
  152. }
  153. BOOL IsEmpty() const { return 0 == GetNextNode(); }
  154. private:
  155. ULONG _eType; // Type of node
  156. WCHAR * _ptr; // Ptr to string associated with node
  157. CWHParamNode * _pNext; // Next node
  158. ULONG _cwcString; // length of the string _ptr
  159. };
  160. //+---------------------------------------------------------------------------
  161. //
  162. // Class: CWHParamNodeIter
  163. //
  164. // Purpose: An iterator over the list of parameter nodes.
  165. //
  166. // History: 9-09-96 srikants Created
  167. //
  168. //----------------------------------------------------------------------------
  169. class CWHParamNodeIter
  170. {
  171. public:
  172. CWHParamNodeIter( CWHParamNode * pNode ) : _root(pNode) {}
  173. void Next()
  174. {
  175. Win4Assert( !AtEnd() );
  176. _root = _root->GetNextNode();
  177. }
  178. BOOL AtEnd() const { return 0 == _root; }
  179. CWHParamNode * Get() { return _root; }
  180. private:
  181. CWHParamNode * _root; // Root of the parameter list
  182. };
  183. class CWTXScanner;
  184. //+---------------------------------------------------------------------------
  185. //
  186. // Class: CWHParamReplacer
  187. //
  188. // Purpose: Parameter replacer for webhits replaceable parameters.
  189. //
  190. // History: 9-09-96 srikants Created
  191. //
  192. // Notes: Adapted from idq.dll
  193. //
  194. //----------------------------------------------------------------------------
  195. class CWHParamReplacer
  196. {
  197. public:
  198. CWHParamReplacer( WCHAR const * wcsString,
  199. WCHAR const * wcsPrefix,
  200. WCHAR const * wcsSuffix );
  201. ~CWHParamReplacer()
  202. {
  203. delete _wcsString;
  204. }
  205. void ParseString( CWHVarSet & variableSet );
  206. void ReplaceParams( CVirtualString & strResult,
  207. CWHVarSet & variableSet,
  208. ULONG ulCodepage );
  209. ULONG GetFlags() const { return _ulFlags; }
  210. private:
  211. void BuildList( CWTXScanner & scanner, CWHParamNode * paramNode );
  212. WCHAR * _wcsString; // The string we've parsed
  213. WCHAR const * _wcsPrefix; // Deliminating prefix
  214. WCHAR const * _wcsSuffix; // Deliminating suffix
  215. ULONG _ulFlags; // Flags associated with replaced variables
  216. CWHParamNode _paramNode; // List of ptrs to replace params
  217. };
  218. //+---------------------------------------------------------------------------
  219. //
  220. // Class: CWTXScanner
  221. //
  222. // Purpose: Breaks up a string into a number of HTX tokens
  223. //
  224. // History: 96/Jan/23 DwightKr Created
  225. //
  226. //----------------------------------------------------------------------------
  227. class CWTXScanner
  228. {
  229. public:
  230. CWTXScanner( CWHVarSet & variableSet,
  231. WCHAR const * wcsPrefix,
  232. WCHAR const * wcsSuffix );
  233. void Init( WCHAR * wcsString );
  234. BOOL FindNextToken();
  235. BOOL IsToken(WCHAR * wcs);
  236. int TokenType() const { return _type; }
  237. WCHAR * GetToken();
  238. private:
  239. WCHAR * _wcsString; // String to parse
  240. WCHAR const * _wcsPrefix; // Token prefix delimiter
  241. WCHAR const * _wcsSuffix; // Token suffix delimiter
  242. WCHAR * _wcsPrefixToken; // Start of current token
  243. WCHAR * _wcsSuffixToken; // End of current token
  244. CWHVarSet & _variableSet; // List of replaceabe parameters
  245. // used to identify replaceable tokens
  246. unsigned _cchPrefix; // Length of the prefix delimiter
  247. unsigned _cchSuffix; // Length of the suffix delimiter
  248. WCHAR * _wcsNextToken; // Ptr to next token
  249. ENodeType _type; // Type of current token
  250. ENodeType _nextType; // Type of next token if not eNone
  251. };
  252. //+---------------------------------------------------------------------------
  253. //
  254. // Class: CWTXFile
  255. //
  256. // Purpose: Scans and parses an HTX file.
  257. //
  258. // History: 96/Jan/23 DwightKr Created
  259. //
  260. //----------------------------------------------------------------------------
  261. class CWTXFile
  262. {
  263. public:
  264. CWTXFile( WCHAR const * wcsTemplateName,
  265. WCHAR const * wcsPhysicalName,
  266. UINT codePage );
  267. ~CWTXFile();
  268. void ParseFile( CWHVarSet & variableSet );
  269. BOOL DoesHeaderExist() const { return 0 != _pVarHeader; }
  270. void GetHeader( CVirtualString & string,
  271. CWHVarSet & variableSet );
  272. BOOL DoesFooterExist() const { return 0 != _pVarFooter; }
  273. void GetFooter( CVirtualString & string,
  274. CWHVarSet & variableSet );
  275. BOOL DoesDetailSectionExist() { return 0 != _pVarRowDetails; }
  276. void GetDetailSection( CVirtualString & string,
  277. CWHVarSet & variableSet )
  278. {
  279. Win4Assert( 0 != _pVarRowDetails );
  280. _pVarRowDetails->ReplaceParams( string, variableSet, _codePage );
  281. }
  282. WCHAR const * GetVirtualName() const { return _wcsVirtualName; }
  283. void GetFileNameAndLineNumber( int offset,
  284. WCHAR const *& wcsFileName,
  285. LONG & lineNumber );
  286. UINT GetCodePage() const { return _codePage; }
  287. private:
  288. WCHAR * ReadFile( WCHAR const * wcsFileName );
  289. LONG CountLines( WCHAR const * wcsStart, WCHAR const * wcsEnd ) const;
  290. WCHAR const * _wcsVirtualName; // Virtual name of HTX file
  291. WCHAR const * _wcsPhysicalName; // Physical name
  292. WCHAR * _wcsFileBuffer; // Contents of HTX file
  293. CWHParamReplacer * _pVarHeader; // Buffer containing HTX header
  294. CWHParamReplacer * _pVarRowDetails; // Buffer containing detail section
  295. CWHParamReplacer * _pVarFooter; // Buffer containing footer
  296. UINT _codePage; // Code page used to read this file
  297. };