]> git.lyx.org Git - lyx.git/blob - src/support/docstream.h
Fix bugs #6078 and #9364
[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 #if defined(_MSC_VER) && (_MSC_VER >= 1600) 
18 // Ugly workaround for MSVC10 STL bug:
19 // std::numpunct has a hardcoded dllimport in definition, but we wanna it with 32 bit 
20 // so we can't import it and must define it but then the compiler complains.
21 #include "support/numpunct_lyx_char_type.h"
22 #endif
23
24 #include <fstream>
25 #include <sstream>
26
27 namespace lyx {
28
29 class iconv_codecvt_facet_exception : public std::exception {
30 public:
31         virtual ~iconv_codecvt_facet_exception() throw() {}
32         virtual const char * what() const throw();
33 };
34
35 /// Base class for UCS4 input streams
36 typedef std::basic_istream<char_type> idocstream;
37
38 /** Base class for UCS4 output streams.
39     If you want to output a single UCS4 character, use \code
40     os.put(c);
41     \endcode, not \code
42     os << c;
43     \endcode . The latter will not output the character, but the code point
44     as number if USE_WCHAR_T is not defined. This is because we can't overload
45     operator<< (our character type is not always a real type but sometimes a
46     typedef). Narrow characters of type char can be output as usual.
47  */
48 typedef std::basic_ostream<char_type> odocstream;
49
50 /// File stream for reading UTF8-encoded files with automatic conversion to
51 /// UCS4.
52 class ifdocstream : public std::basic_ifstream<char_type> {
53         typedef std::basic_ifstream<char_type> base;
54 public:
55         ifdocstream();
56         explicit ifdocstream(const char* s,
57                 std::ios_base::openmode mode = std::ios_base::in,
58                 std::string const & encoding = "UTF-8");
59         ~ifdocstream() {}
60 };
61
62 /// File stream for writing files in 8bit encoding \p encoding with automatic
63 /// conversion from UCS4.
64
65 class ofdocstream : public std::basic_ofstream<char_type> {
66         typedef std::basic_ofstream<char_type> base;
67 public:
68         ofdocstream();
69         explicit ofdocstream(const char* s,
70                 std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc,
71                 std::string const & encoding = "UTF-8");
72         ~ofdocstream() {}
73         ///
74         void reset(std::string const & encoding);
75 };
76
77
78
79 /// UCS4 input stringstream
80 typedef std::basic_istringstream<char_type> idocstringstream;
81
82 /// UCS4 output manipulator
83 typedef odocstream & (*odocstream_manip)(odocstream &);
84
85
86 /// Helper struct for changing stream encoding
87 struct SetEnc {
88         SetEnc(std::string const & e) : encoding(e) {}
89         std::string encoding;
90 };
91
92 /// Helper function for changing stream encoding
93 SetEnc setEncoding(std::string const & encoding);
94
95 /** Change the encoding of \p os to \p e.encoding.
96     \p e.encoding must be a valid iconv name of an 8bit encoding.
97     This does nothing if the stream is not a file stream, since only
98     file streams do have an associated 8bit encoding.
99     Usage: \code
100     os << setEncoding("ISO-8859-1");
101     \endcode
102  */
103 odocstream & operator<<(odocstream & os, SetEnc e);
104 idocstream & operator<<(idocstream & os, SetEnc e);
105
106 }
107
108 #endif