ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
stdiostream.cpp
1
2#if ALIB_STRINGS
3
4#if !DOXYGEN
5
6
7//##################################### std::ostream& operator<< ###################################
8std::ostream& operator<<( std::ostream& stream, const alib::WString& string ) {
10 alib::integer maxConv= 4 * 1024 / static_cast<alib::integer>(MB_CUR_MAX);
11
12 alib::integer startIdx= 0;
13 while( startIdx < string.Length() ) {
14 alib::integer length= (std::min)( alib::integer(maxConv), string.Length() - startIdx);
15 conv.Reset( string.Substring<alib::NC>(startIdx, length) );
16 stream.write( conv.Buffer(), conv.Length() );
17 startIdx+= length;
18 }
19
20 return stream;
21}
22
23std::wostream& operator<<( std::wostream& stream, const alib::NString& string ) {
26 conv << string;
27 stream.write( conv.Buffer(), conv.Length() );
28 return stream;
29}
30
31//########################################### TIStreamLine #########################################
32template<typename TChar>
34 TChar,
35 alib::lang::HeapAllocator>::operator()(
36 TAString<TChar, HeapAllocator>& target,
37 const compatibility::std::TIStreamLine<TChar>& constIsLine ) {
38 // we are required to read from to the param object. So we cast to non-const.
39 // This is OK, as the const specifier came through template programming.
40 compatibility::std::TIStreamLine<TChar>& isLine=
41 const_cast<compatibility::std::TIStreamLine<TChar>&>( constIsLine );
42
43 if ( isLine.TargetData == lang::CurrentData::Clear )
44 target.Reset();
45 integer origLength= target.Length();
46
47 // read loop
48 while( !isLine.IStream->eof() ) {
49 // calc buffer size (if we hit the overall line width)
50 // and check if we reached the limit per line
51 integer actReadSize= (std::min)( isLine.BufferSize,
52 isLine.MaxLineWidth - (target.Length() - origLength) + 1 );
53 if ( actReadSize < 2 )
54 return;
55
56 target.EnsureRemainingCapacity( actReadSize );
57
58 // read
59 integer start= target.Length();
60 isLine.IStream->getline( target.VBuffer() + start, actReadSize );
61 std::streamsize gCount= isLine.IStream->gcount();
62 std::streamsize count= static_cast<std::streamsize>( characters::Length( target.Buffer() + start ) );
63
64 bool lineComplete= count + 1 == gCount;
65
66 // something read?
67 if ( count > 0 ) {
68 // be sure to not have a carriage return at the start
69 if( *(target.Buffer() + start) == '\r' ) {
70 target.template Delete<NC>( static_cast<integer>(start), 1 );
71 --count;
72 }
73
74 // be sure to not have a carriage return at the end
75 start+= integer(count);
76 if( *(target.Buffer() + start -1 ) == '\r' )
77 --start;
78
79 target.SetLength( static_cast<integer>(start) );
80
81 // if we are at the end of the file (without delimiter) we stop now
82 if ( isLine.IStream->eof() ) {
83 isLine.IsEOF= true;
84 return;
85 } }
86
87 // delim read
88 if ( lineComplete )
89 return;
90
91 // buffer was not big enough
92 if ( gCount == actReadSize -1) {
93 if ( isLine.IStream->eof() )
94 return;
95
96 // otherwise, it should really have been the buffer size, so let's clear the bit
97 // and continue with more buffer space
98 ALIB_ASSERT( isLine.IStream->rdstate() == std::iostream::failbit, "STRINGS" )
99 isLine.IStream->clear();
100 continue;
101 }
102
103 // the eof just happened now
104 if ( isLine.IStream->eof() )
105 break;
106
107 if ( isLine.IStream->rdstate() == std::iostream::failbit ) {
108 ALIB_ERROR("STRINGS","Unknown Error Reading File. Maybe method implemented incomplete?")
109 break;
110 }
111
112 // anything else to add here? I guess not! But you never know with this strange
113 // iostream classes!
114
115 //...
116
117 // This happens if \0 is in the file
118 ALIB_ERROR( "STRINGS", "Unknown Error Reading File. Probably not a text file." )
119 break;
120 }
121
122 isLine.IsEOF= true;
123}
124
125// instantiations of AppendableTraits::operator() for char and wchar_t versions of TIStreamLine
126template void alib::strings::AppendableTraits<alib::strings::compatibility::std::TIStreamLine<char >, char , alib::lang::HeapAllocator>::operator()( TAString<char , alib::lang::HeapAllocator>& target, const compatibility::std::TIStreamLine<char >& reader );
127template void alib::strings::AppendableTraits<alib::strings::compatibility::std::TIStreamLine<wchar_t>, wchar_t, alib::lang::HeapAllocator>::operator()( TAString<wchar_t, alib::lang::HeapAllocator>& target, const compatibility::std::TIStreamLine<wchar_t>& reader );
128
129#endif // DOXYGEN
130
131#endif // ALIB_STRINGS
#define ALIB_ASSERT(cond, domain)
#define ALIB_ERROR(domain,...)
void DbgDisableBufferReplacementWarning()
Definition tastring.hpp:236
constexpr integer Length() const
Definition string.hpp:300
constexpr const TChar * Buffer() const
Definition string.hpp:295
integer Length(const TChar *cstring)
Definition functions.hpp:89
ALIB_EXPORT constexpr TEnum operator<<(TEnum lhs, typename std::underlying_type< TEnum >::type rhs) noexcept
platform_specific integer
Definition integers.hpp:32
Definition alox.cpp:14
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
strings::TString< wchar > WString
Type alias in namespace #"%alib".
Definition string.hpp:2177
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
NLocalString< 4096 > NString4K
Type alias name for #"TLocalString;TLocalString<nchar,8192>".