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
16 #include "support/docstring.h"
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"
30 class iconv_codecvt_facet_exception : public std::exception {
32 virtual ~iconv_codecvt_facet_exception() throw() {}
33 virtual const char * what() const throw();
36 /// Base class for UCS4 input streams
37 typedef std::basic_istream<char_type> idocstream;
39 /** Base class for UCS4 output streams.
40 If you want to output a single UCS4 character, use \code
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.
49 typedef std::basic_ostream<char_type> odocstream;
51 /// File stream for reading UTF8-encoded files with automatic conversion to
53 class ifdocstream : public std::basic_ifstream<char_type> {
54 typedef std::basic_ifstream<char_type> base;
57 explicit ifdocstream(const char* s,
58 std::ios_base::openmode mode = std::ios_base::in,
59 std::string const & encoding = "UTF-8");
63 /// File stream for writing files in 8bit encoding \p encoding with automatic
64 /// conversion from UCS4.
66 class ofdocstream : public std::basic_ofstream<char_type> {
67 typedef std::basic_ofstream<char_type> base;
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");
75 void reset(std::string const & encoding);
80 /// UCS4 input stringstream
81 typedef std::basic_istringstream<char_type> idocstringstream;
83 /// UCS4 output manipulator
84 typedef odocstream & (*odocstream_manip)(odocstream &);
86 /** Wrapper class for odocstream.
87 This class is used to automatically count the lines of the exported latex
88 code and also to ensure that no blank lines may be inadvertently output.
89 To this end, use the special variables "breakln" and "safebreakln" as if
90 they were iomanip's to ensure that the next output will start at the
91 beginning of a line. Using "breakln", a '\n' char will be output if needed,
92 while using "safebreakln", "%\n" will be output if needed.
93 The class also records the last output character and can tell whether
94 a paragraph break was just output.
100 otexstream(odocstream & os, TexRow & texrow)
101 : os_(os), texrow_(texrow), canbreakline_(false),
102 protectspace_(false), parbreak_(true), lastchar_(0) {}
104 odocstream & os() { return os_; }
106 TexRow & texrow() { return texrow_; }
108 void put(char_type const & c);
110 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
112 bool canBreakLine() const { return canbreakline_; }
114 void protectSpace(bool protectspace) { protectspace_ = protectspace; }
116 bool protectSpace() const { return protectspace_; }
118 void lastChar(char_type const & c)
120 parbreak_ = (!canbreakline_ && c == '\n');
121 canbreakline_ = (c != '\n');
125 char_type lastChar() const { return lastchar_; }
127 bool afterParbreak() const { return parbreak_; }
143 /// Helper structs for breaking a line
148 struct SafeBreakLine {
152 extern BreakLine breakln;
153 extern SafeBreakLine safebreakln;
156 otexstream & operator<<(otexstream &, BreakLine);
158 otexstream & operator<<(otexstream &, SafeBreakLine);
160 otexstream & operator<<(otexstream &, odocstream_manip);
162 otexstream & operator<<(otexstream &, docstring const &);
164 otexstream & operator<<(otexstream &, std::string const &);
166 otexstream & operator<<(otexstream &, char const *);
168 otexstream & operator<<(otexstream &, char);
170 template <typename Type>
171 otexstream & operator<<(otexstream & ots, Type value);
173 /// Helper struct for changing stream encoding
175 SetEnc(std::string const & e) : encoding(e) {}
176 std::string encoding;
179 /// Helper function for changing stream encoding
180 SetEnc setEncoding(std::string const & encoding);
182 /** Change the encoding of \p os to \p e.encoding.
183 \p e.encoding must be a valid iconv name of an 8bit encoding.
184 This does nothing if the stream is not a file stream, since only
185 file streams do have an associated 8bit encoding.
187 os << setEncoding("ISO-8859-1");
190 odocstream & operator<<(odocstream & os, SetEnc e);
191 idocstream & operator<<(idocstream & os, SetEnc e);