Боровков М В ПИбд-22 6 лабораторная работа #7
@ -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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка информации по Локомативам в хранилище из файла
|
||||
@ -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<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)
|
||||
{
|
||||
return false;
|
||||
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