public void InterpretCommand(string input) { if (input == string.Empty) { return; } string commandName = input.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries) .First(); try { IExecutable command = this.ParseCommand(input, commandName); command.Execute(); } catch (Exception ex) { OutputWriter.DisplayException(ex.Message); } }
public static void TraverseFolder(int depth) { OutputWriter.WriteEmptyLine(); int initialIdentity = SessionData.currentPath.Split('\\').Length; Queue <string> subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.currentPath); while (subFolders.Count > 0) { string currentPath = subFolders.Dequeue(); int identention = currentPath.Split('\\').Length - initialIdentity; OutputWriter.WriteMessageOnNewLine($"{new string('-', identention)}{currentPath}"); if (depth - identention < 0) { break; } try { foreach (string file in Directory.GetFiles(currentPath)) { int indexOfSlash = file.LastIndexOf("\\"); string fileName = file.Substring(indexOfSlash); OutputWriter.WriteMessageOnNewLine($"{new string('-', indexOfSlash)}{fileName}"); } foreach (string directoryPath in Directory.GetDirectories(currentPath)) { subFolders.Enqueue(directoryPath); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayMessage(ExceptionMessages.UnauthorizedAccessExceptionMessage); } } }
public static void ChangeCurrentDirectoryRelative(string relativePath) { if (relativePath == "..") { try { string currentPath = SessionData.currentPath; int indexOfLastSlash = currentPath.LastIndexOf("\\"); string newPath = currentPath.Substring(0, indexOfLastSlash); SessionData.currentPath = newPath; } catch (ArgumentOutOfRangeException) { OutputWriter.DisplayMessage(ExceptionMessages.UnableToGoHigherInPartitionHierarchy); } } else { string currentPath = SessionData.currentPath; currentPath += $"\\{relativePath}"; ChangeCurrentDirectoryAbsolute(currentPath); } }
private static void TryParseParametersForOrderAndTake(string takeCommand, string takeQuantiy, string courseName, string order) { if (takeCommand == "take") { if (takeQuantiy == "all") { StudentsRepository.OrderAndTake(courseName, order); } else { int studetsToTake; bool isParsed = int.TryParse(takeQuantiy, out studetsToTake); if (isParsed) { StudentsRepository.OrderAndTake(courseName, order, studetsToTake); } else { OutputWriter.DisplayExeptions(ExceptionMessages.InvalidTakeQuantityParameter); } } } }
public void TraverseDirectory(int depth) { OutputWriter.WriteEmptyLine(); int initialIndentation = SessionData.currentPath.Split('\\').Length; Queue <string> subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.currentPath); while (subFolders.Count != 0) { string currentPath = subFolders.Dequeue(); int indentation = currentPath.Split('\\').Length - initialIndentation; if (depth - indentation < 0) { break; } OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', indentation), currentPath)); try { foreach (var file in Directory.GetFiles(currentPath)) { int indexOfSlash = file.LastIndexOf('\\'); string fileName = file.Substring(indexOfSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfSlash) + fileName); } foreach (string directoryPath in Directory.GetDirectories(currentPath)) { subFolders.Enqueue(directoryPath); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessException); } } }
private static void TryTraverseFolders(string input, string[] data) { if (data.Length == 1) { IOManager.TraverseDirectory(0); } else if (data.Length == 2) { bool hasParsed = int.TryParse(data[1], out int depth); if (hasParsed) { IOManager.TraverseDirectory(depth); } else { OutputWriter.DisplayException(ExceptionMessages.UnableToParseNumber); } } else { DisplayInvalidCommandMessage(input); } }
private static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayException($"The command '{input}' is invalid"); }
public static void PrintStudent(KeyValuePair <string, double> student) { OutputWriter.WriteMessageOnNewLine($"{student.Key} - {student.Value:F2}"); }
private void DisplayInvalidCommandMessage(string input) { OutputWriter.WriteMessageOnNewLine($"The command {input} is invalid!"); }
public static void PrintStudent(KeyValuePair <string, List <int> > student) { OutputWriter.WriteMessageOnNewLine($"{student.Key} - {string.Join(", ", student.Value)}"); }
public static void PrintStudent(KeyValuePair <string, double> student) { OutputWriter.WriteMessageOnNewLine(string.Format($"{student.Key} - {string.Join(",", student.Value)}")); }
public static void InterpredCommand(string input) { string[] data = input.Split(' '); string command = data[0].ToLower(); switch (command) { case "open": TryOpenFile(input, data); break; case "mkdir": TryCreateDirectory(input, data); break; case "ls": TryTraverseFolders(input, data); break; case "cmp": TryCompareFiles(input, data); break; case "cdrel": TryChangePathRelatively(input, data); break; case "cdabs": TryChangePathAbsolute(input, data); break; case "readdb": TryReadDataBaseFromFile(input, data); break; case "help": TryGetHelp(input, data); break; case "show": TryShowWantedData(input, data); break; case "filter": TryFilterAndTake(input, data); break; case "order": TryOrderAndTake(input, data); break; case "decorder": //TODO break; case "download": //TODO break; case "downloadasync": //TODO break; default: OutputWriter.DisplayExeptions(ExceptionMessages.InvalidCommandMessage(input)); break; } }