ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
std_strings_iostream.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !defined(ALIB_DOX)
10#if !defined (HPP_ALIB_COMPATIBILITY_STD_STRINGS_IOSTREAM)
12#endif
13#endif // !defined(ALIB_DOX)
14
15#if !defined (_GLIBCXX_ALGORITHM) && !defined(_ALGORITHM_)
16# include <algorithm>
17#endif
18
19#if ALIB_STRINGS
20
21
22#if !defined(ALIB_DOX)
23
24
25std::ostream& operator<<( std::ostream& stream, const alib::WString& string )
26{
27 alib::NString1K conv;
28 alib::integer maxConv= 1024 / static_cast<alib::integer>(MB_CUR_MAX);
29
30 alib::integer startIdx= 0;
31 while( startIdx < string.Length() )
32 {
33 alib::integer length= (std::min)( alib::integer(maxConv), string.Length() - startIdx);
34 conv.Reset( string.Substring<false>(startIdx, length) );
35 stream.write( conv.Buffer(), conv.Length() );
36 startIdx+= length;
37 }
38
39 return stream;
40}
41
42std::wostream& operator<<( std::wostream& stream, const alib::NString& string )
43{
46 conv << string;
47 stream.write( conv.Buffer(), conv.Length() );
48 return stream;
49}
50
51template<typename TChar>
53 TAString<TChar>& target,
54 const compatibility::std::TISReadLine<TChar>& reader )
55{
56 // we are required to read from to the param object. So we cast to non-const. This is OK, as
57 // the const specifier came through TMP.
58 compatibility::std::TISReadLine<TChar>& param= const_cast<compatibility::std::TISReadLine<TChar>&>( reader );
59
60 if ( param.TargetData == lang::CurrentData::Clear )
61 target.Reset();
62 alib::integer origLength= target.Length();
63
64 // read loop
65 while( !param.IStream->eof() )
66 {
67 // calc buffer size (if we hit the overall line width)
68 // and check if we reached the limit per line
69 alib::integer actReadSize= (std::min)( param.BufferSize, param.MaxLineWidth - ( target.Length() - origLength) + 1 );
70 if ( actReadSize < 2 )
71 return;
72
73 target.EnsureRemainingCapacity( actReadSize );
74
75 // read
77 int64_t start= target.Length();
78 param.IStream->getline( target.VBuffer() + start, actReadSize );
79 std::streamsize gCount= param.IStream->gcount();
80 std::streamsize count= static_cast<std::streamsize>( characters::CharArray<TChar>::Length( target.Buffer() + start ) );
81
82 bool lineComplete= count + 1 == gCount;
83
84 // something read?
85 if ( count > 0 )
86 {
87 // be sure to not have a carriage return at the start
88 if( *(target.Buffer() + start) == '\r' )
89 {
90 target.template Delete<false>( static_cast<alib::integer>(start), 1 );
91 --count;
92 }
93
94 // be sure to not have a carriage return at the end
95 start+= count;
96 if( *(target.Buffer() + start -1 ) == '\r' )
97 --start;
98
99 target.SetLength( static_cast<alib::integer>(start) );
100
101 // if we are at the end of the file (without delimiter) we stop now
102 if ( param.IStream->eof() )
103 {
104 param.IsEOF= true;
105 return;
106 }
107 }
109
110
111 // delim read
112 if ( lineComplete )
113 return;
114
115 // buffer was not big enough
116 if ( gCount == actReadSize -1)
117 {
118 if ( param.IStream->eof() )
119 return;
120
121
122 // otherwise, it should really have been the buffer size, so let's clear the bit
123 // and continue with more buffer space
124 ALIB_ASSERT( param.IStream->rdstate() == std::iostream::failbit )
125 param.IStream->clear();
126 continue;
127 }
128
129 // the eof just happened now
130 if ( param.IStream->eof() )
131 break;
132
133 if ( param.IStream->rdstate() == std::iostream::failbit )
134 {
135 ALIB_ERROR( "STRINGS", "Unknown Error Reading File. Maybe method implemented incomplete?" )
136 break;
137 }
138
139 // anything else to add here? I guess not! But you never know with this strange
140 // iostream classes!
141
142 //...
143
144 // This happens if \0 is in the file
145 ALIB_ERROR( "STRINGS", "Unknown Error Reading File. Probably not a text file." )
146 break;
147 }
148
149 param.IsEOF= true;
150}
151
152
153// instantiations of T_Append::operator() for char and wchar_t versions of stream reader class
154template void alib::strings::T_Append<alib::strings::compatibility::std::TISReadLine<char >, char >::operator()( TAString<char >& target, const compatibility::std::TISReadLine<char >& reader );
155template void alib::strings::T_Append<alib::strings::compatibility::std::TISReadLine<wchar_t>, wchar_t>::operator()( TAString<wchar_t>& target, const compatibility::std::TISReadLine<wchar_t>& reader );
156
157#endif // defined(ALIB_DOX)
158
159#endif // ALIB_STRINGS
void DbgDisableBufferReplacementWarning()
Definition astring.hpp:353
constexpr integer Length() const
Definition string.hpp:357
constexpr const TChar * Buffer() const
Definition string.hpp:350
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_ERROR(...)
Definition alib.hpp:980
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
#define ALIB_ASSERT(cond)
Definition alib.hpp:983
std::ostream & operator<<(std::ostream &stream, const alib::NString &string)
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286