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