/* filbreak.cpp
ray kelly 11/1999
create smaller portions of a big file: */
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <dir.h>
// function prototypes:
int validate_file ( char source_file[] );
float validate_size ( char source_file[] );
void banner ();
void get_next_file ( char output_fname[], int *suffix, char suffix_str[] ) ;
void main ()
{
FILE *stream;
FILE *output;
char source_file[80]; // their file
char output_fname[12]; // our output file name
char suffix_str[3]; // out output file suffix
char st[1024]; // holds block read
char pwd[MAXPATH]; // holds pwd
char file_lists[30][12]; // holds dest. file names
int j;
int file_is_there; // test file
int suffix = 0; // track file suffix
float piece; // size they chose
float next_portion = 0; // count of what just read
float this_portion_size = 0; // how big a file they want
float ttl_size = 0; // how big we have got
// get their file:
file_is_there = validate_file ( source_file );
if (file_is_there)
{
// ok, get piece size:
piece = validate_size ( source_file );
if (piece)
{
// they chose a valid piece size
getcwd ( pwd, MAXPATH ); // find out where we are
stream = fopen ( source_file, "r+b" );
// open their file
// what we will name their first file:
get_next_file ( output_fname, &suffix, suffix_str );
// add name to array to show later:
strcpy ( file_lists[suffix], output_fname );
output = fopen ( output_fname, "w+b" );
// open it
// read first block:
next_portion = fread ( st, 1, sizeof(st), stream );
gotoxy ( 0, 18 );
printf ( "FILBREAK Progress:" );
gotoxy ( 0, 19 );
printf ( "-------- --------" );
gotoxy ( 0, 20 );
printf ( "Current bytes read = " );
while (next_portion)
{
// continue reading/writing
gotoxy ( 22, 20 );
printf ( "%14.0f", ttl_size );
// show progress
this_portion_size += next_portion;
ttl_size += next_portion;
if (this_portion_size >= piece)
{
// we have exceeded their piece size
// done with this file,
fclose ( output );
// get another file and open it:
get_next_file ( output_fname, &suffix, suffix_str );
output = fopen ( output_fname, "w+b" );
// add name to array to show later:
strcpy ( file_lists[suffix], output_fname );
this_portion_size = 0;
} // end if
fwrite ( st, next_portion, 1, output );
next_portion = fread ( st, 1, sizeof(st), stream );
} // repeat
fclose ( stream );
fclose ( output );
gotoxy ( 0, 21 );
printf ( "Total bytes read = %14.0f", ttl_size );
printf ( "\n\nSource File %s stored in: ", source_file );
// display a report:
for (j = 1; j <= suffix; ++j)
printf ( "\n%2d) %s", j, file_lists[j] );
printf ("\n\nEnter any key to exit ");
getchar();
} // else they did not choose a size
} // else they did not pick a file
atexit(0);
_exit(0);
}
// functions:
// ---------
// function to get and validate the file name:
// ray kelly 12/01/99
int validate_file ( char source_file[] )
{
struct ffblk ffblk;
int valid_file;
int found_file = 0;
do
{
valid_file = 0;
clrscr ();
banner ();
gotoxy ( 1, 5 );
printf ( "Enter Source File Path/Name or 'X' to exit\n" );
printf ( " -->> " );
gets ( source_file );
if (strcmp ( source_file, "X" ) != 0 )
{
// did not pick zero
if (findfirst ( source_file, &ffblk, 0 ) == 0)
{
// the alleged file exists
valid_file = 1;
found_file = 1;
}
else
{
valid_file = 0;
perror ( "\nSource File Error = " );
printf ( "<RETURN> to retry " );
getchar ();
}
}
else
{
valid_file = 1;
}
} while (valid_file == 0);
return (found_file); // what they picked for a file
}
// function to get the portion size desired by user:
// ray kelly 12/01/99
float validate_size ( char source_file[] )
{
char choice[2];
int test_choice;
float byte_count = - 1;
do
{
clrscr ();
banner ();
gotoxy ( 6, 5 );
printf ( "Break %s into pieces of: ", source_file );
gotoxy ( 3, 7 );
printf ( " 1) 360,000 bytes" );
printf ( "\n\n 2) 720,000 bytes" );
printf ( "\n\n 3) 1.44 megabytes" );
printf ( "\n\n 4) 2.00 megabytes" );
printf ( "\n\n\n Enter choice or <RETURN> to exit --->> " );
gets ( choice );
if (strcmp ( choice, "" ) != 0)
{
test_choice = atoi ( choice );
switch (test_choice)
{
case 1: byte_count = 360000; break;
case 2: byte_count = 720000; break;
case 3: byte_count = 1440000; break;
case 4: byte_count = 2000000; break;
}
}
else
{
byte_count = 0;
}
} while (byte_count == - 1);
return byte_count;
}
void banner ()
// show on top:
// ray kelly 12/1999:
{
gotoxy ( 18, 1 );
printf ( "+-+-+-+-+-+-+-+-+-+-+-+" );
gotoxy ( 18, 2 );
printf ( "|--- File Breaker ---|" );
gotoxy ( 18, 3 );
printf ( "+-+-+-+-+-+-+-+-+-+-+-+" );
return;
}
void get_next_file ( char output_fname[], int *suffix, char suffix_str[] )
// get current working dir
// ray kelly 12/1999:
{
const char prefix[] = "PORTION.";
strcpy ( output_fname, prefix ); // start with our prefix:
itoa ( *suffix, suffix_str, 10 );
// turn our suffix into a useable string
strcat ( output_fname, suffix_str );
// add prefix to suffix
*suffix = *suffix + 1;
return;
}
|