Commits

Juri Pakaste committed e8ed20d

Added support for generating strings from mask enums

Comments (0)

Files changed (2)

NSStringFromEnumGenerator.py

     funend = "}"
     return "\n".join((ext_decl, defn, switchstart, "\n".join(cases), switchend, funend))
 
-def funs_from_file(f, includes):
+def create_mask_fun(parsed_enum):
+    name = parsed_enum.preferred_name
+
+    if parsed_enum.typedef_name:
+        typename = name
+    else:
+        typename = "enum {0}".format(parsed_enum.name)
+
+    decl = "NSString* NSStringFrom{0}({1} v)".format(name, typename)
+    ext_decl = "extern {0};".format(decl)
+    defn = "{0} {{".format(decl)
+
+    lines = [ext_decl, defn]
+    lines.append("NSMutableArray *bits = [NSMutableArray array];")
+    for ev in parsed_enum.values:
+        check = "if (v & {0}) [bits addObject:@\"{0}\"];".format(ev.name)
+        lines.append(check)
+    lines.append("return [bits componentsJoinedByString:@\" | \"];")
+    lines.append("}")
+    return "\n".join(lines)
+
+
+
+def funs_from_file(f, includes, mask):
     index = clang.cindex.Index.create()
-    includeargs = reduce(lambda acc, e: acc + e, [["-include", inc] for inc in includes], [])
+    includeargs = reduce(lambda acc, e: acc + e, [["-include", inc] for inc in includes], []) if includes else []
     tu = index.parse(f, args = ["-x", "objective-c"] + includeargs)
     if not tu:
         raise NSGException("Unable to parse input {0} for unknown reason".format(f))
 
     finder = create_finder(f)
     enums = finder(tu.cursor)
-    return [create_fun(e) for e in enums if e.preferred_name]
+    creator = create_mask_fun if mask else create_fun
+    return [creator(e) for e in enums if e.preferred_name]
 
 if __name__ == '__main__':
     for fun in funs_from_file(sys.argv[1]):

nsstringfromenumgen

 aparse = argparse.ArgumentParser(description = "Create NSStringFromEnumeration Objective-C functions based on Objective-C source files")
 aparse.add_argument("files", type=str, nargs="+", help="Files to parse")
 aparse.add_argument("--include", action="append", help="Include file")
+aparse.add_argument("--mask", action="store_true", help="Assume enums are used as bitmasks")
 args = aparse.parse_args()
 
 try:
     for fname in args.files:
-        for fun in funs_from_file(fname, args.include):
+        for fun in funs_from_file(fname, args.include, args.mask):
             sys.stdout.write(fun)
             sys.stdout.write("\n")
 except NSGException, e: