2 # -*- Mode: Python; py-indent-offset: 8 -*-
4 # (C) Copyright Zack Rusin 2005
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:
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
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
27 # Zack Rusin <zack@kde.org>
29 from xml
.sax
import saxutils
30 from xml
.sax
import make_parser
31 from xml
.sax
.handler
import feature_namespaces
37 class PrintGlEnums(gl_XML
.FilterGLAPISpecBase
):
38 name
= "gl_enums.py (from Mesa)"
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")
46 def printRealHeader(self
):
47 print '#include "glheader.h"'
48 print '#include "enums.h"'
49 print '#include "imports.h"'
51 print 'typedef struct {'
52 print ' const char *c;'
56 print 'static enum_elt all_enums[] ='
60 def printRealFooter(self
):
64 #define Elements(x) sizeof(x)/sizeof(*x)
66 typedef int (*cfunc)(const void *, const void *);
68 static enum_elt **index1;
71 static int compar_name( const enum_elt *a, const enum_elt *b )
73 return _mesa_strcmp(a->c, b->c);
77 /* note the extra level of indirection
79 static int compar_nr( const enum_elt **a, const enum_elt **b )
81 return (*a)->n - (*b)->n;
85 static void sort_enums( void )
88 index1 = (enum_elt **)MALLOC( Elements(all_enums) * sizeof(enum_elt *) );
92 return; /* what else can we do? */
94 qsort( all_enums, Elements(all_enums), sizeof(*all_enums),
95 (cfunc) compar_name );
97 for (i = 0 ; i < Elements(all_enums) ; i++)
98 index1[i] = &all_enums[i];
100 qsort( index1, Elements(all_enums), sizeof(*index1), (cfunc) compar_nr );
105 int _mesa_lookup_enum_by_name( const char *symbol )
117 e = (enum_elt *)bsearch( &tmp, all_enums, Elements(all_enums),
118 sizeof(*all_enums), (cfunc) compar_name );
120 return e ? e->n : -1;
124 static char token_tmp[20];
126 const char *_mesa_lookup_enum_by_nr( int nr )
128 enum_elt tmp, *e, **f;
136 f = (enum_elt **)bsearch( &e, index1, Elements(all_enums),
137 sizeof(*index1), (cfunc) compar_nr );
143 /* this isn't re-entrant safe, no big deal here */
144 _mesa_sprintf(token_tmp, "0x%x", nr);
151 def printFunctions(self
):
152 for enum
in self
.enums
:
153 print ' { "%s", 0x%X },' % (enum
.name
, enum
.value
)
158 print "Usage: %s [-f input_file_name]" % sys
.argv
[0]
161 if __name__
== '__main__':
162 file_name
= "gl_API.xml"
165 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:")
169 for (arg
,val
) in args
:
175 parser
= make_parser()
176 parser
.setFeature(feature_namespaces
, 0)
177 parser
.setContentHandler(dh
)