/*
	$VER: Bin2Asm 0.5

	Bin2Asm by LeMUr/ Fire
	             /& blabla

	FreeWare.

		This programm writes bin-data as assembler source-code.

		If You have only KickStart 1.3 then change all "Printf" (dos.library
	v37++) to "printf" (linked from amiga.lib), but it'll make longer
	program.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <proto/dos.h>
#include <proto/exec.h>

#include <exec/memory.h>

// version
const static char Version[]="\0$VER: Bin2Asm 0.5 (15.12.1995) by LeMUr/Fire & blabla\0";

// memory and long
UBYTE *Bufor=NULL;
ULONG Size=NULL;


void out(int kod)
// procedure frees memory and quits
{
	if(Bufor && Size)
		FreeMem(Bufor, Size);

	exit(kod);
} /* out() */


int main(int argc, char *argv[])
{
	UBYTE znak[10];
	ULONG i=0;

	BPTR file, lock;
	struct FileInfoBlock __aligned fib;

	/* let's check arguments... */
	if(argc!=3)
	{
		Printf("I need two file-names as argument!\n");
		out(5);
	}

	/* read file into memory */
	if(!(lock=Lock(argv[1], ACCESS_READ)))
	{
		Printf("Unable to lock \"%s\"\n", argv[1]);
		out(25);
	}

	if(!Examine(lock, &fib))
	{
		Printf("Unable to examine \"%s\"\n", argv[1]);
		UnLock(lock);
		out(30);
	}

	if(!(Size=fib.fib_Size))
	{
		Printf("File couldn't be 0 bytes long!\n");
		UnLock(lock);
		out(32);
	}

	if(!(Bufor=AllocMem(Size, MEMF_CHIP)))
	{
		Printf("Unable to allocate memory!\n");
		UnLock(lock);
		out(35);
	}

	if(!(file=Open(argv[1], MODE_OLDFILE)))
	{
		Printf("Unable to open file \"%s\"\n", argv[1]);
		UnLock(lock);
		out(40);
	}

	Read(file, Bufor, Size);
	Close(file);
	UnLock(lock);

	/* saving source code */
	if(!(file=Open(argv[2], MODE_NEWFILE)))
	{
		Printf("Unable to open file \"%s\"\n", argv[2]);
		UnLock(lock);
		out(40);
	}

	Write(file, "Data:\n", 6);
	/* "conversion" */
	for(i=0; i<Size; i++)
	{
		sprintf(znak, "\t$%X\n", Bufor[i]);
		if(strlen(znak)==4)
			sprintf(znak, "\t$0%X\n", Bufor[i]);
		Write(file, "\tDC.B", 5);
		Write(file, znak, strlen(znak));
	}

	Close(file);

	out(0);
} /* main() */
