00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039
00040 #include "xData.h"
00041
00042 #if defined __cplusplus
00043 namespace GIDI {
00044 using namespace GIDI;
00045 #endif
00046
00047 typedef struct xmlTextStruct_s {
00048 long allocated;
00049 long length;
00050 char *text;
00051 } xmlTextStruct;
00052
00053 static int xData_parseReconstructXML2( xData_rootElement *root, xmlTextStruct *XML, char *text, long textLength );
00054 static int addStringToXML( xmlTextStruct *XML, char *s, long len );
00055 static void xData_parseOutlinePrintRoot( FILE *f, xData_rootElement *root, int outputText );
00056
00057
00058
00059 char *xData_parseReconstructXML( xData_document *xData_doc ) {
00060
00061 int err;
00062 xmlTextStruct XML = { 0, 0, NULL };
00063
00064 err = xData_parseReconstructXML2( &(xData_doc->root), &XML, NULL, 0 );
00065
00066 if( err == 0 ) addStringToXML( &XML, (char*) "\n", -1 );
00067 return( XML.text );
00068 }
00069
00070
00071
00072 static int xData_parseReconstructXML2( xData_rootElement *root, xmlTextStruct *XML, char *text, long textLength ) {
00073
00074 long i, textOffset = 0;
00075 xData_element *child;
00076
00077 for( child = root->children; child != NULL; child = child->next ) {
00078
00079 if( textOffset < (int) child->textOffset ) {
00080 if( addStringToXML( XML, &(text[textOffset]), child->textOffset - textOffset ) ) return( 1 );
00081 textOffset = child->textOffset;
00082 }
00083
00084 if( addStringToXML( XML, (char*) "<", -1 ) != 0 ) return( 1 );
00085 if( addStringToXML( XML, child->name, -1 ) != 0 ) return( 1 );
00086 for( i = 0; i < child->attributes.number; i++ ) {
00087
00088 if( addStringToXML( XML, (char*) " ", -1 ) != 0 ) return( 1 );
00089 if( addStringToXML( XML, child->attributes.attributes[i].name, -1 ) != 0 ) return( 1 );
00090
00091 if( addStringToXML( XML, (char*) "=\"", -1 ) != 0 ) return( 1 );
00092 if( addStringToXML( XML, child->attributes.attributes[i].value, -1 ) != 0 ) return( 1 );
00093
00094 if( addStringToXML( XML, (char*) "\"", -1 ) != 0 ) return( 1 );
00095 }
00096
00097 if( addStringToXML( XML, (char*) ">", -1 ) != 0 ) return( 1 );
00098 if( xData_parseReconstructXML2( &(child->childrenRoot), XML, child->text.text, child->text.length ) != 0 ) return( 1 );
00099
00100 if( addStringToXML( XML, (char*) "</", -1 ) != 0 ) return( 1 );
00101 if( addStringToXML( XML, child->name, -1 ) != 0 ) return( 1 );
00102
00103 if( addStringToXML( XML, (char*) ">", -1 ) != 0 ) return( 1 );
00104 }
00105 if( textOffset < textLength ) if( addStringToXML( XML, &(text[textOffset]), textLength - textOffset ) ) return( 1 );
00106 return( 0 );
00107 }
00108
00109
00110
00111 static int addStringToXML( xmlTextStruct *XML, char *s, long len ) {
00112
00113 long lenS, length, inc;
00114 char *p;
00115
00116 if( len >= 0 ) {
00117 lenS = len; }
00118 else {
00119 lenS = strlen( s );
00120 }
00121 length = XML->length + lenS + 1;
00122 if( XML->allocated < length ) {
00123 inc = ( 140 * XML->allocated ) / 100;
00124 if( inc < 10000 ) inc = 10000;
00125 if( length < inc ) length = inc;
00126 XML->text = (char *) realloc( XML->text, length );
00127 XML->allocated = length;
00128 if( XML->text == NULL ) return( 1 );
00129 }
00130 p = &(XML->text[XML->length]);
00131 if( len >= 0 ) {
00132 strncpy( p, s, len ); }
00133 else {
00134 strcpy( p, s );
00135 }
00136 XML->length += lenS;
00137 return( 0 );
00138 }
00139
00140
00141
00142 int xData_parseOutline( FILE *f, xData_document *xData_doc, int outputText ) {
00143
00144 xData_parseOutlinePrintRoot( f, &(xData_doc->root), outputText );
00145 return( 0 );
00146 }
00147
00148
00149
00150 static void xData_parseOutlinePrintRoot( FILE *f, xData_rootElement *root, int outputText ) {
00151
00152 int i, depth = root->depth;
00153 xData_element *child;
00154
00155 for( child = root->children; child != NULL; child = child->next ) {
00156 for( i = 0; i < depth; i++ ) fprintf( f, " " );
00157 fprintf( f, "name = %s at line = %ld column = %ld textOffset = %ld; attris:",
00158
00159 child->name, (long int)child->docInfo.line, (long int)child->docInfo.column, (long int)child->textOffset );
00160 for( i = 0; i < child->attributes.number; i++ ) {
00161 fprintf( f, " %s = \"%s\"", child->attributes.attributes[i].name, child->attributes.attributes[i].value );
00162 }
00163 if( outputText && child->text.text != NULL ) fprintf( f, "; text: %s", child->text.text );
00164 fprintf( f, "\n" );
00165 xData_parseOutlinePrintRoot( f, &(child->childrenRoot), outputText );
00166 }
00167 }
00168
00169 #if defined __cplusplus
00170 }
00171 #endif