-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleer_struct.py
41 lines (36 loc) · 1.07 KB
/
leer_struct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# encoding: utf-8
import re
from cstruct import CStruct, LITTLE_ENDIAN
def buscar_estructura(archivo=None, nombre=None):
"""
Busca la definición de una estructura de C en un archivo.
Recorre el archivo de atrás hacia adelante.
"""
with open(archivo) as fp:
lineas = reversed(fp.readlines())
contenido = []
encontrado = False
FIN = re.compile(r'\}\s*%s\s*;' % nombre)
INICIO = '{'
for i, linea in enumerate(lineas):
if not encontrado:
if FIN.search(linea):
encontrado = True # Econtramos cierre
else:
if INICIO in linea:
break # Econtramos apertura
contenido.append(linea)
contenido.reverse()
contenido = ''.join(contenido)
tipo_de_dato = type(
nombre,
(CStruct, ),
{
'__byte_order__': LITTLE_ENDIAN,
'__struct__': contenido
}
)
return tipo_de_dato
if __name__ == "__main__":
from pprint import pprint as pp
pp(buscar_estructura(archivo='./c/comm.h', nombre='TMensaje'))