chardata.c revision 302408
192906Smarkm/* Copyright (c) 1998-2003 Thai Open Source Software Center Ltd 292906Smarkm See the file COPYING for copying permission. 322347Spst 422347Spst chardata.c 522347Spst*/ 622347Spst 722347Spst#ifdef HAVE_EXPAT_CONFIG_H 822347Spst#include <expat_config.h> 922347Spst#endif 1022347Spst#include "minicheck.h" 1122347Spst 1222347Spst#include <assert.h> 1322347Spst#include <stdio.h> 1422347Spst#include <string.h> 1522347Spst 1622347Spst#include "chardata.h" 1722347Spst 1822347Spst 1922347Spststatic int 2022347Spstxmlstrlen(const XML_Char *s) 2122347Spst{ 2222347Spst int len = 0; 2322347Spst assert(s != NULL); 2422347Spst while (s[len] != 0) 2522347Spst ++len; 2622347Spst return len; 2722347Spst} 2822347Spst 2922347Spst 3022347Spstvoid 3122347SpstCharData_Init(CharData *storage) 3222347Spst{ 3322347Spst assert(storage != NULL); 3422347Spst storage->count = -1; 3522347Spst} 3622347Spst 3722347Spstvoid 3822347SpstCharData_AppendString(CharData *storage, const char *s) 3922347Spst{ 4022347Spst int maxchars = sizeof(storage->data) / sizeof(storage->data[0]); 4122347Spst int len; 4222347Spst 4322347Spst assert(s != NULL); 4422347Spst len = strlen(s); 4522347Spst if (storage->count < 0) 4622347Spst storage->count = 0; 4722347Spst if ((len + storage->count) > maxchars) { 4822347Spst len = (maxchars - storage->count); 4922347Spst } 5022347Spst if (len + storage->count < (int)sizeof(storage->data)) { 5122347Spst memcpy(storage->data + storage->count, s, len); 5222347Spst storage->count += len; 5322347Spst } 5422347Spst} 5522347Spst 5622347Spstvoid 5722347SpstCharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) 5822347Spst{ 5922347Spst int maxchars; 6022347Spst 6122347Spst assert(storage != NULL); 6222347Spst assert(s != NULL); 6322347Spst maxchars = sizeof(storage->data) / sizeof(storage->data[0]); 6422347Spst if (storage->count < 0) 6522347Spst storage->count = 0; 6622347Spst if (len < 0) 6722347Spst len = xmlstrlen(s); 6822347Spst if ((len + storage->count) > maxchars) { 6922347Spst len = (maxchars - storage->count); 7022347Spst } 7122347Spst if (len + storage->count < (int)sizeof(storage->data)) { 7222347Spst memcpy(storage->data + storage->count, s, 7322347Spst len * sizeof(storage->data[0])); 7422347Spst storage->count += len; 7522347Spst } 7622347Spst} 7722347Spst 7892906Smarkmint 7959118SkrisCharData_CheckString(CharData *storage, const char *expected) 8092906Smarkm{ 8159118Skris char buffer[1280]; 8292906Smarkm int len; 8359118Skris int count; 8492906Smarkm 8529964Sache assert(storage != NULL); 8692906Smarkm assert(expected != NULL); 8729964Sache count = (storage->count < 0) ? 0 : storage->count; 8892906Smarkm len = strlen(expected); 8992906Smarkm if (len != count) { 9029964Sache if (sizeof(XML_Char) == 1) 9192906Smarkm sprintf(buffer, "wrong number of data characters:" 9229964Sache " got %d, expected %d:\n%s", count, len, storage->data); 9392906Smarkm else 9429964Sache sprintf(buffer, 9592906Smarkm "wrong number of data characters: got %d, expected %d", 9622347Spst count, len); 9792906Smarkm fail(buffer); 9822347Spst return 0; 9992906Smarkm } 10092906Smarkm if (memcmp(expected, storage->data, len) != 0) { 10192906Smarkm fail("got bad data bytes"); 10292906Smarkm return 0; 10392906Smarkm } 10492906Smarkm return 1; 10592906Smarkm} 10692906Smarkm 10792906Smarkmint 10892906SmarkmCharData_CheckXMLChars(CharData *storage, const XML_Char *expected) 10992906Smarkm{ 11022347Spst char buffer[1024]; 11122347Spst int len = xmlstrlen(expected); 11292906Smarkm int count; 11322347Spst 11492906Smarkm assert(storage != NULL); 11592906Smarkm count = (storage->count < 0) ? 0 : storage->count; 11692906Smarkm if (len != count) { 11792906Smarkm sprintf(buffer, "wrong number of data characters: got %d, expected %d", 11892906Smarkm count, len); 11992906Smarkm fail(buffer); 12022347Spst return 0; 12192906Smarkm } 12292906Smarkm if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) { 12322347Spst fail("got bad data bytes"); 12492906Smarkm return 0; 12592906Smarkm } 12622347Spst return 1; 12792906Smarkm} 12822347Spst