Enums to strings

Frank Warmerdam warmerdam at POBOX.COM
Fri Sep 16 23:52:35 EDT 2005


On 9/16/05, Steve Lime <steve.lime at dnr.state.mn.us> wrote:
> I was thinking of just parking the array of strings right next to the enum in map.h, but the function sounds fine too. Were you thinking of something like:
> 
> static char **msGetUnitsStrings()
> {
>   static char **values = ...;
>   return values;
> }
> 
> or something like:
> 
> static char *msGetUnitsString(int code)
> {
>   switch (code) {
>     case 0: return "meters";
>     case 1: return "feet";
>       ...
>    }
> 
>    return NULL;
> }
> 
> The last element being the count is a good idea. We're doing that with the error codes in maperror.h but no where else I believe.

Steve,

Either way works, but the (very small) advantage of the second is that
it is easier to convert a enum code into a string into the middle of
an expression, such as the arguments to a debug or error message.   If
you do the second, I would encourage you to return "Unknown" or
something else safe to print as the default case instead of NULL.

Best regards,



-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the mapserver-dev mailing list