1 | // |
2 | // LineEndingConverter.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: LineEndingConverter |
7 | // |
8 | // Definition of the LineEndingConverter class. |
9 | // |
10 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_LineEndingConverter_INCLUDED |
18 | #define Foundation_LineEndingConverter_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/UnbufferedStreamBuf.h" |
23 | #include <istream> |
24 | #include <ostream> |
25 | |
26 | |
27 | namespace Poco { |
28 | |
29 | |
30 | class Foundation_API LineEnding |
31 | /// This class defines valid line ending sequences |
32 | /// for InputLineEndingConverter and OutputLineEndingConverter. |
33 | { |
34 | public: |
35 | static const std::string NEWLINE_DEFAULT; |
36 | static const std::string NEWLINE_CR; |
37 | static const std::string NEWLINE_CRLF; |
38 | static const std::string NEWLINE_LF; |
39 | }; |
40 | |
41 | |
42 | class Foundation_API LineEndingConverterStreamBuf: public UnbufferedStreamBuf |
43 | /// This stream buffer performs line ending conversion |
44 | /// on text streams. The converter can convert from and to |
45 | /// the Unix (LF), Mac (CR) and DOS/Windows/Network (CF-LF) endings. |
46 | /// |
47 | /// Any newline sequence in the source will be replaced by the |
48 | /// target newline sequence. |
49 | { |
50 | public: |
51 | LineEndingConverterStreamBuf(std::istream& istr); |
52 | /// Creates the LineEndingConverterStreamBuf and connects it |
53 | /// to the given input stream. |
54 | |
55 | LineEndingConverterStreamBuf(std::ostream& ostr); |
56 | /// Creates the LineEndingConverterStreamBuf and connects it |
57 | /// to the given output stream. |
58 | |
59 | ~LineEndingConverterStreamBuf(); |
60 | /// Destroys the LineEndingConverterStream. |
61 | |
62 | void setNewLine(const std::string& newLineCharacters); |
63 | /// Sets the target line ending for the converter. |
64 | /// |
65 | /// Possible values are: |
66 | /// * NEWLINE_DEFAULT (whatever is appropriate for the current platform) |
67 | /// * NEWLINE_CRLF (Windows), |
68 | /// * NEWLINE_LF (Unix), |
69 | /// * NEWLINE_CR (Macintosh) |
70 | /// |
71 | /// In theory, any character sequence can be used as newline sequence. |
72 | /// In practice, however, only the above three make sense. |
73 | |
74 | const std::string& getNewLine() const; |
75 | /// Returns the line ending currently in use. |
76 | |
77 | protected: |
78 | int readFromDevice(); |
79 | int writeToDevice(char c); |
80 | |
81 | private: |
82 | std::istream* _pIstr; |
83 | std::ostream* _pOstr; |
84 | std::string _newLine; |
85 | std::string::const_iterator _it; |
86 | char _lastChar; |
87 | }; |
88 | |
89 | |
90 | class Foundation_API LineEndingConverterIOS: public virtual std::ios |
91 | /// The base class for InputLineEndingConverter and OutputLineEndingConverter. |
92 | /// |
93 | /// This class provides common methods and is also needed to ensure |
94 | /// the correct initialization order of the stream buffer and base classes. |
95 | { |
96 | public: |
97 | LineEndingConverterIOS(std::istream& istr); |
98 | /// Creates the LineEndingConverterIOS and connects it |
99 | /// to the given input stream. |
100 | |
101 | LineEndingConverterIOS(std::ostream& ostr); |
102 | /// Creates the LineEndingConverterIOS and connects it |
103 | /// to the given output stream. |
104 | |
105 | ~LineEndingConverterIOS(); |
106 | /// Destroys the stream. |
107 | |
108 | void setNewLine(const std::string& newLineCharacters); |
109 | /// Sets the target line ending for the converter. |
110 | /// |
111 | /// Possible values are: |
112 | /// * NEWLINE_DEFAULT (whatever is appropriate for the current platform) |
113 | /// * NEWLINE_CRLF (Windows), |
114 | /// * NEWLINE_LF (Unix), |
115 | /// * NEWLINE_CR (Macintosh) |
116 | /// |
117 | /// In theory, any character sequence can be used as newline sequence. |
118 | /// In practice, however, only the above three make sense. |
119 | /// |
120 | /// If an empty string is given, all newline characters are removed from |
121 | /// the stream. |
122 | |
123 | const std::string& getNewLine() const; |
124 | /// Returns the line ending currently in use. |
125 | |
126 | LineEndingConverterStreamBuf* rdbuf(); |
127 | /// Returns a pointer to the underlying streambuf. |
128 | |
129 | protected: |
130 | LineEndingConverterStreamBuf _buf; |
131 | }; |
132 | |
133 | |
134 | class Foundation_API InputLineEndingConverter: public LineEndingConverterIOS, public std::istream |
135 | /// InputLineEndingConverter performs line ending conversion |
136 | /// on text input streams. The converter can convert from and to |
137 | /// the Unix (LF), Mac (CR) and DOS/Windows/Network (CF-LF) endings. |
138 | /// |
139 | /// Any newline sequence in the source will be replaced by the |
140 | /// target newline sequence. |
141 | { |
142 | public: |
143 | InputLineEndingConverter(std::istream& istr); |
144 | /// Creates the LineEndingConverterInputStream and connects it |
145 | /// to the given input stream. |
146 | |
147 | InputLineEndingConverter(std::istream& istr, const std::string& newLineCharacters); |
148 | /// Creates the LineEndingConverterInputStream and connects it |
149 | /// to the given input stream. |
150 | |
151 | ~InputLineEndingConverter(); |
152 | /// Destroys the stream. |
153 | }; |
154 | |
155 | |
156 | class Foundation_API OutputLineEndingConverter: public LineEndingConverterIOS, public std::ostream |
157 | /// OutputLineEndingConverter performs line ending conversion |
158 | /// on text output streams. The converter can convert from and to |
159 | /// the Unix (LF), Mac (CR) and DOS/Windows/Network (CF-LF) endings. |
160 | /// |
161 | /// Any newline sequence in the source will be replaced by the |
162 | /// target newline sequence. |
163 | { |
164 | public: |
165 | OutputLineEndingConverter(std::ostream& ostr); |
166 | /// Creates the LineEndingConverterOutputStream and connects it |
167 | /// to the given input stream. |
168 | |
169 | OutputLineEndingConverter(std::ostream& ostr, const std::string& newLineCharacters); |
170 | /// Creates the LineEndingConverterOutputStream and connects it |
171 | /// to the given input stream. |
172 | |
173 | ~OutputLineEndingConverter(); |
174 | /// Destroys the LineEndingConverterOutputStream. |
175 | }; |
176 | |
177 | |
178 | } // namespace Poco |
179 | |
180 | |
181 | #endif // Foundation_LineEndingConverter_INCLUDED |
182 | |