ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
propertyformatter.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 !DOXYGEN
12#endif // !DOXYGEN
13
14namespace alib::lang::format {
15
17 const TCallbackTable& propertyTable,
18 SPFormatter formatter,
19 character ESCCharacter )
20: stdFormatter ( formatter )
21, propertyFormatString( customFormatString )
22, formatString ( customFormatString )
23{
24 if(!formatter.Get())
26
27 integer parsePos= 0;
28 while(parsePos < formatString.Length() )
29 {
30 // find next parse position
31 integer parsePosCopy= parsePos;
32 if( (parsePos= formatString.IndexOf( ESCCharacter, parsePosCopy ) ) < 0 )
33 break;
34
35 String identifier;
36 integer endPos= parsePos+ 1;
37 if( endPos < formatString.Length() )
38 {
39 // double Escape character? -> replace to single!
40 if( formatString[endPos] == ESCCharacter )
41 {
42 formatString.Delete( endPos, 1 );
43 ++parsePos;
44 continue;
45 }
46
47 // find end of Identifier
48 while( endPos < formatString.Length() && isalpha( formatString[endPos] ) )
49 ++endPos;
50
51 identifier= formatString.Substring<NC>( parsePos + 1, endPos - parsePos - 1 );
52 }
53
54 // not found
55 if( identifier.IsEmpty() )
56 continue;
57
58 // add callback
59 auto entryIt= propertyTable.begin();
60 while( entryIt != propertyTable.end() )
61 {
62 if( Substring(identifier).ConsumePartOf<lang::Case::Ignore>( entryIt->Name,
63 entryIt->MinimumRecognitionLength
64 ) == identifier.Length() )
65 {
66 // remove identifier from format string
67 formatString.Delete( parsePos, endPos - parsePos + (formatString[endPos] == ESCCharacter ? 1 : 0) );
68
69 // store callback
70 callBacks.emplace_back( &*entryIt );
71 ++parsePos;
72 break;
73 }
74 ++entryIt;
75 }
76
77 // identifier not found?
78 if( entryIt == propertyTable.end() )
79 {
80 Exception e( ALIB_CALLER_NULLED, FMTExceptions::UnknownPropertyInFormatString,
81 ESCCharacter, identifier, customFormatString );
82 for( auto& row : propertyTable )
83 e.Back().Add( ESCCharacter, row.Name, ", " );
84 e.Back().back()= '.'; // replace the last comma
85
86 throw e;
87 }
88 }
89}
90
91
92void PropertyFormatter::Format( AString& target, const Box& src )
93{
94 // string buffers
95 std::vector<String> heapStrings;
96 String128 localString;
98
99 // collect boxes
100 BoxesMA& results= stdFormatter->GetArgContainer();
101 results.Add( formatString );
102 for( auto& entry : callBacks )
103 {
104 Box argument= entry->Callback( src, localString );
105 if( localString.IsNotEmpty() )
106 {
107 heapStrings.emplace_back( localString );
108 argument= heapStrings.back();
109 }
110
111 results.Add ( argument );
112 localString.Reset();
113 }
114
115 // invoke ALib default formatter
116 try
117 {
118 stdFormatter->FormatArgs( target, results );
119 }
120 catch(Exception& e)
121 {
122 e.Add( ALIB_CALLER_NULLED, FMTExceptions::ErrorInResultingFormatString, propertyFormatString );
123 throw;
124 }
125
126 results.clear();
127}
128
129
130} // namespace [alib::lang::format]
131
TBoxes & Add()
Definition boxes.inl:74
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
static ALIB_API SPFormatter Default
AString formatString
The resulting format string passed to ALib formatters.
ALIB_API PropertyFormatter(const String customFormatString, const TCallbackTable &propertyTable, SPFormatter formatter=nullptr, character ESCCharacter='@')
std::vector< IdentifierEntry > TCallbackTable
Type definition of the callback table.
ALIB_API void Format(AString &target, const Box &src)
SPFormatter stdFormatter
The formatter (as given in the constructor).
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:363
constexpr bool IsNotEmpty() const
Definition string.hpp:389
constexpr integer Length() const
Definition string.hpp:326
#define ALIB_CALLER_NULLED
Definition alib.hpp:1173
platform_specific integer
Definition integers.hpp:43
characters::character character
Type alias in namespace alib.