/* file pcindex2r.c */

#if !defined(COMPILE_ENVIRONMENT)
#include <gp/stdcenvf.h> /* std compile environment for functions */
#endif

  PCIND_T pcindex2r(char *symbol, pcodeset *pc1,
   TTP_PCDRLS_PARAMETERS *ttp_pcdrls_parms)

  /**************************************************************************/
  /*  Returns the index for the item in pcodeset "pc1" that matches symbol. */
  /*  If there is no match, tries to add item to pc1 and, if it a word      */
  /*  or char file, to an ".ADD" file; if that is not possible, returns -1. */
  /*  Uses the ttp pcdrls function to create new pronlex items if necessary */
  /**************************************************************************/
   {char *proc = "pcindex2r";
    PCIND_T i;
    int err;
    boolean found;
    pcode_element x, *px = &x;

/* code: */
 db_enter_msg(proc,3);
if (db_level > 3) printf("%s symbol='%s', pcodeset name = '%s'\n",
 pdb,symbol, pc1->name);
/* error check */
    if (strlen(symbol) < 1) {i = -1; goto RETURN;}
/* look 2-byte-integer-character-like-Mandarin-wise if appropriate */
    if (streqi(pc1->element_type,"char")) i = pcindex_char_lookup(symbol,pc1,&found);
/* look hash-wise if appropriate: */
    else if (pc1->hashed) i = (PCIND_T)pcindex_htab_lookup(symbol,pc1,&found);
    else
/* do binary search if appropriate */
      {if ( (pc1-> ncodes > 30) && (streq(pc1->order,"alphabetic")) )
         {i = (PCIND_T)pcindex_bsearch(symbol,pc1);
          if (i < 0) found = F; else found = T;
         }
/* else do straight linear search */
       else i = pcindex_lsearch(symbol,pc1,&found);
      }
    /* if not found, make up an entry & add it (in some cases) */
    if (!found)
      {if (make_elementr(px,symbol,pc1,ttp_pcdrls_parms) == NULL)
         i = -1;
       else
         {err = 0;
if (db_level > 2) printf("%s word/char not found, adding '%s'\n",pdb,symbol);
          i = (PCIND_T)add_pcode_element(pc1,px,&err);
          if (err > 0) i = -1;
          else
            {report_lex_add2(i,pc1,&err);
             if (err > 0) i = -1;
      }  }  }
RETURN:
db_leave_msg(proc,3);
    return i;
   }
/* end include pcindex2r.c */
