ODIN
swab.cpp
1 #include <stdlib.h>
2 
3 #include <iostream>
4 #include <fstream>
5 
11 int main(int argc, char* argv[]) {
12 
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;
16  return 0;
17  }
18 
19  char *temp1,*temp2;
20  unsigned long n_bytes;
21  unsigned long block_size,i,k,n_data,j;
22  std::ofstream out_data;
23 
24  n_bytes = atoi(argv[1]);
25  if(n_bytes<=0) {
26  std::cerr << "swab: ERROR: invalid n_bytes=" << n_bytes << std::endl;
27  return -1;
28  }
29 
30  std::ifstream in_data(argv[2],std::ios::in|std::ios::binary);
31  if(in_data.bad()) {
32  std::cerr << "swab: ERROR: can't open file " << argv[2] << std::endl;
33  return -1;
34  }
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) {
41  // std::cerr << "swab: block size exceeding file size !" << std::endl
42  // << "setting block size to file size (=" << n_data/1000000. << "MB)" << std::endl;
43  block_size = n_data;
44  }
45  in_data.seekg(0,std::ios::beg);
46 
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;
50  return -1;
51  }
52 
53  temp1=new char[block_size];
54  temp2=new char[block_size];
55 
56  for(k=0;k<n_data/block_size;k++) {
57  if(k == 0) {
58  out_data.open(argv[3],std::ios::out|std::ios::binary);
59  if(out_data.bad()) {
60  std::cerr << "swab: ERROR: can't open file " << argv[3] << std::endl;
61  return -1;
62  }
63  }
64  else {
65  out_data.open(argv[3],std::ios::out|std::ios::binary|std::ios::app);
66  if(out_data.bad()) {
67  std::cerr << "swab: ERROR: can't open file " << argv[3] << std::endl;
68  return -1;
69  }
70  }
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);
78  out_data.close();
79  std::cout << "swab: block "<< k+1 << " written to file " << argv[3] << std::endl;
80  }
81 
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);
91  out_data.close();
92  std::cout << "swab: rest block written to file " << argv[3] << std::endl;
93 
94  in_data.close();
95  return 0;
96 }