[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCH 2/9] libxl: disks: new xlu_disk_parse function



On Thu, 2011-06-02 at 18:55 +0100, Ian Jackson wrote:
> +/* Sets ->format from the string.  IDL should provide something for this. */

(please imagine a thunderclap and cloud of smoke here...)

only compile tested.

I'm not sure if we need to be more flexible, e.g. allow output in both
upper and lower case, accept - where the name has _ etc.

8<------------------------------------------------------------

# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1307095416 -3600
# Node ID fcae8a56112893ac53ff04623af61793a204e555
# Parent  bd110eb1481a650db3406bf2da80c76b1bfd7e7d
libxl: autogenerate to_string and from_string functions for Enumerations.

The generated strings are the lower case enum value names, with underscores.
Accepted string for parsing are the same but are case insensitive

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r bd110eb1481a -r fcae8a561128 tools/libxl/gentypes.py
--- a/tools/libxl/gentypes.py   Fri Jun 03 10:22:22 2011 +0100
+++ b/tools/libxl/gentypes.py   Fri Jun 03 11:03:36 2011 +0100
@@ -113,6 +113,40 @@ def libxl_C_type_destroy(ty, v, indent =
         s = indent + s
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
+def libxl_C_enum_to_string(ty, e, indent = "    "):
+    s = ""
+    s += "switch(%s) {\n" % e
+    for v in ty.values:
+        s += "    case %s:\n" % (v.name)
+        s += "        return \"%s\";\n" % (v.valuename.lower())
+    s += "    default:\n "
+    s += "        return NULL;\n"
+    s += "}\n"
+
+    if s != "":
+        s = indent + s
+    return s.replace("\n", "\n%s" % indent).rstrip(indent)
+
+def libxl_C_enum_from_string(ty, str, e, indent = "    "):
+    s = ""
+    s += "static struct { const char *s; %s v; } lookup[] = {\n" % ty.typename
+    for v in ty.values:
+        s += "    { \"%s\", %s },\n" % (v.valuename.lower(), v.name)
+    s += "};\n"
+    s += "int i;\n"
+    s += "\n"
+    s += "for (i=0; i<(sizeof(lookup)/sizeof(lookup[0])); i++) {\n"
+    s += "    if (!strcasecmp(lookup[i].s, %s)) {\n" % (str)
+    s += "        %s = lookup[i].v;\n" % (e)
+    s += "        return 0;\n"
+    s += "    }\n"
+    s += "}\n"
+    s += "return -1;\n"
+    
+    if s != "":
+        s = indent + s
+    return s.replace("\n", "\n%s" % indent).rstrip(indent)
+
 if __name__ == '__main__':
     if len(sys.argv) < 4:
         print >>sys.stderr, "Usage: gentypes.py <idl> <header> 
<implementation>"
@@ -142,6 +176,9 @@ if __name__ == '__main__':
         f.write(libxl_C_type_define(ty) + ";\n")
         if ty.destructor_fn is not None:
             f.write("void %s(%s *p);\n" % (ty.destructor_fn, ty.typename))
+        if isinstance(ty, libxltypes.Enumeration):
+            f.write("const char *%s_to_string(%s e);\n" % (ty.typename, 
ty.typename))
+            f.write("int %s_from_string(const char *s, %s *e);\n" % 
(ty.typename, ty.typename))
         f.write("\n")
 
     f.write("""#endif /* __LIBXL_TYPES_H */\n""")
@@ -177,4 +214,19 @@ if __name__ == '__main__':
         f.write("    memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
         f.write("}\n")
         f.write("\n")
+
+    for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]:
+        f.write("const char *%s_to_string(%s e)\n" % (ty.typename, 
ty.typename))
+        f.write("{\n")
+        f.write(libxl_C_enum_to_string(ty, "e"))
+        f.write("}\n")
+        f.write("\n")
+
+        f.write("int %s_from_string(const char *s, %s *e)\n" % (ty.typename, 
ty.typename))
+        f.write("{\n")
+        f.write(libxl_C_enum_from_string(ty, "s", "*e"))               
+        f.write("}\n")
+        f.write("\n")
+
     f.close()
+



_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.