00001 #include <stdlib.h>
00002
00003 #include <iostream>
00004 #include <fstream>
00005
00011 int main(int argc, char* argv[]) {
00012
00013 if(argc < 4 || argc > 5){
00014 std::cout << "swab: Mirrors <word-size> bytes, in the case of 2, it swaps adjacent bytes" << std::endl;
00015 std::cout << "Usage: swab <word-size> <infile> <outfile> <block size in MByte (opt.)>" << std::endl;
00016 exit(0);
00017 }
00018
00019 char *temp1,*temp2;
00020 unsigned long n_bytes;
00021 unsigned long block_size,i,k,n_data,j;
00022 std::ofstream out_data;
00023
00024 n_bytes = atoi(argv[1]);
00025 std::ifstream in_data(argv[2],std::ios::in|std::ios::binary);
00026 if(in_data == NULL){
00027 std::cerr << "swab: ERROR: can't open file " << argv[2] << std::endl;
00028 exit(1);
00029 }
00030 in_data.seekg(0,std::ios::end);
00031 if(argc == 5) block_size = (unsigned)atol(argv[4])*1000000;
00032 else block_size = in_data.tellg();
00033 block_size-= block_size%n_bytes;
00034 n_data=in_data.tellg();
00035 if(block_size > n_data){
00036
00037
00038 block_size = n_data;
00039 }
00040 in_data.seekg(0,std::ios::beg);
00041
00042 if((float)n_data/(float)n_bytes-n_data/n_bytes){
00043 std::cerr << "swab: ERROR: swab: size of \n" << argv[2] <<
00044 "not a multiple of no. of swap bytes (" << n_bytes << ")" << std::endl;
00045 exit(1);
00046 }
00047
00048 temp1=new char[block_size];
00049 temp2=new char[block_size];
00050
00051 for(k=0;k<n_data/block_size;k++){
00052 if(k == 0){
00053 out_data.open(argv[3],std::ios::out|std::ios::binary);
00054 if(out_data == NULL){
00055 std::cerr << "swab: ERROR: can't open file " << argv[3] << std::endl;
00056 exit(1);
00057 }
00058 }else{
00059 out_data.open(argv[3],std::ios::out|std::ios::binary|std::ios::app);
00060 if(out_data == NULL){
00061 std::cerr << "swab: ERROR: can't open file " << argv[3] << std::endl;
00062 exit(1);
00063 }
00064 }
00065 std::cout << "swab: reading block "<< k+1 << " of " <<n_data/block_size
00066 << " blocks" << std::endl;
00067 in_data.read(temp1,block_size);
00068 std::cout << "swab: swabbing block "<< k+1 <<" ..." << std::endl;
00069 for(i=0;i<block_size/n_bytes;i++)
00070 for(j=0;j<n_bytes;j++)temp2[i*n_bytes+j]=temp1[(i+1)*n_bytes-1-j];
00071 out_data.write(temp2,block_size);
00072 out_data.close();
00073 std::cout << "swab: block "<< k+1 << " written to file " << argv[3] << std::endl;
00074 }
00075
00076 temp1=new char[n_data%block_size];
00077 temp2=new char[n_data%block_size];
00078 std::cout << "swab: reading rest block of size " << n_data%block_size/1000 << " KByte" << std::endl;
00079 in_data.read(temp1,n_data%block_size);
00080 std::cout << "swab: swabbing rest block ... "<< std::endl;
00081 for(i=0;i<n_data%block_size/n_bytes;i++)
00082 for(j=0;j<n_bytes;j++)temp2[i*n_bytes+j]=temp1[(i+1)*n_bytes-1-j];
00083 out_data.open(argv[3],std::ios::out|std::ios::binary|std::ios::app);
00084 out_data.write(temp2,n_data%block_size);
00085 out_data.close();
00086 std::cout << "swab: rest block written to file " << argv[3] << std::endl;
00087
00088
00089 in_data.close();
00090 return 0;
00091 }