xmlparamsi.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <precomp.h>
  2. #include "xmlparamsi.h"
  3. #include <bfc/wasabi_std.h>
  4. XmlReaderParamsI::~XmlReaderParamsI()
  5. {
  6. parms_list.deleteAll();
  7. }
  8. const wchar_t *XmlReaderParamsI::getItemName(int i)
  9. {
  10. if(i>getNbItems())
  11. return L"";
  12. return
  13. parms_list[i]->parm;
  14. }
  15. const wchar_t *XmlReaderParamsI::getItemValue(int i)
  16. {
  17. if(i>getNbItems())
  18. return L"";
  19. return
  20. parms_list[i]->value;
  21. }
  22. const wchar_t *XmlReaderParamsI::getItemValue(const wchar_t *name)
  23. {
  24. for(int i=0;i<getNbItems();i++)
  25. if(!WCSICMP(parms_list[i]->parm, name))
  26. return parms_list[i]->value;
  27. return NULL;
  28. }
  29. const wchar_t *XmlReaderParamsI::enumItemValues(const wchar_t *name, int nb)
  30. {
  31. int f=0;
  32. for(int i=0;i<getNbItems();i++)
  33. if(!WCSICMP(parms_list[i]->parm, name))
  34. if(f==nb)
  35. return parms_list[i]->value;
  36. else f++;
  37. return NULL;
  38. }
  39. int XmlReaderParamsI::getItemValueInt(const wchar_t *name, int def)
  40. {
  41. for(int i=0;i<getNbItems();i++)
  42. if(!WCSICMP(parms_list[i]->parm, name))
  43. {
  44. // TODO: benski> do we want to return "def" when there's an empty value?
  45. return WTOI(parms_list[i]->value);
  46. }
  47. return def;
  48. }
  49. int XmlReaderParamsI::getNbItems()
  50. {
  51. return parms_list.getNumItems();
  52. }
  53. void XmlReaderParamsI::addItem(const wchar_t *parm, const wchar_t *value)
  54. {
  55. parms_struct *p= new parms_struct;
  56. p->parm = WCSDUP(parm);
  57. p->ownValue = true;
  58. p->value = value;
  59. parms_list.addItem(p);
  60. }
  61. void XmlReaderParamsI::removeItem(const wchar_t *parm)
  62. {
  63. for (int i=0;i<parms_list.getNumItems();i++)
  64. {
  65. parms_struct *s = parms_list.enumItem(i);
  66. if (!WCSICMP(parm, s->parm))
  67. {
  68. delete s;
  69. parms_list.removeByPos(i);
  70. i--;
  71. }
  72. }
  73. }
  74. void XmlReaderParamsI::replaceItem(const wchar_t *parm, const wchar_t *value)
  75. {
  76. if (!value)
  77. {
  78. removeItem(parm);
  79. return;
  80. }
  81. StringW s = value; // if we were passed our current value's pointer ...
  82. const wchar_t *curval = getItemValue(parm);
  83. 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 ("") )
  84. removeItem(parm); // ... then this call would make the value param invalid ...
  85. addItem(parm, s); // ... so we're sending a saved buffer instead
  86. }
  87. int XmlReaderParamsI::findItem(const wchar_t *parm)
  88. {
  89. for(int i=0;i<getNbItems();i++)
  90. if(!WCSICMP(parms_list[i]->parm, parm))
  91. return i;
  92. return -1;
  93. }
  94. // calling this will destroy your String... here for optimization ...
  95. void XmlReaderParamsI::addItemSwapValue(const wchar_t *parm, StringW &value)
  96. {
  97. parms_struct *p= new parms_struct;
  98. p->parm = parm;
  99. p->value.swap(&value);
  100. parms_list.addItem(p);
  101. }