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