/* ItoA - wandelt IBM-Textfiles in AMIGA-Textfiles um.
   Das Problem: Der IBM benutzt andere ASCII-Codes als der AMIGA. Dies macht
   sich vor allem bei den deutschen Umlauten und dem ß bemerkbar.
   Dieses Programm behandelt einen mit AWRITE auf AMIGA-Seite gebrachten Text
   nach. Syntax:
   ItoA <IBMfile> <AMIGAfile>
   <IBMfile>   : Filename des umzuwandelnden Textes
   <AMIGAfile> : Filename des zu erstellenden Textes

   23.3.1989 Tim Pietzcker Software

   Kopieren, Ergänzen, Verstümmeln etc. erwünscht.
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>

#define CONVTBLENGTH 14		/* wenn die Tabelle erweitert wird, einfach
				   neue Anzahl hier eintragen */
#define BUFSIZE      4096l

struct Library    *OpenLibrary();
struct DosBase    *DosBase;
struct FileHandle *source,*dest,*Open();
UBYTE		  *buffer,*sourcename,*destname,*AllocMem();
UBYTE		  ConvTable[] =
		  /* links IBM,rechts AMIGA */
			{ 142,196,	/* Ä */
			  153,214,	/* Ö */
			  154,220,	/* Ü */
			  132,228,	/* ä */
			  148,246,	/* ö */
			  129,252,	/* ü */
			  225,223 };	/* ß */
LONG		  len;

open_files(src,dst)
UBYTE *src,*dst;
{
   source=Open(src,MODE_OLDFILE);
   if (source==NULL) 
   {
      printf("Unable to open source file : %s\n",src);
      get_out();
   }
   dest=Open(dst,MODE_NEWFILE);
   if (dest==NULL)
   {
      printf("Unable to open destination file : %s\n",dst);
      get_out();
   }
}

get_out()
{
   if (DosBase) CloseLibrary(DosBase);
   if (source)  Close(source);
   if (dest)    Close(dest);
   if (buffer)  FreeMem(buffer,BUFSIZE);
   exit(TRUE);
}

convert_block(buf,length) /* wandelt einen Block von BUFSIZE Bytes oder weniger
			     ins richtige Format um */
UBYTE *buf;
LONG length;
{
   UBYTE *ptr;
   LONG i;
   int j;
   for(ptr=buf,i=0;i<length;++i,++ptr)
      if(*ptr & 0x80) /* ASCII-Code > 128 ? */
         for(j=0;j < CONVTBLENGTH; j+=2)	/* Tabelle absuchen */
	    if(*ptr == ConvTable[j]) *ptr=ConvTable[j+1];

   i=Write(dest,buf,length);
   if (i<0)
   {
      printf("Error writing destination file !\n");
      get_out();
   }
}

convert_texts()
{
   long done=FALSE;
   while(done==FALSE)	/* solange, bis EOF erreicht */
   {
      len=Read(source,buffer,BUFSIZE);
      if (len<=0)	/* Error oder fertig ? */
      {
         if (len<0)
         {
            printf("Error reading source file !\n");
	    get_out();
         }
         done=TRUE;
      }   
      else
         convert_block(buffer,len);
   }
}

open_dos()
{
   DosBase=(struct DosBase *)OpenLibrary("dos.library",0l);
   if (DosBase==NULL) exit(FALSE);
   buffer=AllocMem(BUFSIZE,MEMF_CLEAR); 
   if (buffer==NULL)
   {
      printf("Out of Memory !\n");
      get_out();
   }
}

usage(filename)		/* erklärt die korrekte Syntax bei Fehlaufruf */
UBYTE *filename;
{
   printf("USAGE : %s <IBMfile> <AMIGAfile>\n",filename);
   printf("\n<IBMfile>   : Name of the text file still in IBM format.\n");
   printf("              This is a file which has been converted to AMIGA DOS");
   printf(" by the                  IBM command AWRITE (see PC manual)");
   printf("\n<AMIGAfile> : Name of the destination file name.\n");
   exit(FALSE);
}

main(argc,argv)
int argc;
UBYTE *argv[];
{
   /* Testen, ob Aufruf korrekt */
   if (argc<2 || argc>3 || argc==2 && *argv[1]=='?') usage(argv[0]);
   open_dos();
   sourcename = argv[1];
   destname   = argv[2];
   
   open_files(sourcename,destname);
   
   convert_texts();
   get_out();
}

/* End of Source */
