Class EnvTokenExpander
This class is implemented as a very lean Singleton.
Inheritance
Namespace: WizardWrx.Core
Assembly: WizardWrx.Core.dll
Syntax
public class EnvTokenExpander : object
Fields
| Improve this Doc View SourceENV_STR_DLM
For reference, the character that delminits an environment string token is exposed as a public constant.
Declaration
public const char ENV_STR_DLM = null
Field Value
Type | Description |
---|---|
System.Char |
Properties
| Improve this Doc View SourceBusy
Returns TRUE if the EnvTokenExpander is Busy, which means that a thread has acquired it, by calling its Acquire method.
Declaration
public bool Busy { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Remarks
By acquiring the EnvTokenExpander, a thread can shave a tad off the processing needed to find the first occurrance of a token, in order to extract it, if it intends to call ContainsEnvToken before calling ExtractEnvToken.
Methods
| Improve this Doc View SourceAcquire()
Rather than have the caller lock the object, I provide this pair of methods that create a similar effect, by causing a competing caller of an object in a Frozen state to told to wait its turn.
Declaration
public bool Acquire()
Returns
Type | Description |
---|---|
System.Boolean | TRUE, unless the object is already frozen by another thread. |
ContainsEnvToken(String)
Returns TRUE if the input string contains at least one environment string token.
Declaration
public bool ContainsEnvToken(string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | String to evaluate for presence of environment string tokens. |
Returns
Type | Description |
---|---|
System.Boolean | TRUE if the string contains at least one environment string token. Otherwise return FALSE. |
Remarks
An environment string token is defined as a substring delimited by '%' charagers, which are symbolically represented by ENV_STR_DLM in this class.
Because this is a public method of a singleton, it cannot be marked Static. If it were so marked, it would lose access to the private storage that motivated me to finally sit down and figure out how to implement the Singleton pattern in C#.
ExpandEnvironmentTokens(String, String)
Given a string that contains one or more environment string tokens, expand the tokens, returning the resulting string.
Declaration
public static string ExpandEnvironmentTokens(string pstrIn, string pstrDefault = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | String to parse and expand environment string tokens. |
System.String | pstrDefault | Optional string to return as default if an undefined (empty)
environment variable is encountered in input string
|
Returns
Type | Description |
---|---|
System.String | The return value is string |
ExtractEnvToken(String)
If the input string contains an enviroment token, extract and return it.
Declaration
public string ExtractEnvToken(string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | String from which to extract an environment variable token. |
Returns
Type | Description |
---|---|
System.String | String containing the token, minus its delimiters. |
Remarks
An environment string token is defined as a substring delimited by '%' charagers, which are symbolically represented by ENV_STR_DLM in this class.
Since this method returns the string without its delimiters, pass it to static method TokenizeEnvVarName to convert it to a string that can be passed into a Replace method, to replace the token with its value, which you got by calling Environment.GetEnvironmentVariable.
Because this is a public method of a singleton, it cannot be marked Static. If it were so marked, it would lose access to the private storage that motivated me to finally sit down and figure out how to implement the Singleton pattern in C#.
Free()
Rather than have the caller lock the object, I provide this pair of methods that create a similar effect, by causing a competing caller of an object in a Frozen state to be thrown and exception.
Declaration
public bool Free()
Returns
Type | Description |
---|---|
System.Boolean | Always True. |
GetTokenExpander()
All classes that want to call methods on this object must first call this method.
Declaration
public static EnvTokenExpander GetTokenExpander()
Returns
Type | Description |
---|---|
EnvTokenExpander | A reference to the single instance of this object, which all consumers share. |
Remarks
This pseudo-constructor must be declared static. Its only real job is to return a reference to itself, personified as _TheInstance.
TokenizeEnvVarName(String)
Given a string, assumed to be the name of an environment string, return it with the expected escape ("quote") characters appended to both ends.
Declaration
public static string TokenizeEnvVarName(string pstrIn)
Parameters
Type | Name | Description |
---|---|---|
System.String | pstrIn | String to turn into an environment variable token. |
Returns
Type | Description |
---|---|
System.String | String pstrIn, with the escape characters (%) appended to both ends. |
Remarks
Use this method to turn the output of ExtractEnvToken into a string that can be fed to a Replace method to substitute the value of the environment variable for its token.