1 /************************************************************************/
2 /*									*/
3 /*  Elementary XML writing functionality.				*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"appUtilConfig.h"
8 
9 #   include	<stdio.h>
10 #   include	<string.h>
11 
12 #   include	"sioGeneral.h"
13 
14 #   include	"xmlWriter.h"
15 
16 /************************************************************************/
17 
xmlInitXmlWriter(XmlWriter * xw)18 void xmlInitXmlWriter(	XmlWriter *	xw )
19     {
20     xw->xwSos= (SimpleOutputStream *)0;
21     xw->xwCrlf= 0;
22     xw->xwColumn= 0;
23     }
24 
25 /************************************************************************/
26 /*									*/
27 /*  Save a tag with an argument.					*/
28 /*									*/
29 /************************************************************************/
30 
xmlPutString(const char * s,XmlWriter * xw)31 void xmlPutString(		const char *		s,
32 				XmlWriter *		xw )
33     {
34     SimpleOutputStream *	sos= xw->xwSos;
35     int				col= xw->xwColumn;
36 
37     /*  mime  */
38     if  ( col == 0 && s[0] == '-' )
39 	{ sioOutPutString( "&#45;", sos ); col += 5; s++; }
40 
41     sioOutPutString( s, sos ); col += strlen( s );
42 
43     xw->xwColumn= col; return;
44     }
45 
xmlNewLine(XmlWriter * xw)46 void xmlNewLine(	XmlWriter *	xw )
47     {
48     SimpleOutputStream *	sos= xw->xwSos;
49 
50     if  ( xw->xwCrlf )
51 	{ (void)sioOutPutByte( '\r', sos );	}
52 
53     (void)sioOutPutByte( '\n', sos );
54     xw->xwColumn= 0;
55     }
56 
xmlWriteStringAttribute(XmlWriter * xw,const char * name,const char * value)57 void xmlWriteStringAttribute(	XmlWriter *		xw,
58 					const char *		name,
59 					const char *		value )
60     {
61     if  ( xw->xwColumn > 1				&&
62 	  xw->xwColumn+ 1+ strlen( name )+ 1+ 3 > 76	)
63 	{ sioOutPutString( "\n", xw->xwSos ); xw->xwColumn= 0;	}
64     else{ sioOutPutString( " ", xw->xwSos ); (xw->xwColumn)++;	}
65 
66     sioOutPutString( name, xw->xwSos ); (xw->xwColumn) += strlen( name );
67     sioOutPutString( "=\"", xw->xwSos ); (xw->xwColumn) += 2;
68     xmlEscapeCharacters( xw, value, strlen( value ) );
69     sioOutPutString( "\"", xw->xwSos ); (xw->xwColumn) += 1;
70 
71     return;
72     }
73 
xmlWriteRgb8Attribute(XmlWriter * xw,const char * name,const RGB8Color * rgb8)74 void xmlWriteRgb8Attribute(	XmlWriter *		xw,
75 				const char *		name,
76 				const RGB8Color *	rgb8 )
77     {
78     char	scratch[20+1];
79 
80     sprintf( scratch, "#%02x%02x%02x",
81 			rgb8->rgb8Red, rgb8->rgb8Green, rgb8->rgb8Blue );
82 
83     xmlWriteStringAttribute( xw, name, scratch );
84     }
85 
xmlWriteIntAttribute(XmlWriter * xw,const char * name,int value)86 void xmlWriteIntAttribute(		XmlWriter *		xw,
87 					const char *		name,
88 					int			value )
89     {
90     char	scratch[20];
91 
92     sprintf( scratch, "%d", value );
93 
94     xmlWriteStringAttribute( xw, name, scratch );
95 
96     return;
97     }
98 
xmlEscapeCharacters(XmlWriter * xw,const char * ss,int len)99 void xmlEscapeCharacters(	XmlWriter *		xw,
100 				const char *		ss,
101 				int			len )
102     {
103     const unsigned char *	us= (const unsigned char *)ss;
104 
105     while( len > 0 )
106 	{
107 	const char *	escaped= (const char *)0;
108 	char		scratch[10];
109 
110 	if  ( ( *us == '-' && xw->xwColumn == 0 )	||
111 	      *us == '<'				||
112 	      *us == '>'				||
113 	      *us == '&'				||
114 	      *us == '\''				||
115 	      *us == '"'				)
116 	    { sprintf( scratch, "&#%03d;", *us ); escaped= scratch;	}
117 
118 	if  ( escaped )
119 	    {
120 	    sioOutPutString( escaped, xw->xwSos );
121 	    (xw->xwColumn) += strlen( escaped );
122 	    }
123 	else{
124 	    (void)sioOutPutByte( *us, xw->xwSos );
125 	    (xw->xwColumn)++;
126 	    }
127 
128 	us++; len--;
129 	}
130 
131     return;
132     }
133 
xmlEscapeBuffer(XmlWriter * xw,const MemoryBuffer * mb)134 void xmlEscapeBuffer(	XmlWriter *		xw,
135 			const MemoryBuffer *	mb )
136     {
137     xmlEscapeCharacters( xw, (const char *)mb->mbBytes, mb->mbSize );
138     }
139