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
15 #include "support/docstring.h"
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"
29 class iconv_codecvt_facet_exception : public std::exception {
31 virtual ~iconv_codecvt_facet_exception() throw() {}
32 virtual const char * what() const throw();
35 /// Base class for UCS4 input streams
36 typedef std::basic_istream<char_type> idocstream;
38 /** Base class for UCS4 output streams.
39 If you want to output a single UCS4 character, use \code
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.
48 typedef std::basic_ostream<char_type> odocstream;
50 /// File stream for reading UTF8-encoded files with automatic conversion to
52 class ifdocstream : public std::basic_ifstream<char_type> {
53 typedef std::basic_ifstream<char_type> base;
56 explicit ifdocstream(const char* s,
57 std::ios_base::openmode mode = std::ios_base::in,
58 std::string const & encoding = "UTF-8");
62 /// File stream for writing files in 8bit encoding \p encoding with automatic
63 /// conversion from UCS4.
65 class ofdocstream : public std::basic_ofstream<char_type> {
66 typedef std::basic_ofstream<char_type> base;
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");
74 void reset(std::string const & encoding);
79 /// UCS4 input stringstream
80 typedef std::basic_istringstream<char_type> idocstringstream;
82 /// UCS4 output stringstream
83 typedef std::basic_ostringstream<char_type> odocstringstream;
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.
97 explicit otexstream(odocstream & os)
98 : os_(os), lines_(0), canbreakline_(false), protectspace_(false)
101 odocstream & os() { return os_; }
103 void put(char_type const & c);
105 size_t countLines() { size_t l = lines_; lines_ = 0; return l; }
107 void addLines(size_t n) { lines_ += n; }
109 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
111 bool canBreakLine() const { return canbreakline_; }
113 void protectSpace(bool protectspace) { protectspace_ = protectspace; }
115 bool protectSpace() const { return protectspace_; }
127 /// Helper structs for breaking a line
132 struct SafeBreakLine {
136 extern BreakLine breakln;
137 extern SafeBreakLine safebreakln;
140 otexstream & operator<<(otexstream &, BreakLine);
142 otexstream & operator<<(otexstream &, SafeBreakLine);
144 otexstream & operator<<(otexstream &, docstring const &);
146 otexstream & operator<<(otexstream &, char const *);
148 otexstream & operator<<(otexstream &, char);
150 template <typename Type>
151 otexstream & operator<<(otexstream & ots, Type value);
153 /// Helper struct for changing stream encoding
155 SetEnc(std::string const & e) : encoding(e) {}
156 std::string encoding;
159 /// Helper function for changing stream encoding
160 SetEnc setEncoding(std::string const & encoding);
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.
167 os << setEncoding("ISO-8859-1");
170 odocstream & operator<<(odocstream & os, SetEnc e);
171 idocstream & operator<<(idocstream & os, SetEnc e);