using System.Collections.Generic; using System.Text.RegularExpressions; namespace DICUI.Data { public abstract class BaseParameters { /// /// Path to the executable /// public string Path { get; set; } /// /// Program that this set of parameters represents /// public InternalProgram InternalProgram { get; set; } /// /// Populate a Parameters object from a param string /// /// String possibly representing a set of parameters public BaseParameters(string parameters) { // If any parameters are not valid, wipe out everything if (!ValidateAndSetParameters(parameters)) { ResetValues(); } } /// /// Generate parameters based on a set of known inputs /// /// KnownSystem value to use /// MediaType value to use /// Drive letter to use /// Filename to use /// Drive speed to use /// Enable paranoid mode (safer dumping) /// Enable quiet mode (no beeps) /// User-defined reread count public BaseParameters(KnownSystem? system, MediaType? type, char driveLetter, string filename, int? driveSpeed, bool paranoid, bool quietMode, int retryCount) { SetDefaultParameters(system, type, driveLetter, filename, driveSpeed, paranoid, retryCount); } /// /// Blindly generate a parameter string based on the inputs /// /// Correctly formatted parameter string, null on error public abstract string GenerateParameters(); /// /// Get the input path from the implementation /// /// String representing the path, null on error public abstract string InputPath(); /// /// Get the output path from the implementation /// /// String representing the path, null on error public abstract string OutputPath(); /// /// Get the processing speed from the implementation /// /// int? representing the speed, null on error public abstract int? GetSpeed(); /// /// Set the processing speed int the implementation /// /// int? representing the speed public abstract void SetSpeed(int? speed); /// /// Get the MediaType from the current set of parameters /// /// MediaType value if successful, null on error public abstract MediaType? GetMediaType(); /// /// Gets if the current command is considered a dumping command or not /// /// True if it's a dumping command, false otherwise public abstract bool IsDumpingCommand(); /// /// Returns if the current Parameter object is valid /// /// public bool IsValid() { return GenerateParameters() != null; } /// /// Reset all special variables to have default values /// protected abstract void ResetValues(); /// /// Set default parameters for a given system and media type /// /// KnownSystem value to use /// MediaType value to use /// Drive letter to use /// Filename to use /// Drive speed to use /// Enable paranoid mode (safer dumping) /// User-defined reread count protected abstract void SetDefaultParameters( KnownSystem? system, MediaType? type, char driveLetter, string filename, int? driveSpeed, bool paranoid, int retryCount); /// /// Scan a possible parameter string and populate whatever possible /// /// String possibly representing parameters /// protected abstract bool ValidateAndSetParameters(string parameters); /// /// Returns whether or not the selected item exists /// /// List of parameters to check against /// Current index /// True if the next item exists, false otherwise protected bool DoesExist(List parameters, int index) { if (index >= parameters.Count) return false; return true; } /// /// Returns whether a string is a flag (starts with '/') /// /// String value to check /// True if it's a flag, false otherwise protected bool IsFlag(string parameter) { if (parameter.Trim('\"').StartsWith("/")) return true; return false; } /// /// Returns whether a string is a valid drive letter /// /// String value to check /// True if it's a valid drive letter, false otherwise protected bool IsValidDriveLetter(string parameter) { if (!Regex.IsMatch(parameter, @"^[A-Z]:?\\?$")) return false; return true; } /// /// Returns whether a string is a valid bool /// /// String value to check /// True if it's a valid bool, false otherwise protected bool IsValidBool(string parameter) { return bool.TryParse(parameter, out bool temp); } /// /// Returns whether a string is a valid byte /// /// String value to check /// Lower bound (>=) /// Upper bound (<=) /// True if it's a valid byte, false otherwise protected bool IsValidInt8(string parameter, sbyte lowerBound = -1, sbyte upperBound = -1) { if (!sbyte.TryParse(parameter, out sbyte temp)) return false; else if (lowerBound != -1 && temp < lowerBound) return false; else if (upperBound != -1 && temp > upperBound) return false; return true; } /// /// Returns whether a string is a valid Int16 /// /// String value to check /// Lower bound (>=) /// Upper bound (<=) /// True if it's a valid Int16, false otherwise protected bool IsValidInt16(string parameter, short lowerBound = -1, short upperBound = -1) { if (!short.TryParse(parameter, out short temp)) return false; else if (lowerBound != -1 && temp < lowerBound) return false; else if (upperBound != -1 && temp > upperBound) return false; return true; } /// /// Returns whether a string is a valid Int32 /// /// String value to check /// Lower bound (>=) /// Upper bound (<=) /// True if it's a valid Int32, false otherwise protected bool IsValidInt32(string parameter, int lowerBound = -1, int upperBound = -1) { if (!int.TryParse(parameter, out int temp)) return false; else if (lowerBound != -1 && temp < lowerBound) return false; else if (upperBound != -1 && temp > upperBound) return false; return true; } /// /// Returns whether a string is a valid Int64 /// /// String value to check /// Lower bound (>=) /// Upper bound (<=) /// True if it's a valid Int64, false otherwise protected bool IsValidInt64(string parameter, long lowerBound = -1, long upperBound = -1) { if (!long.TryParse(parameter, out long temp)) return false; else if (lowerBound != -1 && temp < lowerBound) return false; else if (upperBound != -1 && temp > upperBound) return false; return true; } } }