Example #1
0
 public Directory(string name, StoreData store)
 {
     this.Name       = name;
     data            = new List <IData>();
     ParentDirectory = store;
 }
Example #2
0
        public void Parse(string command, FileSystem fileSystem)
        {
            string[] predicates = System.Text.RegularExpressions.Regex.Replace(command.Trim(), @"\s+", " ").Split(' ');

            #region CD

            if (predicates[0].ToUpper().Equals("CD") && predicates.Length == 2)
            {
                char[] charsToTrim = { '\\' };

                string[] path = predicates[1].TrimEnd(charsToTrim).Split('\\');

                StoreData store = fileSystem.Find(path[0].ToUpper());

                if (store == null)
                {
                    store = fileSystem.CurrentDirectory.Find(path[0]);
                    if (store != null)
                    {
                        for (int i = 1; i < path.Length; i++)
                        {
                            if (store.Find(path[i]) != null)
                            {
                                store = store.Find(path[i]);
                            }
                            else
                            {
                                return;

                                //ошибка команды - путь не найден
                            }

                            if (i.Equals(path.Length - 1))
                            {
                                fileSystem.CurrentDirectory = store;
                                return;
                            }
                        }
                        fileSystem.CurrentDirectory = store;
                    }
                    else
                    {
                        return;
                        //ошибка команды - путь не найден
                    }
                }
                else
                {
                    if (path.Length == 1)
                    {
                        fileSystem.CurrentDirectory = store;
                    }
                    return;
                }

                for (int i = 1; i < path.Length; i++)
                {
                    if (store.Find(path[i]) != null)
                    {
                        store = store.Find(path[i]);
                    }
                    else
                    {
                        return;
                        //ошибка команды - путь не найден
                    }

                    if (i.Equals(path.Length - 1))
                    {
                        fileSystem.CurrentDirectory = store;
                        return;
                    }
                }
                string s = "";
            }

            #endregion

            #region MD
            if (predicates[0].ToUpper().Equals("MD") && predicates.Length == 2)
            {
                char[] charsToTrim = { '\\' };

                string[] path = predicates[1].TrimEnd(charsToTrim).Split('\\');

                StoreData store = fileSystem.Find(path[0].ToUpper());

                //По относительному пути
                if (store == null)
                {
                    store = fileSystem.CurrentDirectory.Find(path[0]);

                    if (store != null)
                    {
                        Console.WriteLine("Такая директория уже существует");
                        return;
                    }
                    else
                    {
                        if (path.Length == 1)
                        {
                            Console.WriteLine("Директория создана");
                            fileSystem.CurrentDirectory.CreateDirectory(path[0]);
                        }

                        else
                        {
                            Console.WriteLine("Путь не найден");
                            return;
                            //ошибка - директория не существует
                        }
                    }
                }

                for (int i = 1; i < path.Length; i++)
                {
                    if (store.Find(path[i]) != null)
                    {
                        if (i != path.Length - 1)
                        {
                            store = store.Find(path[i]);
                        }
                        else
                        {
                            //Такая директория уже существует
                            Console.WriteLine("Такая директория уже существует");
                            return;
                        }
                    }
                    else
                    {
                        if (i == path.Length - 1)
                        {
                            store.CreateDirectory(path[path.Length - 1]);
                            Console.WriteLine("Директория создана");
                            return;
                        }
                        else
                        {
                            Console.WriteLine("Путь не найден");
                            return;

                            //ошибка команды - путь не найден
                        }
                    }

                    if (i.Equals(path.Length - 1))
                    {
                        fileSystem.CurrentDirectory = store;
                    }
                }
                string s = "";
            }

            #endregion

            #region RD

            if (predicates[0].ToUpper().Equals("RD") && predicates.Length == 2)
            {
                char[] charsToTrim = { '\\' };

                string[] path = predicates[1].TrimEnd(charsToTrim).Split('\\');

                StoreData store = fileSystem.Find(path[0].ToUpper());

                //По относительному пути
                if (store == null)
                {
                    store = fileSystem.CurrentDirectory.Find(path[0]);
                    if (store != null)
                    {
                        for (int i = 1; i < path.Length; i++)
                        {
                            if (store.Find(path[i]) != null)
                            {
                                store = store.Find(path[i]);
                            }
                            else
                            {
                                return;

                                //ошибка команды - путь не найден
                            }

                            if (i.Equals(path.Length - 1))
                            {
                                if ((store as Directory) != null)
                                {
                                    if ((store as Directory).isEmpty())
                                    {
                                        Console.WriteLine("Удалили директорию");
                                        (store as Directory).ParentDirectory.DeleteDirectory(path[path.Length - 1]);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Нельзя удалить директорию с поддиректориями");
                                    }
                                }

                                return;
                            }
                        }
                        if ((store as Directory) != null)
                        {
                            if ((store as Directory).isEmpty())
                            {
                                Console.WriteLine("Удалили директорию");
                                (store as Directory).ParentDirectory.DeleteDirectory(path[path.Length - 1]);
                            }
                            else
                            {
                                Console.WriteLine("Нельзя удалить директорию с поддиректориями");
                            }
                        }
                    }
                    else
                    {
                        return;
                        //ошибка команды - путь не найден
                    }
                }

                for (int i = 1; i < path.Length; i++)
                {
                    if (store.Find(path[i]) != null)
                    {
                        store = store.Find(path[i]);
                    }
                    else
                    {
                        break;
                        //ошибка команды - путь не найден
                    }

                    if (i.Equals(path.Length - 1))
                    {
                        if ((store as Directory) != null)
                        {
                            if ((store as Directory).isEmpty())
                            {
                                Console.WriteLine("Удалили директорию");
                                (store as Directory).ParentDirectory.DeleteDirectory(path[path.Length - 1]);
                            }
                            else
                            {
                                Console.WriteLine("Нельзя удалить директорию с поддиректориями");
                            }
                        }
                    }
                }
                string s = "";
            }

            #endregion

            #region DELTREE

            if (predicates[0].ToUpper().Equals("DELTREE") && predicates.Length == 2)
            {
                char[] charsToTrim = { '\\' };

                string[] path = predicates[1].TrimEnd(charsToTrim).Split('\\');

                StoreData store = fileSystem.Find(path[0].ToUpper());

                //По относительному пути
                if (store == null)
                {
                    store = fileSystem.CurrentDirectory.Find(path[0]);
                    if (store != null)
                    {
                        for (int i = 1; i < path.Length; i++)
                        {
                            if (store.Find(path[i]) != null)
                            {
                                store = store.Find(path[i]);
                            }
                            else
                            {
                                return;

                                //ошибка команды - путь не найден
                            }

                            if (i.Equals(path.Length - 1))
                            {
                                if ((store as Directory) != null)
                                {
                                    Console.WriteLine("Удалили директорию");
                                    (store as Directory).ParentDirectory.DeleteDirectory(path[path.Length - 1]);
                                }

                                return;
                            }
                        }
                        if ((store as Directory) != null)
                        {
                            Console.WriteLine("Удалили директорию");
                            (store as Directory).ParentDirectory.DeleteDirectory(path[path.Length - 1]);
                        }
                    }
                    else
                    {
                        return;
                        //ошибка команды - путь не найден
                    }
                }

                for (int i = 1; i < path.Length; i++)
                {
                    if (store.Find(path[i]) != null)
                    {
                        store = store.Find(path[i]);
                    }
                    else
                    {
                        break;
                        //ошибка команды - путь не найден
                    }

                    if (i.Equals(path.Length - 1))
                    {
                        if ((store as Directory) != null)
                        {
                            Console.WriteLine("Удалили директорию");
                            (store as Directory).ParentDirectory.DeleteDirectory(path[path.Length - 1]);
                        }
                    }
                }
                string s = "";
            }

            #endregion

            #region Print

            if (predicates[0].ToUpper().Equals("PRINT") && predicates.Length == 1)
            {
                foreach (PrintingData VARIABLE in fileSystem.disks)
                {
                    Print(VARIABLE, "", true);
                }
            }

            #endregion

            #region MF

            if (predicates[0].ToUpper().Equals("MF") && predicates.Length == 2)
            {
                char[] charsToTrim = { '\\' };

                string[] path = predicates[1].TrimEnd(charsToTrim).Split('\\');

                StoreData store = fileSystem.Find(path[0].ToUpper());
                File      file;

                //По относительному пути
                if (store == null)
                {
                    file  = fileSystem.CurrentDirectory.FindFile(path[0]);
                    store = fileSystem.CurrentDirectory;

                    if (file != null)
                    {
                        Console.WriteLine("Такой файл уже существует");
                        return;
                    }
                    else
                    {
                        if (path.Length == 1)
                        {
                            Console.WriteLine("Файл создан");
                            fileSystem.CurrentDirectory.CreateFile(path[0]);
                            return;
                        }

                        else
                        {
                            Console.WriteLine("Путь не найден");
                            return;
                            //ошибка - директория не существует
                        }
                    }
                }

                for (int i = 1; i < path.Length - 1; i++)
                {
                    if (store.Find(path[i]) != null)
                    {
                        store = store.Find(path[i]);
                        if (i == path.Length - 2)
                        {
                            store.CreateFile(path[path.Length - 1]);
                            Console.WriteLine("Файл создан");
                            return;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Путь не найден");
                        return;
                    }
                }
                string s = "";
            }


            #endregion

            #region DEL

            if (predicates[0].ToUpper().Equals("DEL") && predicates.Length == 2)
            {
                char[] charsToTrim = { '\\' };

                string[] path = predicates[1].TrimEnd(charsToTrim).Split('\\');

                StoreData store = fileSystem.Find(path[0].ToUpper());
                File      file;

                //По относительному пути

                if (store == null)
                {
                    store = fileSystem.CurrentDirectory;

                    if (path.Length == 1)
                    {
                        if (store.FindFile(path[0]) != null)
                        {
                            store.DeleteFile(path[0]);
                            Console.WriteLine("Удалили файл");
                            return;
                        }
                        else
                        {
                            Console.WriteLine("Файла с таким именем не существует");
                            return;
                        }
                    }
                    else
                    {
                        for (int i = 1; i < path.Length - 1; i++)
                        {
                            if (store.Find(path[i]) != null)
                            {
                                store = store.Find(path[i]);
                                if (i == path.Length - 2)
                                {
                                    if (store.FindFile(path[path.Length - 1]) != null)
                                    {
                                        store.DeleteFile(path[path.Length - 1]);
                                        Console.WriteLine("Удалили файл");
                                        return;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Файла с таким именем не существует");
                                        return;
                                    }
                                }
                            }

                            else
                            {
                                Console.WriteLine("Путь не найден");
                                return;
                            }
                        }
                    }
                }


                for (int i = 1; i < path.Length - 1; i++)
                {
                    if (store.Find(path[i]) != null)
                    {
                        store = store.Find(path[i]);
                        if (i == path.Length - 2)
                        {
                            if (store.FindFile(path[path.Length - 1]) != null)
                            {
                                store.DeleteFile(path[path.Length - 1]);
                                Console.WriteLine("Удалили файл");
                                return;
                            }
                            else
                            {
                                Console.WriteLine("Файла с таким именем не существует");
                                return;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Путь не найден");
                        return;
                    }
                }
                string s = "";
            }

            #endregion

            #region Copy

            if (predicates[0].ToUpper().Equals("COPY") && predicates.Length == 3)
            {
                char[] charsToTrim = { '\\' };

                string[] pathFrom = predicates[1].TrimEnd(charsToTrim).Split('\\');
                string[] pathTo   = predicates[2].TrimEnd(charsToTrim).Split('\\');

                PrintingData from = getPathObject(fileSystem, pathFrom, true);
                PrintingData to   = getPathObject(fileSystem, pathTo, false);

                if (from != null && (to as StoreData) != null)
                {
                    if (from is File)
                    {
                        if ((to as StoreData).FindFile(from.Name) == null)
                        {
                            (to as StoreData).Copy(from as IData);
                            Console.WriteLine("Файл скопирован");
                        }
                        else
                        {
                            from = getPathObject(fileSystem, pathFrom, false);
                        }
                    }

                    if (from is StoreData)
                    {
                        if ((to as StoreData).Find(from.Name) == null)
                        {
                            (to as StoreData).Copy(from as IData);

                            Console.WriteLine("Директория была скопирована");
                        }
                        else
                        {
                            Console.WriteLine("Не удалось выполнить команду");
                        }
                    }
                }
            }

            #endregion

            #region Move

            if (predicates[0].ToUpper().Equals("MOVE") && predicates.Length == 3)
            {
                char[] charsToTrim = { '\\' };

                string[] pathFrom = predicates[1].TrimEnd(charsToTrim).Split('\\');
                string[] pathTo   = predicates[2].TrimEnd(charsToTrim).Split('\\');

                PrintingData from = getPathObject(fileSystem, pathFrom, true);
                PrintingData to   = getPathObject(fileSystem, pathTo, false);

                if (from != null && (to as StoreData) != null)
                {
                    if (from is File)
                    {
                        if ((to as StoreData).FindFile(from.Name) == null)
                        {
                            (to as StoreData).Copy(ObjectCopier.Clone(from) as IData);
                            (from as IData).ParentDirectory.Remove(from as IData);
                            Console.WriteLine("Файл был перемещен");
                        }
                        else
                        {
                            Console.WriteLine("Не удалось выполнить команду");
                        }
                    }

                    if (from is StoreData)
                    {
                        if ((to as StoreData).Find(from.Name) == null && to != from)
                        {
                            (to as StoreData).Copy(ObjectCopier.Clone(from) as IData);
                            (from as IData).ParentDirectory.Remove(from as IData);
                            Console.WriteLine("Директория была перемещен");
                        }
                        else
                        {
                            Console.WriteLine("Не удалось выполнить команду");
                        }
                    }
                }


                else
                {
                    Console.WriteLine("Невозможно переместить директорию в саму себя");
                    return;
                }
            }



            #endregion
        }