Доделаны требования и удалены ненужные файлы

This commit is contained in:
easynickname1 2024-05-26 21:09:44 +04:00
parent 7f7e5253a3
commit 01275ea1dd

View File

@ -116,41 +116,40 @@ public class StorageCollection<T>
File.Delete(filename); File.Delete(filename);
} }
StringBuilder sb = new(); using (StreamWriter wr = new StreamWriter(filename))
sb.Append(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
sb.Append(Environment.NewLine); wr.Write(_collectionKey);
// Не сохраняем пустые коллекции foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
if (value.Value.Count == 0)
{ {
continue; StringBuilder sb = new StringBuilder();
} sb.Append(Environment.NewLine);
// Не сохраняем пустые коллекции
sb.Append(value.Key); if (value.Value.Count == 0)
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.MaxCount);
sb.Append(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{ {
continue; continue;
} }
sb.Append(data); sb.Append(value.Key);
sb.Append(_separatorItems); sb.Append(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue);
sb.Append(value.Value.MaxCount);
sb.Append(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
}
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; return true;
} }
@ -166,62 +165,54 @@ public class StorageCollection<T>
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader sr = new StreamReader(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string str = sr.ReadLine();
UTF8Encoding temp = new(true); if (str == null || str.Length == 0)
while (fs.Read(b, 0, b.Length) > 0)
{
bufferTextFromFile += temp.GetString(b);
}
}
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].Equals(_collectionKey))
{
// Если нет такой записи, то это не те данные
return false;
}
_storages.Clear();
foreach (string data in strs)
{
string[] record = data.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; return false;
} }
collection.MaxCount = Convert.ToInt32(record[2]); if (!str.Equals(_collectionKey))
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{ {
if (elem?.CreateDrawingBoat() is T boat) // Если нет такой записи, то это не те данные
{ return false;
if (collection.Insert(boat) == 0)
{
return false;
}
}
} }
_storages.Add(record[0], collection); _storages.Clear();
} string data = "";
while ((data = sr.ReadLine()) != null)
{
string[] record = data.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);
foreach (string elem in set)
{
if (elem?.CreateDrawingBoat() is T boat)
{
if (collection.Insert(boat) == 0)
{
return false;
}
}
}
_storages.Add(record[0], collection);
}
}
return true; return true;
} }