/* file wrlex2.c */

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

#if !defined(LEX_COMPILE_ENVIRONMENT)
#include <plex/stdcenvf.h> /* phonlex compile environment for functions */
#endif


/************************************************************/
/*                                                          */
/* write_lex_item2(item,del_pron,fp_out,format,perr)        */
/*                                                          */
/* Writes a lex item from file into file *fp_out, in the    */
/* format specified by *format.                             */
/* If a pron i has del_pron[i] == T, it is not written out. */
/* On return, *perr > 0 signals an error.                   */
/* Currently the formats are: "SORT1", "SORT2", "PCD", and  */
/*   "CMU".                                                 */
/* (Max of 5 prons out for the CMU format.)                 */
/* SORT2 differs from SORT1 in that status fields are added */
/* along with a TAGS field for the whole item.              */
/************************************************************/
 void write_lex_item2(LEX_ITEM3 *item, int *del_pron,
   FILE *fp_out, char *format, int *perr)
 {char *proc = "write_lex_item2";
  char snx[LONG_LINE], *key = &snx[0];
  char sxx[LONG_LINE], *sx = &sxx[0];
  int i, max_prons_out, n_prons_out;
/* coding */
 db_enter_msg(proc,0); /* debug only */
  *perr = 0;
  if (streq(format,"SORT1"))
    {orth2key1(key,item->orth,perr);
     if (*perr < 1)
       {fprintf(fp_out,"%s ",key);
        fprintf(fp_out," %s",cocoon(sx,item->orth));
   /*     if (textlen(caps) > 0)       fprintf(fp_out," c[%s]",caps); */
   /*     if (textlen(diacritics) > 0) fprintf(fp_out," d[%s]",diacritics); */
        for (i=1; i <= item->nprons; i++)
          {if (!del_pron[i])
           fprintf(fp_out," {/%s/, \"%s\", \"%s\"}",
            item->pron[i].phon,item->pron[i].cats,item->pron[i].tags);
       }  }
     fprintf(fp_out,"\n");
    }
  if (streq(format,"SORT2"))
    {orth2key1(key,item->orth,perr);
     if (*perr < 1)
       {fprintf(fp_out,"%s ",key);
        fprintf(fp_out,"%s ",cocoon(sx,item->orth));
        fprintf(fp_out,"\"%s\" ",item->tags);
        fprintf(fp_out,"\"%s\"",item->status);
   /*     if (textlen(caps) > 0)       fprintf(fp_out," c[%s]",caps); */
   /*     if (textlen(diacritics) > 0) fprintf(fp_out," d[%s]",diacritics); */
        for (i=1; i <= item->nprons; i++)
          {if (!del_pron[i])
           fprintf(fp_out," {/%s/,\"%s\",\"%s\",\"%s\"}",
            item->pron[i].phon,
            item->pron[i].cats,
            item->pron[i].tags,
            item->pron[i].status);
	  }
        fprintf(fp_out,"\n");
    }  }
  if (streq(format,"PCD"))
    fprintf(fp_out,"%s %s\n",item->orth,item->pron[1].phon);
  if (streq(format,"CMU"))
    {max_prons_out = min(5,item->nprons);
     n_prons_out = 0;
     for (i=1; i <= max_prons_out; i++)
       {if (!del_pron[i])
          {if ((++n_prons_out) == 1)
             fprintf(fp_out,"%s ", item->orth);
           else
             fprintf(fp_out,"%s(%d) ",item->orth,n_prons_out);
           fprintf(fp_out," %s\n",item->pron[i].phon);
    }  }  }
 db_leave_msg(proc,0); /* debug only */
return;
} /* end write_lex_item2 */
