ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
propertyformatter.cpp
1//##################################################################################################
2// ALib C++ Framework
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 "Configuration MACRO 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 ========================================
15#include <vector>
16#include "alib/alib.inl"
17//============================================== Module ============================================
18#if ALIB_C20_MODULES
19 module ALib.Format.PropertyFormatter;
20 import ALib.Lang;
21 import ALib.Strings;
22 import ALib.Exceptions;
23# if ALIB_CAMP
24 import ALib.Camp.Base;
25# endif
26#else
27# include "ALib.Lang.H"
28# include "ALib.Strings.H"
29# include "ALib.Exceptions.H"
31# include "ALib.Camp.Base.H"
32#endif
33//========================================== Implementation ========================================
34namespace alib::format {
35
37 const TCallbackTable& propertyTable,
38 SPFormatter formatter,
39 character ESCCharacter )
40: stdFormatter ( formatter )
41, propertyFormatString( customFormatStringx ) {
42 if(!formatter.Get())
44
46
47 integer parsePos= 0;
48 while(parsePos < formatString.Length() ) {
49 // find next parse position
50 integer parsePosCopy= parsePos;
51 if( (parsePos= formatString.IndexOf( ESCCharacter, parsePosCopy ) ) < 0 )
52 break;
53
54 String identifier;
55 integer endPos= parsePos+ 1;
56 if( endPos < formatString.Length() ) {
57 // double Escape character? -> replace to single!
58 if( formatString[endPos] == ESCCharacter ) {
59 formatString.Delete( endPos, 1 );
60 ++parsePos;
61 continue;
62 }
63
64 // find end of Identifier
65 while( endPos < formatString.Length() && isalpha( formatString[endPos] ) )
66 ++endPos;
67
68 identifier= formatString.Substring<NC>( parsePos + 1, endPos - parsePos - 1 );
69 }
70
71 // not found
72 if( identifier.IsEmpty() )
73 continue;
74
75 // add callback
76 auto entryIt= propertyTable.begin();
77 while( entryIt != propertyTable.end() ) {
78 if( Substring(identifier).ConsumePartOf<lang::Case::Ignore>( entryIt->Name,
79 entryIt->MinimumRecognitionLength
80 ) == identifier.Length() )
81 {
82 // remove identifier from format string
83 formatString.Delete( parsePos, endPos - parsePos + (formatString[endPos] == ESCCharacter ? 1 : 0) );
84
85 // store callback
86 callBacks.emplace_back( &*entryIt );
87 ++parsePos;
88 break;
89 }
90 ++entryIt;
91 }
92
93 // identifier not found?
94 if( entryIt == propertyTable.end() ) {
95 Exception e( ALIB_CALLER_NULLED, FMTExceptions::UnknownPropertyInFormatString,
96 ESCCharacter, identifier, customFormatStringx );
97 for( auto& row : propertyTable )
98 e.Back().Add( ESCCharacter, row.Name, ", " );
99 e.Back().back()= '.'; // replace the last comma
100
101 throw e;
102} } }
103
104
105void PropertyFormatter::Format( AString& target, const Box& src ) {
106 // string buffers
107 std::vector<String> heapStrings;
108 String128 localString;
110
111 // collect boxes
112 BoxesMA& results= stdFormatter->GetArgContainer();
113 results.Add( formatString );
114 for( auto& entry : callBacks ) {
115 Box argument= entry->Callback( src, localString );
116 if( localString.IsNotEmpty() ) {
117 heapStrings.emplace_back( localString );
118 argument= heapStrings.back();
119 }
120
121 results.Add ( argument );
122 localString.Reset();
123 }
124
125 // invoke ALib default formatter
126 try
127 {
128 stdFormatter->FormatArgs( target, results );
129 }
130 catch(Exception& e)
131 {
133 throw;
134 }
135
136 results.clear();
137}
138
139
140} // namespace [alib::format]
#define ALIB_CALLER_NULLED
Definition alib.inl:1105
TBoxes & Add()
Definition boxes.inl:55
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
static SPFormatter DEFAULT
TCallbackResultTable callBacks
The callback functions to receive the format data.
AString formatString
The resulting format string passed to ALib formatters.
SPFormatter stdFormatter
The formatter (as given in the constructor).
PropertyFormatter(const String customFormatString, const TCallbackTable &propertyTable, SPFormatter formatter=nullptr, character ESCCharacter='@')
std::vector< IdentifierEntry > TCallbackTable
Type definition of the callback table.
AString propertyFormatString
The original format string. Used only for exception information.
void Format(AString &target, const Box &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:241
constexpr bool IsNotEmpty() const
Definition string.inl:357
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.inl:42
strings::util::StringEscaperStandard StringEscaperStandard
Type alias in namespace alib.
Definition escaper.inl:186
strings::TEscape< character > Escape
Type alias in namespace alib.
Definition format.inl:532
constexpr const String EMPTY_STRING
An empty string of the default character type.
Definition string.inl:2234
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1135
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
exceptions::Exception Exception
Type alias in namespace alib.
LocalString< 128 > String128
Type alias name for #"TLocalString;TLocalString<character,128>".
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace alib.
Definition boxes.inl:193
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.