]> git.lyx.org Git - lyx.git/blob - src/support/docstream.h
TR1: check in cmake for GCC version, fallback in checktr1.h for other build systems...
[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 /// Helper struct for changing stream encoding
86 struct SetEnc {
87         SetEnc(std::string const & e) : encoding(e) {}
88         std::string encoding;
89 };
90
91 /// Helper function for changing stream encoding
92 SetEnc setEncoding(std::string const & encoding);
93
94 /** Change the encoding of \p os to \p e.encoding.
95     \p e.encoding must be a valid iconv name of an 8bit encoding.
96     This does nothing if the stream is not a file stream, since only
97     file streams do have an associated 8bit encoding.
98     Usage: \code
99     os << setEncoding("ISO-8859-1");
100     \endcode
101  */
102 odocstream & operator<<(odocstream & os, SetEnc e);
103 idocstream & operator<<(idocstream & os, SetEnc e);
104
105 }
106
107 #endif