From a8c254a8cfffc4416ba25662132385e445d0a3e0 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Wed, 2 Feb 2022 18:07:37 +0000 Subject: [PATCH] aux/bindings: Refactor path verify function generation --- src/xrt/auxiliary/bindings/bindings.py | 27 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/xrt/auxiliary/bindings/bindings.py b/src/xrt/auxiliary/bindings/bindings.py index 352d48d07..5f7ddc688 100755 --- a/src/xrt/auxiliary/bindings/bindings.py +++ b/src/xrt/auxiliary/bindings/bindings.py @@ -174,7 +174,7 @@ class Profile: collector = PathsByLengthCollector() for component in self.components: collector.add_paths(component.get_full_openxr_paths()) - self.by_length = collector.to_dict_of_lists() + self.subpaths_by_length = collector.to_dict_of_lists() class Bindings: @@ -211,7 +211,7 @@ header = '''// Copyright 2020-2022, Collabora, Ltd. func_start = ''' bool -oxr_verify_{func}_subpath(const char *str, size_t length) +{name}(const char *str, size_t length) {{ \tswitch (length) {{ ''' @@ -221,6 +221,20 @@ if_strcmp = '''if (strcmp(str, "{check}") == 0) {{ \t\t}} else ''' +def write_verify_func(f, name, dict_of_lists): + """Generate function to check if a string is in a set of strings. + Input is a file to write the code into, a dict where keys are length and + the values are lists of strings of that length. And a suffix if any.""" + + f.write(func_start.format(name=name)) + for length in dict_of_lists: + f.write("\tcase " + str(length) + ":\n\t\t") + for path in dict_of_lists[length]: + f.write(if_strcmp.format(check=path)) + f.write("{\n\t\t\treturn false;\n\t\t}\n") + f.write("\tdefault:\n\t\treturn false;\n\t}\n}\n") + + def generate_bindings_c(file, p): """Generate the file to verify subpaths on a interaction profile.""" f = open(file, "w") @@ -233,13 +247,8 @@ def generate_bindings_c(file, p): ''') for profile in p.profiles: - f.write(func_start.format(func=profile.validation_func_name)) - for length in profile.by_length: - f.write("\tcase " + str(length) + ":\n\t\t") - for path in profile.by_length[length]: - f.write(if_strcmp.format(check=path)) - f.write("{\n\t\t\treturn false;\n\t\t}\n") - f.write("\tdefault:\n\t\treturn false;\n\t}\n}\n") + name = "oxr_verify_" + profile.validation_func_name + "_subpath" + write_verify_func(f, name, profile.subpaths_by_length) f.write( f'\n\nstruct profile_template profile_templates[{len(p.profiles)}] = {{ // array of profile_template\n')