From 6da4c51da629af2961954e02de96f096a516cce9 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 6 Dec 2023 10:04:33 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=206=20?= =?UTF-8?q?=D0=B1=D0=B5=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Generics/LocomotivesGenericStorage.cs | 111 ++++++++---------- 1 file changed, 50 insertions(+), 61 deletions(-) diff --git a/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs b/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs index ddc84fb..b10a6c2 100644 --- a/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs +++ b/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs @@ -110,37 +110,38 @@ namespace ProjectElectricLocomotive.Generics /// /// Путь и имя файла /// - public bool SaveData(string filename) { + public bool SaveData(string filename) + { if (File.Exists(filename)) { File.Delete(filename); } StringBuilder data = new(); - foreach ( - KeyValuePair> - record in _locomotiveStorage) + using (StreamWriter sw = new StreamWriter(filename)) { - StringBuilder records = new(); - foreach (DrawningLocomotive? elem in record.Value.GetCars) + sw.WriteLine("LocomotiveStorage"); + if (_locomotiveStorage.Count == 0) { - records.Append( - $"{elem?.GetDataForSave(_separatorForObject)}" + - $"{_separatorRecords}"); + return false; } - data.Append($"{record.Key}{_separatorForKeyValue}{records}{Environment.NewLine}"); + foreach ( + KeyValuePair> + record in _locomotiveStorage) + { + sw.Write(record.Key); + sw.Write(_separatorForKeyValue); + foreach (DrawningLocomotive? elem in record.Value.GetCars) + { + sw.Write( + $"{elem?.GetDataForSave(_separatorForObject)}" + + $"{_separatorRecords}"); + } + sw.WriteLine(); + } + return true; } - if (data.Length == 0) - { - return false; - } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes( - $"LocomotiveStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); - return true; } /// /// Загрузка информации по Локомативам в хранилище из файла @@ -154,56 +155,44 @@ namespace ProjectElectricLocomotive.Generics 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) + string line; + if (!((line = sr.ReadLine() != null) && line.StartsWith("LocomotiveStorage"))) { - bufferTextFromFile += temp.GetString(b); + //если нет такой записи, то это не те данные + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("LocomotiveStorage")) - { - //если нет такой записи, то это не те данные - return false; - } - _locomotiveStorage.Clear(); - foreach (string data in strs) - { - string[] record = data.Split( - _separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) + _locomotiveStorage.Clear(); + while ((line = sr.ReadLine()) != null) { - continue; - } - LocomotivesGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); - Array.Reverse(set); - foreach (string elem in set) - { - DrawningLocomotive? locomotive = - elem?.CreateDrawningLocomotive( - _separatorForObject, - _pictureWidth, _pictureHeight); - if (locomotive != null) + string[] record = line.Split( + _separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - if ((collection + locomotive) == 0) + continue; + } + LocomotivesGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningLocomotive? locomotive = + elem?.CreateDrawningLocomotive( + _separatorForObject, + _pictureWidth, _pictureHeight); + if (locomotive != null) { - return false; + if ((collection + locomotive) == 0) + { + return false; + } } } + _locomotiveStorage.Add(record[0], collection); } - _locomotiveStorage.Add(record[0], collection); + return true; } - return true; } } }