/*
	ReCoder.c       Recodes different Russian tables:
			ASCII(a,b), MIC, KOI-8,
			and different new line symbols:
			CR+LF <-> CR.
*/

#include "ReCoder.h"
#include "tr.h"


/********************************************************/


#include <stdio.h>
int main (int argc, char *argv[])
{
  int i;
  int ch;

  enum { NONE, toDOS, toUNIX }	DosUnix=NONE;
  FILE *InFile=stdin, *OutFile=stdout;	/* Files ...			*/
  int InGot=0, OutGot=0;		/* Are the filenames got ?	*/

  const BYTE * TableFrom=NULL,	/* Source/Destination coding tables ...	*/
             * TableTo=NULL;	/* The start values have to be defined!	*/

  BYTE theTable[256];	/* The generated table from->to (for the speed) */



  if ((argc==1)||(!STRCMP(argv[argc-1],"-help"))||
      (!STRCMP(argv[argc-1],"--help"))) {
    fputs(HELPTEXT,stdhelp);
    return 1;
  }
  for (i=1;i<argc;i++) {
    if	(!STRCMP(argv[i],		"-d2u"))
      DosUnix = toUNIX;
    else if	(!STRCMP(argv[i],		"-u2d"))
      DosUnix = toDOS;

    /* CONVERT: "-x2y"						*/
    else if  ((argv[i][0] == '-')&&
              (argv[i][2] == '2')&&
              (strlen(argv[i]) == strlen("-x2y"))) {
      switch (argv[i][1]) {			/* "From"-letter...	*/
      case 'a':	TableFrom = Tbl_GOST_alt;	break;
      case 'k':	TableFrom = Tbl_KOI8;		break;
      case 'b':	TableFrom = Tbl_BESTA;		break;
      case '7':	TableFrom = Tbl_KOI7;		break;
      case 'l':	TableFrom = Tbl_KOI7_inv;	break;
      case 'w':	TableFrom = Tbl_CP1251;		break;
      case 'i':	TableFrom = Tbl_ISO8859_5;	break;
      case 'g':	TableFrom = Tbl_MIC;		break;
      case 'o':	TableFrom = Tbl_GOST_main;	break;
      case 'm':	TableFrom = Tbl_Macintosh;	break;
      default:	ERROR2("Illegal parameter `FROM' in command line: ",argv[i]);
      }/*switch "from"*/

      switch (argv[i][3]) {			/* "To"-letter...	*/
      case 'a':	TableTo = Tbl_GOST_alt;		break;
      case 'k':	TableTo = Tbl_KOI8;		break;
      case 'b':	TableTo = Tbl_BESTA;		break;
      case '7':	TableTo = Tbl_KOI7;		break;
      case 'l':	TableTo = Tbl_KOI7_inv;		break;
      case 'w':	TableTo = Tbl_CP1251;		break;
      case 'i':	TableTo = Tbl_ISO8859_5;	break;
      case 'g':	TableTo = Tbl_MIC;		break;
      case 'o':	TableTo = Tbl_GOST_main;	break;
      case 'm':	TableTo = Tbl_Macintosh;	break;
      default:	ERROR2("Illegal parameter `TO' in command line: ",argv[i]);
      }/*switch "to"*/
    }/*if "encoding from-to"*/

    else if (!STRCMP(argv[i],		"--")) {
      if 	(!InGot) {	InFile=stdin;	InGot=1;  }
      else if	(!OutGot){	OutFile=stdout;	OutGot=1; }
      else	fprintf(stderr,"Extra parameter `--' in command line.\n");
    }

    else {
      if	(!InGot) {
        if ((InFile = fopen (argv[i],"rb"))==NULL)
          ERROR("Cannot open input file.");
        InGot=i;
      }
      else if (!OutGot) {
        if (STRCMP(argv[InGot],argv[i]) == 0)
          fclose(InFile), ERROR("Output file is an input file.\n");
        else if ((OutFile= fopen (argv[i],"wb"))==NULL)
          fclose(InFile), ERROR("Cannot open output file.");
        OutGot=i;
      }
      else
        ERROR2("Illegal option or file name : `%s'.\n"
               "To see help run with -help switch.\n",	argv[i]);
    }/*else*/
  }/*for*/

  if (TableFrom == NULL)
    TableFrom = Tbl_GOST_alt;
  if (TableTo == NULL)
    TableTo = Tbl_GOST_alt;

  /*********** Build the Translation Table ************/
  /***********     (for the speed)         ************/

  for(ch=0;ch<256;ch++)
    theTable[ch] = trChar (TableFrom, TableTo, ch, 1);

  /****************** MAIN LOOP ***********************/

  while ((ch=fgetc(InFile)) != EOF) {
    /* Recoding...	*/
    /*ch = trChar (TableFrom, TableTo, ch, 1);*/
    ch = theTable[ch];

    /* New line...	*/
    if	(((BYTE)ch==(BYTE)'\n') && (DosUnix==toDOS)) {
      fputc('\r',OutFile);	fputc('\n',OutFile);
    }
    else if	(((BYTE)ch==(BYTE)'\r') && (DosUnix==toUNIX)) ;

    /* EOF...	*/
    else if	(((BYTE)ch!=(BYTE)CONTROL_Z) || (DosUnix!=toUNIX))
      fputc(ch,OutFile);
  }/*while*/

  if (DosUnix==toDOS)					/* EOF ^Z */
    fputc(CONTROL_Z, OutFile);

  fclose(InFile);
  fclose(OutFile);
  return 0;		/* For pipe to work OK and without messages.	*/
}


/*<EOF>*/

