xDataExtras.cc

Go to the documentation of this file.
00001 /*
00002 # <<BEGIN-copyright>>
00003 # Copyright (c) 2010, Lawrence Livermore National Security, LLC. 
00004 # Produced at the Lawrence Livermore National Laboratory 
00005 # Written by Bret R. Beck, beck6@llnl.gov. 
00006 # CODE-461393
00007 # All rights reserved. 
00008 #  
00009 # This file is part of GIDI. For details, see nuclear.llnl.gov. 
00010 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov. 
00011 # 
00012 # Redistribution and use in source and binary forms, with or without modification, 
00013 # are permitted provided that the following conditions are met: 
00014 #
00015 #      1) Redistributions of source code must retain the above copyright notice, 
00016 #         this list of conditions and the disclaimer below.
00017 #      2) Redistributions in binary form must reproduce the above copyright notice, 
00018 #         this list of conditions and the disclaimer (as noted below) in the 
00019 #          documentation and/or other materials provided with the distribution.
00020 #      3) Neither the name of the LLNS/LLNL nor the names of its contributors may be 
00021 #         used to endorse or promote products derived from this software without 
00022 #         specific prior written permission. 
00023 #
00024 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
00025 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00026 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT 
00027 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR 
00028 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00029 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00030 # OR SERVICES;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
00031 # AND ON  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00032 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00034 # <<END-copyright>>
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     //if( err == 0 ) addStringToXML( &XML, "\n", -1 );
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         //if( textOffset < child->textOffset ) {
00079         if( textOffset < (int) child->textOffset ) {
00080             if( addStringToXML( XML, &(text[textOffset]), child->textOffset - textOffset ) ) return( 1 );
00081             textOffset = child->textOffset;
00082         }
00083         //if( addStringToXML( XML, "<", -1 ) != 0 ) return( 1 );
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             //if( addStringToXML( XML, " ", -1 ) != 0 ) return( 1 );
00088             if( addStringToXML( XML, (char*) " ", -1 ) != 0 ) return( 1 );
00089             if( addStringToXML( XML, child->attributes.attributes[i].name, -1 ) != 0 ) return( 1 );
00090             //if( addStringToXML( XML, "=\"", -1 ) != 0 ) return( 1 );
00091             if( addStringToXML( XML, (char*) "=\"", -1 ) != 0 ) return( 1 );
00092             if( addStringToXML( XML, child->attributes.attributes[i].value, -1 ) != 0 ) return( 1 );
00093             //if( addStringToXML( XML, "\"", -1 ) != 0 ) return( 1 );
00094             if( addStringToXML( XML, (char*) "\"", -1 ) != 0 ) return( 1 );
00095         }
00096         //if( addStringToXML( XML, ">", -1 ) != 0 ) return( 1 );
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         //if( addStringToXML( XML, "</", -1 ) != 0 ) return( 1 );
00100         if( addStringToXML( XML, (char*) "</", -1 ) != 0 ) return( 1 );
00101         if( addStringToXML( XML, child->name, -1 ) != 0 ) return( 1 );
00102         //if( addStringToXML( XML, ">", -1 ) != 0 ) return( 1 );
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             //child->name, child->docInfo.line, child->docInfo.column, child->textOffset );
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

Generated on Mon May 27 17:50:36 2013 for Geant4 by  doxygen 1.4.7