128 lines
4.8 KiB
C#
Raw Normal View History

using ProjectBulldozer.Drawing;
using ProjectBulldozer.Drawning;
using System.Text;
namespace ProjectBulldozer.Generics
{
internal class TractorGenericStorage
{
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
readonly Dictionary<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> _TractorsStorage;
public List<string> Keys => _TractorsStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public TractorGenericStorage(int pictureWidth, int pictureHeight)
{
_TractorsStorage = new Dictionary<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_TractorsStorage.ContainsKey(name))
{
_TractorsStorage.Add(name, new TractorGenericCollection<DrawingTractor, DrawingObjectTractor>(_pictureWidth, _pictureHeight));
}
}
public void DelSet(string name)
{
if (_TractorsStorage.ContainsKey(name))
{
_TractorsStorage.Remove(name);
}
}
public TractorGenericCollection<DrawingTractor, DrawingObjectTractor>? this[string ind]
{
get
{
if (_TractorsStorage.ContainsKey(ind))
{
return _TractorsStorage[ind];
}
return null;
}
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>
record in _TractorsStorage)
{
StringBuilder records = new();
foreach (DrawingTractor? elem in record.Value.GetTractors)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
throw new InvalidOperationException("Невалиданя операция, нет данных для сохранения");
}
using StreamWriter sw = new(filename);
sw.Write($"TractorsStorage{Environment.NewLine}{data}");
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = File.OpenText(filename))
{
string str = sr.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!str.StartsWith("TractorsStorage"))
{
throw new FormatException("Неверный формат данных");
}
_TractorsStorage.Clear();
string strs = "";
while ((strs = sr.ReadLine()) != null)
{
if (strs == null)
{
return false;
}
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
TractorGenericCollection<DrawingTractor, DrawingObjectTractor> collection =
new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingTractor? tractor = elem?.CreateDrawingTractor(_separatorForObject,
_pictureWidth, _pictureHeight);
if (tractor != null)
{
if ((collection + tractor) == -1)
{
throw new ApplicationException("Ошибка добавления в коллекцию");
}
}
}
_TractorsStorage.Add(record[0], collection);
}
return true;
}
}
}
}