Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/base64stream.h

00001 /*
00002  * Copyright (C) 2004 Tommi Maekitalo
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 #ifndef CXXTOOLS_BASE64STREAM_H
00030 #define CXXTOOLS_BASE64STREAM_H
00031 
00032 #include <iostream>
00033 
00034 namespace cxxtools
00035 {
00036 
00037 class Base64stream_streambuf : public std::streambuf
00038 {
00039     std::streambuf* sinksource;
00040     char obuffer[3];
00041     char decodebuf[3];
00042     unsigned count;
00043     bool indecode;
00044     bool eofflag;
00045 
00046   public:
00047     Base64stream_streambuf(std::streambuf* sinksource_)
00048       : sinksource(sinksource_),
00049         count(0),
00050         indecode(false),
00051         eofflag(false)
00052       { }
00053     ~Base64stream_streambuf()
00054       { if (indecode) end(); }
00055 
00056     void end();
00057     void reset()  { eofflag = false; }
00058 
00059   protected:
00060     std::streambuf::int_type overflow(std::streambuf::int_type ch);
00061     std::streambuf::int_type underflow();
00062     int sync();
00063 
00064   private:
00065     void putChar(char ch);
00066     int getval();
00067 };
00068 
00079 class Base64ostream : public std::ostream
00080 {
00081     Base64stream_streambuf streambuf;
00082 
00083   public:
00086     Base64ostream(std::ostream& out)
00087       : std::ostream(0),
00088         streambuf(out.rdbuf())
00089       {
00090         init(&streambuf);
00091       }
00093     Base64ostream(std::streambuf* sb)
00094       : std::ostream(0),
00095         streambuf(sb)
00096       {
00097         init(&streambuf);
00098       }
00099 
00101     void end()  { streambuf.end(); }
00102 };
00103 
00112 class Base64istream : public std::istream
00113 {
00114     Base64stream_streambuf streambuf;
00115 
00116   public:
00117     Base64istream(std::istream& in)
00118       : std::istream(0),
00119         streambuf(in.rdbuf())
00120       { init(&streambuf); }
00121     Base64istream(std::streambuf* sb)
00122       : std::istream(0),
00123         streambuf(sb)
00124       { init(&streambuf); }
00125 
00128     void reset()  { streambuf.reset(); }
00129 };
00130 
00131 }
00132 
00133 #endif // CXXTOOLS_BASE64STREAM_H
00134 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6