public void TestGetNameFile()
 {
     File file = new File("file", null);
     Assert.AreEqual("file", file.getName());
 }
Esempio n. 2
0
        public static void commande(File Actuel)
        {
            Console.Write(Actuel.path + "> ");
            string commandes = Console.ReadLine();
            List<string> action = new List<string> (commandes.Split(' '));
               switch (action[0])
               {
                   case "path":
                       if (action.Count == 1)
                       {
                           Console.WriteLine(Actuel.getPath());
                       }
                       else
                       {
                           runOrNot(Actuel);
                       }
                   break;
                   case "search":
                   if (action.Count == 2)
                   {
                       if (Actuel.isDirectory())
                       {
                           Directory dir = (Directory)Actuel;
                           var list = new List<File>();
                           if (dir.isDirectory())
                              list = dir.search(action[1]);
                           list.ForEach(delegate(File f)
                           {
                               Console.WriteLine("   found : " + f.GetType().ToString().Replace("FileSystem.", " ").Substring(0, 4) + "  " + f.path);
                           });
                       }
                       else
                       {
                           Console.WriteLine("Vous êtes dans un fichier");
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                    break;
                   case "create":
                   if (action.Count == 2)
                   {
                       if (Actuel.isDirectory())
                       {
                           Directory dir = (Directory)Actuel;
                           dir.createNewFile(action[1]);
                           Actuel = dir;
                       }
                       else
                       {
                           Console.WriteLine("Vous n'êtes pas dans un dossier");
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "mkdir":
                   if (action.Count == 2)
                   {
                       if (Actuel.isDirectory())
                       {
                           bool test = false;
                           Directory dir = (Directory)Actuel;
                           foreach (File fichier in dir.Fichiers)
                           {
                               if (fichier.Nom == action[1])
                               {
                                   Console.WriteLine("Ce fichier existe déjà");
                                   test = true;
                                   break;
                               }
                           }
                           if (test == false)
                           {
                               dir.mkdir(action[1]);
                               Actuel = dir;
                           }
                       }
                       else
                       {
                           Console.WriteLine("Vous n'êtes pas dans un dossier");
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "name":
                   if (action.Count == 1)
                   {
                       Console.WriteLine(Actuel.getName());
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "ls":
                   if (action.Count == 1)
                   {
                       if (Actuel.isDirectory())
                       {
                           Directory dir = (Directory)Actuel;
                           if (dir.Fichiers.Count > 0)
                           {
                               Console.WriteLine();
                               foreach (File file in dir.ls())
                               {
                                   if (file.GetType().ToString() == "FileSystem.Directory")
                                   {
                                       Console.WriteLine("D " + file.Nom);
                                   }
                                   else
                                   {
                                       Console.WriteLine("F " + file.Nom);
                                   }
                               }
                               Console.WriteLine();
                           }
                       }
                       else
                       {
                           Console.WriteLine("Vous n'êtes pas dans un dossier");
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "cd":
                       if (action.Count == 2)
                       {
                           if (Actuel.isDirectory())
                           {
                               Directory dir = (Directory)Actuel;
                               Actuel = dir.cd(action[1]);
                           }
                           else
                           {
                               Console.WriteLine("Vous êtes dans un fichier");
                           }
                       }
                       else
                       {
                           runOrNot(Actuel);
                       }
                   break;
                   case "file":
                       if (action.Count == 1)
                       {
                           if (Actuel.isFile() == true)
                           {
                               Console.WriteLine("C'est un fichier");
                           }
                           else
                           {
                               Console.WriteLine("Ce n'est pas un fichier");
                           }
                       }
                       else
                       {
                           runOrNot(Actuel);
                       }

                   break;
                   case "rename":
                   if (action.Count == 3)
                   {
                       if (Actuel.isDirectory())
                       {
                           Directory dir = (Directory)Actuel;
                           dir.renameTo(action[1], action[2]);
                           Actuel = dir;
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "chmod":
                       if (action.Count == 2)
                       {
                           int permission;
                       if (Int32.TryParse(action[1], out permission))
                       {
                           permission = Int32.Parse(action[1]);
                           Actuel.chmod(permission);
                       }
                       else
                       {
                           Console.WriteLine("Permission impossible");
                       }
                       }
                       else
                       {
                           runOrNot(Actuel);
                       }

                   break;
                   case "directory":
                   if (action.Count == 1)
                   {
                       if (Actuel.isDirectory() == true)
                       {
                           Console.WriteLine("C'est un répertoire");
                       }
                       else
                       {
                           Console.WriteLine("Ce n'est pas un répertoire");
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "delete":
                   if (action.Count == 2)
                   {
                       if (Actuel.isDirectory())
                       {
                           Directory dir = (Directory)Actuel;
                           dir.delete(action[1]);
                           Actuel = dir;
                       }
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }

                   break;
                   case "root":
                   if (action.Count == 1)
                   {
                       Console.WriteLine(Actuel.getRoot().Nom);
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "parent":
                   if (action.Count == 1)
                   {
                       if (Actuel.getParent() != Actuel)
                       {
                           Actuel = (Directory)Actuel.getParent();
                       }

                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "exit":
                   if (action.Count == 1)
                   {
                       System.Environment.Exit(-1);
                   }
                   else
                   {
                       runOrNot(Actuel);
                   }
                   break;
                   case "":
                   break;
                   default:
                   Console.WriteLine("Commande inexistante");
                   break;
               }
               commande(Actuel);
        }
Esempio n. 3
0
 public static void getInstruction(string instruction, File Current, List<string> options)
 {
     File old = Current;
     var dir = new Directory();
     if(Current.getDir())
          dir = (Directory)Current;
     switch (instruction)
     {
         case "ls":
             List<File>ls =new List<File>();
             ls = Current.ls();
             if (ls.Count == 0 && Current.getFile())
                 Console.WriteLine("Vous etes dans un fichier");
             else
             if (ls.Count > 0)
             {
                 ls.ForEach(delegate(File f){
                     if (f.canExecute())
                         Console.Write("e");
                     Console.Write('-');
                     if (f.canRead())
                         Console.Write("r");
                     Console.Write("-");
                     if (f.canWrite())
                         Console.Write("w");
                     Console.Write("-");
                     Console.Write("   (" + f.GetType().ToString().Replace("FileSystem.", " ").Substring(0,4) + " )   " + f.path + "\n");
                 });
             }
             break;
         case "cd":
             location = Current.cd(options[1]);
             if (location.path == null)
                 location = old;
             break;
         case "type":
             String type = Current.GetType().ToString();
             string[] t = type.Split('.');
             Console.WriteLine(t[1]);
             break;
         case "mkdir":
             bool created = false;
             if (Current.getDir())
                 created = dir.mkdir(options[1]);
             else
                 Console.WriteLine("Impossible de créer quelque chose dans un fichier");
             break;
         case "parent":
             location = Current.getParent();
             break;
         case "root":
             File root = new File();
             root =Current.getRoot();
             location = (Directory)root;
             break;
         case "create":
             bool c = false;
             if(Current.getDir())
                 c= dir.create(options[1]);
             if (!c)
                 Console.WriteLine("    Une erreur c'est produite");
             break;
         case "rename":
             bool r = false;
             if(Current.getDir())
               r = dir.rename(options[1],options[2]);
             if (r)
                 Console.WriteLine("    " + options[1] + " a été renommé en : " + options[2]);
             else
                 Console.WriteLine("    Impossible de renommer ce fichier.");
             break;
         case "path":
             string path = dir.getPath();
             Console.WriteLine("   " + path);
             break;
         case "name":
             Current.getName();
             break;
         case "file":
             break;
         case "directory":
             if (Current.getDir())
                 Console.WriteLine("C'est un dossier");
             else
                 Console.WriteLine("Ce n'est pas un dossier");
             break;
         case "search":
             var  list = new List<File>();
             if (Current.getDir())
             dir.search(options[1],list);
             list.ForEach(delegate(File f)
             {
                 Console.WriteLine("   found : " + f.GetType().ToString().Replace("FileSystem.", " ").Substring(0,4) + "  "+f.path);
             });
             break;
         case "delete":
             bool deleted = false;
             if(Current.getDir())
                 deleted = dir.delete(options[1]);
             if (deleted)
                 Console.WriteLine("Le dossier " + options[1] + " a été érradiqué");
             else
                 Console.WriteLine("Le dossier n'a pas été trouvé :(");
             break;
         case "chmod":
             if(dir.isRoot==true)
             {
                 Console.WriteLine("Impossible de faire un chmod sur la racine");
                 break;
             }
             int number;
             if (int.TryParse(options[1].ToString(), out number))
                 Current.chmod(int.Parse(options[1]));
             else
                 Console.WriteLine("    You shall not parse !");
             break;
         default:
             Console.WriteLine("Commande inconnue");
             break;
     }
 }