FileStream заменён на StreamReader и StreamWriter

This commit is contained in:
tyxz0 2024-05-11 09:14:27 +04:00
parent a57a758f1f
commit 0b7ebcdcc0

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace DoubleDeckerBus.CollectionGenericObjects; namespace DoubleDeckerBus.CollectionGenericObjects;
@ -90,39 +91,43 @@ public class StorageCollection<T>
File.Delete(filename); File.Delete(filename);
} }
StringBuilder sb = new(); using StreamWriter sw = new(filename);
{
sb.Append(_collectionKey); sw.WriteLine(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
sb.Append(Environment.NewLine);
// не сохраняем пустые коллекции
if (value.Value.Count == 0)
{
continue;
}
sb.Append(value.Key);
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()) foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
string data = item?.GetDataForSave() ?? string.Empty; StringBuilder sb = new();
if (string.IsNullOrEmpty(data))
if (value.Value.Count == 0)
{ {
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);
}
sw.WriteLine(sb.ToString());
} }
} }
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;
} }
@ -134,67 +139,54 @@ public class StorageCollection<T>
return false; return false;
} }
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) using (StreamReader sr = new(filename))
{ {
byte[] b = new byte[fs.Length]; string? bufferLine = sr.ReadLine();
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
{
bufferTextFromFile += temp.GetString(b);
}
}
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); if (bufferLine != _collectionKey)
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]); _storages.Clear();
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); while ((bufferLine = sr.ReadLine()) != null)
foreach (string elem in set)
{ {
if (elem?.CreateDrawningCar() is T car) string[] record = bufferLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{ {
if (collection.Insert(car) == -1) 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?.CreateDrawningCar() is T car)
{ {
return false; if (collection.Insert(car) == -1)
{
return false;
}
} }
} }
_storages.Add(record[0], collection);
} }
_storages.Add(record[0], collection); return true;
} }
return true;
} }
/// <summary> /// <summary>