diff options
Diffstat (limited to 'split.cc')
-rw-r--r-- | split.cc | 33 |
1 files changed, 26 insertions, 7 deletions
@@ -28,13 +28,14 @@ #include <sys/stat.h> #include "lzip.h" +#include "file_index.h" namespace { void first_filename( const std::string & input_filename, const std::string & default_output_filename, - std::string & output_filename ) + std::string & output_filename, const int max_digits ) { if( default_output_filename.size() ) output_filename = default_output_filename; @@ -42,15 +43,17 @@ void first_filename( const std::string & input_filename, output_filename = input_filename; int b = output_filename.size(); while( b > 0 && output_filename[b-1] != '/' ) --b; - output_filename.insert( b, "rec00001" ); + output_filename.insert( b, 1, '1' ); + if( max_digits > 1 ) output_filename.insert( b, max_digits - 1, '0' ); + output_filename.insert( b, "rec" ); } -bool next_filename( std::string & output_filename ) +bool next_filename( std::string & output_filename, const int max_digits ) { int b = output_filename.size(); while( b > 0 && output_filename[b-1] != '/' ) --b; - for( int i = b + 7; i >= b + 3; --i ) // "rec00001" + for( int i = b + max_digits + 2; i > b + 2; --i ) // "rec<max_digits>" { if( output_filename[i] < '9' ) { ++output_filename[i]; return true; } else output_filename[i] = '0'; @@ -96,7 +99,12 @@ int do_split_file( const std::string & input_filename, uint8_t * & base_buffer, struct stat in_stats; const int infd = open_instream( input_filename, &in_stats, true, true ); if( infd < 0 ) return 1; - int size = readblock( infd, buffer, buffer_size + hsize ) - hsize; + File_index file_index( infd ); + const int max_members = ( file_index.retval() ? 999999 : file_index.members() ); + int max_digits = 1; + for( int i = max_members; i >= 10; i /= 10 ) ++max_digits; + + int size = seek_read( infd, buffer, buffer_size + hsize, 0 ) - hsize; bool at_stream_end = ( size < buffer_size ); if( size != buffer_size && errno ) { show_error( "Read error", errno ); return 1; } @@ -105,7 +113,8 @@ int do_split_file( const std::string & input_filename, uint8_t * & base_buffer, if( !verify_header( *(File_header *)buffer, verbosity ) ) return 2; std::string output_filename; - first_filename( input_filename, default_output_filename, output_filename ); + first_filename( input_filename, default_output_filename, output_filename, + max_digits ); int outfd = open_outstream_rw( output_filename, force ); if( outfd < 0 ) { close( infd ); return 1; } @@ -126,7 +135,12 @@ int do_split_file( const std::string & input_filename, uint8_t * & base_buffer, { show_error( "Write error", errno ); return 1; } if( close( outfd ) != 0 ) { show_error( "Error closing output file", errno ); return 1; } - if( !next_filename( output_filename ) ) + if( verbosity >= 1 ) + { + std::printf( "Member '%s' done \r", output_filename.c_str() ); + std::fflush( stdout ); + } + if( !next_filename( output_filename, max_digits ) ) { show_error( "Too many members in file." ); close( infd ); return 1; } outfd = open_outstream_rw( output_filename, force ); if( outfd < 0 ) { close( infd ); return 1; } @@ -159,6 +173,11 @@ int do_split_file( const std::string & input_filename, uint8_t * & base_buffer, close( infd ); if( close( outfd ) != 0 ) { show_error( "Error closing output file", errno ); return 1; } + if( verbosity >= 1 ) + { + std::printf( "Member '%s' done \n", output_filename.c_str() ); + std::fflush( stdout ); + } return 0; } |