diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs index 21ce849..989b385 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/StorageCollection.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace DoubleDeckerBus.CollectionGenericObjects; @@ -90,39 +91,43 @@ public class StorageCollection File.Delete(filename); } - StringBuilder sb = new(); + using StreamWriter sw = new(filename); + { - sb.Append(_collectionKey); - foreach (KeyValuePair> value in _storages) - { - 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); + sw.WriteLine(_collectionKey); + - foreach (T? item in value.Value.GetItems()) + foreach (KeyValuePair> value in _storages) { - string data = item?.GetDataForSave() ?? string.Empty; - if (string.IsNullOrEmpty(data)) + StringBuilder sb = new(); + + + if (value.Value.Count == 0) { continue; } - sb.Append(data); - sb.Append(_separatorItems); + 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(_separatorItems); + } + + sw.WriteLine(sb.ToString()); } } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); return true; } @@ -134,67 +139,54 @@ public class StorageCollection return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) - { - bufferTextFromFile += temp.GetString(b); - } - } + string? bufferLine = sr.ReadLine(); - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - - if (strs == null || strs.Length == 0) - { - return false; - } - - if (!strs[0].Equals(_collectionKey)) - { - //если нет такой записи, то это не те данные - return false; - } - - _storages.Clear(); - - foreach (string data in strs) - { - 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) + if (bufferLine != _collectionKey) { return false; } - collection.MaxCount = Convert.ToInt32(record[2]); + _storages.Clear(); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - - foreach (string elem in set) + while ((bufferLine = sr.ReadLine()) != null) { - if (elem?.CreateDrawningCar() is T car) + string[] record = bufferLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + + if (record.Length != 4) { - if (collection.Insert(car) == -1) + 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(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + + foreach (string elem in set) + { + if (elem?.CreateDrawningCar() is T car) { - return false; + if (collection.Insert(car) == -1) + { + return false; + } } } + _storages.Add(record[0], collection); } - _storages.Add(record[0], collection); + return true; } - return true; + } ///