(I know that most people are going to say this horribly).
I wrote the following macros to easily write switches using strings instead of if / else if / else:
#define str_switch( value ) \
do { \
const char * __strswitchptr__ = (value); \
if( 0 ) \
#define str_case( test ) \
} if( strcmp( __strswitchptr__, (test) ) == 0 ) { \
#define str_default \
} else { \
#define str_switchend \
} while( 0 ); \
I am using this method:
char * sVal = "D";
str_switch( sVal )
{
str_case( "A" )
printf( "Case A" );
break;
str_case( "B" )
printf( "Case B" );
break;
str_case( "C" )
printf( "Case C" );
break;
str_default
printf( "Error" );
}
str_switchend
But I can’t understand how I can change it, so I can use several cases:
char * sVal = "D";
str_switch( sVal )
{
str_case( "A" )
printf( "Case A" );
break;
str_case( "B" )
printf( "Case B" );
break;
str_case( "C" )
str_case( "D" )
str_case( "E" )
printf( "Case C" );
break;
str_default
printf( "Error" );
}
str_switchend
Any idea? Thanks: -)
source
share