123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*
- * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
- *
- * See the COPYING file for the terms of usage and distribution.
- */
- #include <tchar.h>
- #include <string.h>
- #ifdef HAVE_LANGINFO_H
- # include <langinfo.h>
- #endif
- #include "domnode-xml.h"
- /* Generated by bison(1) */
- #include "domnode-xml-parser.h"
- /* Generated by flex(1) */
- #define YY_HEADER_NO_UNDEFS 1
- #include "domnode-xml-scanner.h"
- static const TCHAR version[] = _T("$Id$");
- extern int __sd_domnode_xml_parse(struct __sd_domnode_xml_maker*);
- /******************************************************************************/
- static int xml_fwrite(const sd_domnode_t* ptrThis, FILE* a_stream, int a_indent)
- {
- sd_list_iter_t* iter;
- int i;
- if (!ptrThis || !ptrThis->name || !a_stream)
- return -1;
- for (i = 0; i < a_indent; i++)
- _ftprintf(a_stream, _T(" "));
- if (ptrThis->name && _tcscmp(ptrThis->name, _T("#comment")) == 0) {
- _ftprintf(a_stream, _T("<!-- %s -->\n"), ptrThis->value);
- return 0;
- }
- _ftprintf(a_stream, _T("<%s"), ptrThis->name);
- for (iter = sd_list_begin(ptrThis->attrs); iter != sd_list_end(ptrThis->attrs);
- iter = sd_list_iter_next(iter)) {
- sd_domnode_t* node = (sd_domnode_t*)iter->data;
- _ftprintf(a_stream, _T(" %s=\"%s\""), node->name, node->value);
- }
- if (ptrThis->value || sd_list_get_nelem(ptrThis->children)) {
- _ftprintf(a_stream, _T(">\n"));
- if (ptrThis->value) {
- for (i = 0; i < a_indent + 1; i++)
- _ftprintf(a_stream, _T(" "));
- _ftprintf(a_stream, _T("%s\n"), ptrThis->value);
- }
- for (iter = sd_list_begin(ptrThis->children);
- iter != sd_list_end(ptrThis->children);
- iter = sd_list_iter_next(iter)) {
- sd_domnode_t* node = (sd_domnode_t*)iter->data;
- if (xml_fwrite(node, a_stream, a_indent + 1) == -1)
- return -1;
- }
- for (i = 0; i < a_indent; i++)
- _ftprintf(a_stream, _T(" "));
- _ftprintf(a_stream, _T("</%s>\n"), ptrThis->name);
- } else {
- _ftprintf(a_stream, _T("/>\n"));
- }
- return 0;
- }
- /******************************************************************************/
- extern int __sd_domnode_xml_write(const sd_domnode_t* ptrThis, TCHAR** a_buffer, size_t* a_size)
- {
- /* TODO: to be implemented */
- return -1;
- }
- /******************************************************************************/
- extern int __sd_domnode_xml_fwrite(const sd_domnode_t* ptrThis, FILE* a_stream)
- {
- #ifdef HAVE_NL_LANGINFO
- _ftprintf(a_stream, _T("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n"),nl_langinfo(CODESET));
- #else
- _ftprintf(a_stream, _T("<?xml version=\"1.0\"?>\n\n"));
- #endif
- return xml_fwrite(ptrThis, a_stream, 0);
- }
- /******************************************************************************/
- static int xml_parse(sd_domnode_t** a_node, yyscan_t a_scanner)
- {
- int r;
- struct __sd_domnode_xml_maker maker;
- maker.scanner = a_scanner;
- maker.elements = sd_stack_new(0);
- maker.root = 0;
- if (! (r = __sd_domnode_xml_parse(&maker))) *a_node = maker.root;
- sd_stack_delete(maker.elements, 0);
- return r;
- }
- /******************************************************************************/
- extern int __sd_domnode_xml_fread(sd_domnode_t** a_node, FILE* a_stream)
- {
- int r;
- yyscan_t scanner;
- yylex_init(&scanner);
- yyset_in(a_stream, scanner);
- r = xml_parse(a_node, scanner);
- yylex_destroy(scanner);
- return r;
- }
- /******************************************************************************/
- extern int __sd_domnode_xml_read(sd_domnode_t** a_node, const TCHAR* a_buffer,size_t a_size)
- {
- int r;
- yyscan_t scanner;
- yylex_init(&scanner);
- yy_switch_to_buffer(yy_scan_bytes(a_buffer, a_size, scanner), scanner);
- r = xml_parse(a_node, scanner);
- yylex_destroy(scanner);
- return r;
- }
|