This commit is contained in:
mar-va 2024-04-20 10:14:45 +04:00
parent 2365620cf6
commit 86aefff199

View File

@ -50,8 +50,6 @@ public class StorageCollection<T>
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
{
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
// TODO Прописать логику для добавления
if (_storages.ContainsKey(name) || name == "") return;
if (collectionType == CollectionType.Massive)
@ -69,7 +67,6 @@ public class StorageCollection<T>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
// TODO Прописать логику для удаления коллекции
_storages.Remove(name);
}
@ -82,7 +79,6 @@ public class StorageCollection<T>
{
get
{
// TODO Продумать логику получения объекта
if (name == "")
{
return null;
@ -101,32 +97,34 @@ public class StorageCollection<T>
{
File.Delete(filename);
}
using FileStream fs = new(filename, FileMode.Create);
using StreamWriter sw = new StreamWriter(fs);
sw.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
using (StreamWriter writer = new(filename))
{
sw.Write(Environment.NewLine);
// не сохраняем пустые коллекции
if (value.Value.Count == 0)
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
continue;
}
sw.Write(value.Key);
sw.Write(_separatorForKeyValue);
sw.Write(value.Value.GetCollectionType);
sw.Write(_separatorForKeyValue);
sw.Write(value.Value.MaxCount);
sw.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции
if (value.Value.Count == 0)
{
continue;
}
sw.Write(data);
sw.Write(_separatorItems);
writer.Write(value.Key);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.GetCollectionType);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.MaxCount);
writer.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
}
writer.Write(data);
writer.Write(_separatorItems);
}
}
}
return true;
@ -138,47 +136,43 @@ public class StorageCollection<T>
{
return false;
}
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader reader = new(filename))
{
using StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
if (str == null || str.Length == 0)
string line = reader.ReadLine();
if (line == null || line.Length == 0)
{
return false;
}
if (!str.Equals(_collectionKey))
if (!line.Equals(_collectionKey))
{
return false;
}
_storages.Clear();
while (!sr.EndOfStream)
while ((line = reader.ReadLine()) != null)
{
string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
string[] record = line.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
continue;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
return false;
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
string[] set = record[3].Split(_separatorItems,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
if (elem?.CreateDrawningBus() is T bus)
if (elem?.CreateDrawningBus() is T truck)
{
if (collection.Insert(bus) == -1)
if (collection.Insert(truck) == -1)
{
return false;
}
}
}
_storages.Add(record[0], collection);