| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
#ifdef HAVE_CONFIG_H |
|---|
| 30 |
# include "config.h" |
|---|
| 31 |
#endif |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
#include <vlc_plugin.h> |
|---|
| 35 |
|
|---|
| 36 |
#include "vlc_xml.h" |
|---|
| 37 |
#include "vlc_block.h" |
|---|
| 38 |
#include "vlc_stream.h" |
|---|
| 39 |
|
|---|
| 40 |
#include <ctype.h> |
|---|
| 41 |
#include <stdarg.h> |
|---|
| 42 |
|
|---|
| 43 |
#include <assert.h> |
|---|
| 44 |
|
|---|
| 45 |
#undef XTAG_DEBUG |
|---|
| 46 |
|
|---|
| 47 |
typedef struct _XList |
|---|
| 48 |
{ |
|---|
| 49 |
struct _XList *prev; |
|---|
| 50 |
struct _XList *next; |
|---|
| 51 |
void *data; |
|---|
| 52 |
} XList; |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
typedef struct _XTag |
|---|
| 61 |
{ |
|---|
| 62 |
char *name; |
|---|
| 63 |
char *pcdata; |
|---|
| 64 |
struct _XTag *parent; |
|---|
| 65 |
XList *attributes; |
|---|
| 66 |
XList *children; |
|---|
| 67 |
XList *current_child; |
|---|
| 68 |
} XTag; |
|---|
| 69 |
|
|---|
| 70 |
typedef struct _XAttribute |
|---|
| 71 |
{ |
|---|
| 72 |
char *name; |
|---|
| 73 |
char *value; |
|---|
| 74 |
} XAttribute; |
|---|
| 75 |
|
|---|
| 76 |
typedef struct _XTagParser |
|---|
| 77 |
{ |
|---|
| 78 |
int valid; |
|---|
| 79 |
XTag *current_tag; |
|---|
| 80 |
char *start; |
|---|
| 81 |
char *end; |
|---|
| 82 |
} XTagParser; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
static int Open ( vlc_object_t * ); |
|---|
| 88 |
static void Close( vlc_object_t * ); |
|---|
| 89 |
|
|---|
| 90 |
vlc_module_begin(); |
|---|
| 91 |
set_description( N_("Simple XML Parser") ); |
|---|
| 92 |
set_capability( "xml", 5 ); |
|---|
| 93 |
set_callbacks( Open, Close ); |
|---|
| 94 |
vlc_module_end(); |
|---|
| 95 |
|
|---|
| 96 |
struct xml_reader_sys_t |
|---|
| 97 |
{ |
|---|
| 98 |
XTag *p_root; |
|---|
| 99 |
XTag *p_curtag; |
|---|
| 100 |
XList *p_curattr; |
|---|
| 101 |
bool b_endtag; |
|---|
| 102 |
}; |
|---|
| 103 |
|
|---|
| 104 |
static xml_reader_t *ReaderCreate( xml_t *, stream_t * ); |
|---|
| 105 |
static void ReaderDelete( xml_reader_t * ); |
|---|
| 106 |
static int ReaderRead( xml_reader_t * ); |
|---|
| 107 |
static int ReaderNodeType( xml_reader_t * ); |
|---|
| 108 |
static char *ReaderName( xml_reader_t * ); |
|---|
| 109 |
static char *ReaderValue( xml_reader_t * ); |
|---|
| 110 |
static int ReaderNextAttr( xml_reader_t * ); |
|---|
| 111 |
|
|---|
| 112 |
static int ReaderUseDTD ( xml_reader_t *, bool ); |
|---|
| 113 |
|
|---|
| 114 |
static void CatalogLoad( xml_t *, const char * ); |
|---|
| 115 |
static void CatalogAdd( xml_t *, const char *, const char *, const char * ); |
|---|
| 116 |
|
|---|
| 117 |
static XTag *xtag_new_parse( const char *, int ); |
|---|
| 118 |
static char *xtag_get_name( XTag * ); |
|---|
| 119 |
#if 0 |
|---|
| 120 |
static char *xtag_get_pcdata( XTag * ); |
|---|
| 121 |
static char *xtag_get_attribute( XTag *, char * ); |
|---|
| 122 |
#endif |
|---|
| 123 |
static XTag *xtag_first_child( XTag *, char * ); |
|---|
| 124 |
static XTag *xtag_next_child( XTag *, char * ); |
|---|
| 125 |
static XTag *xtag_free( XTag * ); |
|---|
| 126 |
static int xtag_snprint( char *, int, XTag * ); |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
static int Open( vlc_object_t *p_this ) |
|---|
| 132 |
{ |
|---|
| 133 |
xml_t *p_xml = (xml_t *)p_this; |
|---|
| 134 |
|
|---|
| 135 |
p_xml->pf_reader_create = ReaderCreate; |
|---|
| 136 |
p_xml->pf_reader_delete = ReaderDelete; |
|---|
| 137 |
|
|---|
| 138 |
p_xml->pf_catalog_load = CatalogLoad; |
|---|
| 139 |
p_xml->pf_catalog_add = CatalogAdd; |
|---|
| 140 |
|
|---|
| 141 |
return VLC_SUCCESS; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
static void Close( vlc_object_t *p_this ) |
|---|
| 148 |
{ |
|---|
| 149 |
VLC_UNUSED(p_this); |
|---|
| 150 |
return; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
static void CatalogLoad( xml_t *p_xml, const char *psz_filename ) |
|---|
| 157 |
{ |
|---|
| 158 |
VLC_UNUSED(psz_filename); |
|---|
| 159 |
msg_Dbg( p_xml, "catalog support not implemented" ); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
static void CatalogAdd( xml_t *p_xml, const char *psz_arg1, |
|---|
| 163 |
const char *psz_arg2, const char *psz_filename ) |
|---|
| 164 |
{ |
|---|
| 165 |
VLC_UNUSED(p_xml); VLC_UNUSED(psz_arg1); VLC_UNUSED(psz_arg2); |
|---|
| 166 |
VLC_UNUSED(psz_filename); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *s ) |
|---|
| 173 |
{ |
|---|
| 174 |
xml_reader_t *p_reader; |
|---|
| 175 |
char *p_buffer, *p_new; |
|---|
| 176 |
int i_size, i_pos = 0, i_buffer = 2048; |
|---|
| 177 |
XTag *p_root; |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
p_buffer = malloc( i_buffer ); |
|---|
| 181 |
if( p_buffer == NULL ) |
|---|
| 182 |
return NULL; |
|---|
| 183 |
|
|---|
| 184 |
while( ( i_size = stream_Read( s, &p_buffer[i_pos], 2048 ) ) == 2048 ) |
|---|
| 185 |
{ |
|---|
| 186 |
i_pos += i_size; |
|---|
| 187 |
i_buffer += i_size; |
|---|
| 188 |
p_new = realloc( p_buffer, i_buffer ); |
|---|
| 189 |
if( !p_new ) |
|---|
| 190 |
{ |
|---|
| 191 |
free( p_buffer ); |
|---|
| 192 |
return NULL; |
|---|
| 193 |
} |
|---|
| 194 |
p_buffer = p_new; |
|---|
| 195 |
} |
|---|
| 196 |
p_buffer[ i_pos + i_size ] = 0; |
|---|
| 197 |
|
|---|
| 198 |
if( i_pos + i_size == 0 ) |
|---|
| 199 |
{ |
|---|
| 200 |
msg_Dbg( p_xml, "empty XML" ); |
|---|
| 201 |
free( p_buffer ); |
|---|
| 202 |
return 0; |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
p_root = xtag_new_parse( p_buffer, i_buffer ); |
|---|
| 206 |
if( !p_root ) |
|---|
| 207 |
{ |
|---|
| 208 |
msg_Warn( p_xml, "couldn't parse XML" ); |
|---|
| 209 |
free( p_buffer ); |
|---|
| 210 |
return 0; |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
free( p_buffer ); |
|---|
| 214 |
p_reader = malloc( sizeof(xml_reader_t) ); |
|---|
| 215 |
p_reader->p_sys = malloc( sizeof(xml_reader_sys_t) ); |
|---|
| 216 |
p_reader->p_sys->p_root = p_root; |
|---|
| 217 |
p_reader->p_sys->p_curtag = NULL; |
|---|
| 218 |
p_reader->p_sys->p_curattr = NULL; |
|---|
| 219 |
p_reader->p_sys->b_endtag = false; |
|---|
| 220 |
p_reader->p_xml = p_xml; |
|---|
| 221 |
|
|---|
| 222 |
p_reader->pf_read = ReaderRead; |
|---|
| 223 |
p_reader->pf_node_type = ReaderNodeType; |
|---|
| 224 |
p_reader->pf_name = ReaderName; |
|---|
| 225 |
p_reader->pf_value = ReaderValue; |
|---|
| 226 |
p_reader->pf_next_attr = ReaderNextAttr; |
|---|
| 227 |
p_reader->pf_use_dtd = ReaderUseDTD; |
|---|
| 228 |
|
|---|
| 229 |
return p_reader; |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
static void ReaderDelete( xml_reader_t *p_reader ) |
|---|
| 233 |
{ |
|---|
| 234 |
xtag_free( p_reader->p_sys->p_root ); |
|---|
| 235 |
free( p_reader->p_sys ); |
|---|
| 236 |
free( p_reader ); |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use ) |
|---|
| 240 |
{ |
|---|
| 241 |
VLC_UNUSED(p_reader); VLC_UNUSED(b_use); |
|---|
| 242 |
return VLC_EGENERIC; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
static int ReaderRead( xml_reader_t *p_reader ) |
|---|
| 246 |
{ |
|---|
| 247 |
XTag *p_child; |
|---|
| 248 |
|
|---|
| 249 |
if( !p_reader->p_sys->p_curtag ) |
|---|
| 250 |
{ |
|---|
| 251 |
p_reader->p_sys->p_curtag = p_reader->p_sys->p_root; |
|---|
| 252 |
return 1; |
|---|
| 253 |
} |
|---|
| 254 |
|
|---|
| 255 |
while( 1 ) |
|---|
| 256 |
{ |
|---|
| 257 |
if( (p_child = xtag_next_child( p_reader->p_sys->p_curtag, 0 )) ) |
|---|
| 258 |
{ |
|---|
| 259 |
p_reader->p_sys->p_curtag = p_child; |
|---|
| 260 |
p_reader->p_sys->p_curattr = 0; |
|---|
| 261 |
p_reader->p_sys->b_endtag = false; |
|---|
| 262 |
return 1; |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
if( p_reader->p_sys->p_curtag->name && |
|---|
| 266 |
!p_reader->p_sys->b_endtag ) |
|---|
| 267 |
{ |
|---|
| 268 |
p_reader->p_sys->b_endtag = true; |
|---|
| 269 |
return 1; |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
p_reader->p_sys->b_endtag = false; |
|---|
| 273 |
if( !p_reader->p_sys->p_curtag->parent ) return 0; |
|---|
| 274 |
p_reader->p_sys->p_curtag = p_reader->p_sys->p_curtag->parent; |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
return 0; |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
static int ReaderNodeType( xml_reader_t *p_reader ) |
|---|
| 281 |
{ |
|---|
| 282 |
if( p_reader->p_sys->p_curtag->name && |
|---|
| 283 |
p_reader->p_sys->b_endtag ) return XML_READER_ENDELEM; |
|---|
| 284 |
if( p_reader->p_sys->p_curtag->name ) return XML_READER_STARTELEM; |
|---|
| 285 |
if( p_reader->p_sys->p_curtag->pcdata ) return XML_READER_TEXT; |
|---|
| 286 |
return XML_READER_NONE; |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
static char *ReaderName( xml_reader_t *p_reader ) |
|---|
| 290 |
{ |
|---|
| 291 |
const char *psz_name; |
|---|
| 292 |
|
|---|
| 293 |
if( !p_reader->p_sys->p_curattr ) |
|---|
| 294 |
{ |
|---|
| 295 |
psz_name = xtag_get_name( p_reader->p_sys->p_curtag ); |
|---|
| 296 |
#ifdef XTAG_DEBUG |
|---|
| 297 |
fprintf( stderr, "TAG: %s\n", psz_name ); |
|---|
| 298 |
#endif |
|---|
| 299 |
} |
|---|
| 300 |
else |
|---|
| 301 |
psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->name; |
|---|
| 302 |
|
|---|
| 303 |
if( psz_name ) return strdup( psz_name ); |
|---|
| 304 |
else return 0; |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
static char *ReaderValue( xml_reader_t *p_reader ) |
|---|
| 308 |
{ |
|---|
| 309 |
const char *psz_name; |
|---|
| 310 |
if( p_reader->p_sys->p_curtag->pcdata ) |
|---|
| 311 |
{ |
|---|
| 312 |
#ifdef XTAG_DEBUG |
|---|
| 313 |
fprintf( stderr, "%s\n", p_reader->p_sys->p_curtag->pcdata ); |
|---|
| 314 |
#endif |
|---|
| 315 |
return strdup( p_reader->p_sys->p_curtag->pcdata ); |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
if( !p_reader->p_sys->p_curattr ) return 0; |
|---|
| 319 |
|
|---|
| 320 |
#ifdef XTAG_DEBUG |
|---|
| 321 |
fprintf( stderr, "%s=%s\n", ((XAttribute *)p_reader->p_sys->p_curattr->data)->name, |
|---|
| 322 |
((XAttribute *)p_reader->p_sys->p_curattr->data)->value ); |
|---|
| 323 |
#endif |
|---|
| 324 |
|
|---|
| 325 |
psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->value; |
|---|
| 326 |
|
|---|
| 327 |
if( psz_name ) return strdup( psz_name ); |
|---|
| 328 |
else return 0; |
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
static int ReaderNextAttr( xml_reader_t *p_reader ) |
|---|
| 332 |
{ |
|---|
| 333 |
if( !p_reader->p_sys->p_curattr ) |
|---|
| 334 |
p_reader->p_sys->p_curattr = p_reader->p_sys->p_curtag->attributes; |
|---|
| 335 |
else if( p_reader->p_sys->p_curattr ) |
|---|
| 336 |
p_reader->p_sys->p_curattr = p_reader->p_sys->p_curattr->next; |
|---|
| 337 |
|
|---|
| 338 |
if( p_reader->p_sys->p_curattr ) return VLC_SUCCESS; |
|---|
| 339 |
else return VLC_EGENERIC; |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
static XList *xlist_append( XList *list, void *data ) |
|---|
| 347 |
{ |
|---|
| 348 |
XList *l, *last; |
|---|
| 349 |
|
|---|
| 350 |
l = (XList *)malloc( sizeof(XList) ); |
|---|
| 351 |
l->prev = l->next = NULL; |
|---|
| 352 |
l->data = data; |
|---|
| 353 |
|
|---|
| 354 |
if( list == NULL ) return l; |
|---|
| 355 |
|
|---|
| 356 |
for( last = list; last; last = last->next ) |
|---|
| 357 |
if( last->next == NULL ) break; |
|---|
| 358 |
|
|---|
| 359 |
if( last ) last->next = l; |
|---|
| 360 |
l->prev = last; |
|---|
| 361 |
return list; |
|---|
| 362 |
} |
|---|
| 363 |
|
|---|
| 364 |
static void xlist_free( XList *list ) |
|---|
| 365 |
{ |
|---|
| 366 |
XList *l, *ln; |
|---|
| 367 |
|
|---|
| 368 |
for( l = list; l; l = ln ) |
|---|
| 369 |
{ |
|---|
| 370 |
ln = l->next; |
|---|
| 371 |
free( l ); |
|---|
| 372 |
} |
|---|
| 373 |
} |
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
#define X_NONE 0 |
|---|
| 377 |
#define X_WHITESPACE 1<<0 |
|---|
| 378 |
#define X_OPENTAG 1<<1 |
|---|
| 379 |
#define X_CLOSETAG 1<<2 |
|---|
| 380 |
#define X_DQUOTE 1<<3 |
|---|
| 381 |
#define X_SQUOTE 1<<4 |
|---|
| 382 |
#define X_EQUAL 1<<5 |
|---|
| 383 |
#define X_SLASH 1<<6 |
|---|
| 384 |
#define X_QMARK 1<<7 |
|---|
| 385 |
#define X_DASH 1<<8 |
|---|
| 386 |
#define X_EMARK 1<<9 |
|---|
| 387 |
|
|---|
| 388 |
static int xtag_cin( char c, int char_class ) |
|---|
| 389 |
{ |
|---|
| 390 |
if( char_class & X_WHITESPACE ) if( isspace(c) ) return true; |
|---|
| 391 |
if( char_class & X_OPENTAG ) if( c == '<' ) return true; |
|---|
| 392 |
if( char_class & X_CLOSETAG ) if( c == '>' ) return true; |
|---|
| 393 |
if( char_class & X_DQUOTE ) if( c == '"' ) return true; |
|---|
| 394 |
if( char_class & X_SQUOTE ) if( c == '\'' ) return true; |
|---|
| 395 |
if( char_class & X_EQUAL ) if( c == '=' ) return true; |
|---|
| 396 |
if( char_class & X_SLASH ) if( c == '/' ) return true; |
|---|
| 397 |
if( char_class & X_QMARK ) if( c == '?' ) return true; |
|---|
| 398 |
if( char_class & X_DASH ) if( c == '-' ) return true; |
|---|
| 399 |
if( char_class & X_EMARK ) if( c == '!' ) return true; |
|---|
| 400 |
|
|---|
| 401 |
return false; |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
static int xtag_index( XTagParser *parser, int char_class ) |
|---|
| 405 |
{ |
|---|
| 406 |
char *s = parser->start; |
|---|
| 407 |
int i; |
|---|
| 408 |
|
|---|
| 409 |
for( i = 0; s[i] && s != parser->end; i++ ) |
|---|
| 410 |
{ |
|---|
| 411 |
if( xtag_cin( s[i], char_class ) ) return i; |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
return -1; |
|---|
| 415 |
} |
|---|
| 416 |
|
|---|
| 417 |
static void xtag_skip_over( XTagParser *parser, int char_class ) |
|---|
| 418 |
{ |
|---|
| 419 |
char *s = parser->start; |
|---|
| 420 |
int i; |
|---|
| 421 |
|
|---|
| 422 |
if( !parser->valid ) return; |
|---|
| 423 |
|
|---|
| 424 |
for( i = 0; s[i] && s != parser->end; i++ ) |
|---|
| 425 |
{ |
|---|
| 426 |
if( !xtag_cin( s[i], char_class ) ) |
|---|
| 427 |
{ |
|---|
| 428 |
parser->start = &s[i]; |
|---|
| 429 |
return; |
|---|
| 430 |
} |
|---|
| 431 |
} |
|---|
| 432 |
|
|---|
| 433 |
return; |
|---|
| 434 |
} |
|---|
| 435 |
|
|---|
| 436 |
static void xtag_skip_whitespace( XTagParser * parser ) |
|---|
| 437 |
{ |
|---|
| 438 |
xtag_skip_over( parser, X_WHITESPACE ); |
|---|
| 439 |
} |
|---|
| 440 |
|
|---|
| 441 |
static char *xtag_slurp_to( XTagParser *parser, int good_end, int bad_end ) |
|---|
| 442 |
{ |
|---|
| 443 |
char *ret, *s = parser->start; |
|---|
| 444 |
int xi; |
|---|
| 445 |
|
|---|
| 446 |
if( !parser->valid ) return NULL; |
|---|
| 447 |
|
|---|
| 448 |
xi = xtag_index( parser, good_end | bad_end ); |
|---|
| 449 |
|
|---|
| 450 |
if( xi > 0 && xtag_cin (s[xi], good_end) ) |
|---|
| 451 |
{ |
|---|
| 452 |
ret = malloc( (xi+1) * sizeof(char) ); |
|---|
| 453 |
strncpy( ret, s, xi ); |
|---|
| 454 |
ret[xi] = '\0'; |
|---|
| 455 |
parser->start = &s[xi]; |
|---|
| 456 |
return ret; |
|---|
| 457 |
} |
|---|
| 458 |
|
|---|
| 459 |
return NULL; |
|---|
| 460 |
} |
|---|
| 461 |
|
|---|
| 462 |
static int xtag_assert_and_pass( XTagParser *parser, int char_class ) |
|---|
| 463 |
{ |
|---|
| 464 |
char *s = parser->start; |
|---|
| 465 |
|
|---|
| 466 |
if( !parser->valid ) return false; |
|---|
| 467 |
|
|---|
| 468 |
if( !xtag_cin( s[0], char_class ) ) |
|---|
| 469 |
{ |
|---|
| 470 |
parser->valid = false; |
|---|
| 471 |
return false; |
|---|
| 472 |
} |
|---|
| 473 |
|
|---|
| 474 |
parser->start = &s[1]; |
|---|
| 475 |
|
|---|
| 476 |
return true; |
|---|
| 477 |
} |
|---|
| 478 |
|
|---|
| 479 |
static char *xtag_slurp_quoted( XTagParser *parser ) |
|---|
| 480 |
{ |
|---|
| 481 |
char * ret, *s; |
|---|
| 482 |
int quote = X_DQUOTE; |
|---|
| 483 |
int xi; |
|---|
| 484 |
|
|---|
| 485 |
if( !parser->valid ) return NULL; |
|---|
| 486 |
|
|---|
| 487 |
xtag_skip_whitespace( parser ); |
|---|
| 488 |
|
|---|
| 489 |
s = parser->start; |
|---|
| 490 |
|
|---|
| 491 |
if( xtag_cin( s[0], X_SQUOTE ) ) quote = X_SQUOTE; |
|---|
| 492 |
|
|---|
| 493 |
if( !xtag_assert_and_pass( parser, quote ) ) return NULL; |
|---|
| 494 |
|
|---|
| 495 |
s = parser->start; |
|---|
| 496 |
|
|---|
| 497 |
for( xi = 0; s[xi]; xi++ ) |
|---|
| 498 |
{ |
|---|
| 499 |
if( xtag_cin( s[xi], quote ) ) |
|---|
| 500 |
{ |
|---|
| 501 |
if( !(xi > 1 && s[xi-1] == '\\') ) break; |
|---|
| 502 |
} |
|---|
| 503 |
} |
|---|
| 504 |
|
|---|
| 505 |
ret = malloc( (xi+1) * sizeof(char) ); |
|---|
| 506 |
strncpy( ret, s, xi ); |
|---|
| 507 |
ret[xi] = '\0'; |
|---|
| 508 |
parser->start = &s[xi]; |
|---|
| 509 |
|
|---|
| 510 |
if( !xtag_assert_and_pass( parser, quote ) ) return NULL; |
|---|
| 511 |
|
|---|
| 512 |
return ret; |
|---|
| 513 |
} |
|---|
| 514 |
|
|---|
| 515 |
static XAttribute *xtag_parse_attribute( XTagParser *parser ) |
|---|
| 516 |
{ |
|---|
| 517 |
XAttribute *attr; |
|---|
| 518 |
char *name, *value; |
|---|
| 519 |
char *s; |
|---|
| 520 |
|
|---|
| 521 |
if( !parser->valid ) return NULL; |
|---|
| 522 |
|
|---|
| 523 |
xtag_skip_whitespace( parser ); |
|---|
| 524 |
|
|---|
| 525 |
name = xtag_slurp_to( parser, X_WHITESPACE|X_EQUAL, X_SLASH|X_CLOSETAG ); |
|---|
| 526 |
if( name == NULL ) return NULL; |
|---|
| 527 |
|
|---|
| 528 |
xtag_skip_whitespace( parser ); |
|---|
| 529 |
s = parser->start; |
|---|
| 530 |
|
|---|
| 531 |
if( !xtag_assert_and_pass( parser, X_EQUAL ) ) |
|---|
| 532 |
{ |
|---|
| 533 |
#ifdef XTAG_DEBUG |
|---|
| 534 |
fprintf( stderr, "xtag: attr failed EQUAL on <%s>\n", name ); |
|---|
| 535 |
#endif |
|---|
| 536 |
goto err_free_name; |
|---|
| 537 |
} |
|---|
| 538 |
|
|---|
| 539 |
xtag_skip_whitespace( parser ); |
|---|
| 540 |
|
|---|
| 541 |
value = xtag_slurp_quoted( parser ); |
|---|
| 542 |
|
|---|
| 543 |
if( value == NULL ) |
|---|
| 544 |
{ |
|---|
| 545 |
#ifdef XTAG_DEBUG |
|---|
| 546 |
fprintf (stderr, "Got NULL quoted attribute value\n"); |
|---|
| 547 |
#endif |
|---|
| 548 |
goto err_free_name; |
|---|
| 549 |
} |
|---|
| 550 |
|
|---|
| 551 |
attr = malloc( sizeof (*attr) ); |
|---|
| 552 |
attr->name = name; |
|---|
| 553 |
attr->value = value; |
|---|
| 554 |
return attr; |
|---|
| 555 |
|
|---|
| 556 |
err_free_name: |
|---|
| 557 |
free (name); |
|---|
| 558 |
parser->valid = false; |
|---|
| 559 |
return NULL; |
|---|
| 560 |
} |
|---|
| 561 |
|
|---|
| 562 |
static XTag *xtag_parse_tag( XTagParser *parser ) |
|---|
| 563 |
{ |
|---|
| 564 |
XTag *tag, *inner; |
|---|
| 565 |
XAttribute *attr; |
|---|
| 566 |
char *name; |
|---|
| 567 |
char *pcdata; |
|---|
| 568 |
char *s; |
|---|
| 569 |
int xi; |
|---|
| 570 |
|
|---|
| 571 |
if( !parser->valid ) return NULL; |
|---|
| 572 |
|
|---|
| 573 |
s = parser->start; |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
if( (parser->end - parser->start) > 7 && |
|---|
| 577 |
xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_EMARK ) && |
|---|
| 578 |
xtag_cin( s[2], X_DASH ) && xtag_cin( s[3], X_DASH ) ) |
|---|
| 579 |
{ |
|---|
| 580 |
parser->start = s = &s[4]; |
|---|
| 581 |
while( (xi = xtag_index( parser, X_DASH )) >= 0 ) |
|---|
| 582 |
{ |
|---|
| 583 |
parser->start = s = &s[xi+1]; |
|---|
| 584 |
if( xtag_cin( s[0], X_DASH ) && xtag_cin( s[1], X_CLOSETAG ) ) |
|---|
| 585 |
{ |
|---|
| 586 |
parser->start = &s[2]; |
|---|
| 587 |
xtag_skip_whitespace( parser ); |
|---|
| 588 |
return xtag_parse_tag( parser ); |
|---|
| 589 |
} |
|---|
| 590 |
} |
|---|
| 591 |
return NULL; |
|---|
| 592 |
} |
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
if( (parser->end - parser->start) > 4 && |
|---|
| 596 |
xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_QMARK ) ) |
|---|
| 597 |
{ |
|---|
| 598 |
parser->start = s = &s[2]; |
|---|
| 599 |
while ((xi = xtag_index( parser, X_QMARK )) >= 0) { |
|---|
| 600 |
if (xtag_cin( s[xi+1], X_CLOSETAG )) { |
|---|
| 601 |
parser->start = &s[xi+2]; |
|---|
| 602 |
xtag_skip_whitespace( parser ); |
|---|
| 603 |
return xtag_parse_tag( parser ); |
|---|
| 604 |
} |
|---|
| 605 |
} |
|---|
| 606 |
return NULL; |
|---|
| 607 |
} |
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
if ( (parser->end - parser->start) > 8 && |
|---|
| 611 |
!strncmp( s, "<!DOCTYPE", 9 ) ) { |
|---|
| 612 |
xi = xtag_index( parser, X_CLOSETAG ); |
|---|
| 613 |
if ( xi > 0 ) { |
|---|
| 614 |
parser->start = s = &s[xi+1]; |
|---|
| 615 |
xtag_skip_whitespace( parser ); |
|---|
| 616 |
return xtag_parse_tag( parser ); |
|---|
| 617 |
} |
|---|
| 618 |
else { |
|---|
| 619 |
return NULL; |
|---|
| 620 |
} |
|---|
| 621 |
} |
|---|
| 622 |
|
|---|
| 623 |
if( (pcdata = xtag_slurp_to( parser, X_OPENTAG, X_NONE )) != NULL ) |
|---|
| 624 |
{ |
|---|
| 625 |
tag = malloc( sizeof(*tag) ); |
|---|
| 626 |
tag->name = NULL; |
|---|
| 627 |
tag->pcdata = pcdata; |
|---|
| 628 |
tag->parent = parser->current_tag; |
|---|
| 629 |
tag->attributes = NULL; |
|---|
| 630 |
tag->children = NULL; |
|---|
| 631 |
tag->current_child = NULL; |
|---|
| 632 |
|
|---|
| 633 |
return tag; |
|---|
| 634 |
} |
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
if( xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_SLASH ) ) |
|---|
| 638 |
return NULL; |
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
if ( (parser->end - parser->start) > 8 && |
|---|
| 642 |
!strncmp( s, "<![CDATA[", 9 ) ) { |
|---|
| 643 |
parser->start = s = &s[9]; |
|---|
| 644 |
while (parser->end - s > 2) { |
|---|
| 645 |
if (strncmp( s, "]]>", 3 ) == 0) { |
|---|
| 646 |
if ( !(tag = malloc( sizeof(*tag))) ) return NULL; |
|---|
| 647 |
if ( !(pcdata = malloc( sizeof(char)*(s - parser->start + 1))) ) return NULL; |
|---|
| 648 |
strncpy( pcdata, parser->start, s - parser->start ); |
|---|
| 649 |
pcdata[s - parser->start]='\0'; |
|---|
| 650 |
parser->start = s = &s[3]; |
|---|
| 651 |
tag->name = NULL; |
|---|
| 652 |
tag->pcdata = pcdata; |
|---|
| 653 |
tag->parent = parser->current_tag; |
|---|
| 654 |
tag->attributes = NULL; |
|---|
| 655 |
tag->children = NULL; |
|---|
| 656 |
tag->current_child = NULL; |
|---|
| 657 |
return tag; |
|---|
| 658 |
} |
|---|
| 659 |
else { |
|---|
| 660 |
s++; |
|---|
| 661 |
} |
|---|
| 662 |
} |
|---|
| 663 |
return NULL; |
|---|
| 664 |
} |
|---|
| 665 |
|
|---|
| 666 |
if( !xtag_assert_and_pass( parser, X_OPENTAG ) ) return NULL; |
|---|
| 667 |
|
|---|
| 668 |
name = xtag_slurp_to( parser, X_WHITESPACE|X_SLASH|X_CLOSETAG, X_NONE ); |
|---|
| 669 |
if( name == NULL ) return NULL; |
|---|
| 670 |
|
|---|
| 671 |
#ifdef XTAG_DEBUG |
|---|
| 672 |
fprintf (stderr, "<%s ...\n", name); |
|---|
| 673 |
#endif |
|---|
| 674 |
|
|---|
| 675 |
tag = malloc( sizeof(*tag) ); |
|---|
| 676 |
tag->name = name; |
|---|
| 677 |
tag->pcdata = NULL; |
|---|
| 678 |
tag->parent = parser->current_tag; |
|---|
| 679 |
tag->attributes = NULL; |
|---|
| 680 |
tag->children = NULL; |
|---|
| 681 |
tag->current_child = NULL; |
|---|
| 682 |
|
|---|
| 683 |
s = parser->start; |
|---|
| 684 |
|
|---|
| 685 |
if( xtag_cin( s[0], X_WHITESPACE ) ) |
|---|
| 686 |
{ |
|---|
| 687 |
while( (attr = xtag_parse_attribute( parser )) != NULL ) |
|---|
| 688 |
{ |
|---|
| 689 |
tag->attributes = xlist_append( tag->attributes, attr ); |
|---|
| 690 |
} |
|---|
| 691 |
} |
|---|
| 692 |
|
|---|
| 693 |
xtag_skip_whitespace( parser ); |
|---|
| 694 |
|
|---|
| 695 |
s = parser->start; |
|---|
| 696 |
|
|---|
| 697 |
if( xtag_cin( s[0], X_CLOSETAG ) ) |
|---|
| 698 |
{ |
|---|
| 699 |
parser->current_tag = tag; |
|---|
| 700 |
|
|---|
| 701 |
xtag_assert_and_pass( parser, X_CLOSETAG ); |
|---|
| 702 |
|
|---|
| 703 |
while( (inner = xtag_parse_tag( parser ) ) != NULL ) |
|---|
| 704 |
{ |
|---|
| 705 |
tag->children = xlist_append( tag->children, inner ); |
|---|
| 706 |
} |
|---|
| 707 |
|
|---|
| 708 |
parser->current_tag = tag->parent; |
|---|
| 709 |
xtag_skip_whitespace( parser ); |
|---|
| 710 |
|
|---|
| 711 |
xtag_assert_and_pass( parser, X_OPENTAG ); |
|---|
| 712 |
xtag_assert_and_pass( parser, X_SLASH ); |
|---|
| 713 |
name = xtag_slurp_to( parser, X_WHITESPACE | X_CLOSETAG, X_NONE ); |
|---|
| 714 |
if( name ) |
|---|
| 715 |
{ |
|---|
| 716 |
if( strcmp( name, tag->name ) ) |
|---|
| 717 |
{ |
|---|
| 718 |
#ifdef XTAG_DEBUG |
|---|
| 719 |
fprintf (stderr, "got %s expected %s\n", name, tag->name); |
|---|
| 720 |
#endif |
|---|
| 721 |
parser->valid = false; |
|---|
| 722 |
} |
|---|
| 723 |
free( name ); |
|---|
| 724 |
} |
|---|
| 725 |
|
|---|
| 726 |
xtag_skip_whitespace( parser ); |
|---|
| 727 |
xtag_assert_and_pass( parser, X_CLOSETAG ); |
|---|
| 728 |
xtag_skip_whitespace( parser ); |
|---|
| 729 |
} |
|---|
| 730 |
else |
|---|
| 731 |
{ |
|---|
| 732 |
xtag_assert_and_pass( parser, X_SLASH ); |
|---|
| 733 |
xtag_assert_and_pass( parser, X_CLOSETAG ); |
|---|
| 734 |
xtag_skip_whitespace( parser ); |
|---|
| 735 |
} |
|---|
| 736 |
|
|---|
| 737 |
return tag; |
|---|
| 738 |
} |
|---|
| 739 |
|
|---|
| 740 |
static XTag *xtag_free( XTag *xtag ) |
|---|
| 741 |
{ |
|---|
| 742 |
XList *l; |
|---|
| 743 |
XAttribute *attr; |
|---|
| 744 |
XTag *child; |
|---|
| 745 |
|
|---|
| 746 |
if( xtag == NULL ) return NULL; |
|---|
| 747 |
|
|---|
| 748 |
free( xtag->name ); |
|---|
| 749 |
free( xtag->pcdata ); |
|---|
| 750 |
|
|---|
| 751 |
for( l = xtag->attributes; l; l = l->next ) |
|---|
| 752 |
{ |
|---|
| 753 |
if( (attr = (XAttribute *)l->data) != NULL ) |
|---|
| 754 |
{ |
|---|
| 755 |
free( attr->name ); |
|---|
| 756 |
free( attr->value ); |
|---|
| 757 |
free( attr ); |
|---|
| 758 |
} |
|---|
| 759 |
} |
|---|
| 760 |
xlist_free( xtag->attributes ); |
|---|
| 761 |
|
|---|
| 762 |
for( l = xtag->children; l; l = l->next ) |
|---|
| 763 |
{ |
|---|
| 764 |
child = (XTag *)l->data; |
|---|
| 765 |
xtag_free( child ); |
|---|
| 766 |
} |
|---|
| 767 |
xlist_free( xtag->children ); |
|---|
| 768 |
|
|---|
| 769 |
free( xtag ); |
|---|
| 770 |
|
|---|
| 771 |
return NULL; |
|---|
| 772 |
} |
|---|
| 773 |
|
|---|
| 774 |
static XTag *xtag_new_parse( const char *s, int n ) |
|---|
| 775 |
{ |
|---|
| 776 |
XTagParser parser; |
|---|
| 777 |
XTag *tag, *ttag, *wrapper; |
|---|
| 778 |
|
|---|
| 779 |
parser.valid = true; |
|---|
| 780 |
parser.current_tag = NULL; |
|---|
| 781 |
parser.start = (char *)s; |
|---|
| 782 |
|
|---|
| 783 |
if( n == -1 ) parser.end = NULL; |
|---|
| 784 |
else if( n == 0 ) |
|---|
| 785 |
{ |
|---|
| 786 |
#ifdef XTAG_DEBUG |
|---|
| 787 |
fprintf (stderr, "empty buffer\n"); |
|---|
| 788 |
#endif |
|---|
| 789 |
return NULL; |
|---|
| 790 |
} |
|---|
| 791 |
else parser.end = (char *)&s[n]; |
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
xtag_skip_whitespace( &parser ); |
|---|
| 795 |
|
|---|
| 796 |
tag = xtag_parse_tag( &parser ); |
|---|
| 797 |
|
|---|
| 798 |
if( !parser.valid ) |
|---|
| 799 |
{ |
|---|
| 800 |
#ifdef XTAG_DEBUG |
|---|
| 801 |
fprintf (stderr, "invalid file\n"); |
|---|
| 802 |
#endif |
|---|
| 803 |
xtag_free( tag ); |
|---|
| 804 |
return NULL; |
|---|
| 805 |
} |
|---|
| 806 |
|
|---|
| 807 |
if( (ttag = xtag_parse_tag( &parser )) != NULL ) |
|---|
| 808 |
{ |
|---|
| 809 |
if( !parser.valid ) |
|---|
| 810 |
{ |
|---|
| 811 |
xtag_free( ttag ); |
|---|
| 812 |
return tag; |
|---|
| 813 |
} |
|---|
| 814 |
|
|---|
| 815 |
wrapper = malloc( sizeof(XTag) ); |
|---|
| 816 |
wrapper->name = NULL; |
|---|
| 817 |
wrapper->pcdata = NULL; |
|---|
| 818 |
wrapper->parent = NULL; |
|---|
| 819 |
wrapper->attributes = NULL; |
|---|
| 820 |
wrapper->children = NULL; |
|---|
| 821 |
wrapper->current_child = NULL; |
|---|
| 822 |
|
|---|
| 823 |
wrapper->children = xlist_append( wrapper->children, tag ); |
|---|
| 824 |
wrapper->children = xlist_append( wrapper->children, ttag ); |
|---|
| 825 |
|
|---|
| 826 |
while( (ttag = xtag_parse_tag( &parser )) != NULL ) |
|---|
| 827 |
{ |
|---|
| 828 |
if( !parser.valid ) |
|---|
| 829 |
{ |
|---|
| 830 |
xtag_free( ttag ); |
|---|
| 831 |
return wrapper; |
|---|
| 832 |
} |
|---|
| 833 |
|
|---|
| 834 |
wrapper->children = xlist_append( wrapper->children, ttag ); |
|---|
| 835 |
} |
|---|
| 836 |
return wrapper; |
|---|
| 837 |
} |
|---|
| 838 |
|
|---|
| 839 |
return tag; |
|---|
| 840 |
} |
|---|
| 841 |
|
|---|
| 842 |
static char *xtag_get_name( XTag *xtag ) |
|---|
| 843 |
{ |
|---|
| 844 |
return xtag ? xtag->name : NULL; |
|---|
| 845 |
} |
|---|
| 846 |
|
|---|
| 847 |
#if 0 |
|---|
| 848 |
static char *xtag_get_pcdata( XTag *xtag ) |
|---|
| 849 |
{ |
|---|
| 850 |
XList *l; |
|---|
| 851 |
XTag *child; |
|---|
| 852 |
|
|---|
| 853 |
if( xtag == NULL ) return NULL; |
|---|
| 854 |
|
|---|
| 855 |
for( l = xtag->children; l; l = l->next ) |
|---|
| 856 |
{ |
|---|
| 857 |
child = (XTag *)l->data; |
|---|
| 858 |
if( child->pcdata != NULL ) |
|---|
| 859 |
{ |
|---|
| 860 |
return child->pcdata; |
|---|
| 861 |
} |
|---|
| 862 |
} |
|---|
| 863 |
|
|---|
| 864 |
return NULL; |
|---|
| 865 |
} |
|---|
| 866 |
|
|---|
| 867 |
static char *xtag_get_attribute( XTag *xtag, char *attribute ) |
|---|
| 868 |
{ |
|---|
| 869 |
XList *l; |
|---|
| 870 |
XAttribute *attr; |
|---|
| 871 |
|
|---|
| 872 |
if( xtag == NULL ) return NULL; |
|---|
| 873 |
|
|---|
| 874 |
for( l = xtag->attributes; l; l = l->next ) |
|---|
| 875 |
{ |
|---|
| 876 |
if( (attr = (XAttribute *)l->data) != NULL ) |
|---|
| 877 |
{ |
|---|
| 878 |
if( !strcmp( attr->name, attribute ) ) return attr->value; |
|---|
| 879 |
} |
|---|
| 880 |
} |
|---|
| 881 |
|
|---|
| 882 |
return NULL; |
|---|
| 883 |
} |
|---|
| 884 |
#endif |
|---|
| 885 |
|
|---|
| 886 |
static XTag *xtag_first_child( XTag *xtag, char *name ) |
|---|
| 887 |
{ |
|---|
| 888 |
XList *l; |
|---|
| 889 |
XTag *child; |
|---|
| 890 |
|
|---|
| 891 |
if( xtag == NULL ) return NULL; |
|---|
| 892 |
if( (l = xtag->children) == NULL ) return NULL; |
|---|
| 893 |
|
|---|
| 894 |
if( name == NULL ) |
|---|
| 895 |
{ |
|---|
| 896 |
xtag->current_child = l; |
|---|
| 897 |
return (XTag *)l->data; |
|---|
| 898 |
} |
|---|
| 899 |
|
|---|
| 900 |
for( ; l; l = l->next ) |
|---|
| 901 |
{ |
|---|
| 902 |
child = (XTag *)l->data; |
|---|
| 903 |
|
|---|
| 904 |
if( !strcmp( child->name, name ) ) |
|---|
| 905 |
{ |
|---|
| 906 |
xtag->current_child = l; |
|---|
| 907 |
return child; |
|---|
| 908 |
} |
|---|
| 909 |
} |
|---|
| 910 |
|
|---|
| 911 |
xtag->current_child = NULL; |
|---|
| 912 |
|
|---|
| 913 |
return NULL; |
|---|
| 914 |
} |
|---|
| 915 |
|
|---|
| 916 |
static XTag *xtag_next_child( XTag *xtag, char *name ) |
|---|
| 917 |
{ |
|---|
| 918 |
XList *l; |
|---|
| 919 |
XTag *child; |
|---|
| 920 |
|
|---|
| 921 |
if( xtag == NULL ) return NULL; |
|---|
| 922 |
|
|---|
| 923 |
if( (l = xtag->current_child) == NULL ) |
|---|
| 924 |
return xtag_first_child( xtag, name ); |
|---|
| 925 |
|
|---|
| 926 |
if( (l = l->next) == NULL ) return NULL; |
|---|
| 927 |
|
|---|
| 928 |
if( name == NULL ) |
|---|
| 929 |
{ |
|---|
| 930 |
xtag->current_child = l; |
|---|
| 931 |
return (XTag *)l->data; |
|---|
| 932 |
} |
|---|
| 933 |
|
|---|
| 934 |
for( ; l; l = l->next ) |
|---|
| 935 |
{ |
|---|
| 936 |
child = (XTag *)l->data; |
|---|
| 937 |
|
|---|
| 938 |
if( !strcmp( child->name, name ) ) |
|---|
| 939 |
{ |
|---|
| 940 |
xtag->current_child = l; |
|---|
| 941 |
return child; |
|---|
| 942 |
} |
|---|
| 943 |
} |
|---|
| 944 |
|
|---|
| 945 |
xtag->current_child = NULL; |
|---|
| 946 |
|
|---|
| 947 |
return NULL; |
|---|
| 948 |
} |
|---|
| 949 |
|
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 |
|
|---|
| 953 |
|
|---|
| 954 |
|
|---|
| 955 |
|
|---|
| 956 |
static int xtag_snprints( char *buf, int n, ... ) |
|---|
| 957 |
{ |
|---|
| 958 |
va_list ap; |
|---|
| 959 |
char *s; |
|---|
| 960 |
int len, to_copy, total = 0; |
|---|
| 961 |
|
|---|
| 962 |
va_start( ap, n ); |
|---|
| 963 |
|
|---|
| 964 |
for( s = va_arg( ap, char * ); s; s = va_arg( ap, char *) ) |
|---|
| 965 |
{ |
|---|
| 966 |
len = strlen (s); |
|---|
| 967 |
|
|---|
| 968 |
if( (to_copy = __MIN(n, len) ) > 0 ) |
|---|
| 969 |
{ |
|---|
| 970 |
memcpy( buf, s, to_copy ); |
|---|
| 971 |
buf += to_copy; |
|---|
| 972 |
n -= to_copy; |
|---|
| 973 |
} |
|---|
| 974 |
|
|---|
| 975 |
total += len; |
|---|
| 976 |
} |
|---|
| 977 |
|
|---|
| 978 |
va_end( ap ); |
|---|
| 979 |
|
|---|
| 980 |
return total; |
|---|
| 981 |
} |
|---|
| 982 |
|
|---|
| 983 |
static int xtag_snprint( char *buf, int n, XTag *xtag ) |
|---|
| 984 |
{ |
|---|
| 985 |
int nn, written = 0; |
|---|
| 986 |
XList *l; |
|---|
| 987 |
XAttribute *attr; |
|---|
| 988 |
XTag *child; |
|---|
| 989 |
|
|---|
| 990 |
#define FORWARD(N) \ |
|---|
| 991 |
buf += __MIN(n, N); \ |
|---|
| 992 |
n = __MAX(n-N, 0); \ |
|---|
| 993 |
written += N; |
|---|
| 994 |
|
|---|
| 995 |
if( xtag == NULL ) |
|---|
| 996 |
{ |
|---|
| 997 |
if( n > 0 ) buf[0] = '\0'; |
|---|
| 998 |
return 0; |
|---|
| 999 |
} |
|---|
| 1000 |
|
|---|
| 1001 |
if( xtag->pcdata ) |
|---|
| 1002 |
{ |
|---|
| 1003 |
nn = xtag_snprints( buf, n, xtag->pcdata, NULL ); |
|---|
| 1004 |
FORWARD( nn ); |
|---|
| 1005 |
|
|---|
| 1006 |
return written; |
|---|
| 1007 |
} |
|---|
| 1008 |
|
|---|
| 1009 |
if( xtag->name ) |
|---|
| 1010 |
{ |
|---|
| 1011 |
nn = xtag_snprints( buf, n, "< |
|---|