chardata.c 3.0 KB

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