/*****************************************
 ***
 *** FILE   : Convert.c
 *** ------------------------------------
 *** DATE   : 10.07.91
 *** ------------------------------------
 *** AUTHOR : Frank Enderle
 *** ------------------------------------
 *** VERSION: 1.1
 ***
 *****************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <stdio.h>
#include <libraries/dosextens.h>

struct FileLock *MyLock;
struct FileInfoBlock *MyFIB;
struct FileHandle *MyHandle;

UBYTE *Buffer;
BYTE  CSI[1] = {0x9B};
UBYTE SrcFile[80],DstFile[80],ModName[80],Name[80];
BYTE  Ans[5];
BOOL  OK;
int   i,Length;
FILE  *Handle;

main()
{
 printf("\f\n");
 printf("Object to C V1.1\n");
 printf("----------------\n\n");

 printf("(c) 1991 Frank Enderle\n\n");

 printf("\n");
 printf("Sourcefile      (RAW-DATA)   : ");
 scanf("%s",SrcFile);
 printf("Destinationfile (D  -DATA)   : ");
 scanf("%s",DstFile);
 printf("Module Name                  : ");
 scanf("%s",ModName);

 MyFIB  = AllocMem(sizeof(struct FileInfoBlock),MEMF_CHIP|MEMF_CLEAR);
 if(MyFIB  == NULL) CloseAll("*** No Mem for FileInfoBlock");

 MyLock = Lock(SrcFile,ACCESS_READ);
 if(MyLock == NULL) CloseAll("*** Sourcefile not found");

 OK     = Examine(MyLock,MyFIB);
 if(OK     == NULL) CloseAll("*** Can't examine FileInfoBlock");

 UnLock(MyLock);
 MyLock = NULL;

 if(MyFIB->fib_DirEntryType > NULL) CloseAll("*** File is a Directory");
 printf("\n");
 printf("Filelength                   : %6d ( $%08x ) \n",MyFIB->fib_Size,MyFIB->fib_Size);
 printf("Used Blocks                  : %6d ( $%08x ) \n",MyFIB->fib_NumBlocks,MyFIB->fib_NumBlocks);
 printf("\n");
 printf("Length                       : %6d ( $%08x ) \n",MyFIB->fib_Size,MyFIB->fib_Size);
 printf("Actual Position              : ");

 Buffer = AllocMem(MyFIB->fib_Size,MEMF_PUBLIC|MEMF_CLEAR);

 MyHandle = Open(SrcFile,MODE_OLDFILE);
 if(MyHandle == NULL) CloseAll("*** Error while reading");
 Read(MyHandle,Buffer,MyFIB->fib_Size);
 Close(MyHandle);
 MyHandle = NULL;

 Length = 0;
 sprintf(Name,"%s.c",DstFile);

 Handle = fopen(Name,"w");
 if(Handle == NULL) CloseAll("*** Error while writing");

 fprintf(Handle,"/*\n");
 fprintf(Handle," * Module : %s\n",ModName);
 fprintf(Handle," * File   : %s\n",SrcFile);
 fprintf(Handle," *\n");
 fprintf(Handle," * Created with Object to C by F.Enderle\n");
 fprintf(Handle,"*/\n\n");
 fprintf(Handle,"#include <exec/types.h>\n\n");
 fprintf(Handle,"BYTE %s[%d] = {\n\n",ModName,MyFIB->fib_Size);

 for(i=0;i<MyFIB->fib_Size;i++)
 {
  printf("%s16;32H%6d ( $%08x )",CSI,i,i);
  if(Length==0) fprintf(Handle,"   ");
  fprintf(Handle,"0x%02x",Buffer[i]);
  Length++;
  if(Length<12)
   if(i<MyFIB->fib_Size-1) fprintf(Handle,", ");
  if(Length>=12)
  {
   fprintf(Handle,",\n");
   Length = 0;
  }
 }
 printf("%s16;32H%6d ( $%08x )",CSI,i,i);
 fprintf(Handle,"\n\n};\n");
 fclose(Handle);
 Handle = NULL;

 CloseAll("Operation successful");
}

CloseAll(ErrMsg)
UBYTE ErrMsg[80];
{
 printf("\n\n%s\n",ErrMsg);
 if (MyHandle != NULL) Close(MyHandle);
 if (MyLock != NULL  ) UnLock(MyLock);
 if (MyFIB  != NULL  ) FreeMem(MyFIB,sizeof(struct FileInfoBlock));
 if (Buffer != NULL  ) FreeMem(Buffer,MyFIB->fib_Size);
 if (Handle != NULL  ) fclose(Handle);
 exit(NULL);
}


_abort()
{
 CloseAll("*** ABORTED BY USER");
 exit(NULL);
}





