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>
33 class PrintGlEnums(gl_XML
.gl_print_base
):
36 gl_XML
.gl_print_base
.__init
__(self
)
38 self
.name
= "gl_enums.py (from Mesa)"
39 self
.license
= license
.bsd_license_template
% ( \
40 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
44 def printRealHeader(self
):
45 print '#include "glheader.h"'
46 print '#include "enums.h"'
47 print '#include "imports.h"'
49 print 'typedef struct {'
50 print ' size_t offset;'
58 #define Elements(x) sizeof(x)/sizeof(*x)
60 typedef int (*cfunc)(const void *, const void *);
63 * Compare a key name to an element in the \c all_enums array.
65 * \c bsearch always passes the key as the first parameter and the pointer
66 * to the array element as the second parameter. We can elimiate some
67 * extra work by taking advantage of that fact.
69 * \param a Pointer to the desired enum name.
70 * \param b Pointer to an element of the \c all_enums array.
72 static int compar_name( const char *a, const enum_elt *b )
74 return _mesa_strcmp( a, & enum_string_table[ b->offset ] );
78 * Compare a key enum value to an element in the \c all_enums array.
80 * \c bsearch always passes the key as the first parameter and the pointer
81 * to the array element as the second parameter. We can elimiate some
82 * extra work by taking advantage of that fact.
84 * \param a Pointer to the desired enum name.
85 * \param b Pointer to an index into the \c all_enums array.
87 static int compar_nr( const int *a, const unsigned *b )
89 return a[0] - all_enums[*b].n;
93 static char token_tmp[20];
95 const char *_mesa_lookup_enum_by_nr( int nr )
99 i = (unsigned *) _mesa_bsearch(& nr, reduced_enums,
100 Elements(reduced_enums),
101 sizeof(reduced_enums[0]),
105 return & enum_string_table[ all_enums[ *i ].offset ];
108 /* this is not re-entrant safe, no big deal here */
109 _mesa_sprintf(token_tmp, "0x%x", nr);
114 int _mesa_lookup_enum_by_name( const char *symbol )
118 if ( symbol != NULL ) {
119 f = (enum_elt *) _mesa_bsearch(symbol, all_enums,
122 (cfunc) compar_name);
125 return (f != NULL) ? f->n : -1;
132 def printBody(self
, api
):
133 self
.process_enums( api
)
135 keys
= self
.enum_table
.keys()
143 for [name
, pri
] in self
.enum_table
[ enum
]:
144 name_table
.append( [name
, enum
] )
148 enum_table
[enum
] = name
155 print 'LONGSTRING static const char enum_string_table[] = '
156 for [name
, enum
] in name_table
:
157 print ' "%s\\0"' % (name
)
158 string_offsets
[ name
] = i
165 print 'static const enum_elt all_enums[%u] =' % (len(name_table
))
167 for [name
, enum
] in name_table
:
168 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets
[name
], enum
, name
)
172 print 'static const unsigned reduced_enums[%u] =' % (len(keys
))
175 name
= enum_table
[ enum
]
176 if [name
, enum
] not in name_table
:
177 print ' /* Error! %s, 0x%04x */ 0,' % (name
, enum
)
179 i
= name_table
.index( [name
, enum
] )
181 print ' %4u, /* %s */' % (i
, name
)
189 def process_enums(self
, api
):
192 for obj
in api
.enumIterateByName():
193 if obj
.value
not in self
.enum_table
:
194 self
.enum_table
[ obj
.value
] = []
197 name
= "GL_" + obj
.name
198 priority
= obj
.priority()
199 self
.enum_table
[ obj
.value
].append( [name
, priority
] )
203 print "Usage: %s [-f input_file_name]" % sys
.argv
[0]
206 if __name__
== '__main__':
207 file_name
= "gl_API.xml"
210 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:")
214 for (arg
,val
) in args
:
218 api
= gl_XML
.parse_GL_API( file_name
)
220 printer
= PrintGlEnums()