ParamList.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "ParamList.h"
  2. ParamList::~ParamList()
  3. {
  4. //parms_list.deleteAll();
  5. for (auto obj : parms_list)
  6. {
  7. delete obj;
  8. }
  9. parms_list.clear();
  10. }
  11. const wchar_t *ParamList::getItemName(int i)
  12. {
  13. if(((size_t)i)>=getNbItems())
  14. return L"";
  15. return
  16. parms_list[i]->parm;
  17. }
  18. const wchar_t *ParamList::getItemValueIndex(int i)
  19. {
  20. if(((size_t)i)>=getNbItems())
  21. return L"";
  22. return
  23. parms_list[i]->value;
  24. }
  25. const wchar_t *ParamList::getItemValue(const wchar_t *name)
  26. {
  27. for(size_t i=0;i!=getNbItems();i++)
  28. if(!WCSICMP(parms_list[i]->parm, name))
  29. return parms_list[i]->value;
  30. return NULL;
  31. }
  32. const wchar_t *ParamList::enumItemValues(const wchar_t *name, int nb)
  33. {
  34. int f=0;
  35. for(size_t i=0;i!=getNbItems();i++)
  36. if(!WCSICMP(parms_list[i]->parm, name))
  37. if(f==nb)
  38. return parms_list[i]->value;
  39. else f++;
  40. return NULL;
  41. }
  42. int ParamList::getItemValueInt(const wchar_t *name, int def)
  43. {
  44. for(size_t i=0;i!=getNbItems();i++)
  45. if(!WCSICMP(parms_list[i]->parm, name))
  46. {
  47. // TODO: benski> do we want to return "def" when there's an empty value?
  48. return WTOI(parms_list[i]->value);
  49. }
  50. return def;
  51. }
  52. size_t ParamList::getNbItems()
  53. {
  54. return parms_list.size();
  55. }
  56. void ParamList::addItem(const wchar_t *parm, const wchar_t *value)
  57. {
  58. parms_struct *p= new parms_struct;
  59. p->parm = WCSDUP(parm);
  60. p->ownValue = true;
  61. p->value = value;
  62. parms_list.push_back(p);
  63. }
  64. void ParamList::removeItem(const wchar_t *parm)
  65. {
  66. for (size_t i=0; i!=parms_list.size(); i++)
  67. {
  68. parms_struct *s = parms_list[i];
  69. if (!WCSICMP(parm, s->parm))
  70. {
  71. delete s;
  72. parms_list.erase(parms_list.begin() + i);
  73. i--;
  74. }
  75. }
  76. }
  77. void ParamList::replaceItem(const wchar_t *parm, const wchar_t *value)
  78. {
  79. if (!value)
  80. {
  81. removeItem(parm);
  82. return;
  83. }
  84. StringW s = value; // if we were passed our current value's pointer ...
  85. const wchar_t *curval = getItemValue(parm);
  86. if (s.isequal(value) && curval) return; // (hey, if we're replacing with same value, forget about it, but only if we did have that value, because isequal will return true if curval is NULL and we pass it ("") )
  87. removeItem(parm); // ... then this call would make the value param invalid ...
  88. addItem(parm, s); // ... so we're sending a saved buffer instead
  89. }
  90. int ParamList::findItem(const wchar_t *parm)
  91. {
  92. for(size_t i=0;i!=getNbItems();i++)
  93. if(!WCSICMP(parms_list[i]->parm, parm))
  94. return (int)i;
  95. return -1;
  96. }
  97. #define CBCLASS ParamList
  98. START_DISPATCH;
  99. CB(XMLREADERPARAMS_GETITEMNAME, getItemName)
  100. CB(XMLREADERPARAMS_GETITEMVALUE, getItemValueIndex)
  101. CB(XMLREADERPARAMS_GETITEMVALUE2, getItemValue)
  102. CB(XMLREADERPARAMS_ENUMITEMVALUES, enumItemValues)
  103. CB(XMLREADERPARAMS_GETITEMVALUEINT, getItemValueInt)
  104. CB(XMLREADERPARAMS_GETNBITEMS, getNbItems)
  105. END_DISPATCH;
  106. #undef CBCLASS