/* file find_key3.c */

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

int find_key3(char *pi_tg, int *ib_found, pcodeset *pcdrls, struct BPATH *bpath)
 
  /*****************************************************/
  /*  Tries to find key in pcdrls                      */
  /*  pi_tg points to the first letter of the target.  */
  /*  *bpath specifies the backoff path to take; if    */
  /*  step1=stepn, only that one is executed (in       */
  /*  effect, no backoff).                             */
  /*  Also returns *ib_found, the index of the item in */
  /*  *bpath that was found.                           */
  /* Modified 1/20/99 by WMF to use pcindex5r().       */
  /*****************************************************/
{char *proc = "find_key3";
 char keyx_data[LINE_LENGTH], *keyx = &keyx_data[0];
 char *pi, *pi_le, *pi_re;
 boolean found,dont_add_to_core = F, dont_add_to_file = F;
 int ib,item, le_size, tg_size, re_size;
/* code */
db_enter_msg(proc,1); /* debug only */

 found = F;
 for (ib = bpath->step1; ((!found)&&(ib <= bpath->stepn)); ib++)
   {/* look for key structured acc. to bpath->rpart[ib] */
    le_size = bpath->rpart[ib].le;
    tg_size = bpath->rpart[ib].tg;
    re_size = bpath->rpart[ib].re;
if (db_level > 1) printf("%s ib=%d, le_size=%d, tg_size=%d, re_size=%d\n",
pdb,ib,le_size,tg_size,re_size);
    /* make local key */
    pi_le = pi_tg-le_size;
    pi_re = pi_tg+tg_size;
    keyx = strcpy(keyx,"");
if (db_level > 2) printf("%s before le, keyx=[%s]\n",pdb,keyx);
    keyx = strncat(keyx,pi_le,le_size);
if (db_level > 2) printf("%s after le, keyx=[%s]\n",pdb,keyx);
    keyx = strcat(keyx,".");
    keyx = strncat(keyx,pi_tg,tg_size);
    keyx = strcat(keyx,".");
if (db_level > 2) printf("%s after tg, keyx=[%s]\n",pdb,keyx);
    keyx = strncat(keyx,pi_re,re_size);
if (db_level > 2) printf("%s after re, keyx=[%s]\n",pdb,keyx);
    for (pi = keyx; *pi != '\0'; pi++) if (*pi == ' ') *pi = '_';
    /* try to find it */

if (db_level > 1) printf("%s keyx=[%s]\n",pdb,keyx);

/*****
if (streq(keyx,"i.o.l_"))
  {printf("%s KEYX IS [i.o.l_]\n",pdb);
  }
  *****/

    item = pcindex5r(keyx,pcdrls,dont_add_to_core,dont_add_to_file,NULL);
    if (item > 1)
      {found = T;
       *ib_found = ib;
      }

if (db_level > 1)
{if (found) printf("%s found, item=%d\n",pdb,item);
 else       printf("%s *not* found\n",pdb);
}
   }
if (db_level > 1) printf("%s on exit, item=%d\n",pdb,item);
db_leave_msg(proc,1); /* debug only */
 return item;
}
