Compare commits

...

2 Commits

Author SHA1 Message Date
d85edc7af6 lastkonflict 2024-06-12 18:11:50 +03:00
2d8d0c28a9 so ism 2024-04-24 14:48:16 +03:00

View File

@ -100,47 +100,47 @@ public class StorageCollection<T>
}
}
public bool SaveData(string filename)
public bool SaveData(string filename)
{
if (_storages.Count == 0)
if (_storages.Count == 0)
{
throw new Exception("В хранилище отсутствуют коллекции для сохранения");
}
if (File.Exists(filename))
if (File.Exists(filename))
{
File.Delete(filename);
File.Delete(filename);
}
using (StreamWriter writer = new(filename))
using (StreamWriter writer = new(filename))
{
writer.Write(_collectionKey);
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции
writer.Write(Environment.NewLine);
if (value.Value.Count == 0)
{
continue;
continue;
}
writer.Write(value.Key);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.GetCollectionType);
writer.Write(_separatorForKeyValue);
writer.Write(value.Value.MaxCount);
writer.Write(_separatorForKeyValue);
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))
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
continue;
}
writer.Write(data);
writer.Write(data);
writer.Write(_separatorItems);
}
}
}
return true;
return true;
}
/// <summary>
@ -154,69 +154,51 @@ public class StorageCollection<T>
{
throw new Exception("Файл не существует");
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader fs = File.OpenText(filename))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
string str = fs.ReadLine();
if (str == null || str.Length == 0)
{
bufferTextFromFile += temp.GetString(b);
return false;
}
}
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
throw new Exception("В файле нет данных");
}
if (!strs[0].Equals(_collectionKey))
{
throw new Exception("В файле неверные данные");
}
_storages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
if (!str.StartsWith(_collectionKey))
{
continue;
return false;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType) ??
throw new Exception("Не удалось определить тип коллекции:" + record[1]);
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
_storages.Clear();
string strs = "";
while ((strs = fs.ReadLine()) != null)
{
if (elem?.CreateDrawningWarmlyShip() is T car)
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
try
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);
foreach (string elem in set)
{
if (elem?.CreateDrawningWarmlyShip() is T warmlyship)
{
if (!collection.Insert(car))
if (!collection.Insert(warmlyship))
{
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
return false;
}
}
catch (CollectionOverflowException ex)
{
throw new Exception("Коллекция переполнена", ex);
}
}
_storages.Add(record[0], collection);
}
_storages.Add(record[0], collection);
return true;
}
return true;
}
/// <summary>
/// Создание коллекции по типу
/// </summary>