Geant4  10.01.p03
xDataExtras.cc
Go to the documentation of this file.
1 /*
2 # <<BEGIN-copyright>>
3 # Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4 # Produced at the Lawrence Livermore National Laboratory
5 # Written by Bret R. Beck, beck6@llnl.gov.
6 # CODE-461393
7 # All rights reserved.
8 #
9 # This file is part of GIDI. For details, see nuclear.llnl.gov.
10 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # 1) Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the disclaimer below.
17 # 2) Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the disclaimer (as noted below) in the
19 # documentation and/or other materials provided with the distribution.
20 # 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21 # used to endorse or promote products derived from this software without
22 # specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # <<END-copyright>>
35 */
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "xData.h"
41 
42 #if defined __cplusplus
43 namespace GIDI {
44 using namespace GIDI;
45 #endif
46 
47 typedef struct xmlTextStruct_s {
48  long allocated;
49  long length;
50  char *text;
52 
53 static int xData_parseReconstructXML2( xData_rootElement *root, xmlTextStruct *XML, char *text, long textLength );
54 static int addStringToXML( xmlTextStruct *XML, char *s, long len );
55 static void xData_parseOutlinePrintRoot( FILE *f, xData_rootElement *root, int outputText );
56 /*
57 ************************************************************
58 */
59 char *xData_parseReconstructXML( xData_document *xData_doc ) {
60 
61  int err;
62  xmlTextStruct XML = { 0, 0, NULL };
63 
64  err = xData_parseReconstructXML2( &(xData_doc->root), &XML, NULL, 0 );
65  //if( err == 0 ) addStringToXML( &XML, "\n", -1 );
66  if( err == 0 ) addStringToXML( &XML, (char*) "\n", -1 );
67  return( XML.text );
68 }
69 /*
70 ************************************************************
71 */
72 static int xData_parseReconstructXML2( xData_rootElement *root, xmlTextStruct *XML, char *text, long textLength ) {
73 
74  long i, textOffset = 0;
75  xData_element *child;
76 
77  for( child = root->children; child != NULL; child = child->next ) {
78  //if( textOffset < child->textOffset ) {
79  if( textOffset < (int) child->textOffset ) {
80  if( addStringToXML( XML, &(text[textOffset]), child->textOffset - textOffset ) ) return( 1 );
81  textOffset = child->textOffset;
82  }
83  //if( addStringToXML( XML, "<", -1 ) != 0 ) return( 1 );
84  if( addStringToXML( XML, (char*) "<", -1 ) != 0 ) return( 1 );
85  if( addStringToXML( XML, child->name, -1 ) != 0 ) return( 1 );
86  for( i = 0; i < child->attributes.number; i++ ) {
87  //if( addStringToXML( XML, " ", -1 ) != 0 ) return( 1 );
88  if( addStringToXML( XML, (char*) " ", -1 ) != 0 ) return( 1 );
89  if( addStringToXML( XML, child->attributes.attributes[i].name, -1 ) != 0 ) return( 1 );
90  //if( addStringToXML( XML, "=\"", -1 ) != 0 ) return( 1 );
91  if( addStringToXML( XML, (char*) "=\"", -1 ) != 0 ) return( 1 );
92  if( addStringToXML( XML, child->attributes.attributes[i].value, -1 ) != 0 ) return( 1 );
93  //if( addStringToXML( XML, "\"", -1 ) != 0 ) return( 1 );
94  if( addStringToXML( XML, (char*) "\"", -1 ) != 0 ) return( 1 );
95  }
96  //if( addStringToXML( XML, ">", -1 ) != 0 ) return( 1 );
97  if( addStringToXML( XML, (char*) ">", -1 ) != 0 ) return( 1 );
98  if( xData_parseReconstructXML2( &(child->childrenRoot), XML, child->text.text, child->text.length ) != 0 ) return( 1 );
99  //if( addStringToXML( XML, "</", -1 ) != 0 ) return( 1 );
100  if( addStringToXML( XML, (char*) "</", -1 ) != 0 ) return( 1 );
101  if( addStringToXML( XML, child->name, -1 ) != 0 ) return( 1 );
102  //if( addStringToXML( XML, ">", -1 ) != 0 ) return( 1 );
103  if( addStringToXML( XML, (char*) ">", -1 ) != 0 ) return( 1 );
104  }
105  if( textOffset < textLength ) if( addStringToXML( XML, &(text[textOffset]), textLength - textOffset ) ) return( 1 );
106  return( 0 );
107 }
108 /*
109 ************************************************************
110 */
111 static int addStringToXML( xmlTextStruct *XML, char *s, long len ) {
112 
113  long lenS, length, inc;
114  char *p;
115 
116  if( len >= 0 ) {
117  lenS = len; }
118  else {
119  lenS = strlen( s );
120  }
121  length = XML->length + lenS + 1;
122  if( XML->allocated < length ) {
123  inc = ( 140 * XML->allocated ) / 100;
124  if( inc < 10000 ) inc = 10000;
125  if( length < inc ) length = inc;
126  XML->text = (char *) realloc( XML->text, length );
127  XML->allocated = length;
128  if( XML->text == NULL ) return( 1 );
129  }
130  p = &(XML->text[XML->length]);
131  if( len >= 0 ) {
132  strncpy( p, s, len ); }
133  else {
134  strcpy( p, s );
135  }
136  XML->length += lenS;
137  return( 0 );
138 }
139 /*
140 ************************************************************
141 */
142 int xData_parseOutline( FILE *f, xData_document *xData_doc, int outputText ) {
143 
144  xData_parseOutlinePrintRoot( f, &(xData_doc->root), outputText );
145  return( 0 );
146 }
147 /*
148 ************************************************************
149 */
150 static void xData_parseOutlinePrintRoot( FILE *f, xData_rootElement *root, int outputText ) {
151 
152  int i, depth = root->depth;
153  xData_element *child;
154 
155  for( child = root->children; child != NULL; child = child->next ) {
156  for( i = 0; i < depth; i++ ) fprintf( f, " " );
157  fprintf( f, "name = %s at line = %ld column = %ld textOffset = %ld; attris:",
158  //child->name, child->docInfo.line, child->docInfo.column, child->textOffset );
159  child->name, (long int)child->docInfo.line, (long int)child->docInfo.column, (long int)child->textOffset );
160  for( i = 0; i < child->attributes.number; i++ ) {
161  fprintf( f, " %s = \"%s\"", child->attributes.attributes[i].name, child->attributes.attributes[i].value );
162  }
163  if( outputText && child->text.text != NULL ) fprintf( f, "; text: %s", child->text.text );
164  fprintf( f, "\n" );
165  xData_parseOutlinePrintRoot( f, &(child->childrenRoot), outputText );
166  }
167 }
168 
169 #if defined __cplusplus
170 }
171 #endif
static int xData_parseReconstructXML2(xData_rootElement *root, xmlTextStruct *XML, char *text, long textLength)
Definition: xDataExtras.cc:72
static const double s
Definition: G4SIunits.hh:150
static int addStringToXML(xmlTextStruct *XML, char *s, long len)
Definition: xDataExtras.cc:111
char * xData_parseReconstructXML(xData_document *xData_doc)
Definition: xDataExtras.cc:59
static void xData_parseOutlinePrintRoot(FILE *f, xData_rootElement *root, int outputText)
Definition: xDataExtras.cc:150
int xData_parseOutline(FILE *f, xData_document *xData_doc, int outputText)
Definition: xDataExtras.cc:142
struct xmlTextStruct_s xmlTextStruct