[Bf-python] genutils
alex mole
nal at blueyonder.co.uk
Fri Nov 21 01:22:35 CET 2003
[sorry for the length of this email!]
Hi
Is it ok if i add some things to genutils.[ch]?
I've written a few functions and made a structure that saves a *lot* of
"if...else if..." code by mapping strings to integers, and this sort of
code is needed in many places in the python module code. The code looks
like this:
------------------ 8< ----------------------
typedef struct {
const char *sval;
int ival;
} EXPP_map_pair;
/* maps must end with a pair that has NULL as sval */
int EXPP_map_getIntVal (const EXPP_map_pair *map,
const char *sval, int *ival)
{
while (map->sval)
{
if (STREQ(sval, map->sval))
{
*ival = map->ival;
return 1;
}
++map;
}
return 0;
}
int EXPP_map_getShortVal (const EXPP_map_pair *map,
const char *sval, short *ival)
{
while (map->sval)
{
if (STREQ(sval, map->sval))
{
*ival = map->ival;
return 1;
}
++map;
}
return 0;
}
int EXPP_map_getStrVal (const EXPP_map_pair *map,
int ival, const char **sval)
{
while (map->sval)
{
if (ival == map->ival)
{
*sval = map->sval;
return 1;
}
++map;
}
return 0;
}
-------------------- 8< -----------------------
and this can be used like:
static const EXPP_map_pair tex_flag_map[] = {
{ "ColorBand", EXPP_TEX_FLAG_COLORBAND },
{ "FlipBlend", EXPP_TEX_FLAG_FLIPBLEND },
{ "NegAlpha", EXPP_TEX_FLAG_NEGALPHA },
{ NULL, 0 }
};
//flag names in sf[]
for (i=0; i<3; ++i)
{
if (!sf[i]) break;
if (!expp_tex_getShortVal(tex_flags_map, sf[i], &thisflag))
return EXPP_ReturnPyObjError (PyExc_ValueError,
"invalid texture flag name");
flags |= thisflag;
}
If you compare this code to eg. Material_setMode(), I think you'll agree
that this is much simpler and [probably] less error prone. Plus it has
the advantage of automatically being able to work "backwards" and get
strings from integers.
I currently have this defined just in my Texture.c, but I would like to
use it in MTex.c as well, and I'm sure that other places could benefit
from this....
let me know what you think :)
alex
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs Email
Security System. For more information on a proactive email security
service working around the clock, around the globe, visit
http://www.messagelabs.com
________________________________________________________________________
More information about the Bf-python
mailing list