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