/[debian]/mimetic/branches/upstream/current/examples/b64.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/examples/b64.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File size: 2417 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: b64.cxx,v 1.2 2005/02/23 10:26:14 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 /** \example b64.cc
17 * Usage: b64 [-ed] [in_file [out_file]]
18 *
19 * Base64 encodes or decodes in_file (or standard input) and writes
20 * output to out_file (or standard output)
21 */
22 #include <iostream>
23 #include <iterator>
24 #include <fstream>
25 #include <mimetic/mimetic.h>
26
27 using namespace std;
28 using namespace mimetic;
29
30 void usage()
31 {
32 cout << "b64 [-ed] [in_file [out_file]]" << endl;
33 exit(1);
34 }
35
36 void die_if(bool b, const string& s)
37 {
38 if(b)
39 {
40 cerr << s << endl;
41 exit(-1);
42 }
43 }
44
45 int main(int argc, char** argv)
46 {
47 std::ios_base::sync_with_stdio(false);
48 bool encoding = 0;
49 if(argc < 2)
50 usage();
51
52 string opt = argv[1];
53 if(opt == "-e")
54 encoding = true;
55 else if(opt == "-d")
56 encoding = false;
57 else
58 usage();
59
60 filebuf fin;
61 filebuf fout;
62 if(argc > 2)
63 {
64 fin.open(argv[2], ios::in);
65 die_if(!fin.is_open(), string("unable to open file ")+argv[2]);
66 }
67 if(argc > 3)
68 {
69 fout.open(argv[3], ios::out);
70 die_if(!fout.is_open(), string("unable to open file ")+argv[3]);
71 }
72 istream is( fin.is_open() ? &fin : cin.rdbuf());
73 ostream os( fout.is_open() ? &fout : cout.rdbuf());
74 istreambuf_iterator<char> ibeg(is), iend;
75 ostreambuf_iterator<char> out(os);
76 if(encoding) {
77 Base64::Encoder b64;
78 code(ibeg, iend, b64, out);
79 } else {
80 Base64::Decoder b64;
81 code(ibeg, iend, b64, out);
82 }
83 }
84

  ViewVC Help
Powered by ViewVC 1.1.26