/* file pcindex5.c */

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

PCIND_T pcindex5r(char *symbol, pcodeset *pc1,
   boolean add_to_core, boolean add_to_file,
   TTP_PCDRLS_PARAMETERS *ttp_pcdrls_parms)

  /***************************************************************************/
  /*  Returns the index for the item in pcodeset "pc1" that matches *symbol. */
  /*  Tries to make a new item if symbol isn't found and pcodeset is "word". */
  /*    If add_to_core==T(rue), tries to add item to *pc1 (in core).         */
  /*    If add_to_file==T(rue), tries to add item to external ".ADD" file.   */
  /*  If symbol is not found and efforts to make a new item and add it to    */
  /*  the pcodeset in core or in the appropriate ".ADD" file fail, then      */
  /*  -1 is returned to signal an error.                                     */
  /*  Uses the ttp pcdrls function to create new pronlex items if necessary  */
  /***************************************************************************/
   {char *proc = "pcindex5r";
    PCIND_T i;
    int err;
    boolean found;
    pcode_element x, *px = &x;
/* code: */
 db_enter_msg(proc,3);
if (db_level > 3) printf("*DB: symbol='%s', pcodeset name = '%s'\n",
 symbol, pc1->name);
/* error check */
    if (strlen(symbol) < 1) {i = -1; goto RETURN;}
/* look hash-wise if appropriate: */
    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  /* else do straight linear search */
         {i=0; found=F;
          while ((i <= pc1->ncodes) && (! found))
            {if (pc1->separated)
	       {if (streq(symbol,pc1->pc[i].ac)) found = T;
                else
                  {if (streq(pc1->type_case,"immaterial"))
                     {if (streqi(symbol,pc1->pc[i].ac)) found = T;
	       }  }  }
             else /* not separated */
	       {if ((strlen(pc1->pc[i].ac) == 1)
                  &&(*symbol == *(pc1->pc[i].ac))) found = T;
	       }
             if (!found) i += 1;
      }  }  }
    if (!found)
      {i = -1;
       if (streq(pc1->element_type,"word"))
         {if (add_to_core || add_to_file)
            {px = make_elementr(px,symbol,pc1,ttp_pcdrls_parms);
             if (px == NULL) i = -1;
             else
               {err = 0;
                if (add_to_core)
                  {i = (PCIND_T)add_pcode_element(pc1,px,&err);
                   if (err > 0) i = -1;
                  }
                if ((i != -1)&&(add_to_file)) report_lex_add2(i,pc1,&err);
                if (err > 0) i = -1;
      }  }  }  }
RETURN:
db_leave_msg(proc,3);
    return i;
   }
/* end include pcindex5r.c */
