diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs index 44fde5e..68f1489 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs @@ -1,5 +1,4 @@ using ProjectElectricLocomotive.DrawingObjects; -using ProjectElectricLocomotive.Generics; using ProjectElectricLocomotive.MovementStrategy; using System; using System.Collections.Generic; @@ -7,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectElectricLocomotive +namespace ProjectElectricLocomotive.Generics { internal class LocomotivesGenericStorage { @@ -103,8 +102,7 @@ namespace ProjectElectricLocomotive File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _locomotivesStorage) + foreach (KeyValuePair> record in _locomotivesStorage) { StringBuilder records = new(); foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) @@ -117,10 +115,11 @@ namespace ProjectElectricLocomotive { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using StreamWriter fs = new StreamWriter(filename); + { + fs.WriteLine($"LocomotiveStorage{Environment.NewLine}"); + fs.WriteLine(data); + } return true; } /// @@ -134,56 +133,58 @@ namespace ProjectElectricLocomotive { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + + using (StreamReader fs = File.OpenText(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + + string str = fs.ReadLine(); + + if (str == null || str.Length == 0) { - 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("CarStorage")) - { - //если нет такой записи, то это не те данные - return false; - } - _locomotivesStorage.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) - { - continue; - } - LocomotivesGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + + if (!str.StartsWith("LocomotiveStorage")) { - DrawingLocomotive? loco = elem?.CreateDrawningCar(_separatorForObject, _pictureWidth, _pictureHeight); - if (loco != null) + //если нет такой записи, то это не те данные + return false; + } + + _locomotivesStorage.Clear(); + string strs = ""; + + + while ((strs = fs.ReadLine()) != null) + { + + if (strs == null) { - if (collection + loco == -1) + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotivesGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string thing in set) + { + DrawingLocomotive? loco = thing?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + if (loco != null) { - return false; + if ((collection + loco) == -1) + { + return false; + } } } + _locomotivesStorage.Add(record[0], collection); } - _locomotivesStorage.Add(record[0], collection); + return true; } - return true; } } } -