ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
autosizes.cpp
1namespace alib { namespace strings { namespace util {
2
3
4integer AutoSizes::Actual( Types type, integer requestedSize, integer growthPadding ) {
5 // grow arrays as needed
6 while ( data.size() <= ActualIndex ) {
7 data.emplace_back( type, -1, -1 );
8 dirty= true;
9 }
10
11 if ( data[ActualIndex].type != type ) {
12 data[ActualIndex].type = type;
13 data[ActualIndex].actual= 0;
14 data[ActualIndex].session= -1;
15 dirty= true;
16 }
17
18 if( WriteProtected )
19 return (std::max)( data[ ActualIndex ].actual, requestedSize );
20
21 // set measured size as it would be for the next session
22 integer& session= data[ ActualIndex ].session;
23 if ( session < requestedSize ) {
24 session= requestedSize;
25 dirty= true;
26 }
27
28 // get size as it is for actual values (max of imported and session)
29 integer& actual= data[ ActualIndex ].actual;
30 if ( actual < requestedSize ) {
31 actual= (requestedSize + ( actual < 0 ? 0 : growthPadding ));
32 dirty= true;
33 }
34
35 return actual;
36}
37
38void AutoSizes::Export( AString& target ) {
39 if( WriteProtected )
40 target._( "! ");
41
42 auto it= data.begin();
43 while( it!=data.end() ) {
44 target << ((*it).type == Types::Tabstop ? 'T' : 'F' )
45 << (*it).actual;
46 if( !WriteProtected && (*it).session != (*it).actual)
47 target << ',' << (*it).session;
48
49 ++it;
50 if( it!=data.end())
51 target << '/';
52 else
53 break;
54 }
55
56 // remove unused entries at the end
57 while( target.EndsWith(A_CHAR("/T0" ))
58 || target.EndsWith(A_CHAR("/F0" ))
59 || target.EndsWith(A_CHAR("/T-1" ))
60 || target.EndsWith(A_CHAR("/F-1" )) )
61 target.DeleteEnd<NC>(target.Length() - target.LastIndexOf('/'));
62
63 dirty= false;
64}
65
66void AutoSizes::Import( const String& src, lang::CurrentData session ) {
67 Reset();
68 dirty= false;
69
70 #if ALIB_DEBUG
71 # define PARSERROR ALIB_WARNING( "STRINGS", \
72 "Error reading tab stops string \"{}\":\n at position ", \
73 src, src.Length() - parser.Length() )
74 #else
75 # define PARSERROR
76 #endif
77 Substring parser(src);
79 if(parser.Trim().IsEmpty())
80 return;
81
82 Tokenizer tknzr( parser, '/' );
83 while(tknzr.HasNext()) {
84 parser= tknzr.Next();
85 Types type;
88 else { PARSERROR break; }
89
90 integer actual;
91 parser.ConsumeInt( actual );
92
93 integer sessionValue;
95 sessionValue= actual;
96 else
97 parser.ConsumeInt( sessionValue );
98
99 data.emplace_back( type, actual, sessionValue );
100 }
101
102 if( session == lang::CurrentData::Clear )
103 Consolidate();
104}
105
107 integer tabDiff = 0;
108 integer lastTabStop = 0;
109 for( auto& entry : data ) {
110 integer actDiff= entry.session - entry.actual;
111 if( actDiff > 0 ) // should never happen. Maybe improper data import?
112 actDiff= 0;
113 if( entry.type == Types::Tabstop ) {
114 // reset tab difference if (for some strange application-specific reason) this
115 // tab stop is smaller than the previous one. Obviously some multi-line tab stop
116 // is used (does not happen with ALox)
117 if( entry.actual > lastTabStop ) {
118 lastTabStop= entry.actual;
119 entry.actual= entry.session + tabDiff;
120 } else {
121 lastTabStop= entry.actual;
122 tabDiff= 0;
123 entry.actual= entry.session + tabDiff;
124 }
125 } else {
126 entry.actual = entry.session;
127 }
128 tabDiff+= actDiff;
129 entry.session = -1;
130} }
131
132}}} // namespace [alib::strings::util]
#define A_CHAR(STR)
TAString & DeleteEnd(integer regionLength)
TAString & _(const TAppendable &src)
constexpr integer Length() const
Definition string.hpp:300
constexpr bool IsEmpty() const
Definition string.hpp:349
bool EndsWith(const TString &needle) const
Definition string.hpp:764
integer LastIndexOf(TChar needle, integer startIndex=MAX_LEN) const
Definition string.hpp:909
bool ConsumeInt(std::integral auto &result, TNumberFormat< TChar > *numberFormat=nullptr)
TSubstring & Trim(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
integer Actual(Types type, integer requestedSize, integer growthPadding)
Definition autosizes.cpp:4
void Export(AString &target)
Definition autosizes.cpp:38
bool dirty
Determines whether any value was changed sincel last #".Reset".
Definition autosizes.hpp:86
void Import(const String &source, lang::CurrentData session=lang::CurrentData::Clear)
Definition autosizes.cpp:66
std::vector< Entry > data
The current and measured sizes.
Definition autosizes.hpp:83
Types
The entry type, tab stop or field width.
Definition autosizes.hpp:54
@ Field
denotes a field width entry.
Definition autosizes.hpp:56
@ Tabstop
denotes a tab stop entry.
Definition autosizes.hpp:55
TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:4
@ Clear
Chooses to clear existing data.
@ Trim
Trim whitespaces away.
Definition alox.cpp:14
strings::util::TTokenizer< character > Tokenizer
Type alias in namespace #"%alib".
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TSubstring< character > Substring
Type alias in namespace #"%alib".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".