Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python2 |
| 2 | # -*- Mode: Python; py-indent-offset: 8 -*- |
| 3 | |
| 4 | # (C) Copyright Zack Rusin 2005 |
| 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | # copy of this software and associated documentation files (the "Software"), |
| 9 | # to deal in the Software without restriction, including without limitation |
| 10 | # on the rights to use, copy, modify, merge, publish, distribute, sub |
| 11 | # license, and/or sell copies of the Software, and to permit persons to whom |
| 12 | # the Software is furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice (including the next |
| 15 | # paragraph) shall be included in all copies or substantial portions of the |
| 16 | # Software. |
| 17 | # |
| 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 24 | # IN THE SOFTWARE. |
| 25 | # |
| 26 | # Authors: |
| 27 | # Zack Rusin <zack@kde.org> |
| 28 | |
| 29 | from xml.sax import saxutils |
| 30 | from xml.sax import make_parser |
| 31 | from xml.sax.handler import feature_namespaces |
| 32 | |
| 33 | import license |
| 34 | import gl_XML |
| 35 | import sys, getopt |
| 36 | |
| 37 | class PrintGlEnums(gl_XML.FilterGLAPISpecBase): |
| 38 | name = "gl_enums.py (from Mesa)" |
| 39 | |
| 40 | def __init__(self): |
| 41 | gl_XML.FilterGLAPISpecBase.__init__(self) |
| 42 | self.license = license.bsd_license_template % ( \ |
| 43 | """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL") |
| 44 | |
| 45 | |
| 46 | def printRealHeader(self): |
| 47 | print '#include "glheader.h"' |
| 48 | print '#include "enums.h"' |
| 49 | print '#include "imports.h"' |
| 50 | print '' |
| 51 | print 'typedef struct {' |
| 52 | print ' const char *c;' |
| 53 | print ' int n;' |
| 54 | print '} enum_elt;' |
| 55 | print '' |
| 56 | print 'static enum_elt all_enums[] =' |
| 57 | print '{' |
| 58 | return |
| 59 | |
| 60 | def printRealFooter(self): |
| 61 | print """ |
| 62 | }; |
| 63 | |
| 64 | #define Elements(x) sizeof(x)/sizeof(*x) |
| 65 | |
| 66 | typedef int (*cfunc)(const void *, const void *); |
| 67 | |
| 68 | static enum_elt **index1; |
| 69 | static int sorted; |
| 70 | |
| 71 | static int compar_name( const enum_elt *a, const enum_elt *b ) |
| 72 | { |
| 73 | return _mesa_strcmp(a->c, b->c); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* note the extra level of indirection |
| 78 | */ |
| 79 | static int compar_nr( const enum_elt **a, const enum_elt **b ) |
| 80 | { |
| 81 | return (*a)->n - (*b)->n; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static void sort_enums( void ) |
| 86 | { |
| 87 | GLuint i; |
| 88 | index1 = (enum_elt **)MALLOC( Elements(all_enums) * sizeof(enum_elt *) ); |
| 89 | sorted = 1; |
| 90 | |
| 91 | if (!index1) |
| 92 | return; /* what else can we do? */ |
| 93 | |
| 94 | qsort( all_enums, Elements(all_enums), sizeof(*all_enums), |
| 95 | (cfunc) compar_name ); |
| 96 | |
| 97 | for (i = 0 ; i < Elements(all_enums) ; i++) |
| 98 | index1[i] = &all_enums[i]; |
| 99 | |
| 100 | qsort( index1, Elements(all_enums), sizeof(*index1), (cfunc) compar_nr ); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | int _mesa_lookup_enum_by_name( const char *symbol ) |
| 106 | { |
| 107 | enum_elt tmp; |
| 108 | enum_elt *e; |
| 109 | |
| 110 | if (!sorted) |
| 111 | sort_enums(); |
| 112 | |
| 113 | if (!symbol) |
| 114 | return 0; |
| 115 | |
| 116 | tmp.c = symbol; |
| 117 | e = (enum_elt *)bsearch( &tmp, all_enums, Elements(all_enums), |
| 118 | sizeof(*all_enums), (cfunc) compar_name ); |
| 119 | |
| 120 | return e ? e->n : -1; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static char token_tmp[20]; |
| 125 | |
| 126 | const char *_mesa_lookup_enum_by_nr( int nr ) |
| 127 | { |
| 128 | enum_elt tmp, *e, **f; |
| 129 | |
| 130 | if (!sorted) |
| 131 | sort_enums(); |
| 132 | |
| 133 | tmp.n = nr; |
| 134 | e = &tmp; |
| 135 | |
| 136 | f = (enum_elt **)bsearch( &e, index1, Elements(all_enums), |
| 137 | sizeof(*index1), (cfunc) compar_nr ); |
| 138 | |
| 139 | if (f) { |
| 140 | return (*f)->c; |
| 141 | } |
| 142 | else { |
| 143 | /* this isn't re-entrant safe, no big deal here */ |
| 144 | _mesa_sprintf(token_tmp, "0x%x", nr); |
| 145 | return token_tmp; |
| 146 | } |
| 147 | } |
| 148 | """ |
| 149 | return |
| 150 | |
| 151 | def printFunctions(self): |
| 152 | for enum in self.enums: |
| 153 | print ' { "%s", 0x%X },' % (enum.name, enum.value) |
| 154 | return |
| 155 | |
| 156 | |
| 157 | def show_usage(): |
| 158 | print "Usage: %s [-f input_file_name]" % sys.argv[0] |
| 159 | sys.exit(1) |
| 160 | |
| 161 | if __name__ == '__main__': |
| 162 | file_name = "gl_API.xml" |
| 163 | |
| 164 | try: |
| 165 | (args, trail) = getopt.getopt(sys.argv[1:], "f:") |
| 166 | except Exception,e: |
| 167 | show_usage() |
| 168 | |
| 169 | for (arg,val) in args: |
| 170 | if arg == "-f": |
| 171 | file_name = val |
| 172 | |
| 173 | dh = PrintGlEnums() |
| 174 | |
| 175 | parser = make_parser() |
| 176 | parser.setFeature(feature_namespaces, 0) |
| 177 | parser.setContentHandler(dh) |
| 178 | |
| 179 | f = open(file_name) |
| 180 | |
| 181 | dh.printHeader() |
| 182 | parser.parse(f) |
| 183 | dh.printFooter() |