structdata.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2017 Rhodri James <[email protected]>
  9. Copyright (c) 2017-2021 Sebastian Pipping <[email protected]>
  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. #include "expat_config.h"
  29. #include <assert.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "structdata.h"
  34. #include "minicheck.h"
  35. #define STRUCT_EXTENSION_COUNT 8
  36. #ifdef XML_UNICODE_WCHAR_T
  37. # include <wchar.h>
  38. # define XML_FMT_STR "ls"
  39. # define xcstrlen(s) wcslen(s)
  40. # define xcstrcmp(s, t) wcscmp((s), (t))
  41. #else
  42. # define XML_FMT_STR "s"
  43. # define xcstrlen(s) strlen(s)
  44. # define xcstrcmp(s, t) strcmp((s), (t))
  45. #endif
  46. static XML_Char *
  47. xmlstrdup(const XML_Char *s) {
  48. size_t byte_count = (xcstrlen(s) + 1) * sizeof(XML_Char);
  49. XML_Char *dup = malloc(byte_count);
  50. assert(dup != NULL);
  51. memcpy(dup, s, byte_count);
  52. return dup;
  53. }
  54. void
  55. StructData_Init(StructData *storage) {
  56. assert(storage != NULL);
  57. storage->count = 0;
  58. storage->max_count = 0;
  59. storage->entries = NULL;
  60. }
  61. void
  62. StructData_AddItem(StructData *storage, const XML_Char *s, int data0, int data1,
  63. int data2) {
  64. StructDataEntry *entry;
  65. assert(storage != NULL);
  66. assert(s != NULL);
  67. if (storage->count == storage->max_count) {
  68. StructDataEntry *new;
  69. storage->max_count += STRUCT_EXTENSION_COUNT;
  70. new = realloc(storage->entries,
  71. storage->max_count * sizeof(StructDataEntry));
  72. assert(new != NULL);
  73. storage->entries = new;
  74. }
  75. entry = &storage->entries[storage->count];
  76. entry->str = xmlstrdup(s);
  77. entry->data0 = data0;
  78. entry->data1 = data1;
  79. entry->data2 = data2;
  80. storage->count++;
  81. }
  82. /* 'fail()' aborts the function via a longjmp, so there is no point
  83. * in returning a value from this function.
  84. */
  85. void
  86. StructData_CheckItems(StructData *storage, const StructDataEntry *expected,
  87. int count) {
  88. char buffer[1024];
  89. int i;
  90. assert(storage != NULL);
  91. assert(expected != NULL);
  92. if (count != storage->count) {
  93. sprintf(buffer, "wrong number of entries: got %d, expected %d",
  94. storage->count, count);
  95. StructData_Dispose(storage);
  96. fail(buffer);
  97. } else {
  98. for (i = 0; i < count; i++) {
  99. const StructDataEntry *got = &storage->entries[i];
  100. const StructDataEntry *want = &expected[i];
  101. assert(got != NULL);
  102. assert(want != NULL);
  103. if (xcstrcmp(got->str, want->str) != 0) {
  104. StructData_Dispose(storage);
  105. fail("structure got bad string");
  106. } else {
  107. if (got->data0 != want->data0 || got->data1 != want->data1
  108. || got->data2 != want->data2) {
  109. sprintf(buffer,
  110. "struct '%" XML_FMT_STR
  111. "' expected (%d,%d,%d), got (%d,%d,%d)",
  112. got->str, want->data0, want->data1, want->data2, got->data0,
  113. got->data1, got->data2);
  114. StructData_Dispose(storage);
  115. fail(buffer);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. void
  122. StructData_Dispose(StructData *storage) {
  123. int i;
  124. assert(storage != NULL);
  125. for (i = 0; i < storage->count; i++)
  126. free((void *)storage->entries[i].str);
  127. free(storage->entries);
  128. storage->count = 0;
  129. storage->entries = NULL;
  130. }