ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
plugins.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 ======================================
16// =========================================== Module ==========================================
17#if ALIB_C20_MODULES
19 import ALib.Lang;
20# if ALIB_VARIABLES
21 import ALib.Variables;
22# endif
23# if ALIB_SYSTEM
24 import ALib.System;
25# endif
26 import ALib.Camp.Base;
27#else
28# include "ALib.Lang.H"
29# include "ALib.Variables.H"
30# include "ALib.System.H"
31# include "ALib.Camp.Base.H"
33#endif
34// ====================================== Implementation =======================================
35namespace alib { namespace variables {
36
37
38// #################################################################################################
39// CLIVariablesPlugin
40// #################################################################################################
46
48{
49 #if ALIB_CAMP
50 return alib::BASECAMP.GetResource( "CFGPlgCLI" );
51 #else
52 return A_CHAR("Command Line Arguments");
53 #endif
54}
55
56bool CLIVariablesPlugin::Get( const String& pName, AString& target )
57{
58 int argC = alib::ARG_C;
59 bool isWide;
60 const void** argV = (isWide= (alib::ARG_VN == nullptr)) ? reinterpret_cast<const void**>(alib::ARG_VW)
61 : reinterpret_cast<const void**>(alib::ARG_VN);
62
63 String256 name(pName); name.SearchAndReplace( '/', '_');
64 Substring varNameWithoutCategory= nullptr;
65
66 // check if a default category hits
67 for (auto& defaultCategory : DefaultCategories )
68 if( name.StartsWith( defaultCategory ) )
69 {
70 varNameWithoutCategory= name.Substring(defaultCategory.Length());
71 varNameWithoutCategory.ConsumeChar('_');
72 break;
73 }
74 String256 stringConverter;
75 stringConverter.DbgDisableBufferReplacementWarning();
76
77
78 auto argsIt= AlternativeArgs.begin();
79 if( !AlternativeArgs.empty() )
80 {
81 argC = int(AlternativeArgs.size());
82 }
83
84 for ( int i= !AlternativeArgs.empty() ? 0 : 1 ; i < argC ; ++i )
85 {
86 // create substring on actual variable (trim if somebody would work with quotation marks...)
87 Substring cliArg;
88
89 if( !AlternativeArgs.empty() )
90 {
91 cliArg= *argsIt;
92 ++argsIt;
93 }
94 else
95 {
96 if (!isWide)
97 {
99 if constexpr (!std::same_as<character, char>)
100 {
101 stringConverter.Reset( reinterpret_cast<const char**>(argV)[i] );
102 cliArg= stringConverter;
103 }
104 else
105 cliArg= reinterpret_cast<const character**>(argV)[i];
106 }
107 else
108 {
110 if constexpr (!std::same_as<character, wchar_t>)
111 {
112 stringConverter.Reset( reinterpret_cast<const wchar_t**>(argV)[i] );
113 cliArg= stringConverter;
114 }
115 else
116 cliArg= reinterpret_cast<const character**>(argV)[i];
118 }
120 }
121
122 cliArg.Trim();
123
124 // consume and count hyphens, and check with our recognition settings.
125 int qtyHyphens= 0;
126 while( cliArg.ConsumeChar('-') )
127 ++qtyHyphens;
128 if( qtyHyphens < QtyMandatoryHyphens
129 || qtyHyphens > QtyOptionalHyphens )
130 continue;
131
132 // try names
133 if ( ! cliArg.ConsumeString<lang::Case::Ignore>( name )
134 && !( varNameWithoutCategory.IsNotEmpty() && cliArg.ConsumeString<lang::Case::Ignore>( varNameWithoutCategory ) )
135 )
136 continue; // next arg
137
138 if ( cliArg.Trim().IsEmpty() ) // we return "yes, found!" even when no value is set.
139 return true;
140 if ( cliArg.ConsumeChar<CHK, lang::Whitespaces::Keep>() == '=' )
141 {
142 target.Reset(cliArg.Trim());
143 return true;
144 }
145 }
146
147 return false;
148}
149
150// #################################################################################################
151// EnvironmentVariablesPlugin
152// #################################################################################################
158
160{
161 #if ALIB_CAMP
162 return alib::BASECAMP.GetResource( "CFGPlgEnv" );
163 #else
164 return A_CHAR("Environment Variables");
165 #endif
166}
167
168bool EnvironmentVariablesPlugin::Get( const String& pName, AString& target )
169{
170 String256 result;
172
173 String256 varName(pName);
174 varName.SearchAndReplace( '/', '_');
175 Substring varNameWithoutCategory= nullptr;
176
177
178 // check for full name
180 if ( result.IsNotEmpty() )
181 {
182 target.Reset(result);
183 return true;
184 }
185
186 // check if a default category hits
187 for (auto& defaultCategory : DefaultCategories )
188 if( varName.StartsWith( defaultCategory ) )
189 {
190 varNameWithoutCategory= varName.Substring(defaultCategory.Length());
191 varNameWithoutCategory.ConsumeChar('_');
192 EnvironmentVariables::Get( String256(varNameWithoutCategory), result, lang::CurrentData::Keep );
193 if ( result.IsNotEmpty() )
194 {
195 target.Reset(result);
196 return true;
197 }
198 }
199
200 // nothing found
201 return false;
202}
203
204}} // namespace [alib::variables]
ALIB_DLL integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0, integer endIdx=strings::MAX_LEN)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:245
constexpr bool IsEmpty() const
Definition string.inl:367
constexpr bool IsNotEmpty() const
Definition string.inl:371
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:386
bool StartsWith(const TString &needle) const
Definition string.inl:772
bool ConsumeString(const TString< TChar > &consumable)
TSubstring & Trim(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
ALIB_DLL CLIVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::CLI)
Definition plugins.cpp:41
virtual ALIB_DLL String Name() const override
Definition plugins.cpp:47
virtual ALIB_DLL bool Get(const String &name, AString &target) override
Definition plugins.cpp:56
ALIB_DLL EnvironmentVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::Environment)
Definition plugins.cpp:153
virtual ALIB_DLL bool Get(const String &name, AString &target) override
Definition plugins.cpp:168
virtual ALIB_DLL String Name() const override
Definition plugins.cpp:159
#define A_CHAR(STR)
#define ALIB_WARNINGS_RESTORE
Definition alib.inl:705
#define ALIB_WARNINGS_ALLOW_UNREACHABLE_CODE
Definition alib.inl:612
@ Keep
Chooses not no clear existing data.
@ Keep
Keep whitespaces in string.
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
camp::Basecamp BASECAMP
The singleton instance of ALib Camp class Basecamp.
Definition basecamp.cpp:81
const wchar_t ** ARG_VW
Definition mainargs.cpp:25
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
characters::character character
Type alias in namespace alib.
strings::TSubstring< character > Substring
Type alias in namespace alib.
const char ** ARG_VN
Definition mainargs.cpp:24
int ARG_C
Definition mainargs.cpp:23
See sibling type NC.
Definition chk_nc.inl:33
static ALIB_DLL bool Get(const CString &varName, AString &target, lang::CurrentData targetData=lang::CurrentData::Clear)