1
0

svc_collection.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <precomp.h>
  2. #include "svc_collection.h"
  3. // an named xml overridable collection of objects
  4. #define CBCLASS svc_collectionI
  5. START_DISPATCH;
  6. CB(COLLECTION_TESTTAG, testTag);
  7. VCB(COLLECTION_ADDELEMENT, addElement);
  8. VCB(COLLECTION_REMOVEELEMENT, removeElement);
  9. VCB(COLLECTION_REMOVEALLELEMENTS, removeAllElements);
  10. CB(COLLECTION_GETNUMELEMENTS, getNumElements);
  11. CB(COLLECTION_GETNUMELEMENTSUNIQUE, getNumElementsUnique);
  12. CB(COLLECTION_ENUMELEMENT, enumElement);
  13. CB(COLLECTION_ENUMELEMENTUNIQUE, enumElementUnique);
  14. CB(COLLECTION_GETELEMENT, getElement);
  15. END_DISPATCH;
  16. #undef CBCLASS
  17. #define CBCLASS CollectionElementI
  18. START_DISPATCH;
  19. CB(COLLECTIONELEMENT_GETID, getId);
  20. CB(COLLECTIONELEMENT_GETPARAMVALUE, getParamValue);
  21. CB(COLLECTIONELEMENT_GETPARAMVALUEINT, getParamValueInt);
  22. CB(COLLECTIONELEMENT_GETINCLUDEPATH, getIncludePath);
  23. END_DISPATCH;
  24. #undef CBCLASS
  25. svc_collectionI::svc_collectionI() {
  26. count = 0;
  27. elements.setAutoSort(1);
  28. }
  29. svc_collectionI::~svc_collectionI() {
  30. }
  31. void svc_collectionI::addElement(const char *id, const char *includepath, int incrementalremovalid, skin_xmlreaderparams *params) {
  32. CollectionElementI *cei = new CollectionElementI(this, id, params, incrementalremovalid, includepath);
  33. elements.addItem(cei);
  34. }
  35. void svc_collectionI::removeElement(int removalid) {
  36. for (int i=0;i<elements.getNumItems();i++) {
  37. CollectionElementI *e = elements.enumItem(i);
  38. if (e->getSecCount() == removalid) {
  39. elements.removeItem(e);
  40. delete e;
  41. i--;
  42. }
  43. }
  44. }
  45. void svc_collectionI::removeAllElements() {
  46. elements.deleteAll();
  47. }
  48. int svc_collectionI::getNumElements() {
  49. return elements.getNumItems();
  50. }
  51. int svc_collectionI::getNumElementsUnique() {
  52. int i=0;
  53. int n=0;
  54. const char *previous = NULL;
  55. for (i=0;i<elements.getNumItems();i++) {
  56. const char *id = elements.enumItem(i)->getId();
  57. if (!STRCASEEQLSAFE(id, previous))
  58. n++;
  59. previous = id;
  60. }
  61. return n;
  62. }
  63. CollectionElement *svc_collectionI::enumElementUnique(int n, int *ancestor) {
  64. int i=0;
  65. int _n=-1;
  66. CollectionElement *e=NULL;
  67. CollectionElement *previous = NULL;
  68. elements.sort(1);
  69. for (i=0;i<elements.getNumItems();i++) {
  70. CollectionElement *c = elements.enumItem(i);
  71. if (!STRCASEEQLSAFE(c->getId(), previous ? previous->getId() : NULL)) {
  72. if (_n == n)
  73. break;
  74. _n++;
  75. }
  76. previous = c;
  77. }
  78. if (_n == n)
  79. e = previous;
  80. else
  81. e = NULL;
  82. if (ancestor != NULL) {
  83. if (e != NULL) {
  84. int pos=-1;
  85. elements.findItem(static_cast<CollectionElementI*>(e), &pos);
  86. if (pos > 0) {
  87. CollectionElement *f = elements.enumItem(pos-1);
  88. if (!STRCASEEQLSAFE(f ? f->getId() : NULL, e->getId())) *ancestor = -1;
  89. } else {
  90. *ancestor = -1;
  91. e = NULL;
  92. }
  93. } else
  94. *ancestor = -1;
  95. }
  96. return e;
  97. }
  98. CollectionElement *svc_collectionI::enumElement(int n, int *ancestor) {
  99. CollectionElement *e = elements.enumItem(n);
  100. if (ancestor != NULL) {
  101. CollectionElement *a = elements.enumItem(n-1);
  102. if (!STRCASEEQL(a->getId(), e->getId())) *ancestor = -1;
  103. *ancestor = n-1;
  104. }
  105. return e;
  106. }
  107. CollectionElement *svc_collectionI::getElement(const char *id, int *ancestor) {
  108. int pos=-1;
  109. CollectionElement *e = elements.findLastItem(id, &pos);
  110. if (ancestor != NULL) {
  111. CollectionElement *a = elements.enumItem(pos-1);
  112. if (!STRCASEEQL(a->getId(), e->getId())) *ancestor = -1;
  113. *ancestor = pos-1;
  114. }
  115. return e;
  116. }
  117. CollectionElement *svc_collectionI::getAncestor(CollectionElement *e) {
  118. int pos=-1;
  119. CollectionElementI *ei = static_cast<CollectionElementI *>(e);
  120. elements.findItem(ei, &pos);
  121. if (pos >= 0) {
  122. pos--;
  123. if (STRCASEEQL(elements.enumItem(pos)->getId(), e->getId())) return elements.enumItem(pos);
  124. }
  125. return NULL;
  126. }
  127. CollectionElementI::CollectionElementI(svc_collectionI *col, const char *_id, skin_xmlreaderparams *p, int _seccount, const char *_path) {
  128. id = _id;
  129. for (int i=0;i<p->getNbItems();i++) {
  130. Pair < String, String > *pr = new Pair < String, String >("","");
  131. pr->a = p->getItemName(i);
  132. pr->b = p->getItemValue(i);
  133. params.addItem(pr);
  134. }
  135. seccount = _seccount;
  136. collection = col;
  137. path = _path;
  138. }
  139. CollectionElementI::~CollectionElementI() {
  140. params.deleteAll();
  141. }
  142. const char *CollectionElementI::getId() {
  143. return id;
  144. }
  145. const char *CollectionElementI::getParamValue(const char *param, CollectionElement **item){
  146. CollectionElement *e = getAncestor();
  147. const char *a = e ? e->getParamValue(param) : NULL;
  148. Pair<String, String> *p = params.findItem(param);
  149. a = p ? p->b.getValue() : a;
  150. if (item && p != NULL) *item = this;
  151. return a;
  152. }
  153. int CollectionElementI::getParamValueInt(const char *param){
  154. const char *a = getParamValue(param);
  155. return ATOI(a);
  156. }
  157. int CollectionElementI::getSecCount() {
  158. return seccount;
  159. }
  160. CollectionElement *CollectionElementI::getAncestor() {
  161. return collection->getAncestor(this);
  162. }
  163. const char *CollectionElementI::getIncludePath(const char *param/* =NULL */) {
  164. if (param == NULL) return path;
  165. CollectionElement *i;
  166. if (!getParamValue(param, &i)) return NULL;
  167. if (i != NULL)
  168. return i->getIncludePath(NULL);
  169. return NULL;
  170. }