|
27 | 27 | __version__ = '4.0' |
28 | 28 | __date__ = '15 August 2013' |
29 | 29 |
|
30 | | -import struct |
31 | 30 | from typing import Any, Dict, Optional, Type, Union |
32 | 31 | from .base import ( |
33 | 32 | LITTLE_ENDIAN, |
34 | 33 | BIG_ENDIAN, |
35 | 34 | NATIVE_ORDER, |
36 | 35 | STRUCTS, |
| 36 | + ENUMS, |
37 | 37 | DEFINES, |
38 | 38 | TYPEDEFS, |
39 | | - C_TYPE_TO_FORMAT, |
40 | 39 | CHAR_ZERO, |
41 | 40 | ) |
42 | 41 | from .abstract import CStructMeta, AbstractCStruct, AbstractCEnum |
43 | 42 | from .cstruct import CStruct |
44 | 43 | from .c_parser import parse_struct_def |
45 | 44 | from .mem_cstruct import MemCStruct |
46 | 45 | from .cenum import CEnum |
| 46 | +from .native_types import get_native_type |
47 | 47 |
|
48 | 48 | __all__ = [ |
49 | 49 | 'LITTLE_ENDIAN', |
|
57 | 57 | 'undef', |
58 | 58 | 'getdef', |
59 | 59 | 'typedef', |
| 60 | + 'get_type', |
60 | 61 | 'sizeof', |
61 | 62 | 'parse', |
62 | 63 | ] |
@@ -104,33 +105,62 @@ def typedef(type_: str, alias: str) -> None: |
104 | 105 | TYPEDEFS[alias] = type_ |
105 | 106 |
|
106 | 107 |
|
107 | | -def sizeof(type_: str) -> int: |
| 108 | +def get_type(type_: str) -> Any: |
108 | 109 | """ |
109 | | - Return the size of the type. |
| 110 | + Get a data type (struct, union, enum) by name |
| 111 | +
|
| 112 | + Examples: |
| 113 | + >>> get_type("struct Position") |
| 114 | + <class 'abc.Position'> |
| 115 | + >>> get_type("enum htmlfont") |
| 116 | + <enum 'htmlfont'> |
110 | 117 |
|
111 | 118 | Args: |
112 | | - type_: C type, struct or union (e.g. 'short int' or 'struct ZYZ') |
| 119 | + type_: C type, struct or union (e.g. 'short int' or 'struct ZYZ'), enum or native type |
113 | 120 |
|
114 | 121 | Returns: |
115 | | - size: size in bytes |
| 122 | + class: data type class |
116 | 123 | """ |
117 | 124 | while type_ in TYPEDEFS: |
118 | 125 | type_ = TYPEDEFS[type_] |
119 | 126 | if isinstance(type_, CStructMeta): |
120 | | - return len(type_) |
| 127 | + return type_ |
121 | 128 | elif type_.startswith('struct ') or type_.startswith('union '): |
122 | 129 | kind, type_ = type_.split(' ', 1) |
123 | | - t = STRUCTS.get(type_, None) |
124 | | - if t is None: |
125 | | - raise KeyError("Unknow %s \"%s\"" % (kind, type_)) |
126 | | - else: |
127 | | - return t.sizeof() |
| 130 | + try: |
| 131 | + return STRUCTS[type_] |
| 132 | + except KeyError: |
| 133 | + raise KeyError(f"Unknown {kind} `{type_}`") |
| 134 | + elif type_.startswith('enum '): |
| 135 | + kind, type_ = type_.split(' ', 1) |
| 136 | + try: |
| 137 | + return ENUMS[type_] |
| 138 | + except KeyError: |
| 139 | + raise KeyError(f"Unknown {kind} `{type_}`") |
128 | 140 | else: |
129 | | - ttype = C_TYPE_TO_FORMAT.get(type_, None) |
130 | | - if ttype is None: |
131 | | - raise KeyError("Unknow type \"" + type_ + "\"") |
132 | | - else: |
133 | | - return struct.calcsize(ttype) |
| 141 | + return get_native_type(type_) |
| 142 | + |
| 143 | + |
| 144 | +def sizeof(type_: str) -> int: |
| 145 | + """ |
| 146 | + Return the size of the type. |
| 147 | +
|
| 148 | + Examples: |
| 149 | + >>> sizeof("struct Position") |
| 150 | + 16 |
| 151 | + >>> sizeof("int") |
| 152 | + 4 |
| 153 | +
|
| 154 | + Args: |
| 155 | + type_: C type, struct or union (e.g. 'short int' or 'struct ZYZ'), enum or native type |
| 156 | +
|
| 157 | + Returns: |
| 158 | + size: size in bytes |
| 159 | + """ |
| 160 | + while type_ in TYPEDEFS: |
| 161 | + type_ = TYPEDEFS[type_] |
| 162 | + data_type = get_type(type_) |
| 163 | + return data_type.sizeof() |
134 | 164 |
|
135 | 165 |
|
136 | 166 | def parse( |
|
0 commit comments