private static string[] GetLinesWithPossibleMismatches( string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = String.Empty; Console.WriteLine("Comparing files..."); int minOutputLines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); IOManager.DisplayAlert(Exceptions.InevitableMismatch); } string[] mismatches = new string[minOutputLines]; for (int index = 0; index < minOutputLines; index++) { string actualLine = actualOutputLines[index]; string expectedLine = expectedOutputLines[index]; if (!actualLine.Equals(expectedLine)) { output = $"■ Mismatch at line {index} ─ Expected: \"{expectedLine}\"" + $" <=> Actual: \"{actualLine}\"{Environment.NewLine}"; hasMismatch = true; } else { output = $"{actualLine}{Environment.NewLine}"; } mismatches[index] = output; } return(mismatches); }
public static void CompareContents(string userOutputPath, string expectedOutputPath) { string userOutputFile = IOManager.ExtractFileName(userOutputPath); string expectedOutputFile = IOManager.ExtractFileName(expectedOutputPath); userOutputPath = IOManager.BuildAbsolutePath(userOutputPath); expectedOutputPath = IOManager.BuildAbsolutePath(expectedOutputPath); Console.WriteLine("Reading files..."); try { string[] actualOutputLines = File.ReadAllLines($"{userOutputPath}\\{userOutputFile}"); string[] expectedOutputLines = File.ReadAllLines($"{expectedOutputPath}\\{expectedOutputFile}"); Console.WriteLine("Files read!"); bool hasMismatch; string[] mismatches = GetLinesWithPossibleMismatches( actualOutputLines, expectedOutputLines, out hasMismatch); Console.WriteLine("Comparison results:"); Console.WriteLine($"░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); string mismatchPath = $"{expectedOutputPath}\\mismatches.txt"; PrintOutput(mismatches, hasMismatch, mismatchPath); Console.WriteLine($"░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } } }
public static void CreateDirectory(string path) { path = IOManager.BuildAbsolutePath(path); string existingPath = String.Empty; string[] pathToCreate = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); try { for (int l = 0; l < pathToCreate.Length; l++) { string currentPath = existingPath + pathToCreate[l]; if (!Directory.Exists(currentPath)) { Directory.CreateDirectory(currentPath); Console.WriteLine($"Directory \"{pathToCreate[l]}\" created in {existingPath}"); } existingPath = $"{currentPath}\\"; } } catch (Exception exception) { if (exception is ArgumentException || exception is NotSupportedException) { IOManager.DisplayAlert(Exceptions.InvalidName); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
public static void ChangeDirectory(string destinationPath) { string currentDir = Directory.GetCurrentDirectory(); destinationPath = IOManager.BuildAbsolutePath(destinationPath); try { Directory.SetCurrentDirectory(destinationPath); } catch (Exception exception) { if (exception is DirectoryNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is ArgumentOutOfRangeException) { IOManager.DisplayAlert(Exceptions.InvalidCommandParameter); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
public static void DownloadFile(string sourcePath) { string destinationPath = Directory.GetCurrentDirectory(); string fileName = IOManager.ExtractFileName(sourcePath); sourcePath = IOManager.BuildAbsolutePath(sourcePath); if (String.IsNullOrEmpty(fileName)) { IOManager.DisplayAlert(Exceptions.FileNotSpecified); } else if (!sourcePath.Contains("\\") || sourcePath == destinationPath) { IOManager.DisplayAlert(Exceptions.IncompletePath); } else { try { File.Copy($"{sourcePath}\\{fileName}", $"{destinationPath}\\{fileName}"); Console.WriteLine($"File \"{fileName}\" has been downloaded to your current working folder"); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } else if (exception is IOException) { IOManager.DisplayAlert(Exceptions.FileAlreadyDownloaded); ConsoleKeyInfo choice = Console.ReadKey(); Console.Write(Environment.NewLine); if (choice.Key == ConsoleKey.Y) { File.Copy($"{sourcePath}\\{fileName}", $"{destinationPath}\\{fileName}", true); Console.WriteLine($"File \"{fileName}\" has been overwritten."); } else { Console.WriteLine("Download cancelled."); } } } } }
public static void LoadData(string path) { string fileName = IOManager.ExtractFileName(path); path = IOManager.BuildAbsolutePath(path); try { string[] databaseSource = File.ReadAllLines($"{path}\\{fileName}"); Console.WriteLine("Populating database structure..."); string pattern = @"([A-Z]\#?\+{0,2}[a-zA-Z]*_[A-Z][a-z]{2}_201[4-8])\s+([A-Z][a-z]{0,3}\d{2}_\d{2,4})\s+(100|[1-9][0-9]|[0-9])"; database.Clear(); foreach (string record in databaseSource) { if (!string.IsNullOrEmpty(record) && Regex.IsMatch(record, pattern)) { Match validRecord = Regex.Match(record, pattern); string course = validRecord.Groups[1].Value; string student = validRecord.Groups[2].Value; int score = int.Parse(validRecord.Groups[3].Value); if (!database.ContainsKey(course)) { database.Add(course, new Dictionary <string, List <int> >()); } if (!database[course].ContainsKey(student)) { database[course].Add(student, new List <int>()); } database[course][student].Add(score); } } isDatabaseInitialized = true; Console.WriteLine($"Done! {databaseSource.Length} unique records were loaded in the database."); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
private static bool ParametersCountValid(List <string> parameters, int minRequiredParameters, int maxAllowedParameters) { if (parameters.Count < minRequiredParameters) { IOManager.DisplayAlert(Exceptions.MissingCommandParameter); } else if (parameters.Count >= minRequiredParameters && parameters.Count <= maxAllowedParameters) { return(true); } else if (parameters.Count > maxAllowedParameters) { IOManager.DisplayAlert(Exceptions.RedundantCommandParameters); } return(false); }
public static void OpenFile(string path) { string fileName = IOManager.ExtractFileName(path); path = IOManager.BuildAbsolutePath(path); string filePath = $"{path}\\{fileName}"; try { Console.WriteLine(File.ReadAllText(filePath)); } catch (Exception exception) { if (exception is DirectoryNotFoundException || exception is FileNotFoundException) { IOManager.DisplayAlert(Exceptions.InvalidPath); } else if (exception is UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
private static bool IsQueryValid(string course, string student, string filter, string order) { if (isDatabaseInitialized) { if (database.ContainsKey(course) || course.ToUpper().Equals("ANY")) { if (database.Values.Any(s => s.ContainsKey(student)) || student.ToUpper().Equals("ALL") || int.TryParse(student, out int studentsToTake)) { string[] validFilters = { "EXCELLENT", "AVERAGE", "POOR", "OFF" }; string[] validSorters = { "ASCENDING", "DESCENDING" }; if (validFilters.Contains(filter) && validSorters.Contains(order)) { return(true); } else { IOManager.DisplayAlert(Exceptions.InvalidCommandParameter); } } else { IOManager.DisplayAlert(Exceptions.InexistantStudent); } } else { IOManager.DisplayAlert(Exceptions.InexistantCourse); } } else { IOManager.DisplayAlert(Exceptions.DatabaseNotInitialized); } return(false); }
public static void TraverseDirectory(int depth) { string startDir = Directory.GetCurrentDirectory(); int startDirLevel = startDir.Split('\\').Length; Queue <string> dirTree = new Queue <string>(); dirTree.Enqueue(startDir); while (dirTree.Count != 0) { string currentDir = dirTree.Dequeue(); int currentDirLevel = currentDir.Split('\\').Length; if (currentDirLevel - startDirLevel == depth + 1) { break; } try { string[] currentDirSubdirs = Directory.GetDirectories(currentDir); string[] currentDirFiles = Directory.GetFiles(currentDir); if (currentDirSubdirs.Length == 0 && currentDirFiles.Length == 0) { Console.WriteLine($"{new string(' ', currentDirLevel - startDirLevel)}{currentDir}"); } else { Console.WriteLine($"┌{new string('─', currentDirLevel - startDirLevel)}{currentDir}"); DisplayDirectorySubdirectories(currentDirSubdirs, currentDirFiles, dirTree); DisplayDirectoryFiles(currentDirFiles); } } catch (UnauthorizedAccessException) { IOManager.DisplayAlert(Exceptions.UnauthorizedAccess); } } }
public static void StartProcessingCommands() { Console.Write($"{Directory.GetCurrentDirectory()}> "); string input; while (!(input = Console.ReadLine().Trim()).Equals("EXIT")) { Console.Write(Environment.NewLine); List <string> parameters = input.Split().ToList(); string command = parameters[0].ToUpper(); switch (command) { case "COMPARE": if (ParametersCountValid(parameters, 3, 3)) { string file1 = parameters[1]; string file2 = parameters[2]; Tester.CompareContents(file1, file2); } break; case "DOWNLOAD": if (ParametersCountValid(parameters, 2, 2)) { string sourcePath = parameters[1]; FSManager.DownloadFile(sourcePath); } break; case "EXIT": if (ParametersCountValid(parameters, 1, 1)) { IOManager.DisplayGoodbye(); } break; case "GOTODIR": if (ParametersCountValid(parameters, 2, 2)) { string destinationPath = parameters[1]; FSManager.ChangeDirectory(destinationPath); } break; case "HELP": if (ParametersCountValid(parameters, 1, 1)) { IOManager.DisplayHelp(); } break; case "LISTDIR": if (ParametersCountValid(parameters, 1, 2)) { int depth = 0; if (parameters.Count == 2) { try { depth = int.Parse(parameters[1]); } catch (FormatException) { depth = -1; IOManager.DisplayAlert(Exceptions.InvalidCommandParameter); } } if (depth != -1) { IOManager.TraverseDirectory(depth); } } break; case "LOADDB": if (ParametersCountValid(parameters, 2, 2)) { string path = parameters[1]; DataRepository.LoadData(path); } break; case "MAKEDIR": if (ParametersCountValid(parameters, 2, 2)) { string path = parameters[1]; FSManager.CreateDirectory(path); } break; case "OPEN": if (ParametersCountValid(parameters, 2, 2)) { string path = parameters[1]; FSManager.OpenFile(path); } break; case "READDB": if (ParametersCountValid(parameters, 3, 5)) { string course = parameters[1]; string student = parameters[2]; string filter = "OFF"; string order = "DESCENDING"; if (parameters.Count == 4) { if (parameters[3].Length <= 9 && !parameters[3].ToUpper().Equals("ASCENDING")) { filter = parameters[3].ToUpper(); } else { order = parameters[3].ToUpper(); } } if (parameters.Count == 5) { if (parameters[3].Length <= 9 && !parameters[3].ToUpper().Equals("ASCENDING")) { filter = parameters[3].ToUpper(); order = parameters[4].ToUpper(); } else { filter = parameters[4]; order = parameters[3]; } } DataRepository.ReadDatabase(course, student, filter, order); } break; case "WIPE": Console.Clear(); break; default: IOManager.DisplayAlert(Exceptions.InvalidCommand, command); break; } Console.Write(Environment.NewLine); Console.Write($"{Directory.GetCurrentDirectory()}> "); } }