Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
b64_encode.cpp
Go to the documentation of this file.
1 /*
2  * Base64 encode data
3  *
4  * without parameters reads data from standard input and writes encoded data to standard output
5  * with 1 parameter reads data from the file given and writes encoded data to standard output
6  * with 2 parameters reads data from the file in the 1st argument and writes encoded data to the file in the 2nd argument
7  *
8  */
9 
10 #include <xstream/base64.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  if(1<argc){
29  readfrom = new ifstream(argv[1]);
30  if(2<argc){
31  writeto = new ofstream(argv[2]);
32  }else{
33  writeto = &cout;
34  }
35  }else{
36  readfrom = &cin;
37  writeto = &cout;
38  }
39 
40  base64::ostreambuf b64sb(writeto->rdbuf());
41  ostream b64(&b64sb);
42 
43  //raise exceptions
44  b64.exceptions(ios::badbit);
45 
46 
47  while(readfrom->good()){
48  readfrom->read(buf,len);
49  b64.write(buf,readfrom->gcount());
50  }
51 
52  b64.flush();
53 
54  }
55  catch(exception& e){
56  cerr<<"Error: "<<e.what()<<endl;
57  }
58 
59 
60  //pointers not being freed
61 
62  return 0;
63 }
TEllipse * e
C++ streambuf interface to encode/decode base64 data.
Base64 encode stream class.
Definition: base64.h:31
int main(int argc, char *argv[])
Definition: gendoc.cc:6