7 работа + слив из 6
This commit is contained in:
commit
1ad78ace97
@ -110,37 +110,38 @@ namespace ProjectElectricLocomotive.Generics
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns></returns>
|
||||
public bool SaveData(string filename) {
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
StringBuilder data = new();
|
||||
foreach (
|
||||
KeyValuePair<string, LocomotivesGenericCollection<
|
||||
DrawningLocomotive, DrawningObjectLocomotive>>
|
||||
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<string, LocomotivesGenericCollection<
|
||||
DrawningLocomotive, DrawningObjectLocomotive>>
|
||||
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)
|
||||
{
|
||||
throw new InvalidOperationException("Невалиданя операция, нет данных длясохранения");
|
||||
}
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка информации по Локомативам в хранилище из файла
|
||||
@ -154,56 +155,44 @@ namespace ProjectElectricLocomotive.Generics
|
||||
throw new FileNotFoundException($"Файл {filename} не найден");
|
||||
}
|
||||
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)
|
||||
{
|
||||
throw new FileLoadException("Нет данных для загрузки");
|
||||
}
|
||||
if (!strs[0].StartsWith("LocomotiveStorage"))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
throw new ArgumentException("Неверный формат данных");
|
||||
}
|
||||
_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<DrawningLocomotive, DrawningObjectLocomotive>
|
||||
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<DrawningLocomotive, DrawningObjectLocomotive>
|
||||
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)
|
||||
{
|
||||
throw new InvalidOperationException("Ошибкадобавления в коллекцию");
|
||||
if ((collection + locomotive) == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_locomotiveStorage.Add(record[0], collection);
|
||||
}
|
||||
_locomotiveStorage.Add(record[0], collection);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user