Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bz_compress.cpp
Go to the documentation of this file.
1 /*
2  * Compresses data in Bz2 format
3  *
4  * without parameters reads data from standard input and writes compressed data to standard output
5  * with 1 parameter reads data from the file given and writes compressed data to standard output
6  * with 2 parameters reads data from the file in the 1st argument and writes compressed data to the file in the 2nd argument
7  *
8  */
9 
10 #include <xstream/bz.h>
11 
12 #include <iostream>
13 #include <fstream>
14 
15 using namespace std;;
16 using namespace xstream;;
17 
18 
19 int main(int argc, char* argv[]){
20 
21  const size_t len = 4*1024;
22  char buf[len];
23 
24  istream* readfrom;
25  ostream* writeto;
26 
27  try{
28  /* Binary flags mandatory on win32 systems, otherwise files will be corrupt */
29  if(1<argc){
30  readfrom = new ifstream(argv[1], ios::binary);
31  if(2<argc){
32  writeto = new ofstream(argv[2], ios::binary);
33  }else{
34  writeto = &cout;
35  }
36  }else{
37  readfrom = &cin;
38  writeto = &cout;
39  }
40 
41  bz::ostreambuf bz_o(writeto->rdbuf());
42  ostream bzout(&bz_o);
43 
44  //raise exceptions
45  bzout.exceptions(ios::badbit);
46 
47  while(readfrom->good()){
48  readfrom->read(buf,len);
49  bzout.write(buf,readfrom->gcount());
50  }
51 
52  bzout.flush();
53 
54  }
55  catch(exception& e){
56  cerr<<"Error: "<<e.what()<<endl;
57  }
58 
59  //pointers not being freed
60 
61  return 0;
62 }
TEllipse * e
C++ streambuf interface to read and write bzip2 streams.
int main(int argc, char *argv[])
Definition: gendoc.cc:6