ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/doc/doc2haskell.xsl
Revision: 1.8
Committed: Sat Jul 26 02:08:29 2003 UTC (20 years, 10 months ago) by pcg
Content type: application/xml
Branch: MAIN
Changes since 1.7: +0 -11 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 dec_U8 :: State [Word8] Int
26 dec_U8 = do
27 (x:xs) <- get
28 put xs
29 return x
30
31 dec_U16 :: State [Word8] Int
32 dec_U16 = do
33 a <- getU8
34 b <- getU8
35 return $ a + b * 256
36
37 dec_U64
38 dec_I8
39 dec_I16
40 dec_I32
41 dec_DATA
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 dec_TREE
132 enc_TREE
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
175 </xsl:template>
176
177 <xsl:template match="member[@array = 'yes']" mode="dec">
178 $r->{<xsl:value-of select="@name"/>} = (my $array = []);
179 while (length $data) {
180 push @$array, dec_<xsl:value-of select="@type"/>
181 <xsl:text> </xsl:text>
182 <xsl:value-of select="@default"/>;
183 }
184
185 </xsl:template>
186
187 <xsl:template match="member" mode="dec">
188 <xsl:if test="@guard-cond">
189 -- decode next only when <xsl:value-of select="@guard-member"/> <xsl:value-of select="@guard-cond"/> --
190 </xsl:if>
191 <xsl:text> </xsl:text><xsl:value-of select="@name"/> &lt;- dec<xsl:value-of select="@type"/> -- default <xsl:value-of select="@default"/> --
192 </xsl:template>
193
194 <xsl:template match="member" mode="enc">
195 enc<xsl:value-of select="@type"/> defined $_[0]{<xsl:value-of select="@name"/>} ? $_[0]{<xsl:value-of select="@name"/> s
196 <xsl:text>} : (</xsl:text>
197 <xsl:value-of select="@default"/>
198 <xsl:text>);</xsl:text>
199 </xsl:template>
200
201 <xsl:template match="struct">
202 dec_<xsl:value-of select="@name"/> = do
203 <xsl:apply-templates select="member" mode="dec"/>
204 <xsl:if test="@class">
205 -- result has class <xsl:value-of select="@class"/> --
206 </xsl:if>
207
208 enc_<xsl:value-of select="@name"/> s =
209 <xsl:apply-templates select="member" mode="enc"/>
210
211 </xsl:template>
212
213 <xsl:template match="message">
214 -- <xsl:value-of select="@name"/>
215 dec_<xsl:value-of select="@src"/>_<xsl:value-of select="@type"/> = do
216 -- type "<xsl:value-of select="@name"/>" --
217 <xsl:apply-templates select="member" mode="dec"/>
218
219 enc_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> =
220 encU16 0x<xsl:value-of select="@type"/>
221 <xsl:apply-templates select="member" mode="enc"/>
222
223 </xsl:template>
224
225 <xsl:template match="text()">
226 </xsl:template>
227
228 </xsl:stylesheet>
229