// (c) 2005-2007 Electronic Arts Inc.
#if 0
"------------------------------------------------------------------",
"                                                                  ",
"                         ImageToHex v1.01                         ",
"                                                                  ",
"                 by James Hall and Frank Barchard                 ",
"                                                                  ",
"                    Main program - Jun 14, 2005                   ",
"                                                                  ",
"------------------------------------------------------------------",
"                                                                  ",
" Version Date   SE History:                                       ",
" ------- ----   -- --------                                       ",
" 1.00    021202 JH Original version for MVP used in Fifa Street   ",
" 1.01    050614 FB Width 32 (was 8), documented                   ",
"                                                                  ",
" http://tools.eac.ea.com/util/imagetohex.htm                      "
"                                                                  ",
"------------------------------------------------------------------",
"\0"
};
#endif

#if defined(_MSC_VER)
#pragma warning(disable: 4996)  // warning C4996: 'fscanf' was declared deprecated
#endif
#if defined(__ICL)
#pragma warning(disable: 1478)  // (icl) warning #1478: function "strcpy" was declared "deprecated"
#endif

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

#define COLUMNS_PER_ROW 32

#define VERSION_MAJOR   1
#define VERSION_MINOR   1
#define VERSION_NAME    "Courtenay"
#define VERSION_DATE    "2005.6.19"

#if 0
/*
;
; ABSTRACT
;
; imagetohex - converts a binary file to a table of values for C code
;@ImageToHex
;
; SUMMARY
;     imagetohex <input file> <output file>
;
; DESCRIPTION
;
; ImageToHex converts a binary file to a table of values for C code.
; The code including the data is responsible for declaring the array
;
; #if defined( INCLUDEFILESYSDIR )
; unsigned char gPs2Dir[]=
; {
; #include "ps2dir.h"
; };
;
; RETURNS
;
; 0 for success, >= 1 for an error
;
; SEE ALSO
;
; xd, Spit
;
; SAMPLE OUTPUT
;
; >dvdmap e:\share\bball\fifast-ps2.map e:\share\bball\ps2dir.bin -noroot -invert
; >huff -tj e:\share\bball\ps2dir.bin =e:\share\bball\ps2dir.qbin
; >imagetohex e:\share\bball\ps2dir.qbin \fifast\source\common\main\ps2dir.h
;
; END ABSTRACT
;
*/
#endif


int main( int argc, char* argv[] )
{
    fprintf( stdout, "ImageToHex v%d.%d (%s) %s by James Hall, EAC\n", VERSION_MAJOR, VERSION_MINOR, VERSION_NAME, VERSION_DATE );
    if( argc < 3 )
    {
        fprintf( stderr, "Usage: %s <input file> <output file>\n", argv[ 0 ] );
        return 1;
    }

    int column_count = 0;

    FILE *input_file;
    FILE *output_file;

    input_file = fopen( argv[ 1 ], "rb" );
    if( !input_file )
    {
        fprintf( stderr, "Unable to open input file \"%s\"\n", argv[ 1 ] );
        return( 2 );
    }

    output_file = fopen( argv[ 2 ], "w" );
    if( !output_file )
    {
        fprintf( stderr, "Unable to open output file \"%s\"\n", argv[ 2 ] );
        fclose( input_file );
        return( 3 );
    }

    // Print the header to the output file first.
    fprintf( output_file, "// ========================================================================\n" );
    fprintf( output_file, "// Automatically generated by ImageToHex v%d.%d (%s) %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_NAME, VERSION_DATE );
    fprintf( output_file, "// %s - Hex Dump of %s\n", argv[ 2 ], argv[ 1 ] );
    fprintf( output_file, "// Copyright (C) 2002,2005 Electronic Arts Inc. All rights reserved.\n");
    fprintf( output_file, "// ========================================================================\n\n" );

    int next_char = EOF;
    while( ( next_char = fgetc( input_file ) ) != EOF )
    {
        if (column_count==0)
            fprintf( output_file, "\t" );
        fprintf( output_file, "0x%02X", next_char);
        column_count++;
        if( COLUMNS_PER_ROW == column_count )
        {
            fprintf( output_file, ",\n" );
            column_count = 0;
        }
        else
        {
            fprintf( output_file, "," );
        }
    }

    fprintf( output_file, "\n" );

    fclose( input_file );
    fclose( output_file );

    return 0;
}
