using System; using System.Collections.Generic; using System.Drawing; using System.Security.Policy; using System.Windows.Forms; namespace Advanced_Software_Engineering { /// /// This class simply provides settings and some functions that are used across many components. /// public class SettingsAndHelperFunctions { /// /// The variable simply keep track of the windows that are currently shown (excluding ). /// public static int NumberOfWindows = 0; /// /// This helper function is executed when a window is closed. It shows the when the count is 0 /// public static void WindowClosed() { NumberOfWindows--; if (NumberOfWindows == 0) { System.Console.WriteLine("Showing MainMenu"); Program.MainMenu.Show(); } } /// /// This function removes spaces from the start and the end of the text. This should be unit tested. /// /// Simply any string of any size /// A string without spaces at the begining or the end /// /// For example /// /// string a = " a simple sentence surrounded by spaces "; /// string b = StripSpaces(a); /// Console.WriteLine(b); // => "a simple sentence surrounded by spaces"; /// /// /// /// The code won't remove internal double spaces: /// /// string a = " a simple sentence surrounded by spaces "; /// string b = StripSpaces(a); /// Console.WriteLine(b); // => "a simple sentence surrounded by spaces"; /// /// public static string Strip(string text) { //Console.WriteLine("Stripping: '" + text + "'"); int start; for (start = 0; start < text.Length; start++) { char character = text[start]; if (character != " "[0]) break; } int end; for (end = text.Length - 1; end >= 0; end--) { char character = text[end]; if (character != " "[0]) break; } if (end == -1) return ""; string result = text.Substring(start, end - start + 1); //Console.WriteLine("Started at: " + start); //Console.WriteLine("Ended at: " + end); //Console.WriteLine("Result: " + result); return result; } /// /// Strips the strings of the full array. See /// /// /// string[] tmp = {" text ", " ", " with", "spaces ", ""}; /// Console.WriteLine(StripStringArray(tmp)); // => {"text", "with", "spaces"} /// /// Dirty array with spaces /// A stripped aray public static List StripStringArray(string[] array) { List newStringList = new List(); foreach (string arrayElement in array) { string strippedElement = Strip(arrayElement); if (strippedElement.Length == 0) continue; else newStringList.Add(strippedElement); } return newStringList; } /// /// Parses the commands into a directry that Commander can use. See /// /// A string command /// Directory with the command and its parameters cleanly separated public static Dictionary CommandAndParameterParser(string text) { Dictionary commandAndParameters = new Dictionary(); //prepare the parameters by stripping them of spaces string prepParameters = Strip(text); if (prepParameters.Length == 0) return commandAndParameters; //split the command from the parameters string[] parameters = prepParameters.Split(new char[] { " "[0] }, 2); //There are parameters, make the command if (parameters.Length > 0) { //set command var string command = Strip(parameters[0]); commandAndParameters["command"] = new string[] { command }; } //There is at least one parameter if (parameters.Length > 1) { //seperate all of the parameters by a comma parameters = parameters[1].Split(","[0]); //Remove spaces around parameters parameters = StripStringArray(parameters).ToArray(); //We don't want parameters if there aren't any if (!(parameters.Length == 1 && parameters[0] == "")) { //set parameter list commandAndParameters["parameters"] = parameters; } } return commandAndParameters; } } }