1
0

xmlobject.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <precomp.h>
  2. #include <bfc/bfc_assert.h>
  3. #include "xmlobject.h"
  4. #include <new>
  5. #define PILENODE_AUTOINCREMENT 50
  6. /*
  7. CreateXMLParameters(int master_handle)
  8. addParam(master_handle, this_handle, name, id)
  9. master_handle is key into array of name/this-handle/id
  10. */
  11. class DeleteOnClose
  12. {
  13. public:
  14. ~DeleteOnClose()
  15. {
  16. allocd.freeAll();
  17. }
  18. PtrList<XmlObjectParam> allocd;
  19. };
  20. DeleteOnClose deleter;
  21. template <class t_>
  22. struct PileNode
  23. {
  24. PileNode(int num)
  25. {
  26. sizePile = num;
  27. pile = (t_ *)MALLOC(sizeof(t_) * num);
  28. deleter.allocd.addItem(pile);
  29. }
  30. void Rebuild(int num)
  31. {
  32. sizePile = num;
  33. pile = (t_ *)MALLOC(sizeof(t_) * num);
  34. deleter.allocd.addItem(pile);
  35. }
  36. size_t sizePile;
  37. t_ *pile;
  38. void *Get()
  39. {
  40. sizePile--;
  41. return pile++;
  42. }
  43. };
  44. template <class t_>
  45. struct PileList
  46. {
  47. PileList(int numPtrs, PileList<t_> *_next = 0) : node(numPtrs), next(_next)
  48. {
  49. }
  50. PileNode<t_> node;
  51. PileList *next;
  52. void *Get()
  53. {
  54. if (node.sizePile)
  55. {
  56. return node.Get();
  57. }
  58. else
  59. {
  60. while (next && !next->node.sizePile)
  61. {
  62. PileList<t_> *temp = next;
  63. next = temp->next;
  64. delete temp;
  65. }
  66. if (next)
  67. return next->Get();
  68. else
  69. {
  70. node.Rebuild(PILENODE_AUTOINCREMENT);
  71. return node.Get();
  72. }
  73. }
  74. }
  75. };
  76. PileList<XmlObjectParam> *paramPile = 0;
  77. XmlObjectParam::XmlObjectParam(int xmlhandle, wchar_t *xmlattribute, int xmlattributeid)
  78. : xmlattributename(xmlattribute), attributeid(xmlattributeid), handle(xmlhandle)
  79. {
  80. KEYWORDUPPER(xmlattribute);
  81. }
  82. #define CBCLASS XmlObjectI
  83. START_DISPATCH;
  84. CB(SETXMLPARAM, setXmlParam);
  85. CB(GETXMLPARAMVALUE, getXmlParamValue);
  86. CB(GETXMLPARAM, getXmlParam);
  87. END_DISPATCH;
  88. XmlObjectI::XmlObjectI()
  89. {
  90. handlepos = 0;
  91. }
  92. XmlObjectI::~XmlObjectI()
  93. {
  94. params.removeAll();
  95. }
  96. void XmlObjectI::addParam(int xmlhandle, XMLParamPair &param, int unused)
  97. //void XmlObjectI::addXmlParam(int xmlhandle, const wchar_t *xmlattribute, int xmlattributeid)
  98. {
  99. if (!paramPile)
  100. paramPile = new PileList<XmlObjectParam>(PILENODE_AUTOINCREMENT);
  101. params.addItem(new(paramPile->Get()) XmlObjectParam(xmlhandle, param.name, param.id));
  102. }
  103. int XmlObjectI::setXmlParamById(int xmlhandle, int xmlattribute, const wchar_t *param, const wchar_t *value)
  104. {
  105. return 0;
  106. }
  107. int XmlObjectI::setXmlParam(const wchar_t *param, const wchar_t *value)
  108. {
  109. int pos = -1;
  110. int r = 0;
  111. params.findItem(param, &pos);
  112. if (pos >= 0)
  113. {
  114. XmlObjectParam *xuop = params.enumItem(pos);
  115. ASSERT(xuop != NULL);
  116. r = setXmlParamById(xuop->getXmlHandle(), xuop->getXmlAttributeId(), param, value);
  117. xuop->setLastValue(value);
  118. }
  119. else
  120. {
  121. onUnknownXmlParam(param, value);
  122. }
  123. return r;
  124. }
  125. const wchar_t *XmlObjectI::getXmlParamValue(int n)
  126. {
  127. return params.enumItem(n)->getLastValue();
  128. }
  129. int XmlObjectI::getXmlParam(const wchar_t *param)
  130. {
  131. int pos=-1;
  132. params.findItem(param, &pos);
  133. return pos;
  134. }
  135. const wchar_t *XmlObjectI::getXmlParamByName(const wchar_t *paramname)
  136. {
  137. int pos = getXmlParam(paramname);
  138. if (pos < 0) return NULL;
  139. return getXmlParamValue(pos);
  140. }
  141. int XmlObjectI::newXmlHandle()
  142. {
  143. return handlepos++;
  144. }
  145. int XmlObjectI::onUnknownXmlParam(const wchar_t *paramname, const wchar_t *strvalue)
  146. {
  147. return 0;
  148. }
  149. void XmlObjectI::hintNumberOfParams(int xmlhandle, int numParams)
  150. {
  151. paramPile = new PileList<XmlObjectParam>(numParams, paramPile);
  152. params.setMinimumSize(params.getNumItems() + numParams);
  153. }