Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
z_compress.cpp
Go to the documentation of this file.
1 /*
2  * Compresses data in Zlib 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/z.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  z::ostreambuf z_o(writeto->rdbuf());
42  ostream zout(&z_o);
43 
44  //raise exceptions
45  zout.exceptions(ios::badbit);
46 
47  while(readfrom->good()){
48  readfrom->read(buf,len);
49  zout.write(buf,readfrom->gcount());
50  }
51 
52  zout.flush();
53  //clog<<"Adler32 = "<< (z_o.checksum() )<<endl;
54 
55  }
56  catch(exception& e){
57  cerr<<"Error: "<<e.what()<<endl;
58  }
59 
60 
61  //pointers not being freed
62 
63  return 0;
64 }
TEllipse * e
C++ streambuf interface to read and write file formats supported by Zlib.
int main(int argc, char *argv[])
Definition: gendoc.cc:6