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