diff --git a/Test/Program.cs b/Test/Program.cs index da901ae..99a8d15 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -29,6 +29,100 @@ namespace Test } } + static IEnumerable ParseInput(string input) + { + var sb = new StringBuilder(); + bool openString = false; + foreach (var c in input) + { + if (char.IsWhiteSpace(c)) + { + if (!openString) + { + if (sb.ToString() != "") + { + yield return sb.ToString(); + } + sb.Clear(); + continue; + } + else + { + sb.Append(c); + } + } + + if (c == '"') + { + if (sb.ToString() != "") + { + yield return sb.ToString(); + } + + sb.Clear(); + + openString = !openString; + } + else + { + sb.Append(c); + } + } + + if (sb.ToString() != "") + { + yield return sb.ToString(); + } + } + + static Dictionary> commands = new Dictionary>(); + + #region Commands + static void Help(string[] args) + { + Console.WriteLine("Commands:"); + foreach (var kvp in commands) + { + Console.WriteLine(" - {0}", kvp.Key); + } + } + + static void Mount(string[] args) + { + if (args.Length < 3) + { + Console.WriteLine("Usage: mount "); + return; + } + bool append; + if (!bool.TryParse(args[2], out append)) + { + Console.WriteLine("append can only be true or false"); + } + + PhysFS.PhysFS.Mount(args[0], args[1], append); + } + + static void Enumerate(string[] args) + { + if (args.Length < 1) + { + Console.WriteLine("Usage: enumerate/ls "); + return; + } + + foreach (var f in PhysFS.PhysFS.EnumerateFiles(args[0])) + { + Console.WriteLine(" - {0}", f); + } + } + + static void GetLastError(string[] args) + { + Console.WriteLine(PhysFS.PhysFS.GetLastError()); + } + #endregion + static void Main(string[] args) { try @@ -50,10 +144,46 @@ namespace Test PrintSupportedArchives(); Console.WriteLine("Type 'help' for a list of commands"); - while(true) + + commands.Add("help", Help); + commands.Add("mount", Mount); + commands.Add("enumerate", Enumerate); + commands.Add("ls", Enumerate); + + while (true) { Console.Write("> "); var input = Console.ReadLine(); + var split = ParseInput(input); + if (split.Count() == 0) + { + continue; + } + + if (split.First() == "quit") + { + break; + } + else + { + Action cmd; + if (commands.TryGetValue(split.First(), out cmd)) + { + try + { + cmd(split.Skip(1).ToArray()); + Console.WriteLine("Done."); + } + catch (PhysFS.PhysFSException e) + { + Console.Error.WriteLine("ERROR: {0}", e.Message); + } + } + else + { + Console.Error.WriteLine("Invalid command"); + } + } } PhysFS.PhysFS.Deinit();