Subversion Repositories HelenOS

Rev

Rev 3260 | Rev 3263 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3260 Rev 3262
Line 24... Line 24...
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 struct formats
29
Convert descriptive structure definitions to structure object
30
"""
30
"""
31
 
31
 
32
import struct
32
import struct
33
 
33
 
-
 
34
class Struct:
-
 
35
    def size(self):
-
 
36
        return struct.calcsize(self._format_)
-
 
37
   
-
 
38
    def pack(self):
-
 
39
        list = []
-
 
40
        for variable in self._list_:
-
 
41
            list.append(self.__dict__[variable])
-
 
42
       
-
 
43
        return struct.pack(self._format_, *list)
-
 
44
 
34
def convert(definition):
45
def create(definition):
35
    "Convert structure defition to struct format"
46
    "Create structure object"
36
   
47
   
37
    tokens = definition.split(None)
48
    tokens = definition.split(None)
38
   
49
   
39
    # Initial byte order tag
50
    # Initial byte order tag
40
    struct = {
51
    format = {
41
        "little:":  lambda: "<",
52
        "little:":  lambda: "<",
42
        "big:":     lambda: ">",
53
        "big:":     lambda: ">",
43
        "network:": lambda: "!"
54
        "network:": lambda: "!"
44
    }[tokens[0]]()
55
    }[tokens[0]]()
-
 
56
    inst = Struct()
-
 
57
    list = []
45
   
58
   
46
    # Member tags
59
    # Member tags
47
   
-
 
48
    comment = False
60
    comment = False
-
 
61
    variable = False
49
    for token in tokens[1:]:
62
    for token in tokens[1:]:
50
        if (comment):
63
        if (comment):
51
            if (token == "*/"):
64
            if (token == "*/"):
52
                comment = False
65
                comment = False
53
            continue
66
            continue
54
       
67
       
-
 
68
        if (variable):
-
 
69
            inst.__dict__[token] = None
-
 
70
            list.append(token)
-
 
71
            variable = False
-
 
72
            continue
-
 
73
       
55
        if (token == "/*"):
74
        if (token == "/*"):
56
            comment = True
75
            comment = True
-
 
76
        elif (token[0:8] == "padding["):
-
 
77
            size = token[8:].split("]")[0]
-
 
78
            format += "%dx" % int(size)
57
        elif (token[0:5] == "char["):
79
        elif (token[0:5] == "char["):
58
            size = token[5:].split("]")[0]
80
            size = token[5:].split("]")[0]
59
            struct += ("%d" % int(size)) + "s"
81
            format += "%ds" % int(size)
-
 
82
            variable = True
60
        else:
83
        else:
61
            struct += {
84
            format += {
62
                "uint8_t":  lambda: "B",
85
                "uint8_t":  lambda: "B",
63
                "uint16_t": lambda: "H",
86
                "uint16_t": lambda: "H",
64
                "uint32_t": lambda: "L",
87
                "uint32_t": lambda: "L",
65
                "uint64_t": lambda: "Q",
88
                "uint64_t": lambda: "Q",
66
               
89
               
67
                "int8_t":   lambda: "b",
90
                "int8_t":   lambda: "b",
68
                "int16_t":  lambda: "h",
91
                "int16_t":  lambda: "h",
69
                "int32_t":  lambda: "l",
92
                "int32_t":  lambda: "l",
70
                "int64_t":  lambda: "q"
93
                "int64_t":  lambda: "q"
71
            }[token]()
94
            }[token]()
-
 
95
            variable = True
72
   
96
   
73
    return struct
-
 
74
 
-
 
75
def little_string(string):
97
    inst.__dict__['_format_'] = format
76
    return struct.pack("<" + ("%d" % len(string)) + "s", string)
98
    inst.__dict__['_list_'] = list
77
 
-
 
78
def little_padding(length):
99
    return inst
79
    return struct.pack("<" + ("%d" % length) + "x")
-