| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
#include <stdio.h> |
|---|
| 24 |
#include <stdlib.h> |
|---|
| 25 |
#include <locale.h> |
|---|
| 26 |
#include <err.h> |
|---|
| 27 |
|
|---|
| 28 |
#include "libswish3.h" |
|---|
| 29 |
|
|---|
| 30 |
extern int SWISH_DEBUG; |
|---|
| 31 |
|
|---|
| 32 |
static void free_name_from_hash( |
|---|
| 33 |
void *buffer, |
|---|
| 34 |
xmlChar *name |
|---|
| 35 |
); |
|---|
| 36 |
static void add_name_to_hash( |
|---|
| 37 |
void *ignored, |
|---|
| 38 |
xmlHashTablePtr nbhash, |
|---|
| 39 |
xmlChar *name |
|---|
| 40 |
); |
|---|
| 41 |
static void print_buffer( |
|---|
| 42 |
xmlBufferPtr buffer, |
|---|
| 43 |
xmlChar *label, |
|---|
| 44 |
xmlChar *name |
|---|
| 45 |
); |
|---|
| 46 |
|
|---|
| 47 |
static void |
|---|
| 48 |
add_name_to_hash( |
|---|
| 49 |
void *ignored, |
|---|
| 50 |
xmlHashTablePtr nbhash, |
|---|
| 51 |
xmlChar *name |
|---|
| 52 |
) |
|---|
| 53 |
{ |
|---|
| 54 |
|
|---|
| 55 |
if (swish_hash_exists(nbhash, name)) { |
|---|
| 56 |
SWISH_WARN("%s is already in NamedBuffer hash -- ignoring", name); |
|---|
| 57 |
return; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
if (SWISH_DEBUG == SWISH_DEBUG_NAMEDBUFFER) |
|---|
| 61 |
SWISH_DEBUG_MSG(" adding %s to NamedBuffer\n", name); |
|---|
| 62 |
|
|---|
| 63 |
swish_hash_add(nbhash, name, xmlBufferCreateSize((size_t) SWISH_BUFFER_CHUNK_SIZE)); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
static void |
|---|
| 67 |
free_name_from_hash( |
|---|
| 68 |
void *buffer, |
|---|
| 69 |
xmlChar *name |
|---|
| 70 |
) |
|---|
| 71 |
{ |
|---|
| 72 |
if (SWISH_DEBUG & SWISH_DEBUG_NAMEDBUFFER) |
|---|
| 73 |
SWISH_DEBUG_MSG(" freeing NamedBuffer %s\n", name); |
|---|
| 74 |
|
|---|
| 75 |
xmlBufferFree(buffer); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
swish_NamedBuffer * |
|---|
| 79 |
swish_init_nb( |
|---|
| 80 |
xmlHashTablePtr confhash |
|---|
| 81 |
) |
|---|
| 82 |
{ |
|---|
| 83 |
swish_NamedBuffer *nb = swish_xmalloc(sizeof(swish_NamedBuffer)); |
|---|
| 84 |
nb->stash = NULL; |
|---|
| 85 |
nb->ref_cnt = 0; |
|---|
| 86 |
nb->hash = xmlHashCreate(8); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
xmlHashScan(confhash, (xmlHashScanner)add_name_to_hash, nb->hash); |
|---|
| 90 |
|
|---|
| 91 |
return nb; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
void |
|---|
| 95 |
swish_free_nb( |
|---|
| 96 |
swish_NamedBuffer * nb |
|---|
| 97 |
) |
|---|
| 98 |
{ |
|---|
| 99 |
xmlHashFree(nb->hash, (xmlHashDeallocator)free_name_from_hash); |
|---|
| 100 |
|
|---|
| 101 |
if (nb->ref_cnt != 0) { |
|---|
| 102 |
SWISH_WARN("freeing NamedBuffer with ref_cnt != 0 (%d)", nb->ref_cnt); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
if (nb->stash != NULL) |
|---|
| 106 |
SWISH_WARN("freeing NamedBuffer with non-null stash"); |
|---|
| 107 |
|
|---|
| 108 |
swish_xfree(nb); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
static void |
|---|
| 112 |
print_buffer( |
|---|
| 113 |
xmlBufferPtr buffer, |
|---|
| 114 |
xmlChar *label, |
|---|
| 115 |
xmlChar *name |
|---|
| 116 |
) |
|---|
| 117 |
{ |
|---|
| 118 |
const xmlChar *substr; |
|---|
| 119 |
const xmlChar *buf; |
|---|
| 120 |
int sub_len; |
|---|
| 121 |
|
|---|
| 122 |
SWISH_DEBUG_MSG("%d %s:\n<%s>%s</%s>", xmlBufferLength(buffer), |
|---|
| 123 |
label, name, xmlBufferContent(buffer), name); |
|---|
| 124 |
|
|---|
| 125 |
buf = xmlBufferContent(buffer); |
|---|
| 126 |
while ((substr = xmlStrstr(buf, (const xmlChar *)SWISH_TOKENPOS_BUMPER)) != NULL) { |
|---|
| 127 |
sub_len = substr - buf; |
|---|
| 128 |
SWISH_DEBUG_MSG("%d <%s> substr: %s", sub_len, name, xmlStrsub(buf, 0, sub_len) ); |
|---|
| 129 |
buf = substr + 1; |
|---|
| 130 |
} |
|---|
| 131 |
if (buf != NULL) { |
|---|
| 132 |
SWISH_DEBUG_MSG("%d <%s> substr: %s", xmlStrlen(buf), name, buf ); |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
void |
|---|
| 137 |
swish_debug_nb( |
|---|
| 138 |
swish_NamedBuffer * nb, |
|---|
| 139 |
xmlChar *label |
|---|
| 140 |
) |
|---|
| 141 |
{ |
|---|
| 142 |
xmlHashScan(nb->hash, (xmlHashScanner)print_buffer, label); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
void |
|---|
| 146 |
swish_add_buf_to_nb( |
|---|
| 147 |
swish_NamedBuffer * nb, |
|---|
| 148 |
xmlChar *name, |
|---|
| 149 |
xmlBufferPtr buf, |
|---|
| 150 |
xmlChar *joiner, |
|---|
| 151 |
boolean cleanwsp, |
|---|
| 152 |
boolean autovivify |
|---|
| 153 |
) |
|---|
| 154 |
{ |
|---|
| 155 |
swish_add_str_to_nb(nb, name, (xmlChar *)xmlBufferContent(buf), xmlBufferLength(buf), |
|---|
| 156 |
joiner, cleanwsp, autovivify); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
void |
|---|
| 160 |
swish_add_str_to_nb( |
|---|
| 161 |
swish_NamedBuffer * nb, |
|---|
| 162 |
xmlChar *name, |
|---|
| 163 |
xmlChar *str, |
|---|
| 164 |
unsigned int len, |
|---|
| 165 |
xmlChar *joiner, |
|---|
| 166 |
boolean cleanwsp, |
|---|
| 167 |
boolean autovivify |
|---|
| 168 |
) |
|---|
| 169 |
{ |
|---|
| 170 |
xmlChar *nowhitesp; |
|---|
| 171 |
xmlBufferPtr buf = swish_hash_fetch(nb->hash, name); |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
if (swish_str_all_ws(str)) { |
|---|
| 175 |
if (SWISH_DEBUG & SWISH_DEBUG_NAMEDBUFFER) |
|---|
| 176 |
SWISH_DEBUG_MSG("skipping all whitespace string '%s'", str); |
|---|
| 177 |
|
|---|
| 178 |
return; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
if (!buf) { |
|---|
| 182 |
if (autovivify) { |
|---|
| 183 |
|
|---|
| 184 |
add_name_to_hash(NULL, nb->hash, name); |
|---|
| 185 |
buf = swish_hash_fetch(nb->hash, name); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
if (!buf) |
|---|
| 189 |
SWISH_CROAK("%s is not a named buffer", name); |
|---|
| 190 |
|
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
if (xmlBufferLength(buf)) { |
|---|
| 195 |
swish_append_buffer(buf, joiner, xmlStrlen(joiner)); |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
if (cleanwsp) { |
|---|
| 199 |
|
|---|
| 200 |
nowhitesp = swish_str_skip_ws(str); |
|---|
| 201 |
swish_str_trim_ws(nowhitesp); |
|---|
| 202 |
|
|---|
| 203 |
swish_append_buffer(buf, nowhitesp, xmlStrlen(nowhitesp)); |
|---|
| 204 |
} |
|---|
| 205 |
else { |
|---|
| 206 |
if (SWISH_DEBUG & SWISH_DEBUG_NAMEDBUFFER) |
|---|
| 207 |
SWISH_DEBUG_MSG("adding '%s' to buffer '%s'", str, name); |
|---|
| 208 |
swish_append_buffer(buf, str, len); |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
void |
|---|
| 214 |
swish_append_buffer( |
|---|
| 215 |
xmlBufferPtr buf, |
|---|
| 216 |
xmlChar *txt, |
|---|
| 217 |
int txtlen |
|---|
| 218 |
) |
|---|
| 219 |
{ |
|---|
| 220 |
int ret; |
|---|
| 221 |
|
|---|
| 222 |
if (txtlen == 0) |
|---|
| 223 |
|
|---|
| 224 |
return; |
|---|
| 225 |
|
|---|
| 226 |
if (buf == NULL) { |
|---|
| 227 |
SWISH_CROAK("bad news. buf ptr is NULL"); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
ret = xmlBufferAdd(buf, (const xmlChar *)txt, txtlen); |
|---|
| 231 |
if (ret) { |
|---|
| 232 |
SWISH_CROAK("problem adding \n>>%s<<\n length %d to buffer. Err: %d", txt, txtlen, |
|---|
| 233 |
ret); |
|---|
| 234 |
} |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
xmlChar * |
|---|
| 238 |
swish_nb_get_value( |
|---|
| 239 |
swish_NamedBuffer * nb, |
|---|
| 240 |
xmlChar *key |
|---|
| 241 |
) |
|---|
| 242 |
{ |
|---|
| 243 |
xmlBufferPtr buf; |
|---|
| 244 |
buf = swish_hash_fetch(nb->hash, key); |
|---|
| 245 |
return (xmlChar *)xmlBufferContent(buf); |
|---|
| 246 |
} |
|---|