ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/doc/doc2c++.xsl
Revision: 1.1
Committed: Wed Jul 30 12:28:11 2003 UTC (20 years, 10 months ago) by pcg
Content type: application/xml
Branch: MAIN
CVS Tags: stable, HEAD
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 <!DOCTYPE xsl:stylesheet>
2     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3    
4     <xsl:output method="text" media-type="text/plain" encoding="utf-8"/>
5    
6     <xsl:template match="/"><![CDATA[
7     // This is an automatically generated file.
8     // This is an automatically generated file.
9     // This is an automatically generated file.
10     // This is an automatically generated file.
11     // This is an automatically generated file.
12    
13     // See doc/protocol.xml and doc/doc2C.xsl
14    
15     // this is NOT really c++, but it should compile as such
16    
17     // this code is untested and just exists for demo purposes.
18     // please test and improve :)
19    
20     #include <assert.h>
21    
22     #include <...> should provide
23     typedef unsigned char U16;
24     typedef unsigned short U16;
25     typedef unsigned int U32;
26     typedef unsigned long long U64;
27     typedef signed char I8;
28     typedef signed short I16;
29     typedef signed int I32;
30     typedef signed long long I64;
31    
32     // yeah, not very elegant
33     static U8 *data;
34     static U16 dlen;
35    
36     static U8
37     dec_U8 (void)
38     {
39     assert (dlen > 0);
40     dlen--;
41     return *data++;
42     }
43    
44     static U16
45     dec_U16 (void)
46     {
47     U16 l = dec_U8 ();
48     U16 h = dec_U8 ();
49    
50     return l | (h << 8);
51     }
52    
53     static U32
54     dec_U32 (void)
55     {
56     U32 l = dec_U16 ();
57     U32 h = dec_U16 ();
58    
59     return l | (h << 16);
60     }
61    
62     static U64
63     dec_U64 (void)
64     {
65     U64 l = dec_U32 ();
66     U64 h = dec_U32 ();
67    
68     return l | (h << 32);
69     }
70    
71     #define dec_I8() ((I8)dec_U8 ())
72     #define dec_I16() ((I16)dec_U16 ())
73     #define dec_I32() ((I32)dec_U32 ())
74     #define dec_I64() ((I64)dec_U64 ())
75    
76     // dec_DATA
77     // dec_STRING n
78     // dec_CONSTANT
79    
80     #define dec_password() dec_U64()
81    
82     static void
83     enc_U8 (U8 d)
84     {
85     assert (dlen > 0);
86     dlen--;
87     *data++ = d;
88     }
89    
90     #define enc_U16(d) enc_U8 (d); enc_U16 ((d) >> 8)
91     #define enc_U32(d) enc_U16 (d); enc_U16 ((d) >> 16)
92     #define enc_U64(d) enc_U32 (d); enc_U32 ((d) >> 32)
93     #define enc_I8(d) enc_U8 ((U8 )d)
94     #define enc_I16(d) enc_U16 ((U16)d)
95     #define enc_I32(d) enc_U32 ((U32)d)
96     #define enc_I64(d) enc_U64 ((U64)d)
97    
98     // enc_DATA
99     // enc_STRING ,n
100     // enc_CONSTANT
101    
102     // enc_password
103     // # $hash must be 64 bit
104     // my $hash = new Math::BigInt;
105     // $hash = $hash * 1055 + ord for split //, $_[0];
106     // enc_U64 $hash;
107    
108     ]]>
109    
110     #############################################################################
111     # types
112     <xsl:apply-templates select="descendant::type"/>
113    
114     #############################################################################
115     # structures
116     <xsl:apply-templates select="descendant::struct"/>
117    
118     #############################################################################
119     # "less" primitive types<![CDATA[
120    
121     // dec_TREE
122     // enc_TREE
123     }
124     ]]>
125    
126     #############################################################################
127     # messages
128     <xsl:apply-templates select="descendant::message"/>
129     }
130    
131     1;
132     </xsl:template>
133    
134     <xsl:template match="type[@type = 'S']">
135    
136     static U16 *
137     dec_<xsl:value-of select="@name"/> ()
138     {
139     return dec_STRING (<xsl:value-of select="@length"/>);
140     }
141    
142     static void
143     enc_<xsl:value-of select="@name"/> (const U16 *s)
144     {
145     enc_STRING (s, <xsl:value-of select="@length"/>);
146     }
147     </xsl:template>
148    
149     <xsl:template match="type[@type = 'A']">
150    
151     static char *
152     dec_<xsl:value-of select="@name"/> ()
153     {
154     return dec_ASCIZ (<xsl:value-of select="@length"/>);
155     }
156    
157     static void
158     enc_<xsl:value-of select="@name"/> (const char *s)
159     {
160     enc_ASCIZ (s, <xsl:value-of select="@length"/>);
161     }
162     </xsl:template>
163    
164     <xsl:template match="type[@multiplier]">
165     static float
166     dec_<xsl:value-of select="@name"/> ()
167     {
168     (1. / <xsl:value-of select="@multiplier"/>) * dec_<xsl:value-of select="@type"/> ();
169     }
170    
171     static void
172     enc_<xsl:value-of select="@name"/> (float f)
173     {
174     enc_<xsl:value-of select="@type"/> (f * <xsl:value-of select="@multiplier"/>);
175     }
176     </xsl:template>
177    
178     <xsl:template match="member" mode="decl">
179     <xsl:text> </xsl:text><xsl:value-of select="@type"/><xsl:text> </xsl:text><xsl:value-of select="@name"/>;
180     </xsl:template>
181    
182     <xsl:template match="member[@array = 'yes']" mode="dec">
183     while (dlen)
184     r.<xsl:value-of select="@name"/>->append (dec_<xsl:value-of select="@type"/> ());
185     </xsl:template>
186    
187     <xsl:template match="member" mode="dec">
188     <xsl:if test="@guard-cond">
189     if (r.<xsl:value-of select="@guard-member"/> <xsl:value-of select="@guard-cond"/>)</xsl:if>
190     r.<xsl:value-of select="@name"/> = dec_<xsl:value-of select="@type"/> ();
191     </xsl:template>
192    
193     <xsl:template match="member" mode="enc">
194     enc_<xsl:value-of select="@type"/> (r.<xsl:value-of select="@name"/>);
195     </xsl:template>
196    
197     <xsl:template match="struct">
198     struct KGS_<xsl:value-of select="@name"/> {
199     <xsl:apply-templates select="member" mode="decl"/>};
200    
201     static const struct KGS_<xsl:value-of select="@name"/> &amp;
202     dec_<xsl:value-of select="@name"/> ()
203     {
204     struct KGS_<xsl:value-of select="@name"/> r;
205    
206     <xsl:apply-templates select="member" mode="dec"/>
207    
208     return r;
209     }
210    
211     static void
212     enc_<xsl:value-of select="@name"/> (const struct KGS_<xsl:value-of select="@name"/> &amp;r)
213     {
214     <xsl:apply-templates select="member" mode="enc"/>
215     }
216     </xsl:template>
217    
218     <xsl:template match="message">
219     struct KGS_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> {
220     <xsl:apply-templates select="member" mode="decl"/>};
221    
222     static struct KGS_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> &amp;
223     dec_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> ()
224     {
225     struct KGS_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> r;
226    
227     r.type = MSG_<xsl:value-of select="@name"/>;
228     <xsl:apply-templates select="member" mode="dec"/>
229     }
230    
231     static void
232     enc_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> (const struct KGS_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> &amp;r)
233     {
234     enc_U16 (0x<xsl:value-of select="@type"/>);
235     <xsl:apply-templates select="member" mode="enc"/>
236     }
237    
238     </xsl:template>
239    
240     <xsl:template match="text()">
241     </xsl:template>
242    
243     </xsl:stylesheet>
244