ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
propertyformatter.cpp
1// #################################################################################################
2// ALib C++ Library
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 "Symbol 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
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( customFormatString )
42, formatString ( customFormatString )
43{
44 if(!formatter.Get())
46
47 integer parsePos= 0;
48 while(parsePos < formatString.Length() )
49 {
50 // find next parse position
51 integer parsePosCopy= parsePos;
52 if( (parsePos= formatString.IndexOf( ESCCharacter, parsePosCopy ) ) < 0 )
53 break;
54
55 String identifier;
56 integer endPos= parsePos+ 1;
57 if( endPos < formatString.Length() )
58 {
59 // double Escape character? -> replace to single!
60 if( formatString[endPos] == ESCCharacter )
61 {
62 formatString.Delete( endPos, 1 );
63 ++parsePos;
64 continue;
65 }
66
67 // find end of Identifier
68 while( endPos < formatString.Length() && isalpha( formatString[endPos] ) )
69 ++endPos;
70
71 identifier= formatString.Substring<NC>( parsePos + 1, endPos - parsePos - 1 );
72 }
73
74 // not found
75 if( identifier.IsEmpty() )
76 continue;
77
78 // add callback
79 auto entryIt= propertyTable.begin();
80 while( entryIt != propertyTable.end() )
81 {
82 if( Substring(identifier).ConsumePartOf<lang::Case::Ignore>( entryIt->Name,
83 entryIt->MinimumRecognitionLength
84 ) == identifier.Length() )
85 {
86 // remove identifier from format string
87 formatString.Delete( parsePos, endPos - parsePos + (formatString[endPos] == ESCCharacter ? 1 : 0) );
88
89 // store callback
90 callBacks.emplace_back( &*entryIt );
91 ++parsePos;
92 break;
93 }
94 ++entryIt;
95 }
96
97 // identifier not found?
98 if( entryIt == propertyTable.end() )
99 {
100 Exception e( ALIB_CALLER_NULLED, FMTExceptions::UnknownPropertyInFormatString,
101 ESCCharacter, identifier, customFormatString );
102 for( auto& row : propertyTable )
103 e.Back().Add( ESCCharacter, row.Name, ", " );
104 e.Back().back()= '.'; // replace the last comma
105
106 throw e;
107 }
108 }
109}
110
111
112void PropertyFormatter::Format( AString& target, const Box& src )
113{
114 // string buffers
115 std::vector<String> heapStrings;
116 String128 localString;
118
119 // collect boxes
120 BoxesMA& results= stdFormatter->GetArgContainer();
121 results.Add( formatString );
122 for( auto& entry : callBacks )
123 {
124 Box argument= entry->Callback( src, localString );
125 if( localString.IsNotEmpty() )
126 {
127 heapStrings.emplace_back( localString );
128 argument= heapStrings.back();
129 }
130
131 results.Add ( argument );
132 localString.Reset();
133 }
134
135 // invoke ALib default formatter
136 try
137 {
138 stdFormatter->FormatArgs( target, results );
139 }
140 catch(Exception& e)
141 {
143 throw;
144 }
145
146 results.clear();
147}
148
149
150} // namespace [alib::format]
151
TBoxes & Add()
Definition boxes.inl:61
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
static ALIB_DLL 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).
std::vector< IdentifierEntry > TCallbackTable
Type definition of the callback table.
ALIB_DLL PropertyFormatter(const String customFormatString, const TCallbackTable &propertyTable, SPFormatter formatter=nullptr, character ESCCharacter='@')
AString propertyFormatString
The original format string. Used only for exception information.
ALIB_DLL void Format(AString &target, const Box &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:245
constexpr bool IsNotEmpty() const
Definition string.inl:371
#define ALIB_CALLER_NULLED
Definition alib.inl:1010
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
LocalString< 128 > String128
Type alias name for TLocalString<character,128>.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
exceptions::Exception Exception
Type alias in namespace alib.
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace alib.
Definition boxes.inl:245
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.inl:42
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
characters::character character
Type alias in namespace alib.