home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / wp_dtp / xdme1820.lha / XDME / flags.c < prev    next >
C/C++ Source or Header  |  1993-03-09  |  5KB  |  247 lines

  1. /******************************************************************************
  2.  
  3.     MODUL
  4.     flags.c
  5.  
  6.     DESCRIPTION
  7.     lowlevel flags-support for DME/XDME
  8.  
  9.     NOTES
  10.     is used by vars.c
  11.  
  12.     BUGS
  13.     <none known>
  14.  
  15.     TODO
  16.     -/-
  17.  
  18.     EXAMPLES
  19.     -/-
  20.  
  21.     SEE ALSO
  22.     vars.c
  23.  
  24.     INDEX
  25.  
  26.     HISTORY
  27.     <see RCS-File>
  28.  
  29.  
  30. ******************************************************************************/
  31.  
  32. /*
  33. **  (C)Copyright 1992 by Bernd Noll for null/zero-soft
  34. **  All Rights Reserved
  35. **
  36. **  RCS Header: $Id: flags.c,v 1.65 92/11/09 12:47:21 b_noll Exp $
  37. **
  38. **  Basic Functions to work with (arrays of) DME-Flags
  39. **  (both Local and Global Flags use these functions)
  40. **
  41. **  Put together to start a little bit of modularisation
  42. **  in the sources.
  43. */
  44.  
  45. /**************************************
  46.         Includes
  47. **************************************/
  48. #include "defs.h"
  49.  
  50.  
  51. /**************************************
  52.         Globale Exports
  53. **************************************/
  54. Prototype int     test_arg  (CHAR*, int);
  55. Prototype CHAR * GetFlag   (char*, CHAR*, int, CHAR*);
  56. Prototype char     SetFlag   (char*, CHAR*, CHAR*, int, CHAR*);
  57. Prototype int     IsFlagSet (char*, int, int);
  58.  
  59.  
  60. /**************************************
  61.       Interne Defines & Strukturen
  62. **************************************/
  63. static char bit[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  64.  
  65.  
  66. /*
  67.  *  test_arg  (previous toggler)
  68.  *
  69.  *  simple hack do be used if you wanna implement
  70.  *  a function which can regognize on/off/toggle for
  71.  *  a BOOL (boolean) variable
  72.  *  args: 1 == TRUE == set == on | 0 == FALSE == reset == off | toggle == switch
  73.  */
  74.  
  75. int test_arg (CHAR * str, int bool)
  76. {
  77.     if (str)
  78.     {
  79.     switch(str[0])
  80.     {  /* --- it would be much more senseful to switch with (str[0]&31)<<5|(str[1]&31) */
  81.     case '0':               /* 0 */
  82.     case 'R':               /* Reset */
  83.     case 'r':               /* reset */
  84.     case 'F':               /* FALSE */
  85.     case 'f':               /* false */
  86.         return(0);
  87.  
  88.     case '1':               /* 1 */
  89.         return(1);
  90.  
  91.     case 'S':               /* Set/Switch */
  92.     case 's':               /* set/switch */
  93.         switch (str[1] | 32)
  94.         {
  95.         case 'e':           /* set */
  96.         return(1);
  97.  
  98.         case 'w':           /* switch */
  99.         return(!bool);
  100.  
  101.         default:
  102.         abort(bool);
  103.  
  104.         } /* switch */
  105.  
  106.     case 'T':               /* Toggle/True */
  107.     case 't':               /* toggle/true */
  108.         switch (str[1] | 32)
  109.         {
  110.         case 'r':           /* true */
  111.         return(1);
  112.  
  113.         case 'o':           /* toggle */
  114.         return(bool ? 0 : 1);
  115.  
  116.         default:
  117.         abort(bool);
  118.  
  119.         } /* switch */
  120.  
  121.     case 'O':               /* on / off */
  122.     case 'o':               /* on / off */
  123.         switch(str[1] | 32)
  124.         {
  125.         case 'n':           /* on */
  126.         return(1);
  127.  
  128.         case 'f':           /* off */
  129.         return(0);
  130.  
  131.         default:
  132.         abort(bool);
  133.  
  134.         } /* switch */
  135.  
  136.     default:        /* unexpected value */
  137.         abort (bool);
  138.     } /* switch */
  139.     } else
  140.     {             /* unexpected value */
  141.     abort(bool);
  142.     } /* if */
  143. } /* test_arg */
  144.  
  145.  
  146.  
  147. int IsFlagSet (char * toggles, int number, int max)
  148. {
  149.     if (number < 0 || number >= max)
  150.     {
  151.     abort(0);
  152.     } /* if */
  153.  
  154.     return((toggles[number/8] & bit[number&7]) ? 1 : 0);
  155. } /* IsFlagSet */
  156.  
  157.  
  158.  
  159. /*
  160.  * if the string in name matches the format <xy><num>
  161.  *  <xy>  eq identifier
  162.  *  <num> /N in [0..max]
  163.  * then the flag <num> in toggles is set according to value
  164.  */
  165.  
  166. char SetFlag (char * toggles, CHAR * name, CHAR * value, int max, CHAR * identifier)
  167. {
  168.     int num;
  169.     int idlen = strlen(identifier);
  170.  
  171.     if (strncmp(name,identifier,idlen))
  172.     { /* invalid name */
  173.     return(0);
  174.     } /* if */
  175.     name += idlen;
  176.  
  177.     if (!is_number(name))
  178.     {          /* invalid name */
  179.     return(0);
  180.     } /* if */
  181.  
  182.     num = atoi(name);
  183.     if (num < 0 || num >= max )
  184.     {
  185.     abort(1);
  186.     } /* if */
  187.  
  188.     {
  189.     char res;
  190.     char bn = num & 7;
  191.     num    = num / 8;
  192.     res    = test_arg(value, toggles[num] & bit[bn]);
  193.     toggles[num] = (toggles[num] & ~bit[bn]) | (res ? bit[bn] : 0);  /* shorter: (res * bit[bn]) */
  194.     } /* block */
  195.  
  196.     return(1);
  197. } /* SetFlag */
  198.  
  199.  
  200.  
  201. /*
  202.  * if the string in name matches the format <xy><num>
  203.  *  <xy>  eq identifier
  204.  *  <num> /N in [0..max]
  205.  * then a string is allocated and filled according to flag <num> in toggles
  206.  */
  207.  
  208. CHAR * GetFlag(char * toggles, CHAR * name, int max, CHAR * identifier)
  209. {
  210.     int num;
  211.     int idlen = strlen(identifier);
  212.     CHAR* str;
  213.  
  214.     if (strncmp(name, identifier, idlen))
  215.     { /* invalid name */
  216.     return(NULL);
  217.     } /* if */
  218.     name += idlen;
  219.  
  220.     if (!is_number(name))
  221.     {          /* invalid name */
  222.     return(NULL);
  223.     } /* if */
  224.  
  225.     num = atoi(name);
  226.     if (num < 0 || num >= max )
  227.     {
  228.     abort(NULL);
  229.     } /* if */
  230.  
  231.     if (!(str = malloc(2)))
  232.     {
  233.     nomemory();
  234.     abort(NULL);
  235.     } /* if */
  236.  
  237.     sprintf(str, "%d", IsFlagSet(toggles, num, max), 0);
  238.  
  239.     return(str);
  240. } /* GetFlag */
  241.  
  242.  
  243. /******************************************************************************
  244. *****  ENDE flags.c
  245. ******************************************************************************/
  246.  
  247.