From 9010db3b24e31ebeb5e4db2f93999897dd7e6d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=95=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= Date: Thu, 29 Aug 2024 21:57:17 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B8=D0=B2?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=87=D1=91=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericStorage.cs | 68 +++++++++++----------- Sailboat/Sailboat/ExtentionDrawningBoat.cs | 2 +- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index 0eb3a8a..f39358c 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -128,7 +128,7 @@ namespace Sailboat.Generics /// Загрузка информации по лодкам в хранилище из файла /// /// Путь и имя файла - /// true - загрузка прошла успешно, false - ошибка призагрузке данных + /// true - загрузка прошла успешно, false - ошибка при загрузке данных public bool LoadData(string filename) { if (!File.Exists(filename)) @@ -136,50 +136,52 @@ namespace Sailboat.Generics return false; } - using (StreamReader bs = File.OpenText(filename)) + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) { - string str = bs.ReadLine(); - if (str == null || str.Length == 0) + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) { - return false; + bufferTextFromFile += temp.GetString(b); } - if (!str.StartsWith("BoatStorage")) + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("BoatStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + + _boatStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - return false; + continue; } - - _boatStorages.Clear(); - string strs = ""; - - while ((strs = bs.ReadLine()) != null) + BoatsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) { - if (strs == null) + DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); + if (boat != null) { - return false; - } - - string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) - { - continue; - } - BoatsGenericCollection collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) - { - DrawingBoat? boat = elem?.CreateDrawingBoat(_separatorForObject, _pictureWidth, _pictureHeight); - if (boat != null) + if (!(collection + boat)) { - if (!(collection + boat)) - { - return false; - } + return false; } } - _boatStorages.Add(record[0], collection); } - return true; + _boatStorages.Add(record[0], collection); } + return true; } } } \ No newline at end of file diff --git a/Sailboat/Sailboat/ExtentionDrawningBoat.cs b/Sailboat/Sailboat/ExtentionDrawningBoat.cs index 4e24140..b6e7320 100644 --- a/Sailboat/Sailboat/ExtentionDrawningBoat.cs +++ b/Sailboat/Sailboat/ExtentionDrawningBoat.cs @@ -40,7 +40,7 @@ namespace Sailboat.DrawingObjects /// /// Получение данных для сохранения в файл /// - /// Сохраняемый объект + /// Сохраняемый объект /// Разделитель даннных /// Строка с данными по объекту public static string GetDataForSave(this DrawingBoat drawingBoat, char separatorForObject)