From c570def4d68efc1b2221e3c4b7fd9c2b0a6d1c4c Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Thu, 21 Dec 2023 21:14:15 +0400 Subject: [PATCH] Upload files to '' --- LainersGenericStorage.cs | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 LainersGenericStorage.cs diff --git a/LainersGenericStorage.cs b/LainersGenericStorage.cs new file mode 100644 index 0000000..4d6ce73 --- /dev/null +++ b/LainersGenericStorage.cs @@ -0,0 +1,125 @@ +using ProjectLainer.DrawningObjects; +using ProjectLainer.MovementStrategy; +using System.Text; + +namespace ProjectLainer.Generics +{ + internal class LainersGenericStorage + { + + readonly Dictionary> _lainerStorages; + + public List Keys => _lainerStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + public LainersGenericStorage(int pictureWidth, int pictureHeight) + { + _lainerStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + _lainerStorages.Add(name, new LainersGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) + { + if (!_lainerStorages.ContainsKey(name)) + { + return; + } + _lainerStorages.Remove(name); + } + + public LainersGenericCollection? this[string ind] + { + get + { + if (_lainerStorages.ContainsKey(ind)) + { + return _lainerStorages[ind]; + } + return null; + } + } + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; + public void SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _lainerStorages) + { + StringBuilder records = new(); + foreach (DrawingEntity? elem in record.Value.GetLainers) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + throw new ArgumentException("Невалиданя операция, нет данных для сохранения"); + } + using (StreamWriter sw = new(filename)) + { + sw.WriteLine($"LainerStorage{Environment.NewLine}{data}"); + } + } + public void LoadData(string filename) + { + if (!File.Exists(filename)) + { + throw new NullReferenceException("Файл не найден"); + } + using (StreamReader sr = new(filename)) + { + string str = sr.ReadLine(); + var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + throw new NullReferenceException("нет данных для загрузки"); + } + if (!strs[0].StartsWith("LainerStorage")) + { + throw new ArgumentException("неверный фориат данных"); + } + _lainerStorages.Clear(); + do + { + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + str = sr.ReadLine(); + continue; + } + LainersGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingEntity? lainer = elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight); + if (lainer != null) + { + if (!(collection + lainer)) + { + throw new IOException("Ошибка добавления в коллекцию"); + + } + } + } + _lainerStorages.Add(record[0], collection); + str = sr.ReadLine(); + } while (str != null); + } + } + } +} +