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.

270 lines
6.4 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #pragma warning(disable:4786)
  4. #include <wbemcomn.h>
  5. #include <genutils.h>
  6. #include "dnf.h"
  7. // #include "TwoPropNode.h"
  8. // #include "dumbnode.h"
  9. void CDNFExpression::CreateFromTokens(QL_LEVEL_1_TOKEN*& pLastToken,
  10. BOOL bNegate)
  11. {
  12. QL_LEVEL_1_TOKEN& Head = *pLastToken;
  13. if(Head.nTokenType == QL1_OP_EXPRESSION)
  14. {
  15. CreateFromToken(Head, bNegate);
  16. pLastToken--;
  17. return;
  18. }
  19. // Build arguments
  20. // ===============
  21. pLastToken--;
  22. if(Head.nTokenType == QL1_NOT)
  23. {
  24. CreateFromTokens(pLastToken, !bNegate);
  25. return;
  26. }
  27. CDNFExpression Arg1;
  28. Arg1.CreateFromTokens(pLastToken, bNegate);
  29. CDNFExpression Arg2;
  30. Arg2.CreateFromTokens(pLastToken, bNegate);
  31. if(Head.nTokenType == QL1_AND)
  32. {
  33. CreateAnd(Arg1, Arg2);
  34. }
  35. else
  36. {
  37. CreateOr(Arg1, Arg2);
  38. }
  39. }
  40. void CDNFExpression::CreateAnd(CDNFExpression& Arg1, CDNFExpression& Arg2)
  41. {
  42. for(long lFirst = 0; lFirst < Arg1.GetNumTerms(); lFirst++)
  43. for(long lSecond = 0; lSecond < Arg2.GetNumTerms(); lSecond++)
  44. {
  45. CConjunction* pNewTerm =
  46. new CConjunction(*Arg1.GetTermAt(lFirst),
  47. *Arg2.GetTermAt(lSecond));
  48. m_apTerms.Add(pNewTerm);
  49. }
  50. }
  51. void CDNFExpression::CreateOr(CDNFExpression& Arg1, CDNFExpression& Arg2)
  52. {
  53. int i;
  54. for(i = 0; i < Arg1.GetNumTerms(); i++)
  55. {
  56. m_apTerms.Add(new CConjunction(*Arg1.GetTermAt(i)));
  57. }
  58. for(i = 0; i < Arg2.GetNumTerms(); i++)
  59. {
  60. m_apTerms.Add(new CConjunction(*Arg2.GetTermAt(i)));
  61. }
  62. }
  63. void CDNFExpression::CreateFromToken(QL_LEVEL_1_TOKEN& Token, BOOL bNegate)
  64. {
  65. m_apTerms.Add(new CConjunction(Token, bNegate));
  66. }
  67. #ifdef UNDEFINED
  68. HRESULT CDNFExpression::BuildTree(CContextMetaData* pNamespace,
  69. CImplicationList& Implications,
  70. CEvalNode** ppRes)
  71. {
  72. HRESULT hres;
  73. // Check if there is only one conjunction to talk about
  74. // ====================================================
  75. if(m_apTerms.GetSize() == 1)
  76. {
  77. // Just build that one
  78. // ===================
  79. return m_apTerms[0]->BuildTree(pNamespace, Implications, ppRes);
  80. }
  81. // Build them for all conjunctions and OR together
  82. // ===============================================
  83. COrNode* pRes = new COrNode;
  84. if(pRes == NULL)
  85. return WBEM_E_OUT_OF_MEMORY;
  86. for(int i = 0; i < m_apTerms.GetSize(); i++)
  87. {
  88. CEvalNode* pNew;
  89. hres = m_apTerms[i]->BuildTree(pNamespace, Implications, &pNew);
  90. if(FAILED(hres))
  91. {
  92. delete pRes;
  93. return hres;
  94. }
  95. hres = pRes->AddBranch(pNew);
  96. if(FAILED(hres))
  97. {
  98. delete pRes;
  99. return hres;
  100. }
  101. }
  102. *ppRes = pRes;
  103. return WBEM_S_NO_ERROR;
  104. }
  105. #endif
  106. void CDNFExpression::Sort()
  107. {
  108. for(int i = 0; i < m_apTerms.GetSize(); i++)
  109. {
  110. m_apTerms[i]->Sort();
  111. }
  112. }
  113. CConjunction::CConjunction(QL_LEVEL_1_TOKEN& Token, BOOL bNegate)
  114. {
  115. m_apTokens.Add(new QL_LEVEL_1_TOKEN(Token));
  116. if(bNegate)
  117. {
  118. m_apTokens[0]->nOperator = NegateOperator(m_apTokens[0]->nOperator);
  119. }
  120. }
  121. CConjunction::CConjunction(CConjunction& Other)
  122. {
  123. for(int i = 0; i < Other.GetNumTokens(); i++)
  124. {
  125. m_apTokens.Add(new QL_LEVEL_1_TOKEN(*Other.GetTokenAt(i)));
  126. }
  127. }
  128. CConjunction::CConjunction(CConjunction& Other1, CConjunction& Other2)
  129. {
  130. int i;
  131. for(i = 0; i < Other1.GetNumTokens(); i++)
  132. {
  133. m_apTokens.Add(new QL_LEVEL_1_TOKEN(*Other1.GetTokenAt(i)));
  134. }
  135. for(i = 0; i < Other2.GetNumTokens(); i++)
  136. {
  137. m_apTokens.Add(new QL_LEVEL_1_TOKEN(*Other2.GetTokenAt(i)));
  138. }
  139. }
  140. int CConjunction::NegateOperator(int nOperator)
  141. {
  142. switch(nOperator)
  143. {
  144. case QL1_OPERATOR_EQUALS:
  145. return QL1_OPERATOR_NOTEQUALS;
  146. case QL1_OPERATOR_NOTEQUALS:
  147. return QL1_OPERATOR_EQUALS;
  148. case QL1_OPERATOR_GREATER:
  149. return QL1_OPERATOR_LESSOREQUALS;
  150. case QL1_OPERATOR_LESS:
  151. return QL1_OPERATOR_GREATEROREQUALS;
  152. case QL1_OPERATOR_LESSOREQUALS:
  153. return QL1_OPERATOR_GREATER;
  154. case QL1_OPERATOR_GREATEROREQUALS:
  155. return QL1_OPERATOR_LESS;
  156. case QL1_OPERATOR_LIKE:
  157. return QL1_OPERATOR_UNLIKE;
  158. case QL1_OPERATOR_UNLIKE:
  159. return QL1_OPERATOR_LIKE;
  160. case QL1_OPERATOR_ISA:
  161. return QL1_OPERATOR_ISNOTA;
  162. case QL1_OPERATOR_ISNOTA:
  163. return QL1_OPERATOR_ISA;
  164. case QL1_OPERATOR_INV_ISA:
  165. return QL1_OPERATOR_INV_ISNOTA;
  166. case QL1_OPERATOR_INV_ISNOTA:
  167. return QL1_OPERATOR_INV_ISA;
  168. }
  169. return nOperator;
  170. }
  171. #ifdef UNDEFINED
  172. HRESULT CConjunction::BuildTree(CContextMetaData* pNamespace,
  173. CImplicationList& Implications,
  174. CEvalNode** ppRes)
  175. {
  176. HRESULT hres;
  177. // Build them for all tokens and AND together
  178. // ==========================================
  179. *ppRes = NULL;
  180. CImplicationList BranchImplications(Implications);
  181. for(int i = 0; i < m_apTokens.GetSize(); i++)
  182. {
  183. CEvalNode* pNew = NULL;
  184. hres = CEvalTree::BuildFromToken(pNamespace, BranchImplications,
  185. *m_apTokens[i], &pNew);
  186. if(FAILED(hres))
  187. {
  188. delete *ppRes;
  189. return hres;
  190. }
  191. if(i > 0)
  192. {
  193. CEvalNode* pOld = *ppRes;
  194. CEvalTree::Combine(pOld, pNew, EVAL_OP_AND, pNamespace,
  195. Implications, true, true, ppRes); // delete both
  196. }
  197. else
  198. {
  199. *ppRes = pNew;
  200. }
  201. }
  202. return WBEM_S_NO_ERROR;
  203. }
  204. #endif
  205. void CConjunction::Sort()
  206. {
  207. int i = 0;
  208. while(i < m_apTokens.GetSize() - 1)
  209. {
  210. int nLeft = m_apTokens[i]->PropertyName.GetNumElements();
  211. int nRight = m_apTokens[i+1]->PropertyName.GetNumElements();
  212. if(nLeft > nRight)
  213. {
  214. m_apTokens.Swap(i, i+1);
  215. if(i != 0)
  216. {
  217. i--;
  218. }
  219. }
  220. else
  221. {
  222. i++;
  223. }
  224. }
  225. }