From 421b4deee6b2f087b9dd0253885156b4f3af5770 Mon Sep 17 00:00:00 2001 From: lhausamm <loic_hausammann@hotmail.com> Date: Fri, 5 Jan 2018 15:53:33 +0100 Subject: [PATCH] Add script helping to generate format/name for structure --- pyswiftsim/dev.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pyswiftsim/dev.py diff --git a/pyswiftsim/dev.py b/pyswiftsim/dev.py new file mode 100644 index 0000000..6646ecf --- /dev/null +++ b/pyswiftsim/dev.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +symbol_table = { + " int ": "i", + " double ": "d", + " char ": "c", +} + +def _generateFormatString(filename): + """ + Open a file containing a single struct and generate a format string + from it. (for dev, no guarantee of working). + """ + + struct_format = "" + struct_attribute = [] + + with open(filename, "r") as f: + line = "" + # skip until reaching struct + while ("struct" not in line): + line = f.readline() + + count = 0 + in_struct = False + + while (not in_struct or count != 0): + # count { and } + count += line.count("{") + if (count > 0): + in_struct = True + count -= line.count("}") + line = f.readline() + + for k in symbol_table.keys(): + if k not in line: + continue + + i = line.index(k) + len(k) + + if (line[i] == "*"): + struct_format += "p" + i += 1 + else: + struct_format += symbol_table[k] + + j = line.index(";") + struct_attribute.append(line[i:j]) + + + return struct_format, struct_attribute + + +if __name__ == "__main__": + + filename = "chemistry_data.h" + struct_format, struct_attribute = _generateFormatString(filename) + + attr = "[\n" + for i in struct_attribute: + attr += "\t'%s',\n" % i + attr += "]" + + txt = """ + _format = "{form}" + _name = {attr} + """.format(attr=attr, + form=struct_format) + print(txt) -- GitLab