chardata.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (c) 1998-2003 Thai Open Source Software Center Ltd
  2. See the file COPYING for copying permission.
  3. chardata.c
  4. */
  5. #ifdef HAVE_EXPAT_CONFIG_H
  6. #include <expat_config.h>
  7. #endif
  8. #include "minicheck.h"
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "chardata.h"
  13. static int
  14. xmlstrlen(const XML_Char *s)
  15. {
  16. int len = 0;
  17. assert(s != NULL);
  18. while (s[len] != 0)
  19. ++len;
  20. return len;
  21. }
  22. void
  23. CharData_Init(CharData *storage)
  24. {
  25. assert(storage != NULL);
  26. storage->count = -1;
  27. }
  28. void
  29. CharData_AppendString(CharData *storage, const char *s)
  30. {
  31. int maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
  32. int len;
  33. assert(s != NULL);
  34. len = strlen(s);
  35. if (storage->count < 0)
  36. storage->count = 0;
  37. if ((len + storage->count) > maxchars) {
  38. len = (maxchars - storage->count);
  39. }
  40. if (len + storage->count < (int)sizeof(storage->data)) {
  41. memcpy(storage->data + storage->count, s, len);
  42. storage->count += len;
  43. }
  44. }
  45. void
  46. CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len)
  47. {
  48. int maxchars;
  49. assert(storage != NULL);
  50. assert(s != NULL);
  51. maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
  52. if (storage->count < 0)
  53. storage->count = 0;
  54. if (len < 0)
  55. len = xmlstrlen(s);
  56. if ((len + storage->count) > maxchars) {
  57. len = (maxchars - storage->count);
  58. }
  59. if (len + storage->count < (int)sizeof(storage->data)) {
  60. memcpy(storage->data + storage->count, s,
  61. len * sizeof(storage->data[0]));
  62. storage->count += len;
  63. }
  64. }
  65. int
  66. CharData_CheckString(CharData *storage, const char *expected)
  67. {
  68. char buffer[1280];
  69. int len;
  70. int count;
  71. assert(storage != NULL);
  72. assert(expected != NULL);
  73. count = (storage->count < 0) ? 0 : storage->count;
  74. len = strlen(expected);
  75. if (len != count) {
  76. if (sizeof(XML_Char) == 1)
  77. sprintf(buffer, "wrong number of data characters:"
  78. " got %d, expected %d:\n%s", count, len, storage->data);
  79. else
  80. sprintf(buffer,
  81. "wrong number of data characters: got %d, expected %d",
  82. count, len);
  83. fail(buffer);
  84. return 0;
  85. }
  86. if (memcmp(expected, storage->data, len) != 0) {
  87. fail("got bad data bytes");
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. int
  93. CharData_CheckXMLChars(CharData *storage, const XML_Char *expected)
  94. {
  95. char buffer[1024];
  96. int len = xmlstrlen(expected);
  97. int count;
  98. assert(storage != NULL);
  99. count = (storage->count < 0) ? 0 : storage->count;
  100. if (len != count) {
  101. sprintf(buffer, "wrong number of data characters: got %d, expected %d",
  102. count, len);
  103. fail(buffer);
  104. return 0;
  105. }
  106. if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
  107. fail("got bad data bytes");
  108. return 0;
  109. }
  110. return 1;
  111. }