[gq-commit] gq/src xmlparse.c,1.1,1.2
Status: Beta
Brought to you by:
sur5r
From: <sta...@us...> - 2003-10-04 10:09:08
|
Update of /cvsroot/gqclient/gq/src In directory sc8-pr-cvs1:/tmp/cvs-serv4660 Modified Files: xmlparse.c Log Message: * Properly use g_utf8_* for walking an UTF-8 string * Fixed a nasty character handler bug * in gtk2: use another parser interface allowing to change character encodings * Added heuristics to decide which encoding to use based on the first line of a .gq - EXTREMELY UGLY Index: xmlparse.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/xmlparse.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** xmlparse.c 4 Oct 2003 06:57:03 -0000 1.1 --- xmlparse.c 4 Oct 2003 10:09:02 -0000 1.2 *************** *** 31,35 **** #include "xmlparse.h" - #define malloc g_malloc #define malloc0 g_malloc0 --- 31,34 ---- *************** *** 229,233 **** xmlChar *p = e->cdata, *n = NULL; ! for(c = g_utf8_get_char(p) ; c ; c = g_utf8_next_char(p)) { if (!g_unichar_isspace(c)) { memmove(e->cdata, p, e->len - (p - e->cdata) + 1); --- 228,232 ---- xmlChar *p = e->cdata, *n = NULL; ! for(c = g_utf8_get_char(p) ; c ; p = g_utf8_next_char(p), c = *p) { if (!g_unichar_isspace(c)) { memmove(e->cdata, p, e->len - (p - e->cdata) + 1); *************** *** 237,241 **** p = e->cdata; ! for(c = g_utf8_get_char(p) ; c ; c = g_utf8_next_char(p)) { if (g_unichar_isspace(c)) { if (n == NULL) n = p; --- 236,240 ---- p = e->cdata; ! for(c = g_utf8_get_char(p) ; c ; p = g_utf8_next_char(p), c = *p) { if (g_unichar_isspace(c)) { if (n == NULL) n = p; *************** *** 264,267 **** --- 263,270 ---- if (e->skip) return; + + + + if (e->cdata) { e->len += len; *************** *** 274,280 **** strncat(e->cdata, ch, len); ! e->cdata[len] = 0; } int XMLparse(struct xml_tag *tags, xmlSAXHandler *handler, --- 277,303 ---- strncat(e->cdata, ch, len); ! e->cdata[e->len] = 0; } + #if GTK_MAJOR >= 2 + static int inputReadCallback(FILE *context, + char *buffer, + int len) + { + int rc = fread(buffer, 1, len, context); + if (rc == 0) { + if (ferror(context)) return -1; + } + return rc; + } + + static int inputCloseCallback(FILE *context) + { + int rc = fclose(context); + if (rc != 0) return -1; + return rc; + } + #endif + int XMLparse(struct xml_tag *tags, xmlSAXHandler *handler, *************** *** 285,289 **** int rc; int handler_is_local = 0; ! if (handler == NULL) { handler = calloc(1, sizeof(xmlSAXHandler)); --- 308,314 ---- int rc; int handler_is_local = 0; ! #if GTK_MAJOR >= 2 ! FILE *fp; ! #endif if (handler == NULL) { handler = calloc(1, sizeof(xmlSAXHandler)); *************** *** 306,310 **** --- 331,366 ---- ctx.XMLhandler = handler; + #ifdef HAVE_LIBXML2 + fp = fopen(file, "r"); + if (fp) { + /* nasty heuristics, but the interface does not allow to do it + differently */ + + char firstline[255]; + fgets(firstline, sizeof(firstline) - 1, fp); + + rewind(fp); + + xmlParserCtxtPtr parser = xmlCreateIOParserCtxt(handler, + &ctx, + (xmlInputReadCallback) inputReadCallback, + (xmlInputCloseCallback) inputCloseCallback, + fp, 0); + + if (strstr(firstline, "encoding=") == NULL ) { + /* no encoding specified, heuristics needed */ + extern const char *gq_codeset; + xmlSwitchEncoding(parser, xmlParseCharEncoding(gq_codeset)); + } + + rc = xmlParseDocument(parser); + xmlFreeParserCtxt(parser); + } else { + XMLhandleFatalError(&ctx, "Cannot open file"); + rc = -1; + } + #else rc = xmlSAXUserParseFile(handler, &ctx, file); + #endif free_tagstack(stack); |