Debian Codegraph @

src/Base/Stream.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <QBuffer>
26 #include <QIODevice>
27 #ifdef __GNUC__
28 # include <cstdint>
29 #endif
30
31 #include "Stream.h"
32 #include "Swap.h"
33 #include <CXX/Objects.hxx>
34
35
36 using namespace Base;
37
38 Stream::Stream() = default;
39
40 Stream::~Stream() = default;
41
42 Stream::ByteOrder Stream::byteOrder() const
43 {
44 return _swap ? BigEndian : LittleEndian;
45 }
46
47 void Stream::setByteOrder(ByteOrder bo)
48 {
49 _swap = (bo == BigEndian);
50 }
51
52 OutputStream::OutputStream(std::ostream& rout)
53 : _out(rout)
54 {}
55
56 OutputStream::~OutputStream() = default;
57
58 OutputStream& OutputStream::operator<<(bool b)
59 {
60 _out.write((const char*)&b, sizeof(bool));
61 return *this;
62 }
63
64 OutputStream& OutputStream::operator<<(int8_t ch)
65 {
66 _out.write((const char*)&ch, sizeof(int8_t));
67 return *this;
68 }
69
70 OutputStream& OutputStream::operator<<(uint8_t uch)
71 {
72 _out.write((const char*)&uch, sizeof(uint8_t));
73 return *this;
74 }
75
76 OutputStream& OutputStream::operator<<(int16_t s)
77 {
78 if (isSwapped()) {
79 SwapEndian<int16_t>(s);
80 }
81 _out.write((const char*)&s, sizeof(int16_t));
82 return *this;
83 }
84
85 OutputStream& OutputStream::operator<<(uint16_t us)
86 {
87 if (isSwapped()) {
88 SwapEndian<uint16_t>(us);
89 }
90 _out.write((const char*)&us, sizeof(uint16_t));
91 return *this;
92 }
93
94 OutputStream& OutputStream::operator<<(int32_t i)
95 {
96 if (isSwapped()) {
97 SwapEndian<int32_t>(i);
98 }
99 _out.write((const char*)&i, sizeof(int32_t));
100 return *this;
101 }
102
103 OutputStream& OutputStream::operator<<(uint32_t ui)
104 {
105 if (isSwapped()) {
106 SwapEndian<uint32_t>(ui);
107 }
108 _out.write((const char*)&ui, sizeof(uint32_t));
109 return *this;
110 }
111
112 OutputStream& OutputStream::operator<<(int64_t l)
113 {
114 if (isSwapped()) {
115 SwapEndian<int64_t>(l);
116 }
117 _out.write((const char*)&l, sizeof(int64_t));
118 return *this;
119 }
120
121 OutputStream& OutputStream::operator<<(uint64_t ul)
122 {
123 if (isSwapped()) {
124 SwapEndian<uint64_t>(ul);
125 }
126 _out.write((const char*)&ul, sizeof(uint64_t));
127 return *this;
128 }
129
130 OutputStream& OutputStream::operator<<(float f)
131 {
132 if (isSwapped()) {
133 SwapEndian<float>(f);
134 }
135 _out.write((const char*)&f, sizeof(float));
136 return *this;
137 }
138
139 OutputStream& OutputStream::operator<<(double d)
140 {
141 if (isSwapped()) {
142 SwapEndian<double>(d);
143 }
144 _out.write((const char*)&d, sizeof(double));
145 return *this;
146 }
147
148 OutputStream& OutputStream::write(const char* s, int n)
149 {
150 _out.write(s, n);
151 return *this;
152 }
153
154 InputStream::InputStream(std::istream& rin)
155 : _in(rin)
156 {}
157
158 InputStream::~InputStream() = default;
159
160 InputStream& InputStream::operator>>(bool& b)
161 {
162 _in.read((char*)&b, sizeof(bool));
163 return *this;
164 }
165
166 InputStream& InputStream::operator>>(int8_t& ch)
167 {
168 _in.read((char*)&ch, sizeof(int8_t));
169 return *this;
170 }
171
172 InputStream& InputStream::operator>>(uint8_t& uch)
173 {
174 _in.read((char*)&uch, sizeof(uint8_t));
175 return *this;
176 }
177
178 InputStream& InputStream::operator>>(int16_t& s)
179 {
180 _in.read((char*)&s, sizeof(int16_t));
181 if (isSwapped()) {
182 SwapEndian<int16_t>(s);
183 }
184 return *this;
185 }
186
187 InputStream& InputStream::operator>>(uint16_t& us)
188 {
189 _in.read((char*)&us, sizeof(uint16_t));
190 if (isSwapped()) {
191 SwapEndian<uint16_t>(us);
192 }
193 return *this;
194 }
195
196 InputStream& InputStream::operator>>(int32_t& i)
197 {
198 _in.read((char*)&i, sizeof(int32_t));
199 if (isSwapped()) {
200 SwapEndian<int32_t>(i);
201 }
202 return *this;
203 }
204
205 InputStream& InputStream::operator>>(uint32_t& ui)
206 {
207 _in.read((char*)&ui, sizeof(uint32_t));
208 if (isSwapped()) {
209 SwapEndian<uint32_t>(ui);
210 }
211 return *this;
212 }
213
214 InputStream& InputStream::operator>>(int64_t& l)
215 {
216 _in.read((char*)&l, sizeof(int64_t));
217 if (isSwapped()) {
218 SwapEndian<int64_t>(l);
219 }
220 return *this;
221 }
222
223 InputStream& InputStream::operator>>(uint64_t& ul)
224 {
225 _in.read((char*)&ul, sizeof(uint64_t));
226 if (isSwapped()) {
227 SwapEndian<uint64_t>(ul);
228 }
229 return *this;
230 }
231
232 InputStream& InputStream::operator>>(float& f)
233 {
234 _in.read((char*)&f, sizeof(float));
235 if (isSwapped()) {
236 SwapEndian<float>(f);
237 }
238 return *this;
239 }
240
241 InputStream& InputStream::operator>>(double& d)
242 {
243 _in.read((char*)&d, sizeof(double));
244 if (isSwapped()) {
245 SwapEndian<double>(d);
246 }
247 return *this;
248 }
249
250 InputStream& InputStream::read(char* s, int n)
251 {
252 _in.read(s, n);
253 return *this;
254 }
255
256 // ----------------------------------------------------------------------
257
258 ByteArrayOStreambuf::ByteArrayOStreambuf(QByteArray& ba)
259 : _buffer(new QBuffer(&ba))
260 {
261 _buffer->open(QIODevice::WriteOnly);
262 }
263
264 ByteArrayOStreambuf::~ByteArrayOStreambuf()
265 {
266 _buffer->close();
267 delete _buffer;
268 }
269
270 std::streambuf::int_type ByteArrayOStreambuf::overflow(std::streambuf::int_type c)
271 {
272 if (c != EOF) {
273 char z = static_cast<char>(c);
274 if (_buffer->write(&z, 1) != 1) {
275 return EOF;
276 }
277 }
278 return c;
279 }
280
281 std::streamsize ByteArrayOStreambuf::xsputn(const char* s, std::streamsize num)
282 {
283 return _buffer->write(s, num);
284 }
285
286 std::streambuf::pos_type ByteArrayOStreambuf::
287 seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode /*mode*/)
288 {
289 off_type endpos = 0;
290 off_type curpos = _buffer->pos();
291 switch (way) {
292 case std::ios_base::beg:
293 endpos = off;
294 break;
295 case std::ios_base::cur:
296 endpos = curpos + off;
297 break;
298 case std::ios_base::end:
299 endpos = _buffer->size();
300 break;
301 default:
302 return {off_type(-1)};
303 }
304
305 if (endpos != curpos) {
306 if (!_buffer->seek(endpos)) {
307 endpos = -1;
308 }
309 }
310
311 return {endpos};
312 }
313
314 std::streambuf::pos_type ByteArrayOStreambuf::seekpos(std::streambuf::pos_type pos, std::ios_base::openmode /*mode*/)
315 {
316 return seekoff(pos, std::ios_base::beg);
317 }
318
319 // ----------------------------------------------------------------------
320
321 ByteArrayIStreambuf::ByteArrayIStreambuf(const QByteArray& data)
322 : _buffer(data)
323 , _beg(0)
324 , _end(data.size())
325 , _cur(0)
326 {}
327
328 ByteArrayIStreambuf::~ByteArrayIStreambuf() = default;
329
330 ByteArrayIStreambuf::int_type ByteArrayIStreambuf::underflow()
331 {
332 if (_cur == _end) {
333 return traits_type::eof();
334 }
335
336 return static_cast<ByteArrayIStreambuf::int_type>(_buffer[_cur]) & 0x000000ff;
337 }
338
339 ByteArrayIStreambuf::int_type ByteArrayIStreambuf::uflow()
340 {
341 if (_cur == _end) {
342 return traits_type::eof();
343 }
344
345 return static_cast<ByteArrayIStreambuf::int_type>(_buffer[_cur++]) & 0x000000ff;
346 }
347
348 ByteArrayIStreambuf::int_type ByteArrayIStreambuf::pbackfail(int_type ch)
349 {
350 if (_cur == _beg || (ch != traits_type::eof() && ch != _buffer[_cur - 1])) {
351 return traits_type::eof();
352 }
353
354 return static_cast<ByteArrayIStreambuf::int_type>(_buffer[--_cur]) & 0x000000ff;
355 }
356
357 std::streamsize ByteArrayIStreambuf::showmanyc()
358 {
359 return _end - _cur;
360 }
361
362 std::streambuf::pos_type ByteArrayIStreambuf::
363 seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode /*mode*/)
364 {
365 int p_pos = -1;
366 if (way == std::ios_base::beg) {
367 p_pos = _beg;
368 }
369 else if (way == std::ios_base::end) {
370 p_pos = _end;
371 }
372 else if (way == std::ios_base::cur) {
373 p_pos = _cur;
374 }
375
376 if (p_pos > _end) {
377 return traits_type::eof();
378 }
379
380 if (((p_pos + off) > _end) || ((p_pos + off) < _beg)) {
381 return traits_type::eof();
382 }
383
384 _cur = p_pos + off;
385
386 return ((p_pos + off) - _beg);
387 }
388
389 std::streambuf::pos_type ByteArrayIStreambuf::seekpos(std::streambuf::pos_type pos, std::ios_base::openmode /*mode*/)
390 {
391 return seekoff(pos, std::ios_base::beg);
392 }
393
394 // ----------------------------------------------------------------------
395
396 IODeviceOStreambuf::IODeviceOStreambuf(QIODevice* dev)
397 : device(dev)
398 {}
399
400 IODeviceOStreambuf::~IODeviceOStreambuf() = default;
401
402 std::streambuf::int_type IODeviceOStreambuf::overflow(std::streambuf::int_type c)
403 {
404 if (c != EOF) {
405 char z = static_cast<char>(c);
406 if (device->write(&z, 1) != 1) {
407 return EOF;
408 }
409 }
410 return c;
411 }
412
413 std::streamsize IODeviceOStreambuf::xsputn(const char* s, std::streamsize num)
414 {
415 return device->write(s, num);
416 }
417
418 std::streambuf::pos_type IODeviceOStreambuf::
419 seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode /*mode*/)
420 {
421 off_type endpos = 0;
422 off_type curpos = device->pos();
423 switch (way) {
424 case std::ios_base::beg:
425 endpos = off;
426 break;
427 case std::ios_base::cur:
428 endpos = curpos + off;
429 break;
430 case std::ios_base::end:
431 endpos = device->size();
432 break;
433 default:
434 return {off_type(-1)};
435 }
436
437 if (endpos != curpos) {
438 if (!device->seek(endpos)) {
439 endpos = -1;
440 }
441 }
442
443 return {endpos};
444 }
445
446 std::streambuf::pos_type IODeviceOStreambuf::seekpos(std::streambuf::pos_type pos, std::ios_base::openmode /*mode*/)
447 {
448 return seekoff(pos, std::ios_base::beg);
449 }
450
451 // ----------------------------------------------------------------------
452
453 IODeviceIStreambuf::IODeviceIStreambuf(QIODevice* dev)
454 : device(dev)
455 {
456 setg(
457 buffer + pbSize, // beginning of putback area
458 buffer + pbSize, // read position
459 buffer + pbSize
460 ); // end position
461 }
462
463 IODeviceIStreambuf::~IODeviceIStreambuf() = default;
464
465 std::streambuf::int_type IODeviceIStreambuf::underflow()
466 {
467 #ifndef _MSC_VER
468 using std::memcpy;
469 #endif
470
471 // is read position before end of buffer?
472 if (gptr() < egptr()) {
473 return *gptr();
474 }
475
476 /* process size of putback area
477 * - use number of characters read
478 * - but at most size of putback area
479 */
480 int numPutback {};
481 numPutback = gptr() - eback();
482 if (numPutback > pbSize) {
483 numPutback = pbSize;
484 }
485
486 /* copy up to pbSize characters previously read into
487 * the putback area
488 */
489 memcpy(buffer + (pbSize - numPutback), gptr() - numPutback, numPutback);
490
491 // read at most bufSize new characters
492 int num {};
493 num = device->read(buffer + pbSize, bufSize);
494 if (num <= 0) {
495 // ERROR or EOF
496 return EOF;
497 }
498
499 // reset buffer pointers
500 setg(
501 buffer + (pbSize - numPutback), // beginning of putback area
502 buffer + pbSize, // read position
503 buffer + pbSize + num
504 ); // end of buffer
505
506 // return next character
507 return *gptr();
508 }
509
510 std::streambuf::pos_type IODeviceIStreambuf::
511 seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode /*mode*/)
512 {
513 off_type endpos = 0;
514 off_type curpos = device->pos();
515 switch (way) {
516 case std::ios_base::beg:
517 endpos = off;
518 break;
519 case std::ios_base::cur:
520 endpos = curpos + off;
521 break;
522 case std::ios_base::end:
523 endpos = device->size();
524 break;
525 default:
526 return {off_type(-1)};
527 }
528
529 if (endpos != curpos) {
530 if (!device->seek(endpos)) {
531 endpos = -1;
532 }
533 }
534
535 return {endpos};
536 }
537
538 std::streambuf::pos_type IODeviceIStreambuf::seekpos(std::streambuf::pos_type pos, std::ios_base::openmode /*mode*/)
539 {
540 return seekoff(pos, std::ios_base::beg);
541 }
542
543 // ---------------------------------------------------------
544
545 #define PYSTREAM_BUFFERED
546
547 // http://www.mr-edd.co.uk/blog/beginners_guide_streambuf
548 // http://www.icce.rug.nl/documents/cplusplus/cplusplus24.html
549 PyStreambuf::PyStreambuf(PyObject* o, std::size_t buf_size, std::size_t put_back)
550 : inp(o)
551 , put_back(std::max(put_back, std::size_t(1)))
552 , buffer(std::max(buf_size, put_back) + put_back)
553 {
554 Py_INCREF(inp);
555 char* end = &buffer.front() + buffer.size();
556 setg(end, end, end);
557 #ifdef PYSTREAM_BUFFERED
558 char* base = &buffer.front();
559 setp(base, base + buffer.size());
560 #endif
561 }
562
563 PyStreambuf::~PyStreambuf()
564 {
565 PyStreambuf::sync();
566 Py_DECREF(inp);
567 }
568
569 PyStreambuf::int_type PyStreambuf::underflow()
570 {
571 if (gptr() < egptr()) {
572 return traits_type::to_int_type(*gptr());
573 }
574
575 char* base = &buffer.front();
576 char* start = base;
577
578 if (eback() == base) { // true when this isn't the first fill
579 std::memmove(base, egptr() - put_back, put_back);
580 start += put_back;
581 }
582
583 std::size_t n {};
584 Py::Tuple arg(1);
585 long len = static_cast<long>(buffer.size() - (start - base));
586 arg.setItem(0, Py::Long(len));
587 Py::Callable meth(Py::Object(inp).getAttr("read"));
588
589 try {
590 std::string c;
591 Py::Object res(meth.apply(arg));
592 if (res.isBytes()) {
593 c = static_cast<std::string>(Py::Bytes(res));
594 }
595 else if (res.isString()) {
596 c = static_cast<std::string>(Py::String(res));
597 }
598 else {
599 // wrong type
600 return traits_type::eof();
601 }
602
603 n = c.size();
604 if (n == 0) {
605 return traits_type::eof();
606 }
607
608 // Check: bugprone-not-null-terminated-result
609 std::memcpy(start, c.data(), c.size());
610 }
611 catch (Py::Exception& e) {
612 e.clear();
613 return traits_type::eof();
614 }
615
616 setg(base, start, start + n);
617 return traits_type::to_int_type(*gptr());
618 }
619
620 PyStreambuf::int_type PyStreambuf::overflow(PyStreambuf::int_type ch)
621 {
622 #ifdef PYSTREAM_BUFFERED
623 sync();
624 if (ch != traits_type::eof()) {
625 *pptr() = static_cast<char>(ch);
626 pbump(1);
627 return ch;
628 }
629
630 return traits_type::eof();
631 #else
632 if (ch != EOF) {
633 char z = ch;
634 if (!writeStr(&z, 1)) {
635 return traits_type::eof();
636 }
637 }
638
639 return ch;
640 #endif
641 }
642
643 int PyStreambuf::sync()
644 {
645 #ifdef PYSTREAM_BUFFERED
646 if (pptr() > pbase()) {
647 flushBuffer();
648 }
649 return 0;
650 #else
651 return std::streambuf::sync();
652 #endif
653 }
654
655 bool PyStreambuf::flushBuffer()
656 {
657 std::ptrdiff_t n = pptr() - pbase();
658 pbump(-n);
659 return writeStr(pbase(), n);
660 }
661
662 bool PyStreambuf::writeStr(const char* str, std::streamsize num)
663 {
664 try {
665 Py::Tuple arg(1);
666 Py::Callable meth(Py::Object(inp).getAttr("write"));
667
668 if (type == StringIO) {
669 arg.setItem(0, Py::String(str, num));
670 meth.apply(arg);
671 return true;
672 }
673 if (type == BytesIO) {
674 arg.setItem(0, Py::Bytes(str, num));
675 meth.apply(arg);
676 return true;
677 }
678
679 // try out what works
680 try {
681 arg.setItem(0, Py::String(str, num));
682 meth.apply(arg);
683 type = StringIO;
684 return true;
685 }
686 catch (Py::Exception& e) {
687 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
688 e.clear();
689 arg.setItem(0, Py::Bytes(str, num));
690 meth.apply(arg);
691 type = BytesIO;
692 return true;
693 }
694
695 throw; // re-throw
696 }
697 }
698 catch (Py::Exception& e) {
699 e.clear();
700 }
701
702 return false;
703 }
704
705 std::streamsize PyStreambuf::xsputn(const char* s, std::streamsize num)
706 {
707 #ifdef PYSTREAM_BUFFERED
708 return std::streambuf::xsputn(s, num);
709 #else
710 if (!writeStr(s, num)) {
711 return 0;
712 }
713 return num;
714 #endif
715 }
716
717 PyStreambuf::pos_type PyStreambuf::
718 seekoff(PyStreambuf::off_type offset, PyStreambuf::seekdir dir, PyStreambuf::openmode /*mode*/)
719 {
720 int whence = 0;
721 switch (dir) {
722 case std::ios_base::beg:
723 whence = 0;
724 break;
725 case std::ios_base::cur:
726 whence = 1;
727 break;
728 case std::ios_base::end:
729 whence = 2;
730 break;
731 default:
732 return {off_type(-1)};
733 }
734
735 try {
736 Py::Tuple arg(2);
737 arg.setItem(0, Py::Long(static_cast<long>(offset)));
738 arg.setItem(1, Py::Long(whence));
739 Py::Callable seek(Py::Object(inp).getAttr("seek"));
740 seek.apply(arg);
741
742 // get current position
743 Py::Tuple arg2;
744 Py::Callable tell(Py::Object(inp).getAttr("tell"));
745 Py::Long pos(tell.apply(arg2));
746 long cur_pos = static_cast<long>(pos);
747 return static_cast<pos_type>(cur_pos);
748 }
749 catch (Py::Exception& e) {
750 e.clear();
751 return {off_type(-1)};
752 }
753 }
754
755 PyStreambuf::pos_type PyStreambuf::seekpos(PyStreambuf::pos_type offset, PyStreambuf::openmode mode)
756 {
757 return seekoff(offset, std::ios::beg, mode);
758 }
759
760 // ---------------------------------------------------------
761
762 Streambuf::Streambuf(const std::string& data)
763 {
764 _beg = data.begin();
765 _end = data.end();
766 _cur = _beg; // NOLINT
767 }
768
769 Streambuf::~Streambuf() = default;
770
771 Streambuf::int_type Streambuf::underflow()
772 {
773 if (_cur == _end) {
774 return traits_type::eof();
775 }
776
777 return static_cast<Streambuf::int_type>(*_cur) & 0x000000ff;
778 }
779
780 Streambuf::int_type Streambuf::uflow()
781 {
782 if (_cur == _end) {
783 return traits_type::eof();
784 }
785
786 return static_cast<Streambuf::int_type>(*_cur++) & 0x000000ff;
787 }
788
789 Streambuf::int_type Streambuf::pbackfail(int_type ch)
790 {
791 /* coverity[negative_returns] _cur is an iterator */
792 if (_cur == _beg || (ch != traits_type::eof() && ch != _cur[-1])) {
793 return traits_type::eof();
794 }
795
796 return static_cast<Streambuf::int_type>(*--_cur) & 0x000000ff;
797 }
798
799 std::streamsize Streambuf::showmanyc()
800 {
801 return _end - _cur;
802 }
803
804 std::streambuf::pos_type Streambuf::
805 seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode /*mode*/)
806 {
807 std::string::const_iterator p_pos;
808 if (way == std::ios_base::beg) {
809 p_pos = _beg;
810 }
811 else if (way == std::ios_base::end) {
812 p_pos = _end;
813 }
814 else if (way == std::ios_base::cur) {
815 p_pos = _cur;
816 }
817
818 if (p_pos > _end) {
819 return traits_type::eof();
820 }
821
822 if (((p_pos + off) > _end) || ((p_pos + off) < _beg)) {
823 return traits_type::eof();
824 }
825
826 _cur = p_pos + off;
827
828 return ((p_pos + off) - _beg);
829 }
830
831 std::streambuf::pos_type Streambuf::seekpos(std::streambuf::pos_type pos, std::ios_base::openmode /*mode*/)
832 {
833 return seekoff(pos, std::ios_base::beg);
834 }
835
836 // The custom string handler written by realthunder for the LinkStage3 toponaming code, to handle
837 // reading multi-line strings directly into a std::string. Imported from LinkStage3 and refactored
838 // during the TNP mitigation project in February 2024.
839 TextInputStream& TextInputStream::operator>>(std::string& outputString)
840 {
841 uint32_t numberOfLines;
842 char inputChar;
843 // The number of lines is followed by a colon as the delimiter. The string itself is then
844 // allowed to start with any character.
845 _in >> numberOfLines >> inputChar;
846
847 _ss.str("");
848 for (uint32_t lineNumber = 0; lineNumber < numberOfLines && _in; ++lineNumber) {
849 while (true) {
850 if (!_in.get(inputChar)) {
851 break;
852 }
853 // Normalize \r\n to \n
854 if (inputChar == '\r') {
855 if (!_in.get(inputChar)) {
856 break;
857 }
858 if (inputChar == '\n') {
859 break;
860 }
861 _ss.put('\r');
862 _ss.put(inputChar);
863 }
864 else {
865 _ss.put(inputChar);
866 if (inputChar == '\n') {
867 break;
868 }
869 }
870 }
871 }
872
873 // Reading the last line
874 while (_in.get(inputChar)) {
875 // Normalize \r\n to \n, but DO NOT insert '\n' into the extracted
876 // line, because the last '\n' is inserted by us (See OutputStream
877 // operator>>(const char*) above)
878 if (inputChar == '\r') {
879 if (!_in.get(inputChar)) {
880 break;
881 }
882 if (inputChar == '\n') {
883 break;
884 }
885 _ss.put('\r');
886 }
887 else if (inputChar == '\n') {
888 break;
889 }
890
891 _ss.put(inputChar);
892 }
893
894 outputString = _ss.str();
895 return *this;
896 }