/*
  -----------------------------------------------------------------------
  Copyright 2007  Alfred Steffens Jr.
  -----------------------------------------------------------------------
  -----------------------------------------------------------------------
    Copying Permission:

    hexxer is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    hexxer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with hexxer (see file called "COPYING"); if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307  USA
  ------------------------------------------------------------------------

    Mscore release 1.1


    Description:

	hexxer.c		2/5/05				A. Steffens Jr.

	Converts a binary file to an ascii file
	Each byte will be a 2-digit hexidecimal number
	The numbers will be listed in a single column.

	Usage:

		hexxer  <input file>   <output file>  [b]
*/

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


void main(int argc, char *argv[])
{
static char infilename[128], outfilename[128];
FILE *infile, *outfile;
int reverse_flag=0;

	//
	//	check basic usage
	//
	if (argc < 3) {
		fprintf(stderr, "Usage:  hexxer  <input_file>   <output_file>  [b]\n");
		fprintf(stderr, "	where the optional b is used for hex to binary mode\n");
		exit(1);
	}

	//
	//	get the file names from the command line
	//
	strcpy(infilename, argv[1]);
	strcpy(outfilename, argv[2]);

	if (argc == 4) {
		if (strcmp(argv[3],"b")==0) {
			//
			//	hex to binary -- mode
			//
			reverse_flag = 1;
		}
	}




	if (!reverse_flag) {
		int ch;
		//
		//	binary to hex conversion
		//

		//
		//	open the input file (binary)
		//
		infile = fopen(infilename, "rb");
		if (infile == NULL) {
			fprintf(stderr, "ERROR: can't open input file (%s)\n", infilename);
			exit(1);
		}
	
		//
		//	open the output file  (hex list)
		//
		outfile = fopen(outfilename, "w");
		if (outfile == NULL) {
			fprintf(stderr, "ERROR: can't create output file (%s)\n", outfilename);
			exit(1);
		}


		//
		//	read the binary file until EOF
		//
		while (feof(infile)==0) {

			ch = fgetc(infile);
			if (ch == EOF) {
				break;
			}

			fprintf(outfile, "%02x\n", ch & 0x00ff);

		} // while -- feof


	}
	else {
		int ch, err;
		//
		//	convert a hex list into a binary file
		//

		//
		//	open the input file (hex list)
		//
		infile = fopen(infilename, "r");
		if (infile == NULL) {
			fprintf(stderr, "ERROR: can't open input file (%s)\n", infilename);
			exit(1);
		}
	
		//
		//	open the output file  (binary)
		//
		outfile = fopen(outfilename, "wb");
		if (outfile == NULL) {
			fprintf(stderr, "ERROR: can't create output file (%s)\n", outfilename);
			exit(1);
		}

		//
		//	read the binary file until EOF
		//
		while (feof(infile)==0) {

			err = fscanf(infile, "%02x\n", &ch);
			if (err != 1) {
				fprintf(stderr, "ERROR: reading hex input file\n");
				exit(1);
			}

			err = fputc(ch, outfile);
			if (err == EOF) {
				break;
			}

		} // while -- feof


	} // else -- hex to binary



	fclose(infile);
	fclose(outfile);
}
/*****************************  main  ***************************************/




