Upload files to ''

This commit is contained in:
Ivan_Starostin 2023-12-21 21:14:15 +04:00
parent f8a8938579
commit c570def4d6

125
LainersGenericStorage.cs Normal file
View File

@ -0,0 +1,125 @@
using ProjectLainer.DrawningObjects;
using ProjectLainer.MovementStrategy;
using System.Text;
namespace ProjectLainer.Generics
{
internal class LainersGenericStorage
{
readonly Dictionary<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> _lainerStorages;
public List<string> Keys => _lainerStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public LainersGenericStorage(int pictureWidth, int pictureHeight)
{
_lainerStorages = new Dictionary<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_lainerStorages.Add(name, new LainersGenericCollection<DrawingEntity, DrawningObjectLainer>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_lainerStorages.ContainsKey(name))
{
return;
}
_lainerStorages.Remove(name);
}
public LainersGenericCollection<DrawingEntity, DrawningObjectLainer>? this[string ind]
{
get
{
if (_lainerStorages.ContainsKey(ind))
{
return _lainerStorages[ind];
}
return null;
}
}
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
public void SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, LainersGenericCollection<DrawingEntity, DrawningObjectLainer>> record in _lainerStorages)
{
StringBuilder records = new();
foreach (DrawingEntity? elem in record.Value.GetLainers)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
throw new ArgumentException("Невалиданя операция, нет данных для сохранения");
}
using (StreamWriter sw = new(filename))
{
sw.WriteLine($"LainerStorage{Environment.NewLine}{data}");
}
}
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
throw new NullReferenceException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
throw new NullReferenceException("нет данных для загрузки");
}
if (!strs[0].StartsWith("LainerStorage"))
{
throw new ArgumentException("неверный фориат данных");
}
_lainerStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
LainersGenericCollection<DrawingEntity, DrawningObjectLainer> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingEntity? lainer = elem?.CreateDrawningLainer(_separatorForObject, _pictureWidth, _pictureHeight);
if (lainer != null)
{
if (!(collection + lainer))
{
throw new IOException("Ошибка добавления в коллекцию");
}
}
}
_lainerStorages.Add(record[0], collection);
str = sr.ReadLine();
} while (str != null);
}
}
}
}