ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
autosizes.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_STRINGS_UTIL_AUTOSIZES)
12#endif
13
14#if !defined (HPP_ALIB_STRINGS_LOCALSTRING)
16#endif
17
18#if !defined (HPP_ALIB_STRINGS_CSTRING)
20#endif
21
22#if !defined (HPP_ALIB_STRINGS_SUBSTRING)
24#endif
25#endif // !defined(ALIB_DOX)
26
27namespace alib { namespace strings { namespace util {
28
29integer AutoSizes::Actual( Types type, integer requestedSize, integer growthPadding )
30{
31 // grow arrays as needed
32 while ( data.size() <= ActualIndex )
33 data.emplace_back( type, -1, -1 );
34
35 data[ActualIndex].type= type;
36
37 // set measured size as it would be for the next session
38 integer session= data[ ActualIndex ].session;
39 if ( session < requestedSize )
40 {
41 data[ ActualIndex ].session = requestedSize;
42 }
43
44 // get size as it is for actual values (the ones that might have been imported)
45 integer actual= data[ ActualIndex ].actual;
46 if ( actual < requestedSize )
47 {
48 actual= data[ ActualIndex ].actual= (requestedSize + ( actual < 0 ? 0 : growthPadding ));
49 }
50
51 return static_cast<int>(actual);
52}
53
54
56{
57 for( auto& entry : data )
58 {
59 target._( '(' ) ._( entry.type == Types::Tabstop ? 'T' : 'F')._( ',' )
60 ._( entry.actual )._( ',' )
61 ._( entry.session ) ._( ')' );
62 }
63
64 // remove unused entries at the end
65 while( target.EndsWith(A_CHAR(",0,0)") ) )
66 target.DeleteEnd<false>(target.Length() - target.LastIndexOf('('));
67}
68
69void AutoSizes::Import( const String& sourceString, lang::CurrentData session )
70{
71 Reset();
72
73 #if ALIB_DEBUG
74 # define PARSERROR ALIB_WARNING( \
75 NString512("Error reading tab stops string \"") << NString512(sourceString) \
76 << "\":\n at position " << (sourceString.Length() - parser.Length()) )
77 #else
78 # define PARSERROR
79 #endif
80 Substring parser(sourceString);
81 while(parser.Trim().IsNotEmpty())
82 {
83 parser.TrimStart();
84 Types type;
86 {
87 PARSERROR
88 break;
89 }
90
92 type= Types::Tabstop;
94 type= Types::Field;
95 else
96 {
97 PARSERROR
98 break;
99 }
100
102 {
103 PARSERROR
104 break;
105 }
106
107
108 integer actual;
109 parser.ConsumeInt( actual );
110
112 {
113 PARSERROR
114 break;
115 }
116
117
118 integer sessionValue;
119 parser.ConsumeInt( sessionValue );
120
122 {
123 PARSERROR
124 break;
125 }
126
127 data.emplace_back( type, actual, sessionValue );
128 }
129
130 if( session == lang::CurrentData::Clear )
131 Consolidate();
132}
133
135{
136 integer tabDiff = 0;
137 integer lastTabStop = 0;
138 for( auto& entry : data )
139 {
140 integer actDiff= entry.session - entry.actual;
141 if( actDiff > 0 ) // should never happen. Maybe improper data import?
142 actDiff= 0;
143 if( entry.type == Types::Tabstop )
144 {
145 // reset tab difference if (for some strange application specific reason) this
146 // tab stop is smaller than the previous one. Obviously some multi-line tab stop
147 // is used (does not happen for example with ALox)
148 if( entry.actual > lastTabStop )
149 {
150 lastTabStop= entry.actual;
151 entry.actual= entry.session + tabDiff;
152 }
153 else
154 {
155 lastTabStop= entry.actual;
156 tabDiff= 0;
157 entry.actual= entry.session + tabDiff;
158 }
159 }
160 else
161 {
162 entry.actual = entry.session;
163 }
164 tabDiff+= actDiff;
165 entry.session = -1;
166 }
167}
168
169
170}}} // namespace [alib::strings::util]
TAString & DeleteEnd(integer regionLength)
Definition astring.hpp:1589
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1056
constexpr bool IsNotEmpty() const
Definition string.hpp:420
constexpr integer Length() const
Definition string.hpp:357
ALIB_WARNINGS_RESTORE integer LastIndexOf(TChar needle, integer startIndex=MAX_LEN) const
Definition string.hpp:1027
bool EndsWith(const TString &needle) const
Definition string.hpp:847
TSubstring & Trim(const TCString< TChar > &whiteSpaces=TT_StringConstants< TChar >::DefaultWhitespaces())
bool ConsumeInt(TIntegral &result, TNumberFormat< TChar > *numberFormat=nullptr)
TSubstring & TrimStart(const TCString< TChar > &whiteSpaces=TT_StringConstants< TChar >::DefaultWhitespaces())
Definition substring.hpp:89
std::vector< Entry > data
ALIB_API void Import(const String &source, lang::CurrentData session=lang::CurrentData::Clear)
Definition autosizes.cpp:69
@ Field
denotes a field width entry.
@ Tabstop
denotes a tab stop entry.
ALIB_API integer Actual(Types type, integer requestedSize, integer growthPadding)
Definition autosizes.cpp:29
ALIB_API void Export(AString &target)
Definition autosizes.cpp:55
#define A_CHAR(STR)
@ Trim
Trim whitespaces away.
@ Clear
Chooses to clear existing data.
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286