1 | // |
---|
2 | // LineEndingConverter.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/LineEndingConverter.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Streams |
---|
8 | // Module: LineEndingConverter |
---|
9 | // |
---|
10 | // Definition of the LineEndingConverter class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_LineEndingConverter_INCLUDED |
---|
40 | #define Foundation_LineEndingConverter_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/UnbufferedStreamBuf.h" |
---|
45 | #include <istream> |
---|
46 | #include <ostream> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | class Foundation_API LineEnding |
---|
53 | /// This class defines valid line ending sequences |
---|
54 | /// for InputLineEndingConverter and OutputLineEndingConverter. |
---|
55 | { |
---|
56 | public: |
---|
57 | static const std::string NEWLINE_DEFAULT; |
---|
58 | static const std::string NEWLINE_CR; |
---|
59 | static const std::string NEWLINE_CRLF; |
---|
60 | static const std::string NEWLINE_LF; |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | class Foundation_API LineEndingConverterStreamBuf: public UnbufferedStreamBuf |
---|
65 | /// This stream buffer performs line ending conversion |
---|
66 | /// on text streams. The converter can convert from and to |
---|
67 | /// the Unix (LF), Mac (CR) and DOS/Windows/Network (CF-LF) endings. |
---|
68 | /// |
---|
69 | /// Any newline sequence in the source will be replaced by the |
---|
70 | /// target newline sequence. |
---|
71 | { |
---|
72 | public: |
---|
73 | LineEndingConverterStreamBuf(std::istream& istr); |
---|
74 | /// Creates the LineEndingConverterStreamBuf and connects it |
---|
75 | /// to the given input stream. |
---|
76 | |
---|
77 | LineEndingConverterStreamBuf(std::ostream& ostr); |
---|
78 | /// Creates the LineEndingConverterStreamBuf and connects it |
---|
79 | /// to the given output stream. |
---|
80 | |
---|
81 | ~LineEndingConverterStreamBuf(); |
---|
82 | /// Destroys the LineEndingConverterStream. |
---|
83 | |
---|
84 | void setNewLine(const std::string& newLineCharacters); |
---|
85 | /// Sets the target line ending for the converter. |
---|
86 | /// |
---|
87 | /// Possible values are: |
---|
88 | /// * NEWLINE_DEFAULT (whatever is appropriate for the current platform) |
---|
89 | /// * NEWLINE_CRLF (Windows), |
---|
90 | /// * NEWLINE_LF (Unix), |
---|
91 | /// * NEWLINE_CR (Macintosh) |
---|
92 | /// |
---|
93 | /// In theory, any character sequence can be used as newline sequence. |
---|
94 | /// In practice, however, only the above three make sense. |
---|
95 | |
---|
96 | const std::string& getNewLine() const; |
---|
97 | /// Returns the line ending currently in use. |
---|
98 | |
---|
99 | protected: |
---|
100 | int readFromDevice(); |
---|
101 | int writeToDevice(char c); |
---|
102 | |
---|
103 | private: |
---|
104 | std::istream* _pIstr; |
---|
105 | std::ostream* _pOstr; |
---|
106 | std::string _newLine; |
---|
107 | std::string::const_iterator _it; |
---|
108 | char _lastChar; |
---|
109 | }; |
---|
110 | |
---|
111 | |
---|
112 | class Foundation_API LineEndingConverterIOS: public virtual std::ios |
---|
113 | /// The base class for InputLineEndingConverter and OutputLineEndingConverter. |
---|
114 | /// |
---|
115 | /// This class provides common methods and is also needed to ensure |
---|
116 | /// the correct initialization order of the stream buffer and base classes. |
---|
117 | { |
---|
118 | public: |
---|
119 | LineEndingConverterIOS(std::istream& istr); |
---|
120 | /// Creates the LineEndingConverterIOS and connects it |
---|
121 | /// to the given input stream. |
---|
122 | |
---|
123 | LineEndingConverterIOS(std::ostream& ostr); |
---|
124 | /// Creates the LineEndingConverterIOS and connects it |
---|
125 | /// to the given output stream. |
---|
126 | |
---|
127 | ~LineEndingConverterIOS(); |
---|
128 | /// Destroys the stream. |
---|
129 | |
---|
130 | void setNewLine(const std::string& newLineCharacters); |
---|
131 | /// Sets the target line ending for the converter. |
---|
132 | /// |
---|
133 | /// Possible values are: |
---|
134 | /// * NEWLINE_DEFAULT (whatever is appropriate for the current platform) |
---|
135 | /// * NEWLINE_CRLF (Windows), |
---|
136 | /// * NEWLINE_LF (Unix), |
---|
137 | /// * NEWLINE_CR (Macintosh) |
---|
138 | /// |
---|
139 | /// In theory, any character sequence can be used as newline sequence. |
---|
140 | /// In practice, however, only the above three make sense. |
---|
141 | /// |
---|
142 | /// If an empty string is given, all newline characters are removed from |
---|
143 | /// the stream. |
---|
144 | |
---|
145 | const std::string& getNewLine() const; |
---|
146 | /// Returns the line ending currently in use. |
---|
147 | |
---|
148 | LineEndingConverterStreamBuf* rdbuf(); |
---|
149 | /// Returns a pointer to the underlying streambuf. |
---|
150 | |
---|
151 | protected: |
---|
152 | LineEndingConverterStreamBuf _buf; |
---|
153 | }; |
---|
154 | |
---|
155 | |
---|
156 | class Foundation_API InputLineEndingConverter: public LineEndingConverterIOS, public std::istream |
---|
157 | /// InputLineEndingConverter performs line ending conversion |
---|
158 | /// on text input 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 | InputLineEndingConverter(std::istream& istr); |
---|
166 | /// Creates the LineEndingConverterInputStream and connects it |
---|
167 | /// to the given input stream. |
---|
168 | |
---|
169 | InputLineEndingConverter(std::istream& istr, const std::string& newLineCharacters); |
---|
170 | /// Creates the LineEndingConverterInputStream and connects it |
---|
171 | /// to the given input stream. |
---|
172 | |
---|
173 | ~InputLineEndingConverter(); |
---|
174 | /// Destroys the stream. |
---|
175 | }; |
---|
176 | |
---|
177 | |
---|
178 | class Foundation_API OutputLineEndingConverter: public LineEndingConverterIOS, public std::ostream |
---|
179 | /// OutputLineEndingConverter performs line ending conversion |
---|
180 | /// on text output streams. The converter can convert from and to |
---|
181 | /// the Unix (LF), Mac (CR) and DOS/Windows/Network (CF-LF) endings. |
---|
182 | /// |
---|
183 | /// Any newline sequence in the source will be replaced by the |
---|
184 | /// target newline sequence. |
---|
185 | { |
---|
186 | public: |
---|
187 | OutputLineEndingConverter(std::ostream& ostr); |
---|
188 | /// Creates the LineEndingConverterOutputStream and connects it |
---|
189 | /// to the given input stream. |
---|
190 | |
---|
191 | OutputLineEndingConverter(std::ostream& ostr, const std::string& newLineCharacters); |
---|
192 | /// Creates the LineEndingConverterOutputStream and connects it |
---|
193 | /// to the given input stream. |
---|
194 | |
---|
195 | ~OutputLineEndingConverter(); |
---|
196 | /// Destroys the LineEndingConverterOutputStream. |
---|
197 | }; |
---|
198 | |
---|
199 | |
---|
200 | } // namespace Poco |
---|
201 | |
---|
202 | |
---|
203 | #endif // Foundation_LineEndingConverter_INCLUDED |
---|