ALib C++ Library
Library Version: 2511 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
18 module ALib.Variables.Plugins;
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//##################################################################################################
45
47 #if ALIB_CAMP
48 return alib::BASECAMP.GetResource( "CFGPlgCLI" );
49 #else
50 return A_CHAR("Command Line Arguments");
51 #endif
52}
53
54bool CLIVariablesPlugin::Get( const String& pName, AString& target ) {
55 int argC = alib::ARG_C;
56 bool isWide;
57 const void** argV = (isWide= (alib::ARG_VN == nullptr)) ? reinterpret_cast<const void**>(alib::ARG_VW)
58 : reinterpret_cast<const void**>(alib::ARG_VN);
59
60 String256 name(pName); name.SearchAndReplace( '/', '_');
61 Substring varNameWithoutCategory= nullptr;
62
63 // check if a default category hits
64 for (auto& defaultCategory : DefaultCategories )
65 if( name.StartsWith( defaultCategory ) ) {
66 varNameWithoutCategory= name.Substring(defaultCategory.Length());
67 varNameWithoutCategory.ConsumeChar('_');
68 break;
69 }
70 String256 stringConverter;
71 stringConverter.DbgDisableBufferReplacementWarning();
72
73
74 auto argsIt= AlternativeArgs.begin();
75 if( !AlternativeArgs.empty() ) {
76 argC = int(AlternativeArgs.size());
77 }
78
79 for ( int i= !AlternativeArgs.empty() ? 0 : 1 ; i < argC ; ++i ) {
80 // create substring on actual variable (trim if somebody would work with quotation marks...)
81 Substring cliArg;
82
83 if( !AlternativeArgs.empty() ) {
84 cliArg= *argsIt;
85 ++argsIt;
86 } else {
87 if (!isWide) {
89 if constexpr (!std::same_as<character, char>) {
90 stringConverter.Reset( reinterpret_cast<const char**>(argV)[i] );
91 cliArg= stringConverter;
92 }
93 else
94 cliArg= reinterpret_cast<const character**>(argV)[i];
95 } else {
97 if constexpr (!std::same_as<character, wchar_t>) {
98 stringConverter.Reset( reinterpret_cast<const wchar_t**>(argV)[i] );
99 cliArg= stringConverter;
100 }
101 else
102 cliArg= reinterpret_cast<const character**>(argV)[i];
104 }
106 }
107
108 cliArg.Trim();
109
110 // consume and count hyphens, and check with our recognition settings.
111 int qtyHyphens= 0;
112 while( cliArg.ConsumeChar('-') )
113 ++qtyHyphens;
114 if( qtyHyphens < QtyMandatoryHyphens
115 || qtyHyphens > QtyOptionalHyphens )
116 continue;
117
118 // try names
119 if ( ! cliArg.ConsumeString<lang::Case::Ignore>( name )
120 && !( varNameWithoutCategory.IsNotEmpty() && cliArg.ConsumeString<lang::Case::Ignore>( varNameWithoutCategory ) )
121 )
122 continue; // next arg
123
124 if ( cliArg.Trim().IsEmpty() ) // we return "yes, found!" even when no value is set.
125 return true;
126 if ( cliArg.ConsumeChar<CHK, lang::Whitespaces::Keep>() == '=' ) {
127 target.Reset(cliArg.Trim());
128 return true;
129 } }
130
131 return false;
132}
133
134//##################################################################################################
135// EnvironmentVariablesPlugin
136//##################################################################################################
141
143 #if ALIB_CAMP
144 return alib::BASECAMP.GetResource( "CFGPlgEnv" );
145 #else
146 return A_CHAR("Environment Variables");
147 #endif
148}
149
150bool EnvironmentVariablesPlugin::Get( const String& pName, AString& target ) {
151 String256 result;
153
154 String256 varName(pName);
155 varName.SearchAndReplace( '/', '_');
156 Substring varNameWithoutCategory= nullptr;
157
158
159 // check for full name
161 if ( result.IsNotEmpty() ) {
162 target.Reset(result);
163 return true;
164 }
165
166 // check if a default category hits
167 for (auto& defaultCategory : DefaultCategories )
168 if( varName.StartsWith( defaultCategory ) ) {
169 varNameWithoutCategory= varName.Substring(defaultCategory.Length());
170 varNameWithoutCategory.ConsumeChar('_');
171 EnvironmentVariables::Get( String256(varNameWithoutCategory), result, lang::CurrentData::Keep );
172 if ( result.IsNotEmpty() ) {
173 target.Reset(result);
174 return true;
175 } }
176
177 // nothing found
178 return false;
179}
180
181}} // namespace [alib::variables]
ALIB_DLL integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0, integer endIdx=strings::MAX_LEN)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:244
constexpr bool IsEmpty() const
Definition string.inl:365
constexpr bool IsNotEmpty() const
Definition string.inl:369
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:384
bool StartsWith(const TString &needle) const
Definition string.inl:751
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:46
virtual ALIB_DLL bool Get(const String &name, AString &target) override
Definition plugins.cpp:54
ALIB_DLL EnvironmentVariablesPlugin(MonoAllocator &ma, Priority pPriority=Priority::Environment)
Definition plugins.cpp:137
virtual ALIB_DLL bool Get(const String &name, AString &target) override
Definition plugins.cpp:150
virtual ALIB_DLL String Name() const override
Definition plugins.cpp:142
#define A_CHAR(STR)
#define ALIB_WARNINGS_RESTORE
Definition alib.inl:719
#define ALIB_WARNINGS_ALLOW_UNREACHABLE_CODE
Definition alib.inl:622
@ 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:2189
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)