ALib C++ Library
Library Version: 2511 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
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( customFormatString )
42, formatString ( customFormatString ) {
43 if(!formatter.Get())
45
46 integer parsePos= 0;
47 while(parsePos < formatString.Length() ) {
48 // find next parse position
49 integer parsePosCopy= parsePos;
50 if( (parsePos= formatString.IndexOf( ESCCharacter, parsePosCopy ) ) < 0 )
51 break;
52
53 String identifier;
54 integer endPos= parsePos+ 1;
55 if( endPos < formatString.Length() ) {
56 // double Escape character? -> replace to single!
57 if( formatString[endPos] == ESCCharacter ) {
58 formatString.Delete( endPos, 1 );
59 ++parsePos;
60 continue;
61 }
62
63 // find end of Identifier
64 while( endPos < formatString.Length() && isalpha( formatString[endPos] ) )
65 ++endPos;
66
67 identifier= formatString.Substring<NC>( parsePos + 1, endPos - parsePos - 1 );
68 }
69
70 // not found
71 if( identifier.IsEmpty() )
72 continue;
73
74 // add callback
75 auto entryIt= propertyTable.begin();
76 while( entryIt != propertyTable.end() ) {
77 if( Substring(identifier).ConsumePartOf<lang::Case::Ignore>( entryIt->Name,
78 entryIt->MinimumRecognitionLength
79 ) == identifier.Length() )
80 {
81 // remove identifier from format string
82 formatString.Delete( parsePos, endPos - parsePos + (formatString[endPos] == ESCCharacter ? 1 : 0) );
83
84 // store callback
85 callBacks.emplace_back( &*entryIt );
86 ++parsePos;
87 break;
88 }
89 ++entryIt;
90 }
91
92 // identifier not found?
93 if( entryIt == propertyTable.end() ) {
94 Exception e( ALIB_CALLER_NULLED, FMTExceptions::UnknownPropertyInFormatString,
95 ESCCharacter, identifier, customFormatString );
96 for( auto& row : propertyTable )
97 e.Back().Add( ESCCharacter, row.Name, ", " );
98 e.Back().back()= '.'; // replace the last comma
99
100 throw e;
101} } }
102
103
104void PropertyFormatter::Format( AString& target, const Box& src ) {
105 // string buffers
106 std::vector<String> heapStrings;
107 String128 localString;
109
110 // collect boxes
111 BoxesMA& results= stdFormatter->GetArgContainer();
112 results.Add( formatString );
113 for( auto& entry : callBacks ) {
114 Box argument= entry->Callback( src, localString );
115 if( localString.IsNotEmpty() ) {
116 heapStrings.emplace_back( localString );
117 argument= heapStrings.back();
118 }
119
120 results.Add ( argument );
121 localString.Reset();
122 }
123
124 // invoke ALib default formatter
125 try
126 {
127 stdFormatter->FormatArgs( target, results );
128 }
129 catch(Exception& e)
130 {
132 throw;
133 }
134
135 results.clear();
136}
137
138
139} // namespace [alib::format]
TBoxes & Add()
Definition boxes.inl:55
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:244
constexpr bool IsNotEmpty() const
Definition string.inl:369
#define ALIB_CALLER_NULLED
Definition alib.inl:1027
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:1149
exceptions::Exception Exception
Type alias in namespace alib.
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace alib.
Definition boxes.inl:193
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.inl:42
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
characters::character character
Type alias in namespace alib.