From: gor@cs.strath.ac.uk (Gordon Russell)
Newsgroups: comp.lang.c
Subject: Re: Do you know of "switch" for "strings" ?
Date: 10 Apr 92 09:02:48 GMT
Distribution: comp.lang.c
Organization: Comp. Sci. Dept., Strathclyde Univ., Glasgow, Scotland.


 Yes I do know of a switch system for strings.....

   I was kind of bored one afternoon, and I wanted to get more experienced
in writing macros, so here it is...

-----------------------Cut Here For the Demo Program-------------------------
#include "string_switch.h"

main()
{
  string_switch("yes way")
  { 
    string_case("yes way"):
    string_case("no way") :
              printf ("No way\n"); 

    string_case("hello")  :
              printf ("Hello\n");
              printf ("Goodbye!\n");
              break;

    string_default:    printf ("Default\n");
  } end_switch_string;
}
-----------------------End Of Demo Program-----------------------------------
----------------------Cut Here For The Macros (string_switch.h)--------------
/* -------------------------------------------------------------------------- */
/*  Author : Gordon Russell                                                   */
/*  Title  : string_switch.h                                                  */
/*  Description :                                                             */
/*     This collection of macros allows the user to set up string based       */
/*     switches. The following layout is used...                              */
/*                                                                            */
/*  string_switch (string)                                                    */
/*  {                                                                         */
/*      string_case("string1") : <commands>                                   */
/*                               <break>                                      */
/*      string_case("string2") : <commands>                                   */
/*                               <break>                                      */
/*      string_default : <commands>                                           */
/*                       <break>                                              */
/*  } end_string_switch;                                                      */
/*                                                                            */
/*  Note that the number of string_case used is variable, and the             */
/*  string_default usage is optional.                                         */
/*                                                                            */
/* -------------------------------------------------------------------------- */

#define string_switch(str) {char *sQpZtP;int sQpZtQ;sQpZtP=str;for(sQpZtQ=0;sQpZtQ!=1;sQpZtQ=1) {if (0) 
#define string_case(str)   sQpZtQ=1;} if ((sQpZtQ==1)||(strcmp(sQpZtP,str)==0)) {0?0
#define string_default     ;}if(sQpZtQ==0) {0?0
#define end_switch_string  }}
---------------------------- End of Macro File --------------------------------


+--------------------------------+-------------------------------------------+
|  Gordon Russell                |  EMAIL     : gor@cs.strath.ac.uk          |
|  L13.13a, Livingstone Tower,   |  TELEPHONE : 041-552-4400   Ex 3703       |
|  University Of Strathclyde,    |  FAX       : 041-552-0775                 |
|  26 Richmond Street,           +-------------------------------------------+
|  Glasgow, G1 1XH               | Spelling mistakes within this document are|
|  Scotland, UK                  | caused by internet compaction algorithms. |
+--------------------------------+-------------------------------------------+
Xref: sparky comp.lang.c:7634 comp.std.c:1819 comp.arch:5034