4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
12 #ifndef LYX_DOCSTREAM_H
13 #define LYX_DOCSTREAM_H
15 #include "support/docstring.h"
22 class iconv_codecvt_facet_exception : public std::exception {
24 virtual ~iconv_codecvt_facet_exception() throw() {}
25 virtual const char * what() const throw();
28 /// Base class for UCS4 input streams
29 typedef std::basic_istream<char_type> idocstream;
31 /** Base class for UCS4 output streams.
32 If you want to output a single UCS4 character, use \code
36 \endcode . The latter will not output the character, but the code point
37 as number if USE_WCHAR_T is not defined. This is because we can't overload
38 operator<< (our character type is not always a real type but sometimes a
39 typedef). Narrow characters of type char can be output as usual.
41 typedef std::basic_ostream<char_type> odocstream;
43 /// File stream for reading UTF8-encoded files with automatic conversion to
45 /// Buffering must be switched off if the encoding is changed after
46 /// construction by calling rdbuf()->pubsetbuf(0, 0).
47 class ifdocstream : public std::basic_ifstream<char_type> {
48 typedef std::basic_ifstream<char_type> base;
51 explicit ifdocstream(const char* s,
52 std::ios_base::openmode mode = std::ios_base::in,
53 std::string const & encoding = "UTF-8");
57 /// File stream for writing files in 8bit encoding \p encoding with automatic
58 /// conversion from UCS4.
60 class ofdocstream : public std::basic_ofstream<char_type> {
61 typedef std::basic_ofstream<char_type> base;
64 explicit ofdocstream(const char* s,
65 std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc,
66 std::string const & encoding = "UTF-8");
69 void reset(std::string const & encoding);
74 /// UCS4 input stringstream
75 typedef std::basic_istringstream<char_type> idocstringstream;
77 /// UCS4 output manipulator
78 typedef odocstream & (*odocstream_manip)(odocstream &);
81 /// Helper struct for changing stream encoding
84 * It is important that this constructor is explicit.
85 * Otherwise the attempt to output a std::string to an odocstream
86 * would compile, but cause a (probably failing) encoding change
87 * instead of string output (we do not define
88 * operator<<(odocstream &, std::string) since we want to avoid
89 * outputting strings with unspecified encoding)
91 explicit SetEnc(std::string const & e) : encoding(e) {}
95 /// Helper function for changing stream encoding
96 SetEnc setEncoding(std::string const & encoding);
98 /** Change the encoding of \p os to \p e.encoding.
99 \p e.encoding must be a valid iconv name of an 8bit encoding.
100 This does nothing if the stream is not a file stream, since only
101 file streams do have an associated 8bit encoding.
103 os << setEncoding("ISO-8859-1");
106 odocstream & operator<<(odocstream & os, SetEnc e);
107 idocstream & operator<<(idocstream & os, SetEnc e);