11 int main(
int argc,
char* argv[]) {
13 if(argc < 4 || argc > 5) {
14 std::cout <<
"swab: Mirrors <word-size> bytes, in the case of 2, it swaps adjacent bytes" << std::endl;
15 std::cout <<
"Usage: swab <word-size> <infile> <outfile> [<block size in MB>]" << std::endl;
20 unsigned long n_bytes;
21 unsigned long block_size,i,k,n_data,j;
22 std::ofstream out_data;
24 n_bytes = atoi(argv[1]);
26 std::cerr <<
"swab: ERROR: invalid n_bytes=" << n_bytes << std::endl;
30 std::ifstream in_data(argv[2],std::ios::in|std::ios::binary);
32 std::cerr <<
"swab: ERROR: can't open file " << argv[2] << std::endl;
35 in_data.seekg(0,std::ios::end);
36 if(argc == 5) block_size = (unsigned)atol(argv[4])*1000000;
37 else block_size = in_data.tellg();
38 block_size-= block_size%n_bytes;
39 n_data=in_data.tellg();
40 if(block_size > n_data) {
45 in_data.seekg(0,std::ios::beg);
47 if((
float)n_data/(float)n_bytes-n_data/n_bytes) {
48 std::cerr <<
"swab: ERROR: swab: size of \n" << argv[2] <<
49 "not a multiple of no. of swap bytes (" << n_bytes <<
")" << std::endl;
53 temp1=
new char[block_size];
54 temp2=
new char[block_size];
56 for(k=0;k<n_data/block_size;k++) {
58 out_data.open(argv[3],std::ios::out|std::ios::binary);
60 std::cerr <<
"swab: ERROR: can't open file " << argv[3] << std::endl;
65 out_data.open(argv[3],std::ios::out|std::ios::binary|std::ios::app);
67 std::cerr <<
"swab: ERROR: can't open file " << argv[3] << std::endl;
71 std::cout <<
"swab: reading block "<< k+1 <<
" of " <<n_data/block_size
72 <<
" blocks" << std::endl;
73 in_data.read(temp1,block_size);
74 std::cout <<
"swab: swabbing block "<< k+1 <<
" ..." << std::endl;
75 for(i=0;i<block_size/n_bytes;i++)
76 for(j=0;j<n_bytes;j++)temp2[i*n_bytes+j]=temp1[(i+1)*n_bytes-1-j];
77 out_data.write(temp2,block_size);
79 std::cout <<
"swab: block "<< k+1 <<
" written to file " << argv[3] << std::endl;
82 temp1=
new char[n_data%block_size];
83 temp2=
new char[n_data%block_size];
84 std::cout <<
"swab: reading rest block of size " << n_data%block_size/1000 <<
" KByte" << std::endl;
85 in_data.read(temp1,n_data%block_size);
86 std::cout <<
"swab: swabbing rest block ... "<< std::endl;
87 for(i=0;i<n_data%block_size/n_bytes;i++)
88 for(j=0;j<n_bytes;j++)temp2[i*n_bytes+j]=temp1[(i+1)*n_bytes-1-j];
89 out_data.open(argv[3],std::ios::out|std::ios::binary|std::ios::app);
90 out_data.write(temp2,n_data%block_size);
92 std::cout <<
"swab: rest block written to file " << argv[3] << std::endl;