ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
plugins.cpp
1namespace alib { namespace variables {
2
3
4//##################################################################################################
5// CLIVariablesPlugin
6//##################################################################################################
11
13 #if ALIB_CAMP
14 return alib::BASECAMP.GetResource( "CFGPlgCLI" );
15 #else
16 return A_CHAR("Command Line Arguments");
17 #endif
18}
19
20bool CLIVariablesPlugin::Get( const String& pName, AString& target ) {
21 int argC = alib::ARG_C;
22 bool isWide;
23 const void** argV = (isWide= (alib::ARG_VN == nullptr)) ? reinterpret_cast<const void**>(alib::ARG_VW)
24 : reinterpret_cast<const void**>(alib::ARG_VN);
25
26 String256 name(pName); name.SearchAndReplace( '/', '_');
27 Substring varNameWithoutCategory= nullptr;
28
29 // check if a default category hits
30 for (auto& defaultCategory : DefaultCategories )
31 if( name.StartsWith( defaultCategory ) ) {
32 varNameWithoutCategory= name.Substring(defaultCategory.Length());
33 varNameWithoutCategory.ConsumeChar('_');
34 break;
35 }
36 String256 stringConverter;
37 stringConverter.DbgDisableBufferReplacementWarning();
38
39
40 auto argsIt= AlternativeArgs.begin();
41 if( !AlternativeArgs.empty() ) {
42 argC = int(AlternativeArgs.size());
43 }
44
45 for ( int i= !AlternativeArgs.empty() ? 0 : 1 ; i < argC ; ++i ) {
46 // create substring on actual variable (trim if somebody would work with quotation marks...)
47 Substring cliArg;
48
49 if( !AlternativeArgs.empty() ) {
50 cliArg= *argsIt;
51 ++argsIt;
52 } else {
53 if (!isWide) {
55 if constexpr (!std::same_as<character, char>) {
56 stringConverter.Reset( reinterpret_cast<const char**>(argV)[i] );
57 cliArg= stringConverter;
58 }
59 else
60 cliArg= reinterpret_cast<const character**>(argV)[i];
61 } else {
63 if constexpr (!std::same_as<character, wchar_t>) {
64 stringConverter.Reset( reinterpret_cast<const wchar_t**>(argV)[i] );
65 cliArg= stringConverter;
66 }
67 else
68 cliArg= reinterpret_cast<const character**>(argV)[i];
70 }
72 }
73
74 cliArg.Trim();
75
76 // consume and count hyphens, and check with our recognition settings.
77 int qtyHyphens= 0;
78 while( cliArg.ConsumeChar('-') )
79 ++qtyHyphens;
80 if( qtyHyphens < QtyMandatoryHyphens
81 || qtyHyphens > QtyOptionalHyphens )
82 continue;
83
84 // try names
85 if ( ! cliArg.ConsumeString<lang::Case::Ignore>( name )
86 && !( varNameWithoutCategory.IsNotEmpty() && cliArg.ConsumeString<lang::Case::Ignore>( varNameWithoutCategory ) )
87 )
88 continue; // next arg
89
90 if ( cliArg.Trim().IsEmpty() ) // we return "yes, found!" even when no value is set.
91 return true;
92 if ( cliArg.ConsumeChar<CHK, lang::Whitespaces::Keep>() == '=' ) {
93 target.Reset(cliArg.Trim());
94 return true;
95 } }
96
97 return false;
98}
99
100//##################################################################################################
101// EnvironmentVariablesPlugin
102//##################################################################################################
107
109 #if ALIB_CAMP
110 return alib::BASECAMP.GetResource( "CFGPlgEnv" );
111 #else
112 return A_CHAR("Environment Variables");
113 #endif
114}
115
116bool EnvironmentVariablesPlugin::Get( const String& pName, AString& target ) {
117 String256 result;
119
120 String256 varName(pName);
121 varName.SearchAndReplace( '/', '_');
122 Substring varNameWithoutCategory= nullptr;
123
124
125 // check for full name
127 if ( result.IsNotEmpty() ) {
128 target.Reset(result);
129 return true;
130 }
131
132 // check if a default category hits
133 for (auto& defaultCategory : DefaultCategories )
134 if( varName.StartsWith( defaultCategory ) ) {
135 varNameWithoutCategory= varName.Substring(defaultCategory.Length());
136 varNameWithoutCategory.ConsumeChar('_');
137 EnvironmentVariables::Get( String256(varNameWithoutCategory), result, lang::CurrentData::Keep );
138 if ( result.IsNotEmpty() ) {
139 target.Reset(result);
140 return true;
141 } }
142
143 // nothing found
144 return false;
145}
146
147}} // namespace [alib::variables]
#define A_CHAR(STR)
#define ALIB_ALLOW_UNREACHABLE_CODE
#define ALIB_POP_ALLOWANCE
integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0, integer endIdx=strings::MAX_LEN)
void DbgDisableBufferReplacementWarning()
Definition tastring.hpp:236
constexpr bool IsEmpty() const
Definition string.hpp:349
constexpr bool IsNotEmpty() const
Definition string.hpp:353
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:368
bool StartsWith(const TString &needle) const
Definition string.hpp:735
bool ConsumeString(const TString< TChar > &consumable)
TSubstring & Trim(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
CLIVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::CLI)
Definition plugins.cpp:7
virtual String Name() const override
Definition plugins.cpp:12
virtual bool Get(const String &name, AString &target) override
Definition plugins.cpp:20
EnvironmentVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::Environment)
Definition plugins.cpp:103
virtual bool Get(const String &name, AString &target) override
Definition plugins.cpp:116
virtual String Name() const override
Definition plugins.cpp:108
@ Keep
Chooses not no clear existing data.
@ Keep
Keep whitespaces in string.
Definition alox.cpp:14
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
const wchar_t ** ARG_VW
Definition mainargs.cpp:4
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TSubstring< character > Substring
Type alias in namespace #"%alib".
camp::Basecamp BASECAMP
The singleton instance of ALib Camp class #"Basecamp".
Definition basecamp.cpp:2
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
characters::character character
Type alias in namespace #"%alib".
int ARG_C
Definition mainargs.cpp:2
const char ** ARG_VN
Definition mainargs.cpp:3
See sibling type #"NC".
Definition chk_nc.hpp:30
static bool Get(const CString &varName, AString &target, lang::CurrentData targetData=lang::CurrentData::Clear)