/*******************************************************************************
* YORKTOWN ADVANCED SGML PARSER (YASP)                                         *
*         An SGML Parser conforming to International Standard ISO 8879 --      *
*         Standard Generalized Markup Language                                 *
*------------------------------------------------------------------------------*
* Written 1987 - 1993 by                                                       *
*         Pierre G. Richard                                                    *
*         IBM T.J. Watson Research Center                                      *
*         Yorktown Heights, New York                                           *
*         petrus@watson.ibm.com                                                *
*------------------------------------------------------------------------------*
* FUNCTION NAME    = YASP / SGML Library                                       *
* STATUS           = Version 0, Release 1, Level 20 and up                     *
* COMPONENT NAME   = choose.c                                                  *
* DESCRIPTIVE NAME = Find a keyword in a list of choices                       *
* DATE_WR          = 10/20/1988                                                *
* AUTHOR           = Pierre G. Richard - PETRUS at YKTVMX                      *
*******************************************************************************/

/*-------------+
|Include Files |
+-------------*/
#include "machine.h"
#include "portable.h"
#include "yaspapi.h"
#include "global.h"
#include "usrlib.h"
#include "sgml.h"
#include "gtype.h"

/*--------------------------------------------------------------------choos_in-+
|Find a keyword in a list of choices, return its associated value              |
|                                                                              |
|On entry, choice_p point a list of choice, each entry formed by a pair        |
|  { GNWD ptr (keyword), integer (associated value) }                          |
|The list is terminated by a dummy entry with a NULL GNWD ptr, and the value   |
|that must be returned when the keyword is not found.                          |
+-----------------------------------------------------------------------------*/
int P_FCT choos_in(GA_P gbla_p, CHOICES const *choice_p, GNWD *base_p)
{
   register uchar *c_p;
   register uchar *d_p;
   uint count;
   uchar name_M1[NAMELEN_MAX+2];   /* (-1): dummy entry  (optimization) */

   /* OPTIMIZED (this explains the strange look of some lines) */

   count = 0;
   d_p = name_M1;
   if (is_namestrt(GA(curr_char))) {
      do {
         if (++count > NAMELEN) {
            if (count == NAMELEN + 1) {
               errorh(gbla_p,BIGNAMET,ERH_W_SEV+ERH_LOCATE);
            }
         }else {
            *++d_p = IS_BITON(GA(opt).ot,GENERAL_CASE_OT) ?
            GA(uctab)[GA(curr_char)] : (uchar)GA(curr_char);
         }
      }while is_name(em_read(gbla_p));
   }
   *++d_p = '\0';
   --choice_p;
   while ((c_p = (uchar *)base_p + (++choice_p)->gnwd_ofs) >= (uchar *)base_p) {
      c_p = ((GNWD *)c_p)->s.p;
      --c_p;
      d_p = name_M1;
      while (*++c_p == *++d_p) if (!*c_p) return(choice_p->value);
   }
   em_unrd(gbla_p,count);
   return(choice_p->value);
}
/******************************************************************************/
