[ Download ] [ Theory ] [ Bugs ] [ License ] [ Examples ] [ Home ]

 

The sos home page

Switching on strings in standard C

 

Examples

 

 

Instructions for downloading and using sos are here.

sos modes

Please note that sos have three modes:

The switch skeleton mode is preferable for two reasons:

Note that the development version of sos (nsos) can process suitable input to create switch statements nested inside another switch. This is very useful to scan a file composed of attribute/value pairs. For example, this input will generate the following output.

Example 1: Using the "-p" option

This example can be downloaded here. The distribution copy can be compiled using "make examples".

The following "#define" directives and sos10q prototype were generated by the "-p" option and were simplified somewhat to enhance clarity (the collision detection CPP code was removed).

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* code generated by sos */
long long int sos10q(char *) ;
#define  sos_switch(x)  switch(sos10q(x)) 
#define  red                                          74052ULL 
#define  blue                                        574789ULL 
#define  black                                     36704459ULL 
#define  violet                                 23777296724ULL 
#define  aquamarine                       22891914789950341ULL  /* check! */
/* end of generated code */

int main()
  {
  int   n ;
  char  sp[100] ;

  while(1)
    {
    puts("Enter a color in lowercase (CTRL-D to exit): ") ;
    n = scanf("%99s", sp) ;
    if (n != 1) exit(EXIT_FAILURE) ;

    sos_switch(sp)
      {
      case (red) : 
        printf("I see red \n") ;
        break ;
      case (blue) : 
        printf("I see blue \n") ;
        break ;
      case (black) : 
        printf("I see black \n") ;
        break ;
      case (violet) : 
        printf("I see violet \n") ;
        break ;
      case (aquamarine) :                   /* > 9 chars */
        if (strcmp("aquamarine", sp)) goto nocolor ;
        printf("I see aquamarine \n") ;
        break ;
      default:
      nocolor:
        printf("Sorry, unrecognized colour...\n") ;
      }
    }
  }

Note the strcmp invalid input check for "AQUAMARINE".

The round parentheses around the case values are not required but they serve as a visual aid and may help the compiler to spot problems.

Example 2: Using the "-s" option

This example can be downloaded here. The distribution copy can be compiled with "make examples".

The following skeleton code was generated by sos "-s" option and somewhat simplified to enhance clarity:


long long int sos10q(char *) ;
#define sos_switch(x) switch(sos10q(x))

sos_switch(your-string-ptr) 
  { 
  case                74052ULL :            /* "red" */ 
    ... 
    break ; 
  case               574789ULL :            /* "blue" */ 
    ... 
    break ; 
  case             36704459ULL :            /* "black" */ 
    ... 
    break ; 
  case          23777296724ULL :            /* "violet" */ 
    ... 
    break ; 
  case    22891914789950341ULL :            /* "aquamarine" */ 
    if (strcmp("aquamarine", your-string-ptr)) goto badsink ; 
    ... 
    break ; 
  default : 
  badsink : 
    fprintf(stderr, "sos: unrecognized input... \n") ; 
    ... 
  } 
 

Note the generated code provides the original string in a comment and even the "strcmp" check. Beware, if you have more than one generated switch in a function the check labels must be different.