This commit is contained in:
easynickname1 2024-05-26 21:56:15 +04:00
commit 491ddf5ae5

View File

@ -116,11 +116,12 @@ public class StorageCollection<T>
File.Delete(filename);
}
StringBuilder sb = new();
sb.Append(_collectionKey);
using (StreamWriter wr = new StreamWriter(filename))
{
wr.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
StringBuilder sb = new StringBuilder();
sb.Append(Environment.NewLine);
// Не сохраняем пустые коллекции
if (value.Value.Count == 0)
@ -146,11 +147,10 @@ public class StorageCollection<T>
sb.Append(data);
sb.Append(_separatorItems);
}
wr.Write(sb);
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length);
}
return true;
}
/// <summary>
@ -164,30 +164,23 @@ public class StorageCollection<T>
throw new Exception("Файл не существует");
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(filename))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
string str = sr.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)
if (!str.Equals(_collectionKey))
{
throw new Exception("В файле нет данных");
}
if (!strs[0].Equals(_collectionKey))
{
throw new Exception("В файле неверные данные");
// Если нет такой записи, то это не те данные
return false;
}
_storages.Clear();
foreach (string data in strs)
string data = "";
while ((data = sr.ReadLine()) != null)
{
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
@ -196,8 +189,12 @@ public class StorageCollection<T>
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType) ??
throw new Exception("Не удалось определить тип коллекции:" + 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);
@ -205,22 +202,18 @@ public class StorageCollection<T>
{
if (elem?.CreateDrawingBoat() is T boat)
{
try
if (collection.Insert(boat) == 0)
{
if (collection.Insert(boat) == -1)
{
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
}
}
catch (CollectionOverflowException ex)
{
throw new Exception("Коллекция переполнена", ex);
return false;
}
}
}
_storages.Add(record[0], collection);
}
}
return true;
}
/// <summary>
/// Создание коллекции по типу