PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/MapsCollection.cs

115 lines
3.8 KiB
C#
Raw Permalink Normal View History

2022-11-29 01:53:02 +04:00
using System;
using System.Collections.Generic;
using System.IO;
2022-11-29 01:53:02 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsApp1
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>> _mapStorages;
2022-11-29 01:53:02 +04:00
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
2022-11-29 01:53:02 +04:00
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>>();
2022-11-29 01:53:02 +04:00
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name)) return; //уникальное имя
else
{
_mapStorages.Add(name, new MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
2022-11-29 01:53:02 +04:00
}
}
public void DelMap(string name)
{
if (_mapStorages.ContainsKey(name))
{
_mapStorages.Remove(name);
}
}
public MapWithSetTraktorGeneric<IDrawningObject, AbstractMap> this[string ind]
2022-11-29 01:53:02 +04:00
{
get
{
_mapStorages.TryGetValue(ind, out var result);
return result;
}
}
private static void WriteToFile(string text, FileStream stream)
{
byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length);
}
2022-12-23 00:48:39 +04:00
public void SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter fs = new(filename))
{
fs.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
2022-11-29 01:53:02 +04:00
}
}
2022-11-29 01:53:02 +04:00
2022-12-23 00:48:39 +04:00
public void LoadData(string filename)
{
if (!File.Exists(filename))
{
2022-12-23 00:48:39 +04:00
throw new FileNotFoundException("Файл не найден");
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
2022-12-23 00:48:39 +04:00
throw new FileFormatException("Формат данных в файле неправильный");
}
//очищаем записи
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "Простая карта":
map = new SimpleMap();
break;
case "Поле":
map = new FieldMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetTraktorGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
2022-11-29 01:53:02 +04:00
}
}
}