//A recursive function for handling the creation of the macro commands public static Command CreateMacro(MacroCommand macroCommand, StreamReader sr) { var line = sr.ReadLine(); //base case, macro section is complete if (line.ElementAt(0) == 'E') { return(macroCommand); } //nested macro case else if (line.ElementAt(0) == 'B') { macroCommand.commands.Add(CreateMacro(new MacroCommand(), sr)); } //add line to macro and run function again else { ArgStruct args = Parser.ParseLine(line); macroCommand.commands.Add(Factory.MakeCommand(args)); CreateMacro(macroCommand, sr); } return(macroCommand); }
/// <summary> /// Parse the entire file, send off to subroutines as required to create Commands and save them to the invoker /// </summary> /// <param name="path"></param> public static void ParseFile(string path) { using (StreamReader sr = new StreamReader(path)) //Read the file in, create Commands and add to list { while (!sr.EndOfStream) { Command newCommand; var line = sr.ReadLine(); if (line.ElementAt(0) != 'B') { ArgStruct args = ParseLine(line); newCommand = Factory.MakeCommand(args); } else { newCommand = Factory.CreateMacro(new MacroCommand(), sr); } Invoker.commands.Add(newCommand); } } }
/// <summary> /// Creates a list of Commands from the temp file /// </summary> /// <param name="filePath"></param> private void CreateCommandList(string filePath) { commands.Clear(); commands.Add(new MacroCommand()); using (StreamReader sr = new StreamReader(filePath)) { while (!sr.EndOfStream) { Command newCommand; var line = sr.ReadLine(); if (line.ElementAt(0) != 'B') { ArgStruct args = ParseLine(line); newCommand = FunctionsDictionary[args._command](args); } else { newCommand = CreateMacro(new MacroCommand(), sr); } commands.Add(newCommand); } } }
static Command MakeUpdate(ArgStruct args) { Command command = new UpdateCommand(args._database, args._key, args._value); return(command); }
static Command MakeAdd(ArgStruct args) { Command command = new AddCommand(args._database, args._key, args._value); return(command); }
//The following methods are for creating the Command Objects public static Command MakeCommand(ArgStruct args) { return(FunctionsDictionary[args._command](args)); }
static Command MakeUpdate(ArgStruct args) { return(new UpdateCommand(args._database, args._key, args._value)); }
static Command MakeRemove(ArgStruct args) { return(new RemoveCommand(args._database, args._key, args._value)); }
static Command MakeAdd(ArgStruct args) { return(new AddCommand(args._database, args._key, args._value)); }