/*
** MCPP.trans: Konvertiert Maxon C Fehlerdateien für MegaEd
** von M-J, 100% PD
*/

#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <string.h>

void main(void)
{
 const char *file1="T:MegaEdMake-ErrFile";
 const char *file2="T:MegaEdMake-Errors";
 BPTR con;
 BPTR fileh;
 char *old=NULL,*new=NULL;
 char *off, *off2, *dest, *warn, *last;
 LONG len;
 short i;
 BOOL errors=FALSE;
 							//MCPP Datei lesen
 if(!(fileh=Open(file1,MODE_OLDFILE))) return;
 Seek(fileh,0,OFFSET_END);
 len=Seek(fileh,0,OFFSET_BEGINNING);
 if(len)
 {
  if(!(old=(char*)AllocVec(len+1,MEMF_PUBLIC)))
   {Close(fileh); return;}
  if(Read(fileh,old,len)!=len)
   {FreeVec(old); Close(fileh); return;}
 }
 Close(fileh);
 if(!len) return;
 old[len]=0;
							// Konvertierung
 if(!(new=(char*)AllocVec(len+1,MEMF_PUBLIC))) return;

 off=old;
 dest=new;
 for(i=0;i<3;i++)	// 3 Zeilen überlesen
 {
  if(!(off=strchr(off,10))) goto esc;
  off++;
 }
 for(;;)
 {
  warn=dest;
  dest++;
  if(!(off2=strchr(off,10))) break;
  *off2=0;
  if(!(off=strchr(off,','))) break;
  off++;
  if(!(off2=strrchr(off,','))) break;
  *off2=0;
  for(;*off;off++)	// Zeile
   if(isdigit(*off)) break;
  if(!*off) break;
  for(;isdigit(*off);off++,dest++) *dest=*off;
  off+=strlen(off)+1;
  *dest='/';
  dest++;

  for(;*off;off++)	// Spalte
   if(isdigit(*off)) break;
  if(!*off) break;
  for(;isdigit(*off);off++,dest++) *dest=*off;
  off+=strlen(off)+1;
  *dest=10;
  dest++;

			// Text
  if(!(off2=strchr(off,10)))
   off2=strchr(off,0);
  *off2=0;
  strcpy(dest,off);

  errors=TRUE;
  if(strncmp(off,"Warn",4)) *warn='E';
  else *warn='W';

  off+=strlen(off)+1;
  dest+=strlen(dest);
  *dest=10;
  dest++;
  last=dest;

  if(!*off) break;
 }
 esc:
 if(errors)
 {
  if(fileh=Open(file2,MODE_NEWFILE))
  {
   len=(LONG)last-(LONG)new;
   Write(fileh,new,len);
   Close(fileh);
  }
 }
 else DeleteFile(file2);
 if(old) FreeVec(old);
 if(new) FreeVec(new);
}

