Advertisement
Danny_Berova

BashSoft CommandInterpreter

Feb 5th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.09 KB | None | 0 0
  1. using System.Diagnostics;
  2.  
  3. namespace BashSoft
  4. {
  5.     using SimpleJudge;
  6.  
  7.     public static class CommandInterpreter
  8.     {
  9.         public static void IntepredCommand(string input)
  10.         {
  11.             string[] data = input.Split(' ');
  12.             string command = data[0];
  13.             switch (command)
  14.             {
  15.                 case "open":
  16.                     TryOpenFile(input, data);
  17.                     break;
  18.                 case "mkdir":
  19.                     TryCreateDirectory(input, data);
  20.                     break;
  21.                 case "ls":
  22.                     TryTraverseFolders(input, data);
  23.                     break;
  24.                 case "cmp":
  25.                     TryComparеFiles(input, data);
  26.                     break;
  27.                 case "cdRel":
  28.                     TryChangePathRelatively(input, data);
  29.                     break;
  30.                 case "cdAbs":
  31.                     TryChangePathAbsolute(input, data);
  32.                     break;
  33.                 case "readDb":
  34.                     TryReadDatabaseFromFile(input, data);
  35.                     break;
  36.                 case "help":
  37.                     TryGetHelp(); break;
  38.                 case "show":
  39.                     TryShowWantedData(input, data);
  40.                     break;
  41.                 case "filter":
  42.                     TryFilterAndTake(input, data);
  43.                     break;
  44.                 case "order":
  45.                     TryOrderAndTake(input, data);
  46.                     break;
  47.                 // TODO: case "dec0rder": break;
  48.                 // TODO: case "download":TryDownloadRequestedFile(input, data); break;
  49.                 // TODO: case "downloadAsynch" TryDownloadRequestedFileAsync(input, data); break;
  50.                 default:
  51.                     DisplayInvalidCommandMessage(input);
  52.                     break;
  53.             }
  54.         }
  55.  
  56.         private static void DisplayInvalidCommandMessage(string input)
  57.         {
  58.             OutputWriter.DisplayException(string.Format(ExceptionMessages.InvalidCommand, input));
  59.         }
  60.  
  61.         // order courseName ascending/descending take 3/26/52/all
  62.         private static void TryOrderAndTake(string input, string[] data)
  63.         {
  64.             if (data.Length == 5)
  65.             {
  66.                 string courseName = data[1];
  67.                 string comparison = data[2].ToLower();
  68.                 string takeCommand = data[3].ToLower();
  69.                 string takeQuantity = data[4].ToLower();
  70.  
  71.                 TryParseParametersForOrderAndTake(takeCommand, takeQuantity, courseName, comparison);
  72.             }
  73.             else
  74.             {
  75.                 DisplayInvalidCommandMessage(input);
  76.             }
  77.         }
  78.  
  79.         private static void TryParseParametersForOrderAndTake(string takeCommand, string takeQuantity, string courseName, string comparison)
  80.         {
  81.             if (takeCommand == "take")
  82.             {
  83.                 if (takeQuantity == "all")
  84.                 {
  85.                     StudentsRepository.OrderAndTake(courseName, comparison);
  86.                 }
  87.                 else
  88.                 {
  89.                     int studentsToTake;
  90.                     bool hasParsed = int.TryParse(takeQuantity, out studentsToTake);
  91.                     if (hasParsed)
  92.                     {
  93.                         StudentsRepository.OrderAndTake(courseName, comparison, studentsToTake);
  94.                     }
  95.                     else
  96.                     {
  97.                         OutputWriter.DisplayException(ExceptionMessages.InvalidTakeQuantityParameter);
  98.                     }
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 OutputWriter.DisplayException(ExceptionMessages.InvalidTakeQuantityParameter);
  104.             }
  105.         }
  106.  
  107.         private static void TryFilterAndTake(string input, string[] data)
  108.         {
  109.             if (data.Length == 5)
  110.             {
  111.                 string courseName = data[1];
  112.                 string filter = data[2].ToLower();
  113.                 string takeCommand = data[3].ToLower();
  114.                 string takeQuantity = data[4].ToLower();
  115.  
  116.                 TryParseParametersForFilterAndTake(takeCommand, takeQuantity, courseName, filter);
  117.             }
  118.             else
  119.             {
  120.                 DisplayInvalidCommandMessage(input);
  121.             }
  122.         }
  123.  
  124.         private static void TryParseParametersForFilterAndTake(string takeCommand, string takeQuantity, string courseName, string filter)
  125.         {
  126.             if (takeCommand == "take")
  127.             {
  128.                 if (takeQuantity == "all")
  129.                 {
  130.                     StudentsRepository.FilterAndTake(courseName, filter);
  131.                 }
  132.                 else
  133.                 {
  134.                     int studentsToTake;
  135.                     bool hasParsed = int.TryParse(takeQuantity, out studentsToTake);
  136.  
  137.                     if (hasParsed)
  138.                     {
  139.                         StudentsRepository.FilterAndTake(courseName, filter, studentsToTake);
  140.                     }
  141.                     else
  142.                     {
  143.                         OutputWriter.DisplayException(ExceptionMessages.InvalidTakeQuantityParameter);
  144.                     }
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 OutputWriter.DisplayException(ExceptionMessages.InvalidTakeQuantityParameter);
  150.             }
  151.         }
  152.  
  153.         private static void TryShowWantedData(string input, string[] data)
  154.         {
  155.             if (data.Length == 2)
  156.             {
  157.                 string courseName = data[1];
  158.                 StudentsRepository.GetAllStudentsFromCourse(courseName);
  159.             }
  160.             else if (data.Length == 3)
  161.             {
  162.                 string courseName = data[1];
  163.                 string userName = data[2];
  164.                 StudentsRepository.GetStudentScoresFromCourse(courseName, userName);
  165.             }
  166.             else
  167.             {
  168.                 DisplayInvalidCommandMessage(input);
  169.             }
  170.         }
  171.  
  172.         // help
  173.         private static void TryGetHelp()
  174.         {
  175.             OutputWriter.WriteMessageOnNewLine($"{new string('_', 100)}");
  176.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "make directory - mkdir: path "));
  177.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "traverse directory - ls: depth "));
  178.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "comparing files - cmp: path1 path2"));
  179.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "change directory - cdRel: relative path"));
  180.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "change directory - cdAbs: absolute path"));
  181.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "read students data base - readDb: path"));
  182.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "filter {courseName} excelent/average/poor take 2/5/all students - filterExcelent (the output is written on the console)"));
  183.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "order students - order {courseName} ascending/descending take 20/10/all (the output is written on the console)"));
  184.             //OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "download file - download: path of file (saved in current directory)"));
  185.             //OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "download file asinchronously - downloadAsynch: path of file (save in the current directory)"));
  186.             OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "get help – help"));
  187.             OutputWriter.WriteMessageOnNewLine($"{new string('_', 100)}");
  188.             OutputWriter.WriteEmptyLine();
  189.         }
  190.  
  191.         private static void TryReadDatabaseFromFile(string input, string[] data)
  192.         {
  193.             if (data.Length == 2)
  194.             {
  195.                 string fileName = data[1];
  196.                 StudentsRepository.InitializeData(fileName);
  197.             }
  198.             else
  199.             {
  200.                 DisplayInvalidCommandMessage(input);
  201.             }
  202.         }
  203.  
  204.         private static void TryChangePathAbsolute(string input, string[] data)
  205.         {
  206.             if (data.Length == 2)
  207.             {
  208.                 string absPath = data[1];
  209.                 IOManager.ChangeCurrentDirectoryAbsolute(absPath);
  210.             }
  211.             else
  212.             {
  213.                 DisplayInvalidCommandMessage(input);
  214.             }
  215.         }
  216.  
  217.         // cdRel relativePath
  218.         private static void TryChangePathRelatively(string input, string[] data)
  219.         {
  220.             if (data.Length == 2)
  221.             {
  222.                 string relPath = data[1];
  223.                 IOManager.ChangeCurrentDirectoryRelative(relPath);
  224.             }
  225.             else
  226.             {
  227.                 DisplayInvalidCommandMessage(input);
  228.             }
  229.         }
  230.  
  231.         // cmp absolutePath1 absolutePath2
  232.         private static void TryComparеFiles(string input, string[] data)
  233.         {
  234.             if (data.Length == 3)
  235.             {
  236.                 string firstPath = data[1];
  237.                 string secondPath = data[2];
  238.                 Tester.CompareContent(firstPath, secondPath);
  239.             }
  240.             else
  241.             {
  242.                 DisplayInvalidCommandMessage(input);
  243.             }
  244.         }
  245.  
  246.         private static void TryTraverseFolders(string input, string[] data)
  247.         {
  248.             if (data.Length == 1)
  249.             {
  250.                 IOManager.TraverseDirectory(0);
  251.             }
  252.             else if (data.Length == 2)
  253.             {
  254.                 int depth;
  255.                 bool hasParsed = int.TryParse(data[1], out depth);
  256.  
  257.                 if (hasParsed)
  258.                 {
  259.                     IOManager.TraverseDirectory(depth);
  260.                 }
  261.                 else
  262.                 {
  263.                     OutputWriter.DisplayException(ExceptionMessages.UnableToParseNumber);
  264.                 }
  265.             }
  266.             else
  267.             {
  268.                 DisplayInvalidCommandMessage(input);
  269.             }
  270.         }
  271.  
  272.         private static void TryCreateDirectory(string input, string[] data)
  273.         {
  274.             if (data.Length == 2)
  275.             {
  276.                 string folderName = data[1];
  277.                 IOManager.CreateDirectoryInCurrentFolder(folderName);
  278.             }
  279.             else
  280.             {
  281.                 DisplayInvalidCommandMessage(input);
  282.             }
  283.         }
  284.  
  285.         private static void TryOpenFile(string input, string[] data)
  286.         {
  287.             if (data.Length == 2)
  288.             {
  289.                 var fileName = data[1];
  290.                 fileName = SessionData.currentPath + "\\" + fileName;
  291.  
  292.                 var process = new Process();
  293.                 process.StartInfo = new ProcessStartInfo()
  294.                 {
  295.                     UseShellExecute = true,
  296.                     FileName = fileName,
  297.                 };
  298.  
  299.                 process.Start();
  300.             }
  301.             else
  302.             {
  303.                 DisplayInvalidCommandMessage(input);
  304.             }
  305.         }
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement