]> git.lyx.org Git - lyx.git/blob - src/support/docstream.h
Following rev 21967: final touch to odocfstream:
[lyx.git] / src / support / docstream.h
1 // -*- C++ -*-
2 /**
3  * \file docstream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Georg Baum
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_DOCSTREAM_H
13 #define LYX_DOCSTREAM_H
14
15 #include "support/docstring.h"
16
17 #include <fstream>
18 #include <sstream>
19
20 namespace lyx {
21
22 class iconv_codecvt_facet_exception : public std::exception {
23 public:
24         virtual ~iconv_codecvt_facet_exception() throw() {}
25         virtual const char * what() const throw();
26 };
27
28 /// Base class for UCS4 input streams
29 typedef std::basic_istream<char_type> idocstream;
30
31 /** Base class for UCS4 output streams.
32     If you want to output a single UCS4 character, use \code
33     os.put(c);
34     \endcode, not \code
35     os << c;
36     \endcode . The latter will not output the character, but the code point
37     as number. This is because we can't overload operator<< (our character
38     type is not a real type but a typedef). Narrow characters of type char
39     can be output as usual.
40  */
41 typedef std::basic_ostream<char_type> odocstream;
42
43 /// File stream for reading UTF8-encoded files with automatic conversion to
44 /// UCS4.
45 class idocfstream : public std::basic_ifstream<char_type> {
46         typedef std::basic_ifstream<char_type> base;
47 public:
48         idocfstream(std::string const & encoding = "UTF-8");
49         explicit idocfstream(const char* s,
50                 std::ios_base::openmode mode = std::ios_base::in,
51                 std::string const & encoding = "UTF-8");
52         ~idocfstream() {}
53 };
54
55 /// File stream for writing files in 8bit encoding \p encoding with automatic
56 /// conversion from UCS4.
57 class odocfstream : public std::basic_ofstream<char_type> {
58         typedef std::basic_ofstream<char_type> base;
59 public:
60         odocfstream();
61         explicit odocfstream(const char* s,
62                 std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc,
63                 std::string const & encoding = "UTF-8");
64         ~odocfstream() {}
65         ///
66         void reset(std::string const & encoding);
67 };
68
69 /// UCS4 input stringstream
70 typedef std::basic_istringstream<char_type> idocstringstream;
71
72 /// UCS4 output stringstream
73 typedef std::basic_ostringstream<char_type> odocstringstream;
74
75 /// Helper struct for changing stream encoding
76 struct SetEnc {
77         SetEnc(std::string const & e) : encoding(e) {}
78         std::string encoding;
79 };
80
81 /// Helper function for changing stream encoding
82 SetEnc setEncoding(std::string const & encoding);
83
84 /** Change the encoding of \p os to \p e.encoding.
85     \p e.encoding must be a valid iconv name of an 8bit encoding.
86     This does nothing if the stream is not a file stream, since only
87     file streams do have an associated 8bit encoding.
88     Usage: \code
89     os << setEncoding("ISO-8859-1");
90     \endcode
91  */
92 odocstream & operator<<(odocstream & os, SetEnc e);
93
94 }
95
96 #endif