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

/home/tommi/cxxtools/include/cxxtools/iconvstream.h

00001 /*
00002  * Copyright (C) 2003 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_ICONVSTREAM_H
00030 #define CXXTOOLS_ICONVSTREAM_H
00031 
00032 #include <iostream>
00033 #include <iconv.h>
00034 
00035 namespace cxxtools
00036 {
00037 
00042 class iconvstreambuf : public std::streambuf
00043 {
00044     std::ostream* sink;
00045     iconv_t cd;
00046     char buffer[256];
00047 
00048   public:
00049     iconvstreambuf()
00050       : sink(0),
00051         cd((iconv_t)-1)
00052       { }
00053     ~iconvstreambuf()
00054       { close(); }
00055 
00056     iconvstreambuf* open(std::ostream& sink_,
00057       const char* tocode, const char* fromcode);
00058     iconvstreambuf* close() throw();
00059 
00061     int_type overflow(int_type c);
00063     int_type underflow();
00065     int sync();
00066 
00067 };
00068 
00088 class iconvstream : public std::ostream
00089 {
00090     iconvstreambuf streambuf;
00091 
00092   public:
00093     iconvstream(std::ostream& sink, const char* tocode, const char* fromcode)
00094       : std::ostream(0)
00095     {
00096       init(&streambuf);
00097       open(sink, tocode, fromcode);
00098     }
00099     iconvstream()
00100       : std::ostream(0)
00101     {
00102       init(&streambuf);
00103     }
00104 
00105     void open(std::ostream& sink_,
00106       const char* tocode, const char* fromcode);
00107     void close() throw()
00108       { streambuf.close(); }
00109 };
00110 
00111 }
00112 
00113 #endif // CXXTOOLS_ICONVSTREAM_H
00114 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6