// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace Spludlow { public class Arguments { public static Spludlow.Variations WordVariations; static Arguments() { WordVariations = new Variations(new string[] { "key k", "host h", "queue q que", "assembly a ass", "type t ty", "method m meth me", "flags f flag fl", "index i idx", "parameters p params param par", "constructor c con", "print pr", "data dat", // databse query "text", // post/get text bounce back as file e.g. spew out .bat "file", "web", "mime", "filename" }); } // print HTTP.POST printer to use (balank default) // data HTTP.GET get data ??? // file HTTP.GET filename to read, HTTP.POST filename to write // web HTTP.GET web proxy // mime HTTP.GET file/web mime reply in header // filename HTTP.GET filename to reply in header // subject Report.aspx // type public static Dictionary> Decode(string[] args) { Dictionary> parameters = new Dictionary>(); string current = ""; for (int index = 0; index < args.Length; ++index) { string arg = args[index]; if (arg.StartsWith("-") == true) current = Match(arg.Substring(1)); //WordVariations.Match(arg.Substring(1)); if (parameters.ContainsKey(current) == false) parameters.Add(current, new List()); if (arg.StartsWith("-") == false) parameters[current].Add(arg); } return parameters; } private static string Match(string key) { int digitIndex = -1; for (int index = 0; index < key.Length; ++index) { if (Char.IsDigit(key[index]) == true) { digitIndex = index; break; } } if (digitIndex == -1) { return WordVariations.Match(key); } else { string matchKey = WordVariations.Match(key.Substring(0, digitIndex)); return matchKey + key.Substring(digitIndex); } } public static Dictionary> Decode(HttpRequest request) { return Decode(request.ServerVariables["QUERY_STRING"]); } public static Dictionary> Decode(string queryString) { Dictionary> parameters = new Dictionary>(); if (queryString.Length > 0) { string[] parts = queryString.Split(new char[] { '&' }); foreach (string part in parts) { string[] words = part.Split(new char[] { '=' }); if (words.Length < 1 || words.Length > 2) throw new ApplicationException("Bad querystring around equals:\t" + part); string key = Match(words[0]); //string key = WordVariations.Match(words[0]); if (parameters.ContainsKey(key) == false) parameters.Add(key, new List()); if (words.Length > 1) { string[] arrayParts = words[1].Split(new char[] { ',' }); foreach (string arrayPart in arrayParts) { if (arrayPart.Length > 0) parameters[key].Add(HttpUtility.UrlDecode(arrayPart)); } } if (parameters[key].Count == 0) parameters[key].Add(""); // Empty String, just key passed. used for flags e.g. "INLINE" } } return parameters; } public static string LastValue(Dictionary> arguments, string name) { return LastValue(arguments, name, false); } public static string LastValue(Dictionary> arguments, string name, bool required) { string key = WordVariations.Match(name); if (arguments.ContainsKey(key) == true) { if (arguments[key].Count == 0) throw new ApplicationException("Argument must contain data:\t" + name); return arguments[key][arguments[key].Count - 1]; } if (required == true) throw new ApplicationException("Required Argument not found:\t" + name); return null; } } }