forked from tat/mimetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb64.cxx
75 lines (67 loc) · 1.76 KB
/
b64.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/***************************************************************************
copyright : (C) 2002-2008 by Stefano Barbato
email : [email protected]
$Id: b64.cxx,v 1.3 2008-10-07 11:06:25 tat Exp $
***************************************************************************/
/** \example b64.cc
* Usage: b64 [-ed] [in_file [out_file]]
*
* Base64 encodes or decodes in_file (or standard input) and writes
* output to out_file (or standard output)
*/
#include <iostream>
#include <iterator>
#include <fstream>
#include <mimetic/mimetic.h>
using namespace std;
using namespace mimetic;
void usage()
{
cout << "b64 [-ed] [in_file [out_file]]" << endl;
exit(1);
}
void die_if(bool b, const string& s)
{
if(b)
{
cerr << s << endl;
exit(-1);
}
}
int main(int argc, char** argv)
{
std::ios_base::sync_with_stdio(false);
bool encoding = 0;
if(argc < 2)
usage();
string opt = argv[1];
if(opt == "-e")
encoding = true;
else if(opt == "-d")
encoding = false;
else
usage();
filebuf fin;
filebuf fout;
if(argc > 2)
{
fin.open(argv[2], ios::in);
die_if(!fin.is_open(), string("unable to open file ")+argv[2]);
}
if(argc > 3)
{
fout.open(argv[3], ios::out);
die_if(!fout.is_open(), string("unable to open file ")+argv[3]);
}
istream is( fin.is_open() ? &fin : cin.rdbuf());
ostream os( fout.is_open() ? &fout : cout.rdbuf());
istreambuf_iterator<char> ibeg(is), iend;
ostreambuf_iterator<char> out(os);
if(encoding) {
Base64::Encoder b64;
code(ibeg, iend, b64, out);
} else {
Base64::Decoder b64;
code(ibeg, iend, b64, out);
}
}