]> git.lyx.org Git - lyx.git/blob - src/support/docstream.h
40f8e004dbceaf624469b2a81cf25912c55c4fd4
[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. This is because we can't overload operator<< (our character
45     type is not a real type but a typedef). Narrow characters of type char
46     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 stringstream
83 typedef std::basic_ostringstream<char_type> odocstringstream;
84
85 /** Wrapper class for odocstream.
86     This class helps ensuring that no blank lines may be inadvertently output.
87     Use the special variables "breakln" and "safebreakln" as if they were
88     iomanip's to ensure that the next output will start at the beginning of
89     a line. Using "breakln", a '\n' char will be output if needed, while
90     using "safebreakln", "%\n" will be output if needed.
91     Use countLines() to retrieve the number of \n output since previous call.
92   */
93
94 class otexstream {
95 public:
96         ///
97         explicit otexstream(odocstream & os)
98                 : os_(os), lines_(0), canbreakline_(false), protectspace_(false)
99         {}
100         ///
101         odocstream & os() { return os_; }
102         ///
103         void put(char_type const & c);
104         ///
105         size_t countLines() { size_t l = lines_; lines_ = 0; return l; }
106         ///
107         void addLines(size_t n) { lines_ += n; }
108         ///
109         void canBreakLine(bool breakline) { canbreakline_ = breakline; }
110         ///
111         bool canBreakLine() const { return canbreakline_; }
112         ///
113         void protectSpace(bool protectspace) { protectspace_ = protectspace; }
114         ///
115         bool protectSpace() const { return protectspace_; }
116 private:
117         ///
118         odocstream & os_;
119         ///
120         size_t lines_;
121         ///
122         bool canbreakline_;
123         ///
124         bool protectspace_;
125 };
126
127 /// Helper structs for breaking a line
128 struct BreakLine {
129         char n;
130 };
131
132 struct SafeBreakLine {
133         char n;
134 };
135
136 extern BreakLine breakln;
137 extern SafeBreakLine safebreakln;
138
139 ///
140 otexstream & operator<<(otexstream &, BreakLine);
141 ///
142 otexstream & operator<<(otexstream &, SafeBreakLine);
143 ///
144 otexstream & operator<<(otexstream &, docstring const &);
145 ///
146 otexstream & operator<<(otexstream &, char const *);
147 ///
148 otexstream & operator<<(otexstream &, char);
149 ///
150 template <typename Type>
151 otexstream & operator<<(otexstream & ots, Type value);
152
153 /// Helper struct for changing stream encoding
154 struct SetEnc {
155         SetEnc(std::string const & e) : encoding(e) {}
156         std::string encoding;
157 };
158
159 /// Helper function for changing stream encoding
160 SetEnc setEncoding(std::string const & encoding);
161
162 /** Change the encoding of \p os to \p e.encoding.
163     \p e.encoding must be a valid iconv name of an 8bit encoding.
164     This does nothing if the stream is not a file stream, since only
165     file streams do have an associated 8bit encoding.
166     Usage: \code
167     os << setEncoding("ISO-8859-1");
168     \endcode
169  */
170 odocstream & operator<<(odocstream & os, SetEnc e);
171 idocstream & operator<<(idocstream & os, SetEnc e);
172
173 }
174
175 #endif