ALib C++ Library
Library Version: 2402 R1
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 !defined(ALIB_DOX)
10# if !defined(HPP_ALIB_LANG_FORMAT_PROPERTY_FORMATTER)
12# endif
13# if !defined(HPP_ALIB_LANG_FORMAT_EXCEPTIONS)
15# endif
16#endif // !defined(ALIB_DOX)
17
18namespace alib::lang::format {
19
21 const TCallbackTable& propertyTable,
22 SPFormatter formatter,
23 character ESCCharacter )
24: stdFormatter ( formatter )
25, propertyFormatString( customFormatString )
26, formatString ( customFormatString )
27{
28 if(!formatter.get())
30
31 integer parsePos= 0;
32 while(parsePos < formatString.Length() )
33 {
34 // find next parse position
35 integer parsePosCopy= parsePos;
36 if( (parsePos= formatString.IndexOf( ESCCharacter, parsePosCopy ) ) < 0 )
37 break;
38
39 String identifier;
40 integer endPos= parsePos+ 1;
41 if( endPos < formatString.Length() )
42 {
43 // double Escape character? -> replace to single!
44 if( formatString[endPos] == ESCCharacter )
45 {
46 formatString.Delete( endPos, 1 );
47 ++parsePos;
48 continue;
49 }
50
51 // find end of Identifier
52 while( endPos < formatString.Length() && isalpha( formatString[endPos] ) )
53 ++endPos;
54
55 identifier= formatString.Substring<false>( parsePos + 1, endPos - parsePos - 1 );
56 }
57
58 // not found
59 if( identifier.IsEmpty() )
60 continue;
61
62 // add callback
63 auto entryIt= propertyTable.begin();
64 while( entryIt != propertyTable.end() )
65 {
66 if( Substring(identifier).ConsumePartOf<lang::Case::Ignore>( entryIt->Name,
67 entryIt->MinimumRecognitionLength
68 ) == identifier.Length() )
69 {
70 // remove identifier from format string
71 formatString.Delete( parsePos, endPos - parsePos + (formatString[endPos] == ESCCharacter ? 1 : 0) );
72
73 // store callback
74 callBacks.emplace_back( &*entryIt );
75 ++parsePos;
76 break;
77 }
78 ++entryIt;
79 }
80
81 // identifier not found?
82 if( entryIt == propertyTable.end() )
83 {
85 ESCCharacter, identifier, customFormatString );
86 for( auto& row : propertyTable )
87 e.Back().Add( ESCCharacter, row.Name, ", " );
88 e.Back().back()= '.'; // replace the last comma
89
90 throw e;
91 }
92 }
93}
94
95
96void PropertyFormatter::Format( AString& target, const Box& src )
97{
98 // string buffers
99 std::vector<String> heapStrings;
100 String128 localString;
102
103 // collect boxes
104 Boxes& results= stdFormatter->Acquire(ALIB_CALLER_PRUNED);
105 results.Add( formatString );
106 for( auto& entry : callBacks )
107 {
108 Box argument= entry->Callback( src, localString );
109 if( localString.IsNotEmpty() )
110 {
111 heapStrings.emplace_back( localString );
112 argument= heapStrings.back();
113 }
114
115 results.Add ( argument );
116 localString.Reset();
117 }
118
119 // invoke ALib default formatter
120 try
121 {
122 stdFormatter->FormatArgs( target, results );
123 }
124 catch(Exception& e)
125 {
127 throw;
128 }
129
130 stdFormatter->Release();
131 results.clear();
132}
133
134
135} // namespace [alib::lang::format]
ALIB_API Message & Back() const
Exception & Add(const NCString &file, int line, const NCString &func, TEnum type, TArgs &&... args)
static SPFormatter GetDefault()
ALIB_API PropertyFormatter(const String customFormatString, const TCallbackTable &propertyTable, SPFormatter formatter=nullptr, character ESCCharacter='@')
std::vector< IdentifierEntry > TCallbackTable
ALIB_API void Format(AString &target, const Box &src)
TAString & Delete(integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1490
void DbgDisableBufferReplacementWarning()
Definition astring.hpp:353
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:889
constexpr bool IsEmpty() const
Definition string.hpp:414
constexpr bool IsNotEmpty() const
Definition string.hpp:420
constexpr integer Length() const
Definition string.hpp:357
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:314
integer ConsumePartOf(const TString< TChar > &consumable, int minChars=1)
#define ALIB_CALLER_NULLED
Definition alib.hpp:846
#define ALIB_CALLER_PRUNED
Definition alib.hpp:845
platform_specific integer
Definition integers.hpp:50
strings::TSubstring< character > Substring
Type alias in namespace alib.
std::shared_ptr< lang::format::Formatter > SPFormatter
characters::character character
Type alias in namespace alib.