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 75 76 77 78 79 80 81 82 83 84 85 86
|
/***************************************************************************
copyright : (C) 2002-2005 by Stefano Barbato
email : stefano@codesink.org
$Id: qp.cxx,v 1.2 2005/02/23 10:26:14 tat Exp $
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/** \example qp.cc
* Usage: qp [-ed] [in_file [out_file]]
*
* Quoted-Printable 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 << "qp [-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)
{
QP::Encoder qp;
encode(ibeg, iend, qp, out);
} else {
QP::Decoder qp;
decode(ibeg, iend, qp, out);
}
}
|