ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/doc/doc2haskell.xsl
Revision: 1.5
Committed: Fri Jul 25 22:40:53 2003 UTC (20 years, 10 months ago) by pcg
Content type: application/xml
Branch: MAIN
Changes since 1.4: +18 -23 lines
Log Message:
*** empty log message ***

File Contents

# Content
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/doc2haskell.xsl
14
15 -- primitive enc/decoders
16
17 import Data.Word
18 import Control.Monad.State
19
20 hasData :: State [Word8] Bool
21 hasData = do
22 xs <- get
23 return $ not $ null xs
24
25 getU8 :: State [Word8] Int
26 getU8 = do
27 (x:xs) <- get
28 put xs
29 return x
30
31 getU16 :: State [Word8] Int
32 getU16 = do
33 a <- getU8
34 b <- getU8
35 return $ a + b * 256
36
37 decU64
38 decI8
39 decI16
40 decI32
41 decDATA
42
43 sub dec_STRING {
44 $data =~ s/^((?:..)*?)(?:\x00\x00|\Z)//s;
45 # use Encode...
46 join "", map chr, unpack "v*", $1;
47 }
48
49 sub dec_CONSTANT {
50 $_[0];
51 }
52
53 sub dec_password {
54 dec_U64;
55 }
56
57 sub dec_HEX { # for debugging
58 "HEX: " . unpack "H*", $data;#d#
59 }
60
61 #############################################################################
62
63 sub enc_U8 {
64 $data .= pack "C", $_[0];
65 }
66
67 sub enc_U16 {
68 $data .= pack "v", $_[0];
69 }
70
71 sub enc_U32 {
72 $data .= pack "V", $_[0];
73 }
74
75 sub enc_U64 {
76 enc_U32 $_[0] & 0xffffffff;
77 enc_U32 +($_[0] >> 32) & 0xffffffff;
78 }
79
80 sub enc_I8 {
81 $data .= pack "c", $_[0];
82 }
83
84 sub enc_I16 {
85 enc_U16 unpack "S", pack "s", $_[0];
86 }
87
88 sub enc_I32 {
89 enc_U32 unpack "I", pack "i", $_[0];
90 }
91
92 sub enc_DATA {
93 # a dream!
94 $data .= $_[0];
95 }
96
97 sub enc_STRING {
98 # should use encode for speed and clarity ;)
99 $data .= pack "v*", map ord, split //, $_[0];
100 }
101
102 sub enc_CONSTANT {
103 # nop
104 }
105
106 sub enc_password {
107 require Math::BigInt; # I insist on 32-bit-perl.. should use C
108 # $hash must be 64 bit
109 my $hash = new Math::BigInt;
110 $hash = $hash * 1055 + ord for split //, $_[0];
111 enc_U64 $hash;
112 }
113
114 sub enc_HEX {
115 die "enc_HEX not defined for good";
116 }
117
118 ]]>
119
120 #############################################################################
121 # types
122 <xsl:apply-templates select="descendant::type"/>
123
124 #############################################################################
125 # structures
126 <xsl:apply-templates select="descendant::struct"/>
127
128 #############################################################################
129 # "less" primitive types<![CDATA[
130
131 decTREE
132 encTREE
133
134 ]]>
135
136 #############################################################################
137 # messages
138 <xsl:apply-templates select="descendant::message"/>
139 }
140
141 1;
142 </xsl:template>
143
144 <xsl:template match="type[@type = 'S']">
145 dec<xsl:value-of select="@name"/> :: State [Word8] [Char]
146 dec<xsl:value-of select="@name"/> = do
147 -- decode UCS-2 string of length <xsl:value-of select="@length"/>
148 -- the first 0 terminates the string, but not the field
149
150 enc<xsl:value-of select="@name"/> =
151 -- likewise, encode as UCS-2, fill space with 0
152
153 </xsl:template>
154
155 <xsl:template match="type[@type = 'A']">
156 dec<xsl:value-of select="@name"/> :: State [Word8] [Char]
157 dec<xsl:value-of select="@name"/> = do
158 -- decode ASCII string of length <xsl:value-of select="@length"/>
159 -- the first 0 terminates the string, but not the field
160
161 enc<xsl:value-of select="@name"/> =
162 -- likewise, encode as ASCII, fill space with 0
163
164 </xsl:template>
165
166 <xsl:template match="type[@multiplier]">
167 dec<xsl:value-of select="@name"/> = do
168 n &lt;- dec<xsl:value-of select="@type"/>
169 return n * (1 / <xsl:value-of select="@multiplier"/>)
170
171 enc<xsl:value-of select="@name"/> n =
172 enc<xsl:value-of select="@type"/> $ n * <xsl:value-of select="@multiplier"/>
173
174 </xsl:template>
175
176 <xsl:template match="member[@array = 'yes']" mode="dec">
177 $r->{<xsl:value-of select="@name"/>} = (my $array = []);
178 while (length $data) {
179 push @$array, dec_<xsl:value-of select="@type"/>
180 <xsl:text> </xsl:text>
181 <xsl:value-of select="@default"/>;
182 }
183 </xsl:template>
184
185 <xsl:template match="member" mode="dec">
186 <xsl:if test="@guard-cond">
187 -- decode next only when <xsl:value-of select="@guard-member"/> <xsl:value-of select="@guard-cond"/> --
188 </xsl:if>
189 dec<xsl:value-of select="@type"/> -- default <xsl:value-of select="@default"/> --
190 </xsl:template>
191
192 <xsl:template match="member" mode="enc">
193 enc<xsl:value-of select="@type"/> defined $_[0]{<xsl:value-of select="@name"/>} ? $_[0]{<xsl:value-of select="@name"/> s
194 <xsl:text>} : (</xsl:text>
195 <xsl:value-of select="@default"/>
196 <xsl:text>);</xsl:text>
197 </xsl:template>
198
199 <xsl:template match="struct">
200 dec<xsl:value-of select="@name"/> = do
201 <xsl:apply-templates select="member" mode="dec"/>
202 <xsl:if test="@class">
203 -- result has class <xsl:value-of select="@class"/> --
204 </xsl:if>
205
206 enc<xsl:value-of select="@name"/> s =
207 <xsl:apply-templates select="member" mode="enc"/>
208
209 </xsl:template>
210
211 <xsl:template match="message">
212 # <xsl:value-of select="@name"/>
213 dec<xsl:value-of select="@src"/>_<xsl:value-of select="@type"/> = do
214 -- type "<xsl:value-of select="@name"/>" --
215 <xsl:apply-templates select="member" mode="dec"/>
216
217 enc<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> =
218 encU16 0x<xsl:value-of select="@type"/>
219 <xsl:apply-templates select="member" mode="enc"/>
220
221 </xsl:template>
222
223 <xsl:template match="member">
224 [<xsl:value-of select="@name"/>
225 <xsl:text>=> "</xsl:text>
226 <xsl:value-of select="@type"/>
227 <xsl:text>", "</xsl:text>
228 <xsl:value-of select="@default"/>
229 <xsl:text>", "</xsl:text>
230 <xsl:value-of select="@guard"/>
231 <xsl:text>;"],</xsl:text>
232 </xsl:template>
233
234 <xsl:template match="text()">
235 </xsl:template>
236
237 </xsl:stylesheet>
238