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

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

00001 /* cxxtools/iconvstream.h
00002  *
00003  * cxxtools - general purpose C++-toolbox
00004  * Copyright (C) 2003 Tommi Maekitalo
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  *
00020  */
00021 
00022 #ifndef CXXTOOLS_ICONVSTREAM_H
00023 #define CXXTOOLS_ICONVSTREAM_H
00024 
00025 #include <iostream>
00026 #include <iconv.h>
00027 
00028 namespace cxxtools
00029 {
00030 
00035 class iconvstreambuf : public std::streambuf
00036 {
00037     std::ostream* sink;
00038     iconv_t cd;
00039     char buffer[256];
00040 
00041   public:
00042     iconvstreambuf()
00043       : sink(0),
00044         cd((iconv_t)-1)
00045       { }
00046     ~iconvstreambuf()
00047       { close(); }
00048 
00049     iconvstreambuf* open(std::ostream& sink_,
00050       const char* tocode, const char* fromcode);
00051     iconvstreambuf* close() throw();
00052 
00054     int_type overflow(int_type c);
00056     int_type underflow();
00058     int sync();
00059 
00060 };
00061 
00081 class iconvstream : public std::ostream
00082 {
00083     iconvstreambuf streambuf;
00084 
00085   public:
00086     iconvstream(std::ostream& sink, const char* tocode, const char* fromcode)
00087       : std::ostream(0)
00088     {
00089       init(&streambuf);
00090       open(sink, tocode, fromcode);
00091     }
00092     iconvstream()
00093       : std::ostream(0)
00094     {
00095       init(&streambuf);
00096     }
00097 
00098     void open(std::ostream& sink_,
00099       const char* tocode, const char* fromcode);
00100     void close() throw()
00101       { streambuf.close(); }
00102 };
00103 
00104 }
00105 
00106 #endif // CXXTOOLS_ICONVSTREAM_H
00107 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6