1
0

XMLParameters.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "XMLParameters.h"
  2. #include <wchar.h>
  3. #ifdef __APPLE__
  4. #include <Carbon/Carbon.h>
  5. int _wcsicmp(const wchar_t *str1, const wchar_t *str2)
  6. {
  7. CFStringRef cfstr1 = CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str1, wcslen(str1)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
  8. CFStringRef cfstr2 = CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8 *)str2, wcslen(str2)*sizeof(wchar_t), kCFStringEncodingUTF32, false);
  9. int result = CFStringCompare(cfstr1, cfstr2, kCFCompareCaseInsensitive);
  10. CFRelease(cfstr1);
  11. CFRelease(cfstr2);
  12. return result;
  13. }
  14. #endif
  15. XMLParameters::XMLParameters(const wchar_t **_parameters)
  16. : parameters(_parameters), numParameters(0), numParametersCalculated(false)
  17. {
  18. }
  19. void XMLParameters::CountTo(int x)
  20. {
  21. if (numParametersCalculated || x < numParameters)
  22. return;
  23. while (1)
  24. {
  25. if (parameters[numParameters*2] == 0)
  26. {
  27. numParametersCalculated=true;
  28. return;
  29. }
  30. numParameters++;
  31. if (numParameters == x)
  32. return;
  33. }
  34. }
  35. void XMLParameters::Count()
  36. {
  37. if (numParametersCalculated)
  38. return;
  39. while (1)
  40. {
  41. if (parameters[numParameters*2] == 0)
  42. {
  43. numParametersCalculated=true;
  44. return;
  45. }
  46. numParameters++;
  47. }
  48. }
  49. const wchar_t *XMLParameters::GetItemName(int i)
  50. {
  51. CountTo(i);
  52. if (i < numParameters)
  53. return parameters[i*2];
  54. else
  55. return 0;
  56. }
  57. const wchar_t *XMLParameters::GetItemValueIndex(int i)
  58. {
  59. CountTo(i);
  60. if (i < numParameters)
  61. return parameters[i*2+1];
  62. else
  63. return 0;
  64. }
  65. int XMLParameters::GetNumItems()
  66. {
  67. Count();
  68. return numParameters;
  69. }
  70. const wchar_t *XMLParameters::GetItemValue(const wchar_t *name)
  71. {
  72. int i=0;
  73. while(1)
  74. {
  75. CountTo(i+1);
  76. if (i<numParameters)
  77. {
  78. if (!_wcsicmp(name, parameters[i*2]))
  79. return parameters[i*2+1];
  80. }
  81. else
  82. return 0;
  83. i++;
  84. };
  85. }
  86. int XMLParameters::GetItemValueInt(const wchar_t *name, int def)
  87. {
  88. const wchar_t *val = GetItemValue(name);
  89. if (val && *val)
  90. return wcstol(val, 0, 10);
  91. else
  92. return def;
  93. }
  94. const wchar_t *XMLParameters::EnumItemValues(const wchar_t *name, int nb)
  95. {
  96. int i=0;
  97. while(1)
  98. {
  99. CountTo(i+1);
  100. if (i<numParameters)
  101. {
  102. if (!_wcsicmp(name, parameters[i*2]) && nb--)
  103. return parameters[i*2+1];
  104. }
  105. else
  106. return 0;
  107. i++;
  108. };
  109. }
  110. #define CBCLASS XMLParameters
  111. START_DISPATCH;
  112. CB(XMLREADERPARAMS_GETITEMNAME, GetItemName)
  113. CB(XMLREADERPARAMS_GETITEMVALUE, GetItemValueIndex)
  114. CB(XMLREADERPARAMS_GETITEMVALUE2, GetItemValue)
  115. CB(XMLREADERPARAMS_ENUMITEMVALUES, EnumItemValues)
  116. CB(XMLREADERPARAMS_GETNBITEMS, GetNumItems)
  117. END_DISPATCH;
  118. #undef CBCLASS