 /* file encode3r.c */

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

  void encode3r(char *sex, pcodeset *pc1, TTP_PCDRLS_PARAMETERS *ttp_pcdrls_parms,
     PROTECTED_INT_STR *sin, int *perr)
  /******************************************************************/
  /*                                                                */
  /*  Converts sex to sin, according to pcode "pc1".                */
  /*  sex is a string, external (ASCII);                            */
  /*  sin is a string, internal (INT) (protected).                  */
  /*  On return, *perr indicates exception:                         */
  /*    *perr  condition                                            */
  /*      0     a.o.k.                                              */
  /*      1     symbol code not found in pcstr                      */
  /*     11     symbol string overflow                              */
  /*                                                                */
  /* Created 12/10/98 by WMF, based on the earlier encode2r().      */
  /******************************************************************/

  {char *proc = " encode3r";
   PROTECTED_CHR_STR *psymb;
   PROTECTED_CHR_STR *psdbx;
   SUBSTRING ssremx, *ssrem = &ssremx;
   SUBSTRING psymbss_x, *psymbss = &psymbss_x;
   int int_code, max_nonsep_symbol_length = 2;
   boolean done;
/* code: */
 db_enter_msg(proc,1); /* debug only */
   psdbx = create_prot_chr_str(133);
 if (db_level > 2)
  {printf("%s on entry, *sex='%s'\n",pdb,sex);
   if (pc1->separated) printf("%s pcode pc1 is separated.\n",pdb);
   else                printf("%s pcode pc1 is NOT separated.\n",pdb);
  }
   *perr = 0;
   psymb = create_prot_chr_str(133);
   sin->str[0] = 0;
/* check for degenerate case */
   if (sex != NULL)
     {ssrem->start = pltrimf(sex);
      ssrem->end   = prtrim(sex);
      done = F;
      while ((! done) && (*perr == 0))
        {if (pc1->separated)
           {*psymbss = sstok(ssrem,pc1->sep_codes);
            if (substr_length(psymbss) < 1) break;
if (db_level > 2)
 {ss_to_ps(psdbx,ssrem);
  printf("%s ssrem = [%s]\n",pdb,psdbx->str);
 }
if (db_level > 2)
 {ss_to_ps(psdbx,psymbss);
  printf("%s psymbss = [%s]\n",pdb,psdbx->str);
 }
            ss_to_ps(psymb,psymbss);
if (db_level > 2) printf("%s trying to encode '%s'\n",pdb,psymb->str);
            int_code = pcindex2r(psymb->str,pc1,ttp_pcdrls_parms);
           }
         else
           {psymbss->start = ssrem->start;
            psymbss->end = psymbss->start+max_nonsep_symbol_length-1;
            do
              {ss_to_ps(psymb,psymbss);
if (db_level > 2) printf("%s trying to encode '%s'\n",pdb,psymb->str);
               int_code = pcindex2r(psymb->str,pc1,ttp_pcdrls_parms);
               psymbss->end -= 1;
              }
            while ((int_code < 0)&&(substr_length(psymbss) > 0));
           }
         if (int_code >= 0) nstrcat_prot_i(sin,int_code);
         else
           {fprintf(stderr,"*ERR:%s: invalid code '%s'.\n",proc,psymb->str);
            *perr=1;
           }
         ssrem->start = psymbss->end + 1;
         if (substr_length(ssrem) < 1) done = T;
     }  }
   destroy_prot_chr_str(psymb);
   destroy_prot_chr_str(psdbx);
 db_leave_msg(proc,1); /* debug only */
   return;
  } /* end of encode3r */
