From a8023f8637ee570ace08db998d1c4c1dca6e80c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=20=D0=9F=D0=B5=D1=80=D0=BC=D1=8F?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2?= Date: Mon, 22 Apr 2024 13:51:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 140 ++++++++++-------- 1 file changed, 76 insertions(+), 64 deletions(-) diff --git a/AccordionBus/AccordionBus/CollectionGenericObjects/StorageCollection.cs b/AccordionBus/AccordionBus/CollectionGenericObjects/StorageCollection.cs index 7f6248c..c1f6f09 100644 --- a/AccordionBus/AccordionBus/CollectionGenericObjects/StorageCollection.cs +++ b/AccordionBus/AccordionBus/CollectionGenericObjects/StorageCollection.cs @@ -49,84 +49,96 @@ namespace AccordionBus.CollectionGenericObjects public bool SaveData(string filename) { - if (_storage.Count == 0) return false; - - if (File.Exists(filename)) File.Delete(filename); - - StringBuilder sb = new(); - - sb.Append(_collectionKey); - foreach(KeyValuePair> value in _storage) + if (_storage.Count == 0) { - sb.Append(Environment.NewLine); - if (value.Value.Count == 0) { continue; } - - sb.Append(value.Key); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.MaxCount); - sb.Append(_separatorForKeyValue); - - foreach(T? item in value.Value.GetItems()) - { - string data = item?.GetDataForSave() ?? string.Empty; - if (string.IsNullOrEmpty(data)) continue; - - sb.Append(data); - sb.Append(_separatorItem); - } + return false; + } + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storage) + { + StringBuilder sb = new(); + sb.Append(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) + { + continue; + } + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + sb.Append(data); + sb.Append(_separatorItem); + } + writer.Write(sb); + } + return true; } - - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding().GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); - return true; } public bool LoadData(string filename) { - if (!File.Exists(filename)) return false; - - string bufferTextFromFile = ""; - using(FileStream fs = new(filename, FileMode.Open)) + if (!File.Exists(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new UTF8Encoding(true); - while (fs.Read(b, 0, b.Length) > 0) - { - bufferTextFromFile += temp.GetString(b); - } + return false; } - - string[] strs = bufferTextFromFile.Split(new char[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries); - if (strs.Length == 0 || strs == null) return false; - if (!strs[0].Equals(_collectionKey)) return false; - - _storage.Clear(); - foreach(string data in strs) + using (StreamReader reader = File.OpenText(filename)) { - string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) continue; - - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); - if (collection == null) return false; - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItem, StringSplitOptions.RemoveEmptyEntries); - foreach(string elem in set) + string str = reader.ReadLine(); + if (str == null || str.Length == 0) { - if (elem?.CreateDrawningBus() is T bus) + return false; + } + if (!str.StartsWith(_collectionKey)) + { + return false; + } + _storage.Clear(); + string strs = ""; + while ((strs = reader.ReadLine()) != null) + { + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { - if (!collection.Insert(bus)) return false; + continue; } + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) + { + return false; + } + collection.MaxCount = Convert.ToInt32(record[2]); + string[] set = record[3].Split(_separatorItem, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + if (elem?.CreateDrawningBus() is T bus) + { + if (!collection.Insert(bus)) + { + return false; + } + } + } + _storage.Add(record[0], collection); } - - _storage.Add(record[0], collection); + return true; } - - return true; } private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType)