using System; namespace MPF.ExecutionContexts.Data { /// /// Represents a single input for an execution context /// public abstract class Input { #region Properties /// /// Primary name for the input /// public readonly string Name; /// /// Alternative name for the input /// protected readonly string[] _altNames; /// /// Indicates if the value following is required or not /// protected readonly bool _required; /// /// Indicates if a value has been set /// public abstract bool ValueSet { get; } #endregion #region Constructors /// Flag name / value public Input(string name) { Name = name; _altNames = []; _required = true; } /// Flag name / value /// Indicates if a following value is required public Input(string name, bool required) { Name = name; _altNames = []; _required = required; } /// Flag name / value /// Verbose flag name / value public Input(string shortName, string longName) { Name = longName; _altNames = [shortName]; _required = true; } /// Flag name / value /// Verbose flag name / value /// Indicates if a following value is required public Input(string shortName, string longName, bool required) { Name = longName; _altNames = [shortName]; _required = required; } /// Set of names to use public Input(string[] names) { Name = names.Length > 0 ? names[0] : string.Empty; _altNames = names; _required = true; } /// Set of names to use /// Indicates if a following value is required public Input(string[] names, bool required) { Name = names.Length > 0 ? names[0] : string.Empty; _altNames = names; _required = required; } #endregion #region Functionality /// /// Clear any accumulated value /// public abstract void ClearValue(); /// /// Create a formatted representation of the input and possible value /// /// Use an equal sign as a separator on output public abstract string Format(bool useEquals); /// /// Process the current index, if possible /// /// Parts array to be referenced /// Reference to the position in the parts /// True if a value could be determined, false otherwise public abstract bool Process(string[] parts, ref int index); #endregion #region Helpers /// /// Get the trimmed value and multiplication factor from a value /// /// String value to treat as suffixed number /// Trimmed value and multiplication factor internal static string ExtractFactorFromValue(string value, out long factor) { value = value.Trim('"'); factor = 1; // Characters if (value.EndsWith("c", StringComparison.Ordinal)) { factor = 1; value = value.TrimEnd('c'); } // Words else if (value.EndsWith("w", StringComparison.Ordinal)) { factor = 2; value = value.TrimEnd('w'); } // Double Words else if (value.EndsWith("d", StringComparison.Ordinal)) { factor = 4; value = value.TrimEnd('d'); } // Quad Words else if (value.EndsWith("q", StringComparison.Ordinal)) { factor = 8; value = value.TrimEnd('q'); } // Kilobytes else if (value.EndsWith("k", StringComparison.Ordinal)) { factor = 1024; value = value.TrimEnd('k'); } // Megabytes else if (value.EndsWith("M", StringComparison.Ordinal)) { factor = 1024 * 1024; value = value.TrimEnd('M'); } // Gigabytes else if (value.EndsWith("G", StringComparison.Ordinal)) { factor = 1024 * 1024 * 1024; value = value.TrimEnd('G'); } return value; } /// /// Removes a leading 0x if it exists, case insensitive /// /// String with removed leading 0x /// internal static string RemoveHexIdentifier(string value) { if (value.Length <= 2) return value; if (value[0] != '0') return value; if (value[1] != 'x' && value[1] != 'X') return value; return value.Substring(2); } #endregion } /// /// Represents a single input for an execution context /// public abstract class Input : Input { #region Properties /// /// Represents the last value stored /// public T? Value { get; protected set; } /// public override bool ValueSet => Value != null; #endregion #region Constructors /// public Input(string name) : base(name) { } /// public Input(string name, bool required) : base(name, required) { } /// public Input(string shortName, string longName) : base(shortName, longName) { } /// public Input(string shortName, string longName, bool required) : base(shortName, longName, required) { } /// public Input(string[] names) : base(names) { } /// public Input(string[] names, bool required) : base(names, required) { } #endregion #region Functionality /// public override void ClearValue() { Value = default; } /// /// Set a new value /// public void SetValue(T value) { Value = value; } #endregion } }