structdata.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000-2017 Expat development team
  10. Licensed under the MIT license:
  11. Permission is hereby granted, free of charge, to any person obtaining
  12. a copy of this software and associated documentation files (the
  13. "Software"), to deal in the Software without restriction, including
  14. without limitation the rights to use, copy, modify, merge, publish,
  15. distribute, sublicense, and/or sell copies of the Software, and to permit
  16. persons to whom the Software is furnished to do so, subject to the
  17. following conditions:
  18. The above copyright notice and this permission notice shall be included
  19. in all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  23. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  24. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  25. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  26. USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #ifdef HAVE_EXPAT_CONFIG_H
  29. # include "expat_config.h"
  30. #endif
  31. #include <assert.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include "structdata.h"
  36. #include "minicheck.h"
  37. #define STRUCT_EXTENSION_COUNT 8
  38. #ifdef XML_UNICODE_WCHAR_T
  39. # include <wchar.h>
  40. # define XML_FMT_STR "ls"
  41. # define xcstrlen(s) wcslen(s)
  42. # define xcstrcmp(s, t) wcscmp((s), (t))
  43. #else
  44. # define XML_FMT_STR "s"
  45. # define xcstrlen(s) strlen(s)
  46. # define xcstrcmp(s, t) strcmp((s), (t))
  47. #endif
  48. static XML_Char *
  49. xmlstrdup(const XML_Char *s) {
  50. size_t byte_count = (xcstrlen(s) + 1) * sizeof(XML_Char);
  51. XML_Char *dup = malloc(byte_count);
  52. assert(dup != NULL);
  53. memcpy(dup, s, byte_count);
  54. return dup;
  55. }
  56. void
  57. StructData_Init(StructData *storage) {
  58. assert(storage != NULL);
  59. storage->count = 0;
  60. storage->max_count = 0;
  61. storage->entries = NULL;
  62. }
  63. void
  64. StructData_AddItem(StructData *storage, const XML_Char *s, int data0, int data1,
  65. int data2) {
  66. StructDataEntry *entry;
  67. assert(storage != NULL);
  68. assert(s != NULL);
  69. if (storage->count == storage->max_count) {
  70. StructDataEntry *new;
  71. storage->max_count += STRUCT_EXTENSION_COUNT;
  72. new = realloc(storage->entries,
  73. storage->max_count * sizeof(StructDataEntry));
  74. assert(new != NULL);
  75. storage->entries = new;
  76. }
  77. entry = &storage->entries[storage->count];
  78. entry->str = xmlstrdup(s);
  79. entry->data0 = data0;
  80. entry->data1 = data1;
  81. entry->data2 = data2;
  82. storage->count++;
  83. }
  84. /* 'fail()' aborts the function via a longjmp, so there is no point
  85. * in returning a value from this function.
  86. */
  87. void
  88. StructData_CheckItems(StructData *storage, const StructDataEntry *expected,
  89. int count) {
  90. char buffer[1024];
  91. int i;
  92. assert(storage != NULL);
  93. assert(expected != NULL);
  94. if (count != storage->count) {
  95. sprintf(buffer, "wrong number of entries: got %d, expected %d",
  96. storage->count, count);
  97. StructData_Dispose(storage);
  98. fail(buffer);
  99. } else {
  100. for (i = 0; i < count; i++) {
  101. const StructDataEntry *got = &storage->entries[i];
  102. const StructDataEntry *want = &expected[i];
  103. assert(got != NULL);
  104. assert(want != NULL);
  105. if (xcstrcmp(got->str, want->str) != 0) {
  106. StructData_Dispose(storage);
  107. fail("structure got bad string");
  108. } else {
  109. if (got->data0 != want->data0 || got->data1 != want->data1
  110. || got->data2 != want->data2) {
  111. sprintf(buffer,
  112. "struct '%" XML_FMT_STR
  113. "' expected (%d,%d,%d), got (%d,%d,%d)",
  114. got->str, want->data0, want->data1, want->data2, got->data0,
  115. got->data1, got->data2);
  116. StructData_Dispose(storage);
  117. fail(buffer);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. void
  124. StructData_Dispose(StructData *storage) {
  125. int i;
  126. assert(storage != NULL);
  127. for (i = 0; i < storage->count; i++)
  128. free((void *)storage->entries[i].str);
  129. free(storage->entries);
  130. storage->count = 0;
  131. storage->entries = NULL;
  132. }