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

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