#include "stdafx.h" #include "file_io_lib.h" //------------------------------------------------------------------------------------ // FindNumberOfDigits // // Description - Finds and returns number of digits in 'in_number' integer //------------------------------------------------------------------------------------ int FindNumberOfDigits(int in_number) { int digit_cnt = 0; // Simply add 1 digit for every division by 10 that does not result in 0 while ( in_number != 0 ) { in_number /= 10; ++digit_cnt; } return digit_cnt; } //------------------------------------------------------------------------------------ // ExtensionTypeToString // // Description - Converts constant number to appropriate file extension string //------------------------------------------------------------------------------------ char* ExtensionTypeToString(int file_type) { char* str; if (file_type == INPUT_IS_BMP) str = ".BMP"; else if (file_type == INPUT_IS_RAW) str = ".RAW"; else if (file_type == INPUT_IS_BIN) str = ".BIN"; else if (file_type == INPUT_IS_RAW_STACK) str = ".STACK.RAW"; else if (file_type == INPUT_IS_BIN_STACK) str = ".STACK.BIN"; else if (file_type == INPUT_IS_CBIN_STACK) str = ".STACK.CBIN"; else str = "INVALID"; return str; } //------------------------------------------------------------------------------------ // DeleteFilesWithIndex // // Description - Deletes all files with prefix 'file_prefix' with an appended // index from 'first_index' to 'last_index', and extension number 'file_type' //------------------------------------------------------------------------------------ bool DeleteFilesWithIndex(char* file_prefix, int file_type, int first_index, int last_index) { char * file_name; int max_digits = FindNumberOfDigits( last_index ); // Loop for each index, creating file name and deleting file for (int i = first_index; i <= last_index; i++) { file_name = AppendDigitsAndExtension(file_prefix, i, max_digits, file_type); // Delete the file if ( !DeleteFile( file_name ) ) return false; free( file_name ); } return true; } //------------------------------------------------------------------------------------ // DeleteFilesWithIndex // // Description - Deletes all files with prefix 'file_prefix' with an appended // index from 'first_index' to 'last_index', and extension number 'file_type' //------------------------------------------------------------------------------------ char* AppendDigitsAndExtension(char* file_prefix, int num, int max_dig_cnt, int ext) { char* file_name = (char*) malloc(sizeof(char)*512); // Pad with '0's if needed sprintf(file_name, "%s", file_prefix); for ( int cur_dig = FindNumberOfDigits(num); cur_dig < max_dig_cnt; cur_dig++ ) { sprintf(file_name, "%s0", file_name); } // There should only be a BMP, BIN, or RAW extension if (ext == INPUT_IS_BMP) sprintf(file_name, "%s%d.bmp", file_name, num); else if (ext == INPUT_IS_RAW) sprintf(file_name, "%s%d.raw", file_name, num); else if (ext == INPUT_IS_BIN) sprintf(file_name, "%s%d.bin", file_name, num); return file_name; } //------------------------------------------------------------------------------------ // OpenFileForAppendAtLine // // Description - Opens file 'file_name', and writes '\n' until 'line_number' is // total number of lines in the file, and returns a pointer to the file. //------------------------------------------------------------------------------------ int OpenFileForAppendAtLine( int line_number, char *file_name, ofstream &res_file ) { ifstream file_in; char buffer[512]; file_in.open( file_name, ios::in ); // Make sure file is open if( !file_in.is_open() ) { return 1; } // Figure out how many lines there are by counting '\n' characters int line_cnt = 0; do { file_in.getline( buffer, 512 ); ++line_cnt; } while ( !file_in.eof() ); // Now we know how many lines there currently are, reopen the file // in append mode, write out however many lines are remining and return file_in.close(); res_file.open( file_name, ios::out | ios::app ); while ( (++line_cnt) <= line_number ) { res_file << endl; } return 0; }