home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / devcon / sanfrancisco_1989 / sf-devcon89.1 / commodities / aztec / cxsupp / invert.c < prev    next >
C/C++ Source or Header  |  1992-08-27  |  3KB  |  164 lines

  1. /* invert.c -- string-to-inputevents    */
  2.  
  3. /*
  4. Copyright (c) 1987, 1988, 1989 Jim Mackraz and I&I Computing.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers.  All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include "cx/cxusr.h"
  14.  
  15. #define D(x)    ;
  16.  
  17. struct InputEvent    *
  18. InvertString(str, km)
  19. UBYTE    *str;
  20. CPTR    *km;        /* keymap    */
  21. {
  22.     /* bugs:
  23.         can't escape '>'
  24.         puts '\0' on closing angles
  25.      */
  26.     /* allocate input event for each character in string    */
  27.  
  28.     struct InputEvent    *chain = NULL;
  29.     struct InputEvent    *tail;
  30.     struct InputEvent    *ie;
  31.     int                    abort = 0;
  32.     UBYTE                cc;
  33.  
  34.     BYTE doesc();
  35.  
  36.     if (!str || !*str) return (NULL);
  37.  
  38.     tail = (struct InputEvent *) &chain;    /* jvudu    */
  39.  
  40.     do {    /* have checked that str is not null     */
  41.         /* allocate the next ie and link it in    */
  42.         if (!(ie = 
  43.             AllocMem((LONG) sizeof (struct InputEvent), (LONG) MEMF_CLEAR)))
  44.         {
  45.             abort = 1;
  46.             goto DONE;
  47.         }
  48.         tail->ie_NextEvent = ie;    /* link in */
  49.         tail = ie;
  50.  
  51.         /* now fill in the input event    */
  52.         switch (cc = *str)
  53.         {
  54.         case    '<':
  55.             ++str;
  56.             if (!doangle(&str, ie)) 
  57.             {
  58.                 D( printf("doangle failed.  abort\n") );
  59.                 abort = 1;
  60.                 goto DONE;
  61.             }
  62.             break;
  63.  
  64.         case    '\\':
  65.             D( printf("is: doesc\n"); )
  66.             if ((cc = doesc( *(++str) )) == -1)    /* get escaped character    */
  67.             {
  68.                 abort = 1;
  69.                 goto DONE;
  70.             }
  71.             /* fall through    */
  72.         default:
  73.             InvertKeyMap((ULONG) cc, ie, km);
  74.         }
  75.  
  76.  
  77.     } while (*(++str));
  78.  
  79. DONE:
  80.     if (abort)
  81.     {
  82.         FreeIEvents(ie);
  83.         return (NULL);
  84.     }
  85.     return (chain);
  86. }
  87.  
  88. BYTE
  89. doesc(cc)
  90. BYTE    cc;
  91. {
  92.     switch(cc)
  93.     {
  94.     case    'n':    /* \n means "return"    */
  95.     case    'r':
  96.         return ('\r');
  97.         break;
  98.     case    't':
  99.         return ('\t');
  100.         break;
  101.     case    '0':
  102.         return ('\0');
  103.         break;
  104.  
  105.     case    '\\':
  106.     case    '\"':
  107.     case    '\'':
  108.     case    '<':
  109.         break;
  110.     default:
  111.         cc = -1;
  112.     }
  113.  
  114.     return (cc);
  115. }
  116.  
  117. /* return 0 if problem    */
  118. doangle(strp, ie)
  119. char                **strp;
  120. struct InputEvent    *ie;
  121. {
  122.     IX    ix;
  123.     char    *cp = NULL;
  124.     ULONG    retval;
  125.  
  126.     /* find closing angle '>', put a null there    */
  127.     for (cp = *strp; *cp; ++cp)
  128.     {
  129.         if (*cp == '>')
  130.         {
  131.             D( printf("found >") );
  132.             *cp = '\0';
  133.             break;
  134.         }
  135.         D( printf("> look past %c\n", *cp) );
  136.     }
  137.  
  138.     retval = ParseIX(*strp, &ix);
  139.  
  140.     D( printf("> parseix back\n") );
  141.  
  142.     if (cp)
  143.     {
  144.         *cp = '>';    /* fix it    */
  145.         *strp = cp;    /* point to char following '>'    */
  146.     }
  147.     else
  148.     {
  149.         *strp = cp - 1;    /* ++will point to terminating null    */
  150.     }
  151.  
  152.     if (retval)     /* error */
  153.     {
  154.         return (0);
  155.     }
  156.  
  157.     /* use IX to describe a suitable InputEvent    */
  158.     ie->ie_Class        = ix.ix_Class;
  159.     ie->ie_Code            = ix.ix_Code;
  160.     ie->ie_Qualifier    = ix.ix_Qualifier;
  161.  
  162.     return (1);
  163. }
  164.