ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
plugins.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
16#endif // !DOXYGEN
17
18namespace alib { namespace config {
19
20
21// #################################################################################################
22// CLIVariablesPlugin
23// #################################################################################################
25: ConfigurationPlugin( pPriority)
26, AlternativeArgs (ma)
27, DefaultCategories(ma)
28{}
29
31{
32 return alib::CONFIG.GetResource( "CfgPlgCLI" );
33}
34
35bool CLIVariablesPlugin::Get( const String& pName, AString& target )
36{
37 int argC = alib::ARG_C;
38 bool isWide;
39 const void** argV = (isWide= (alib::ARG_VN == nullptr)) ? reinterpret_cast<const void**>(alib::ARG_VW)
40 : reinterpret_cast<const void**>(alib::ARG_VN);
41
42 String256 name(pName); name.SearchAndReplace( '/', '_');
43 Substring varNameWithoutCategory= nullptr;
44
45 // check if a default category hits
46 for (auto& defaultCategory : DefaultCategories )
47 if( name.StartsWith( defaultCategory ) )
48 {
49 varNameWithoutCategory= name.Substring(defaultCategory.Length());
50 varNameWithoutCategory.ConsumeChar('_');
51 break;
52 }
53 String256 stringConverter;
54 stringConverter.DbgDisableBufferReplacementWarning();
55
56
57 auto argsIt= AlternativeArgs.begin();
58 if( !AlternativeArgs.empty() )
59 {
60 argC = int(AlternativeArgs.size());
61 }
62
63 for ( int i= !AlternativeArgs.empty() ? 0 : 1 ; i < argC ; ++i )
64 {
65 // create substring on actual variable (trim if somebody would work with quotation marks...)
66 Substring cliArg;
67
68 if( !AlternativeArgs.empty() )
69 {
70 cliArg= *argsIt;
71 ++argsIt;
72 }
73 else
74 {
76 if (!isWide)
77 {
78 if constexpr (!ATMP_EQ(character, char))
79 {
80 stringConverter.Reset( reinterpret_cast<const char**>(argV)[i] );
81 cliArg= stringConverter;
82 }
83 else
84 cliArg= reinterpret_cast<const character**>(argV)[i];
85 }
86 else
87 {
88 if constexpr (!ATMP_EQ(character, wchar_t))
89 {
90 stringConverter.Reset( reinterpret_cast<const wchar_t**>(argV)[i] );
91 cliArg= stringConverter;
92 }
93 else
94 cliArg= reinterpret_cast<const character**>(argV)[i];
95 }
97 }
98
99 cliArg.Trim();
100
101 // consume and count hyphens, and check with our recognition settings.
102 int qtyHyphens= 0;
103 while( cliArg.ConsumeChar('-') )
104 ++qtyHyphens;
105 if( qtyHyphens < QtyMandatoryHyphens
106 || qtyHyphens > QtyOptionalHyphens )
107 continue;
108
109 // try names
110 if ( ! cliArg.ConsumeString<lang::Case::Ignore>( name )
111 && !( varNameWithoutCategory.IsNotEmpty() && cliArg.ConsumeString<lang::Case::Ignore>( varNameWithoutCategory ) )
112 )
113 continue; // next arg
114
115 if ( cliArg.Trim().IsEmpty() ) // we return "yes, found!" even when no value is set.
116 return true;
117 if ( cliArg.ConsumeChar<CHK, lang::Whitespaces::Keep>() == '=' )
118 {
119 target.Reset(cliArg.Trim());
120 return true;
121 }
122 }
123
124 return false;
125}
126
127// #################################################################################################
128// EnvironmentVariablesPlugin
129// #################################################################################################
131 Priority pPriority )
132: ConfigurationPlugin( pPriority)
133, DefaultCategories( ma )
134{}
135
137{
138 return alib::CONFIG.GetResource( "CfgPlgEnv" );
139}
140
141bool EnvironmentVariablesPlugin::Get( const String& pName, AString& target )
142{
143 String256 result;
145
146 String256 varName(pName);
147 varName.SearchAndReplace( '/', '_');
148 Substring varNameWithoutCategory= nullptr;
149
150
151 // check for full name
153 if ( result.IsNotEmpty() )
154 {
155 target.Reset(result);
156 return true;
157 }
158
159 // check if a default category hits
160 for (auto& defaultCategory : DefaultCategories )
161 if( varName.StartsWith( defaultCategory ) )
162 {
163 varNameWithoutCategory= varName.Substring(defaultCategory.Length());
164 varNameWithoutCategory.ConsumeChar('_');
165 EnvironmentVariables::Get( String256(varNameWithoutCategory), result, lang::CurrentData::Keep );
166 if ( result.IsNotEmpty() )
167 {
168 target.Reset(result);
169 return true;
170 }
171 }
172
173 // nothing found
174 return false;
175}
176
177}} // namespace [alib::config]
virtual ALIB_API bool Get(const String &name, AString &target) override
Definition plugins.cpp:35
virtual ALIB_API String Name() const override
Definition plugins.cpp:30
ALIB_API CLIVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::CLI)
Definition plugins.cpp:24
virtual ALIB_API bool Get(const String &name, AString &target) override
Definition plugins.cpp:141
virtual ALIB_API String Name() const override
Definition plugins.cpp:136
ALIB_API EnvironmentVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::Environment)
Definition plugins.cpp:130
const String & GetResource(const NString &name)
ALIB_API integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0, integer endIdx=strings::MAX_LEN)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:363
constexpr bool IsEmpty() const
Definition string.hpp:383
constexpr bool IsNotEmpty() const
Definition string.hpp:389
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:406
bool StartsWith(const TString &needle) const
Definition string.hpp:820
bool ConsumeString(const TString< TChar > &consumable)
TSubstring & Trim(const TCString< TChar > &whiteSpaces=TT_CStringConstants< TChar >::DefaultWhitespaces())
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:849
#define ATMP_EQ( T, TEqual)
Definition tmp.hpp:27
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:760
@ Keep
Keep whitespaces in string.
@ Keep
Chooses not no clear existing data.
Definition alib.cpp:69
config::ConfigCamp CONFIG
The singleton instance of ALib Camp class ConfigCamp.
const char ** ARG_VN
Definition alib.cpp:72
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
int ARG_C
Definition alib.cpp:71
characters::character character
Type alias in namespace alib.
const wchar_t ** ARG_VW
Definition alib.cpp:73
See sibling type NC.
Definition alib.hpp:1097
static ALIB_API bool Get(const CString &varName, AString &target, lang::CurrentData targetData=lang::CurrentData::Clear)