]> git.lyx.org Git - lyx.git/blob - src/support/docstream.h
Update my email and status.
[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 "TexRow.h"
16 #include "support/docstring.h"
17
18 #if defined(_MSC_VER) && (_MSC_VER >= 1600) 
19 // Ugly workaround for MSVC10 STL bug:
20 // std::numpunct has a hardcoded dllimport in definition, but we wanna it with 32 bit 
21 // so we can't import it and must define it but then the compiler complains.
22 #include "support/numpunct_lyx_char_type.h"
23 #endif
24
25 #include <fstream>
26 #include <sstream>
27
28 namespace lyx {
29
30 class iconv_codecvt_facet_exception : public std::exception {
31 public:
32         virtual ~iconv_codecvt_facet_exception() throw() {}
33         virtual const char * what() const throw();
34 };
35
36 /// Base class for UCS4 input streams
37 typedef std::basic_istream<char_type> idocstream;
38
39 /** Base class for UCS4 output streams.
40     If you want to output a single UCS4 character, use \code
41     os.put(c);
42     \endcode, not \code
43     os << c;
44     \endcode . The latter will not output the character, but the code point
45     as number if USE_WCHAR_T is not defined. This is because we can't overload
46     operator<< (our character type is not always a real type but sometimes a
47     typedef). Narrow characters of type char can be output as usual.
48  */
49 typedef std::basic_ostream<char_type> odocstream;
50
51 /// File stream for reading UTF8-encoded files with automatic conversion to
52 /// UCS4.
53 class ifdocstream : public std::basic_ifstream<char_type> {
54         typedef std::basic_ifstream<char_type> base;
55 public:
56         ifdocstream();
57         explicit ifdocstream(const char* s,
58                 std::ios_base::openmode mode = std::ios_base::in,
59                 std::string const & encoding = "UTF-8");
60         ~ifdocstream() {}
61 };
62
63 /// File stream for writing files in 8bit encoding \p encoding with automatic
64 /// conversion from UCS4.
65
66 class ofdocstream : public std::basic_ofstream<char_type> {
67         typedef std::basic_ofstream<char_type> base;
68 public:
69         ofdocstream();
70         explicit ofdocstream(const char* s,
71                 std::ios_base::openmode mode = std::ios_base::out|std::ios_base::trunc,
72                 std::string const & encoding = "UTF-8");
73         ~ofdocstream() {}
74         ///
75         void reset(std::string const & encoding);
76 };
77
78
79
80 /// UCS4 input stringstream
81 typedef std::basic_istringstream<char_type> idocstringstream;
82
83 /// UCS4 output stringstream
84 typedef std::basic_ostringstream<char_type> odocstringstream;
85
86 /// UCS4 output manipulator
87 typedef odocstream & (*odocstream_manip)(odocstream &);
88
89 /** Wrapper class for odocstream.
90     This class is used to automatically count the lines of the exported latex
91     code and also to ensure that no blank lines may be inadvertently output.
92     To this end, use the special variables "breakln" and "safebreakln" as if
93     they were iomanip's to ensure that the next output will start at the
94     beginning of a line. Using "breakln", a '\n' char will be output if needed,
95     while using "safebreakln", "%\n" will be output if needed.
96     The class also records the last output character.
97   */
98
99 class otexstream {
100 public:
101         ///
102         otexstream(odocstream & os, TexRow & texrow)
103                 : os_(os), texrow_(texrow),
104                   canbreakline_(false), protectspace_(false), lastchar_(0) {}
105         ///
106         odocstream & os() { return os_; }
107         ///
108         TexRow & texrow() { return texrow_; }
109         ///
110         void put(char_type const & c);
111         ///
112         void canBreakLine(bool breakline) { canbreakline_ = breakline; }
113         ///
114         bool canBreakLine() const { return canbreakline_; }
115         ///
116         void protectSpace(bool protectspace) { protectspace_ = protectspace; }
117         ///
118         bool protectSpace() const { return protectspace_; }
119         ///
120         void lastChar(char_type const & c) { lastchar_ = c; }
121         ///
122         char_type lastChar() const { return lastchar_; }
123 private:
124         ///
125         odocstream & os_;
126         ///
127         TexRow & texrow_;
128         ///
129         bool canbreakline_;
130         ///
131         bool protectspace_;
132         ///
133         char_type lastchar_;
134 };
135
136 /// Helper structs for breaking a line
137 struct BreakLine {
138         char n;
139 };
140
141 struct SafeBreakLine {
142         char n;
143 };
144
145 extern BreakLine breakln;
146 extern SafeBreakLine safebreakln;
147
148 ///
149 otexstream & operator<<(otexstream &, BreakLine);
150 ///
151 otexstream & operator<<(otexstream &, SafeBreakLine);
152 ///
153 otexstream & operator<<(otexstream &, odocstream_manip);
154 ///
155 otexstream & operator<<(otexstream &, docstring const &);
156 ///
157 otexstream & operator<<(otexstream &, std::string const &);
158 ///
159 otexstream & operator<<(otexstream &, char const *);
160 ///
161 otexstream & operator<<(otexstream &, char);
162 ///
163 template <typename Type>
164 otexstream & operator<<(otexstream & ots, Type value);
165
166 /// Helper struct for changing stream encoding
167 struct SetEnc {
168         SetEnc(std::string const & e) : encoding(e) {}
169         std::string encoding;
170 };
171
172 /// Helper function for changing stream encoding
173 SetEnc setEncoding(std::string const & encoding);
174
175 /** Change the encoding of \p os to \p e.encoding.
176     \p e.encoding must be a valid iconv name of an 8bit encoding.
177     This does nothing if the stream is not a file stream, since only
178     file streams do have an associated 8bit encoding.
179     Usage: \code
180     os << setEncoding("ISO-8859-1");
181     \endcode
182  */
183 odocstream & operator<<(odocstream & os, SetEnc e);
184 idocstream & operator<<(idocstream & os, SetEnc e);
185
186 }
187
188 #endif