/* file ttp_pcdrls2ss_prot.c */

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

PROTECTED_CHR_STR *ttp_pcdrls2ss_prot(PROTECTED_CHR_STR *sp, SUBSTRING *sst_in,
  pcodeset *pcdrls, struct BPATH *bpath,int *perr)

  /*********************************************/
  /*  Converts text to phones w/pcdrls table   */
  /*  Input:  *sst_in is a text sub-string;    */
  /*  Output: *sp is a protected phone string; */
  /*  (Returns sp.)                            */
  /*  Process: applies ttp rules that are in   */
  /*  the form of pcodeset *pcdrls.            */
  /*  On return, *perr > 0 iff error.          */
  /*  *perr == 1 iff invalid char              */
  /*********************************************/
   {char *proc = "ttp_pcdrls2ss";
    PROTECTED_CHR_STR *ps_in, *psx;
    PROTECTED_CHR_STR *psdbx;
    SUBSTRING sstr_data, *ss_the_rest = &sstr_data;
    SUBSTRING sstk_data, *ss_token = &sstk_data;
    char *pi;
    int i, ib, item, npad = 8;
  /* code */
 db_enter_msg(proc,1); /* debug only */
    *perr = 0;
    ps_in = create_prot_chr_str(133);
    psx = create_prot_chr_str(133);
    psdbx = create_prot_chr_str(133);
if (db_level > 2)
 {ss_to_ps(psdbx,sst_in);
  printf("%s sst_in = [%s]\n",pdb,psdbx->str);
 }
   /* put sst_in into ps_in, padding word tokens w/space to cover initial le and final re */
    *ss_the_rest = *sst_in;
if (db_level > 2) printf("%s looping padding tokens\n",pdb);
    do
      {
if (db_level > 2)
 {ss_to_ps(psdbx,ss_the_rest);
  printf("%s ss_the_rest = [%s]\n",pdb,psdbx->str);
 }
       find_next_token(ss_the_rest,ss_token);
if (db_level > 2)
 {ss_to_ps(psdbx,ss_token);
  printf("%s ss_token = [%s]\n",pdb,psdbx->str);
 }
       if (substr_length(ss_token) > 0)
         {for (i=1; i <= npad; i++) strcat_prot_s(ps_in," ");
          ss_to_ps(psx,ss_token);
          strcat_prot_p(ps_in,psx);
          ss_the_rest->start = ss_token->end+1;
         }
      } while ((substr_length(ss_the_rest) > 0)&&(substr_length(ss_token) > 0));
    for (i=1; i <= npad; i++) strcat_prot_s(ps_in," ");

if (db_level > 2)
 {printf("%stext='%s'\n",pdb,ps_in->str);
 }
   /* empty output string */
    sp = strcpy_prot_s(sp,"");
   /* apply rules encoded in pcdrls */
    pi = pltrimf(ps_in->str)-1;
    while (*pi != '\0')
      {item = find_key3(pi,&ib,pcdrls,bpath);
       if (item >= 0)
         {sp = strcat_prot_s(sp,pcdrls->pc[item].lc_ext);
if (db_level > 2) printf("%s pcdrls->pc[item].ac=[%s]\n",pdb,pcdrls->pc[item].ac);
if (db_level > 2) printf("%s pcdrls->pc[item].lc_ext=[%s]\n",pdb,pcdrls->pc[item].lc_ext);
          pi += bpath->rpart[ib].tg;
if (db_level > 1) printf("%s hit, new sp->str=[%s]\n",pdb,sp->str);
         }
       else
         {pi += 1;
if (db_level > 2) printf("%s no hit\n",pdb);
      }  }  
if (db_level > 2) printf("%s leaving ttp, output='%s'\n",pdb,sp->str);

   destroy_prot_chr_str(ps_in);
   destroy_prot_chr_str(psx);
   destroy_prot_chr_str(psdbx);
  db_leave_msg(proc,1); /* debug only */
    return sp;
   }
