Rev 3262 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3262 | Rev 3263 | ||
---|---|---|---|
1 | # |
1 | # |
2 | # Copyright (c) 2008 Martin Decky |
2 | # Copyright (c) 2008 Martin Decky |
3 | # All rights reserved. |
3 | # All rights reserved. |
4 | # |
4 | # |
5 | # Redistribution and use in source and binary forms, with or without |
5 | # Redistribution and use in source and binary forms, with or without |
6 | # modification, are permitted provided that the following conditions |
6 | # modification, are permitted provided that the following conditions |
7 | # are met: |
7 | # are met: |
8 | # |
8 | # |
9 | # - Redistributions of source code must retain the above copyright |
9 | # - Redistributions of source code must retain the above copyright |
10 | # notice, this list of conditions and the following disclaimer. |
10 | # notice, this list of conditions and the following disclaimer. |
11 | # - Redistributions in binary form must reproduce the above copyright |
11 | # - Redistributions in binary form must reproduce the above copyright |
12 | # notice, this list of conditions and the following disclaimer in the |
12 | # notice, this list of conditions and the following disclaimer in the |
13 | # documentation and/or other materials provided with the distribution. |
13 | # documentation and/or other materials provided with the distribution. |
14 | # - The name of the author may not be used to endorse or promote products |
14 | # - The name of the author may not be used to endorse or promote products |
15 | # derived from this software without specific prior written permission. |
15 | # derived from this software without specific prior written permission. |
16 | # |
16 | # |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | # |
27 | # |
28 | """ |
28 | """ |
29 | Convert descriptive structure definitions to structure object |
29 | Convert descriptive structure definitions to structure object |
30 | """ |
30 | """ |
31 | 31 | ||
32 | import struct |
32 | import struct |
33 | 33 | ||
34 | class Struct: |
34 | class Struct: |
35 | def size(self): |
35 | def size(self): |
36 | return struct.calcsize(self._format_) |
36 | return struct.calcsize(self._format_) |
37 | 37 | ||
38 | def pack(self): |
38 | def pack(self): |
39 | list = [] |
39 | list = [] |
40 | for variable in self._list_: |
40 | for variable in self._list_: |
41 | list.append(self.__dict__[variable]) |
41 | list.append(self.__dict__[variable]) |
42 | 42 | ||
43 | return struct.pack(self._format_, *list) |
43 | return struct.pack(self._format_, *list) |
44 | 44 | ||
45 | def create(definition): |
45 | def create(definition): |
46 | "Create structure object" |
46 | "Create structure object" |
47 | 47 | ||
48 | tokens = definition.split(None) |
48 | tokens = definition.split(None) |
49 | 49 | ||
50 | # Initial byte order tag |
50 | # Initial byte order tag |
51 | format = { |
51 | format = { |
52 | "little:": lambda: "<", |
52 | "little:": lambda: "<", |
53 | "big:": lambda: ">", |
53 | "big:": lambda: ">", |
54 | "network:": lambda: "!" |
54 | "network:": lambda: "!" |
55 | }[tokens[0]]() |
55 | }[tokens[0]]() |
56 | inst = Struct() |
56 | inst = Struct() |
57 | list = [] |
57 | list = [] |
58 | 58 | ||
59 | # Member tags |
59 | # Member tags |
60 | comment = False |
60 | comment = False |
61 | variable = False |
61 | variable = None |
62 | for token in tokens[1:]: |
62 | for token in tokens[1:]: |
63 | if (comment): |
63 | if (comment): |
64 | if (token == "*/"): |
64 | if (token == "*/"): |
65 | comment = False |
65 | comment = False |
66 | continue |
66 | continue |
67 | 67 | ||
68 | if (variable): |
- | |
69 | inst.__dict__[token] = None |
- | |
70 | list.append(token) |
- | |
71 | variable = False |
- | |
72 | continue |
- | |
73 | - | ||
74 | if (token == "/*"): |
68 | if (token == "/*"): |
75 | comment = True |
69 | comment = True |
- | 70 | continue |
|
- | 71 | ||
- | 72 | if (variable != None): |
|
- | 73 | subtokens = token.split("[") |
|
- | 74 | ||
- | 75 | if (len(subtokens) > 1): |
|
- | 76 | format += "%d" % int(subtokens[1].split("]")[0]) |
|
- | 77 | ||
- | 78 | format += variable |
|
- | 79 | ||
- | 80 | inst.__dict__[subtokens[0]] = None |
|
- | 81 | list.append(subtokens[0]) |
|
- | 82 | ||
- | 83 | variable = None |
|
- | 84 | continue |
|
- | 85 | ||
76 | elif (token[0:8] == "padding["): |
86 | if (token[0:8] == "padding["): |
77 | size = token[8:].split("]")[0] |
87 | size = token[8:].split("]")[0] |
78 | format += "%dx" % int(size) |
88 | format += "%dx" % int(size) |
79 | elif (token[0:5] == "char["): |
- | |
80 | size = token[5:].split("]")[0] |
- | |
81 | format += "%ds" % int(size) |
- | |
82 | variable = True |
89 | continue |
83 | else: |
90 | |
84 | format += { |
91 | variable = { |
- | 92 | "char": lambda: "s", |
|
85 | "uint8_t": lambda: "B", |
93 | "uint8_t": lambda: "B", |
86 | "uint16_t": lambda: "H", |
94 | "uint16_t": lambda: "H", |
87 | "uint32_t": lambda: "L", |
95 | "uint32_t": lambda: "L", |
88 | "uint64_t": lambda: "Q", |
96 | "uint64_t": lambda: "Q", |
89 | 97 | ||
90 | "int8_t": lambda: "b", |
98 | "int8_t": lambda: "b", |
91 | "int16_t": lambda: "h", |
99 | "int16_t": lambda: "h", |
92 | "int32_t": lambda: "l", |
100 | "int32_t": lambda: "l", |
93 | "int64_t": lambda: "q" |
101 | "int64_t": lambda: "q" |
94 | }[token]() |
102 | }[token]() |
95 | variable = True |
- | |
96 | 103 | ||
97 | inst.__dict__['_format_'] = format |
104 | inst.__dict__['_format_'] = format |
98 | inst.__dict__['_list_'] = list |
105 | inst.__dict__['_list_'] = list |
99 | return inst |
106 | return inst |
100 | 107 |