 /* file encode2r.c */

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

  void encode2r(char *sex, pcodeset *pcstr, TTP_PCDRLS_PARAMETERS *ttp_pcdrls_parms,
     int *sinin, int *plinin, int *perr)
  /*********************************************************/
  /*                                                       */
  /*       input: sex                                      */
  /*  parameters: pcstr, ttp_pcdrls_parms                  */
  /*      output: sinin,*plinin,perr                       */
  /*                                                       */
  /*  Converts sex to sinin, according to pcode "pcstr",   */
  /*   using ttp pcdrls parameters "ttp_pcdrls_parms"      */
  /*   if necessary to create new lexical entries.         */
  /*  Sex is a string (external) of ASCII pcode elements,  */
  /*   which may be phones, words, or other symbols.       */
  /*  Sinin is an integer string (internal) of pcode       */
  /*   elements, from *sinin[1] thru *sinin[*plinin].      */
  /*  Will not put out more than <MAX_SYMBS_IN_STR> ints.  */
  /*  Uses pcodeset indexes for internal codes;            */
  /*   if there is no match, returns 0; and will work on   */
  /*   both separated and non-separated (single character) */
  /*   codes.                                              */
  /*  On return, *perr indicates exception:                */
  /*    *perr  condition                                   */
  /*      0     a.o.k.                                     */
  /*      1     symbol code not found in pcstr             */
  /*     11     symbol string overflow                     */
  /*                                                       */
  /* Created 6/1/98 by WMF, based on the earlier encode2().*/
  /* Modified 6/5/98 by WMF, just cosmetically.            */
  /*********************************************************/

  {char *proc = " encode2r";
   char psxx[LINE_LENGTH], *psymb = &psxx[0], *prem;
   SUBSTRING ssremx, *ssrem = &ssremx;
   SUBSTRING psymbss_x, *psymbss = &psymbss_x;
   int int_code;
   boolean done;
/* code: */
 db_enter_msg(proc,2); /* debug only */
 if (db_level > 2)
  {printf("*DB: on entry, *sex='%s'\n",sex);
   if (pcstr->separated) printf("*DB: pcode pcstr is separated.\n");
   else                  printf("*DB: pcode pcstr is NOT separated.\n");
  }
   *perr = 0;
   *plinin = 0;
/* check for degenerate case */
   if (sex != NULL)
     {ssrem->start = sex;
      ssrem->end   = prtrim(sex);
      done = F;
      if (pcstr->separated)
        {while ((! done) && (*perr == 0))
           {*psymbss = sstok(ssrem,pcstr->sep_codes);
            if (substr_length(psymbss) < 1) break;
            psymb = substr_to_str(psymbss,psymb,LINE_LENGTH);
if (db_level > 2) printf("*DB: trying to encode '%s'\n",psymb);
            if (*plinin < MAX_SYMBS_IN_STR)
              {*plinin += 1;
               int_code = pcindex2r(psymb,pcstr,ttp_pcdrls_parms);
               if (int_code >= 0)  
                 sinin[*plinin] = int_code;
               else
                 {fprintf(stderr,"*ERR:%s: invalid code '%s'.\n",proc,psymb);
                  *plinin -= 1;
                  *perr=1;
              }  }
            else
              {fprintf(stderr,
                "*ERR:%s:symbol string ovf; increase MAX_SYMBS_IN_STR.\n",proc);
               *perr=11;
               break;
              }
            ssrem->start = psymbss->end + 1;
	}  }
      else
        {psymb[1] = '\0';
      /* NOTE - This is a quick fix useful for 1-symbol codes only!!! */
         for (prem=sex; *prem != '\0'; prem++)
           {*psymb = *prem;
            if (*plinin < MAX_SYMBS_IN_STR)
              {*plinin += 1;
               int_code = pcindex2r(psymb,pcstr,NULL);
               if (int_code >= 0)  
                 sinin[*plinin] = int_code;
               else
                 {fprintf(stderr,"*ERR:%s: invalid code '%s'.\n",proc,psymb);
                  *plinin -= 1;
                  *perr=1; goto RETURN;
              }  }
            else
              {fprintf(stderr,
                "*ERR:%s:symbol string ovf; increase MAX_SYMBS_IN_STR.\n",proc);
               *perr=11; goto RETURN;
     }  }  }  }
 RETURN:
 db_leave_msg(proc,2); /* debug only */
   return;
  } /* end of encode2r */
