home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / misc / sci / gfft / source / name.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-25  |  3.5 KB  |  146 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1994  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *          See gfft.c, or run program itself, for details.              *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        name.c
  13.  * Purpose:     identify name and/or invoke method on it
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     30-May-1993 CPP; Created.
  16.  * Comments:    (1) Error display puts a (^) below first bad character
  17.  */
  18.  
  19. #include <strings.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "gfft.h"
  23. #include "errcodes.h"
  24.  
  25. static int cindex = 0;
  26.  
  27. Name_Info_St *invoke_method (char *command, 
  28.                   Name_Info_St *command_name_list)
  29. {
  30.     Name_Info_St *this_command;
  31.     char *argument_string;
  32.  
  33.     this_command = identify_name (command, command_name_list, TRUE);
  34.     if (command[cindex] == '\0')
  35.     {
  36.     argument_string = &command[cindex];
  37.     }
  38.     else
  39.     {
  40.     argument_string = &command[ cindex + 1];
  41.     }
  42.     (*this_command->cfunction) (argument_string);
  43.     return this_command;
  44. }
  45.  
  46.  
  47. /*
  48.  * if report_error is TRUE,
  49.  * there will be no direct return on error
  50.  * only longjmp
  51.  */
  52. Name_Info_St *identify_name (char *command, 
  53.                 Name_Info_St *command_name_list, 
  54.                 BOOLEAN report_error)
  55. {
  56.     int i;
  57.     int lastmatch = 0;
  58.     int error_code = NO_SUCH_COMMAND;
  59.     int command_offset;
  60.  
  61.     if (command[0] == '\0')
  62.     {
  63.     if (report_error)
  64.     {
  65.         RAISE_ERROR (NOTHING_SPECIAL); /* longjmp outa here! */
  66.     }
  67.     else
  68.     {
  69.         return NULL;
  70.     }
  71.     }
  72.  
  73.     for (i = 0; strlen (command_name_list[i].full_string); i++)
  74.     {
  75. /*
  76.  * First, find a minimum string that matches
  77.  */
  78.     cindex = 0;
  79.     while (command[cindex] && command_name_list[i].min_string[cindex] &&
  80.            command[cindex] != ' ' &&
  81.            toupper (command[cindex]) == toupper 
  82.            (command_name_list[i].min_string[cindex]))
  83.     {
  84.         cindex++;
  85.         if (cindex > lastmatch)
  86.         {
  87.         lastmatch = cindex;
  88.         }
  89.     }
  90.     if (command_name_list[i].min_string[cindex] != '\0')
  91.         {
  92.         if (command[cindex] == '\0' || command[cindex] == ' ')
  93.         {
  94.         error_code = AMBIGUOUS_COMMAND;
  95.         }
  96.         continue;
  97.     }
  98. /*
  99.  * Then, insure that following characters don't mismatch
  100.  */
  101.     while (command[cindex] && command_name_list[i].full_string[cindex] &&
  102.            command[cindex] != ' ' &&
  103.            toupper (command[cindex]) == toupper 
  104.            (command_name_list[i].full_string[cindex]))
  105.     {
  106.         cindex++;
  107.         if (cindex > lastmatch)
  108.         {
  109.         lastmatch = cindex;
  110.         }
  111.     }
  112.     if (command[cindex] == '\0' || command[cindex] == ' ')
  113.     {
  114.         return &command_name_list[i];  /* SUCCESSFUL return */
  115.     }
  116.     continue;
  117.     }
  118. /*
  119.  * No command has matched completely!
  120.  */
  121.     command_offset = command - Command;
  122.     if (command_offset)
  123.     {
  124.     if (error_code == NO_SUCH_COMMAND)
  125.     {
  126.         error_code = NO_SUCH_ARGUMENT;
  127.     }
  128.     else if (error_code == AMBIGUOUS_COMMAND)
  129.     {
  130.         error_code = AMBIGUOUS_ARGUMENT;
  131.     }
  132.     }
  133.     if (report_error)
  134.     {
  135.     command_error_message (error_code, command + lastmatch);
  136.     RAISE_ERROR (NOTHING_SPECIAL); /* longjmp outa here! */
  137.     }
  138.     else
  139.     {
  140.     return NULL;
  141.     }
  142. /*
  143.  * all returns or jumps above
  144.  */
  145. }
  146.