/* Name Main.c Author Peter Wright Notice (c)1998, GCSL. All Rights Reserved Notes This is the main source file for Converter, a program that will convert a binary file into a text .h file that can be included into a project. */ #include #include #include /* Definitions for functions to come */ void PrintStatus(); void UpdateStatus(); int CheckFile( char * sFileToCheck ); void DisplayInfo ( char * sFileName, char * HeaderFile, char * sVariable, char * sComment ); void ConvertFile ( char * sFilename, char * HeaderFile, char * sVariable, char * sComment ); /* Globals*/ char sFilename[255]; char sHeaderFile[255]; char sComment[255]; char sVariable[255]; int main() { int bDone; printf("Converter, Version 1.0, By Peter J. Wright\n"); printf("(c) 1998, GCSL. All Rights Reserved.......\n"); printf("\n\n\n"); /*Get the name of the file to convert ( I know Gets is dangerous...but it copes with strings with spaces in them.*/ bDone = 0; while ( ! bDone ) { printf("File to convert : "); gets( sFilename ); bDone = CheckFile(sFilename ); if ( !bDone ) printf("\n\n\nThe file %s does not exist. Try again.\n\n\n", sFilename); } bDone = 0; while ( ! bDone ) { printf("Name of Headerfile: "); gets( sHeaderFile ); bDone = 1; /* Check that the user isn't overwriting the original file*/ if ( strcmp( sFilename, sHeaderFile ) == 0 ) { printf("You cannot use the same name as the input file.\n\n\n"); bDone = 0; } /* Check that the user actually entered a name*/ if ( strlen( sHeaderFile ) == 0 ) { printf("You must enter a name for the output file.\n\n\n"); bDone = 0; } } /* Now that we have the name of the input and output files, its time to get the name of the variable, and the comment string*/ bDone = 0 ; while ( ! bDone ) { printf("Enter a variable name : "); gets( sVariable ); bDone = 1; if ( strlen( sVariable ) == 0 ) { printf("You have to enter a name for the array variable.\n\n\n"); bDone = 0; } } printf("Enter a comment to go above the variable..Hit return when done\n\n\n"); gets( sComment ); DisplayInfo( sFilename, sHeaderFile, sVariable, sComment ); ConvertFile( sFilename, sHeaderFile, sVariable, sComment ); return (0); } int CheckFile( char * sFileToCheck ) { int bReturn; /* Checks to see if a file exists, by attempting to open it for reading*/ FILE * TestFile; TestFile = fopen( sFileToCheck, "r"); bReturn = ( TestFile != 0); if ( bReturn ) fclose( TestFile ); return( bReturn ); } void PrintStatus() { /* Prints a status bar header, ready for the user to update it */ printf("|----Status bar------|\n\n\n"); } void UpdateStatus() { /* Updates the status bar, by printing . for position */ static nPosition = 0; /* We need to space forward if this is the first time the routine got called*/ if ( nPosition == 0 ) printf(" "); /* Update the position*/ nPosition++; /* If we are a multiple of 5 through the position, print a .*/ if ( nPosition % 5 == 0) printf("."); /* If the position is now 100, meaning we are done, then we can newline*/ if ( nPosition >= 100 ) printf("\n\n\n"); } void ConvertFile( char * sFilename, char * sHeaderFile, char * sVariable, char * sComment ) { /* Converts the file specified */ FILE * InputFile; FILE * OutputFile; char HexCodes[] = { "0123456789ABCDEF" }; char Input[10]; long nIndex; /* Open the output file first, and dump out the generic header stuff*/ OutputFile = fopen( sHeaderFile, "w" ); fprintf(OutputFile, "/* %s */\n", sComment ); fprintf(OutputFile, "char %s[] = {\n", sVariable ); /* Now go ahead and open the input file*/ InputFile = fopen( sFilename, "r"); nIndex = 0; while ( !feof(InputFile) ) { fread( Input, 1, 1, InputFile); if ( nIndex > 0 ) fprintf(OutputFile, ", "); fprintf(OutputFile, "0x"); fwrite( &HexCodes[ (((unsigned char)Input[0] & 0xF0) >> 4) ], 1, 1, OutputFile ); fwrite( &HexCodes[ (((unsigned char)Input[0] & 0x0F)) ], 1, 1, OutputFile ); nIndex++; if ( nIndex % 15 == 0 ) fprintf(OutputFile, "\n" ); } fprintf(OutputFile, "};"); fclose(InputFile); fclose(OutputFile); printf("\n\nConversion complete!\n\n"); } void DisplayInfo( char * sFilename, char * sHeaderFile, char * sVariable, char * sComment ) { /* Displays information on the selected conversion */ printf("\n\n\n\n"); printf("CONVERSION SUMMARY\n"); printf("\n"); printf("Input File : %s\n", sFilename); printf("Output File: %s\n", sHeaderFile); printf("\nBuilding output file as \n"); printf("------------------------------------------------\n"); printf(" /* %s */ \n", sComment); printf(" char %s[] = { ..... };\n",sVariable); printf("------------------------------------------------\n"); printf("\n"); }