home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / manip / keyword.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  2.4 KB  |  117 lines

  1. #include "all.h"
  2. #include "keyword.h"
  3.  
  4.  
  5. /*------------------------------------------------------------------*/
  6. /*      Find the KEY WORD *cp, and return it's number       */
  7. /*------------------------------------------------------------------*/
  8.  
  9. /* You can add a function, you MUST place it in alphabetical order  */
  10.  
  11.  
  12. #define NKEYS (sizeof mkeywfn / sizeof(struct mkeyw))
  13. struct mkeyw { char *word; int index;  } mkeywfn[] = {
  14.       "!",        k_comment
  15.     , "AT",        k_at
  16.     , "BLANK",    k_clear
  17.     , "CALL",    k_call
  18.     , "CLEAR",    k_clear
  19.     , "CLOSE",    k_close
  20.     , "COPY",    k_copy
  21.     , "DATA",    k_data
  22.     , "DEL",    k_delete
  23.     , "DELETE",    k_delete
  24.     , "ELSE",    k_else
  25.     , "EXIT",    k_exit
  26.     , "FIT",     k_fit
  27.     , "GEN",    k_generate
  28.     , "GENERATE",    k_generate
  29.     , "GOTO",    k_goto
  30.     , "HELP",    k_help
  31.     , "IF",        k_if
  32.     , "INPUT",    k_input
  33.     , "INSERT",    k_insert
  34.     , "LET",    k_let
  35.     , "LIST",    k_list
  36.     , "LOAD",    k_load
  37.     , "LOG",    k_logging
  38.     , "LOGG",    k_logging
  39.     , "LOGGING",    k_logging
  40.     , "MOVE",    k_move
  41.     , "NEW",     k_new
  42.     , "NEXT",    k_next
  43.     , "PARSUM",    k_parsum
  44.     , "PRINT",    k_print
  45.     , "PROP",    k_prop
  46.     , "PROPAGATE",    k_prop
  47.     , "QUIT",    k_quit
  48.     , "SAVE",    k_save
  49.     , "SET",    k_set
  50.     , "SHELL",    k_shell
  51.     , "SORT",    k_sort
  52.     , "SUM",    k_sum
  53.     , "SWAP",    k_swap
  54. };
  55. int binsearchk(char *word, struct mkeyw tab[], int n);
  56.  
  57. cmd_name(int idx, char **cp)
  58. {
  59.     int i;
  60.     int k;
  61.     static char *kp;
  62.     static char fail[]="Keyword not found";
  63.     if (kp==NULL) kp = myallocz(80);
  64.     for (i=0;i<NKEYS;i++) {
  65.         if (mkeywfn[i].index==idx) {
  66.             strcpy(kp,mkeywfn[i].word);
  67.             *cp = kp;
  68.             return;
  69.         }
  70.     }
  71.     *cp = &fail[0];
  72. }
  73. find_mkey(char *cp, int *idx)
  74. {
  75.     int i;
  76.     i = binsearchk(cp,mkeywfn,NKEYS);
  77.     if (i==-1) { *idx = 0; return;}
  78.     *idx = mkeywfn[i].index;
  79. }
  80.  
  81. /*------------------------------------------------------------------*/
  82. /* Simple binary search                         */
  83. /*------------------------------------------------------------------*/
  84. binsearchk(char *word, struct mkeyw tab[], int n)
  85. {
  86.     int cond,low,high,mid;
  87.     low = 0;
  88.     high = n-1;
  89.     while (low <= high) {
  90.         mid = (low+high) / 2;
  91.         if ((cond = strcmp(word,tab[mid].word)) < 0)
  92.             high = mid - 1;
  93.         else if (cond > 0)
  94.             low = mid + 1;
  95.         else
  96.             return mid;
  97.     }
  98.     return -1;
  99. }
  100.  
  101.  
  102. /*-------------------------------------------------------------------------*/
  103. /* This is for the tex primitives */
  104.  
  105. struct mkeyw tkeywfn[] = {
  106. 0,0};
  107.  
  108. #define NTKEYS (sizeof tkeywfn / sizeof(struct mkeyw))
  109. find_primcmd(char *cp)
  110. {
  111.     int i;
  112.     i = binsearchk(cp,tkeywfn,NTKEYS);
  113.     if (i==-1) return 0;
  114.     return tkeywfn[i].index;
  115. }
  116.  
  117.