iconvstream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_ICONVSTREAM_H
30 #define CXXTOOLS_ICONVSTREAM_H
31 
32 #include <iostream>
33 #include <stdexcept>
34 #include "iconvwrap.h"
35 
36 namespace cxxtools
37 {
38 
39 class iconv_error : public std::runtime_error
40 {
41  private:
42  mutable std::string msg;
43  size_t pos;
44 
45  public:
46  explicit iconv_error(size_t pos);
47  virtual size_t position() const throw();
48  virtual const char *what() const throw();
49  virtual ~iconv_error() throw();
50 };
51 
56 class iconvstreambuf : public std::streambuf
57 {
58  public:
60  typedef enum {
68  } mode_t;
69 
70  private:
71  std::ostream* sink;
72  iconvwrap conv;
73  char buffer[256];
74  mode_t mode;
75  size_t pos;
76 
77  public:
79  : sink(0), mode(mode_default), pos(0)
80  {
81  setg(buffer, buffer, buffer + (sizeof(buffer) - 1));
82  setp(buffer, buffer + (sizeof(buffer) - 1));
83  }
85  { close(); }
86 
87  iconvstreambuf* open(std::ostream& sink_,
88  const char* tocode, const char* fromcode);
89  iconvstreambuf* open(std::ostream& sink_,
90  const char* tocode, const char* fromcode, mode_t mode_);
91  iconvstreambuf* close() throw();
92 
94  int_type overflow(int_type c);
96  int_type underflow();
98  int sync();
102  virtual std::streampos seekoff(std::streamoff off,
103  std::ios_base::seekdir way, std::ios_base::openmode which);
107  virtual std::streampos seekpos(std::streampos sp,
108  std::ios_base::openmode which);
109 };
110 
130 class iconvstream : public std::ostream
131 {
132  iconvstreambuf streambuf;
133 
134  public:
135  iconvstream(std::ostream& sink, const char* tocode, const char* fromcode)
136  : std::ostream(0)
137  {
138  init(&streambuf);
139  open(sink, tocode, fromcode);
140  }
141 
142  iconvstream(std::ostream& sink, const char* tocode, const char* fromcode,
144  : std::ostream(0)
145  {
146  init(&streambuf);
147  open(sink, tocode, fromcode, mode);
148  }
150  : std::ostream(0)
151  {
152  init(&streambuf);
153  }
154 
155  void open(std::ostream& sink_,
156  const char* tocode, const char* fromcode);
157  void open(std::ostream& sink_,
158  const char* tocode, const char* fromcode,
159  iconvstreambuf::mode_t mode_);
160 
161  void close() throw()
162  { streambuf.close(); }
163 };
164 
165 }
166 
167 #endif // CXXTOOLS_ICONVSTREAM_H
168