ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
stdiostream.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16#include <algorithm>
17//============================================== Module ============================================
18#if ALIB_C20_MODULES
19 module ALib.Strings.StdIOStream;
20 import ALib.Strings;
21#else
22# include "ALib.Strings.H"
24#endif
25//========================================== Implementation ========================================
26
27#if ALIB_STRINGS
28
30
31#if !DOXYGEN
32
33
34//##################################### std::ostream& operator<< ###################################
35std::ostream& operator<<( std::ostream& stream, const alib::WString& string ) {
36 alib::NString4K conv;
37 alib::integer maxConv= 4 * 1024 / static_cast<alib::integer>(MB_CUR_MAX);
38
39 alib::integer startIdx= 0;
40 while( startIdx < string.Length() ) {
41 alib::integer length= (std::min)( alib::integer(maxConv), string.Length() - startIdx);
42 conv.Reset( string.Substring<alib::NC>(startIdx, length) );
43 stream.write( conv.Buffer(), conv.Length() );
44 startIdx+= length;
45 }
46
47 return stream;
48}
49
50std::wostream& operator<<( std::wostream& stream, const alib::NString& string ) {
53 conv << string;
54 stream.write( conv.Buffer(), conv.Length() );
55 return stream;
56}
57
58//########################################### TIStreamLine #########################################
59template<typename TChar>
61 TChar,
62 alib::lang::HeapAllocator>::operator()(
63 TAString<TChar, HeapAllocator>& target,
64 const compatibility::std::TIStreamLine<TChar>& constIsLine ) {
65 // we are required to read from to the param object. So we cast to non-const.
66 // This is OK, as the const specifier came through template programming.
67 compatibility::std::TIStreamLine<TChar>& isLine=
68 const_cast<compatibility::std::TIStreamLine<TChar>&>( constIsLine );
69
70 if ( isLine.TargetData == lang::CurrentData::Clear )
71 target.Reset();
72 alib::integer origLength= target.Length();
73
74 // read loop
75 while( !isLine.IStream->eof() ) {
76 // calc buffer size (if we hit the overall line width)
77 // and check if we reached the limit per line
78 integer actReadSize= (std::min)( isLine.BufferSize,
79 isLine.MaxLineWidth - (target.Length() - origLength) + 1 );
80 if ( actReadSize < 2 )
81 return;
82
83 target.EnsureRemainingCapacity( actReadSize );
84
85 // read
86 integer start= target.Length();
87 isLine.IStream->getline( target.VBuffer() + start, actReadSize );
88 std::streamsize gCount= isLine.IStream->gcount();
89 std::streamsize count= static_cast<std::streamsize>( characters::Length( target.Buffer() + start ) );
90
91 bool lineComplete= count + 1 == gCount;
92
93 // something read?
94 if ( count > 0 ) {
95 // be sure to not have a carriage return at the start
96 if( *(target.Buffer() + start) == '\r' ) {
97 target.template Delete<NC>( static_cast<alib::integer>(start), 1 );
98 --count;
99 }
100
101 // be sure to not have a carriage return at the end
102 start+= count;
103 if( *(target.Buffer() + start -1 ) == '\r' )
104 --start;
105
106 target.SetLength( static_cast<alib::integer>(start) );
107
108 // if we are at the end of the file (without delimiter) we stop now
109 if ( isLine.IStream->eof() ) {
110 isLine.IsEOF= true;
111 return;
112 } }
113
114 // delim read
115 if ( lineComplete )
116 return;
117
118 // buffer was not big enough
119 if ( gCount == actReadSize -1) {
120 if ( isLine.IStream->eof() )
121 return;
122
123 // otherwise, it should really have been the buffer size, so let's clear the bit
124 // and continue with more buffer space
125 ALIB_ASSERT( isLine.IStream->rdstate() == std::iostream::failbit, "STRINGS" )
126 isLine.IStream->clear();
127 continue;
128 }
129
130 // the eof just happened now
131 if ( isLine.IStream->eof() )
132 break;
133
134 if ( isLine.IStream->rdstate() == std::iostream::failbit ) {
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 isLine.IsEOF= true;
150}
151
152// instantiations of AppendableTraits::operator() for char and wchar_t versions of TIStreamLine
153template 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 );
154template 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 );
155
156#endif // DOXYGEN
157
158#endif // ALIB_STRINGS
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:244
constexpr integer Length() const
Definition string.inl:316
constexpr const TChar * Buffer() const
Definition string.inl:311
#define ALIB_ASSERT(cond, domain)
Definition alib.inl:1065
#define ALIB_ERROR(domain,...)
Definition alib.inl:1062
integer Length(const TChar *cstring)
Definition functions.inl:89
ALIB_EXPORT constexpr TEnum operator<<(TEnum lhs, typename std::underlying_type< TEnum >::type rhs) noexcept
platform_specific integer
Definition integers.inl:32
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2198
strings::TString< wchar > WString
Type alias in namespace alib.
Definition string.inl:2201
NLocalString< 4096 > NString4K
Type alias name for TLocalString<nchar,8192>.