Add PRAGMA_PACK for struct defintion in struct_parser and use it for tagBITMAPFILEHEADER

This commit is contained in:
Clement Rouault
2016-08-08 18:11:44 +02:00
parent 20fe59cf78
commit c7a2f7663a
4 changed files with 250 additions and 223 deletions
@@ -12,6 +12,7 @@ typedef struct tagRGBTRIPLE {
BYTE rgbtRed;
} RGBTRIPLE, *PRGBTRIPLE, *NPRGBTRIPLE, *LPRGBTRIPLE;
PRAGMA_PACK 2
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
@@ -19,6 +20,7 @@ typedef struct tagBITMAPFILEHEADER {
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
PRAGMA_NOPACK
typedef struct tagBITMAPCOREHEADER {
DWORD bcSize;
+19 -1
View File
@@ -5,6 +5,10 @@ from simpleparser import *
class WinStructParser(Parser):
def __init__(self, *args, **kwargs):
super(WinStructParser, self).__init__(*args, **kwargs)
self.pack = None
def parse_array(self, ):
self.assert_token_type(OpenSquareBracketToken)
number = self.assert_token_type(NameToken).value
@@ -98,7 +102,7 @@ class WinStructParser(Parser):
struct_name = self.assert_token_type(NameToken)
self.assert_token_type(OpenBracketToken)
result = WinDefType(struct_name.value)
result = WinDefType(struct_name.value, self.pack)
while type(self.peek()) != CloseBracketToken:
tok_type, tok_name, nb_rep = self.parse_def()
@@ -114,7 +118,21 @@ class WinStructParser(Parser):
strucs = []
enums = []
while self.peek() is not None:
# HANDLE PRAGMA_PACK / PRAGMA_NOPACK
if type(self.peek()) == NameToken:
pragma = self.next_token().value
if pragma == "PRAGMA_NOPACK":
self.pack = None
continue
if pragma != "PRAGMA_PACK":
raise ValueError("Expected struct/union def or PRAGMA_[NO]PACK")
pack_value = self.promote_to_int(self.next_token())
self.pack = pack_value
x = self.parse_winstruct()
#x.packing = self.pack
#if x.packing != None:
# print("{0} pack = {1}".format(x.name, x.packing))
if type(x) == WinStruct or type(x) == WinUnion:
strucs.append(x)
elif type(x) == WinEnum:
+9 -3
View File
@@ -29,8 +29,9 @@ class Ptr(object):
class WinStruct(object):
ctypes_type = "Structure"
def __init__(self, name):
def __init__(self, name, pack=None):
self.name = name
self.pack = pack
self.fields = []
self.typedef = {}
@@ -60,6 +61,8 @@ class WinStruct(object):
res += ["""class {0}(Structure): pass""".format(self.name)]
res += [self.generate_typedef_ctypes()]
if self.pack:
res += ["{0}._pack_ = ".format(self.pack)]
res += ["{0}._fields_ = [".format(self.name)]
for (ftype, name, nb_rep) in self.fields:
if nb_rep == 1:
@@ -70,8 +73,11 @@ class WinStruct(object):
return "\n".join(res)
def generate_ctypes_class(self):
res = """class {0}({1}):
_fields_ = [\n""".format(self.name, self.ctypes_type)
res = "class {0}({1}):\n".format(self.name, self.ctypes_type)
if self.pack:
res += " _pack_ = {0}\n".format(self.pack)
res += " _fields_ = [\n"""
for (ftype, name, nb_rep) in self.fields:
if nb_rep == 1:
File diff suppressed because it is too large Load Diff