Final version

This commit is contained in:
strwbrry1 2024-04-20 09:16:18 +04:00
parent cf07e0696e
commit 326e4cf801

View File

@ -77,23 +77,24 @@ namespace Catamaran.CollectionGenericObjects
File.Delete(filename);
}
StringBuilder sb = new();
sb.Append(_collectionKey);
using (StreamWriter writer = new(filename))
{
writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{
sb.Append(Environment.NewLine);
writer.Write(Environment.NewLine);
if (value.Value.Count == 0)
{
continue;
}
sb.Append(value.Key);
sb.Append(_separatorKeyValue);
sb.Append(value.Value.GetCollectionType);
sb.Append(_separatorKeyValue);
sb.Append(value.Value.MaxCount);
sb.Append(_separatorKeyValue);
writer.Write(value.Key);
writer.Write(_separatorKeyValue);
writer.Write(value.Value.GetCollectionType);
writer.Write(_separatorKeyValue);
writer.Write(value.Value.MaxCount);
writer.Write(_separatorKeyValue);
foreach (T? item in value.Value.GetItems())
{
@ -103,15 +104,14 @@ namespace Catamaran.CollectionGenericObjects
continue;
}
sb.Append(data);
sb.Append(_separatorItems);
writer.Write(data);
writer.Write(_separatorItems);
}
}
writer.Close();
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length);
fs.Close();
return true;
}
@ -122,32 +122,22 @@ namespace Catamaran.CollectionGenericObjects
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader reader = new(filename))
{
byte[] b = new byte[fs.Length];
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 (strs == null || strs.Length == 0)
string line = reader.ReadLine();
if (line == null || line.Length == 0)
{
return false;
}
if (!strs[0].Equals(_collectionKey))
if (!line.Equals(_collectionKey))
{
return false;
}
_storages.Clear();
foreach (string data in strs)
while ((line = reader.ReadLine()) != null)
{
string[] record = data.Split(_separatorKeyValue, StringSplitOptions.RemoveEmptyEntries);
string[] record = line.Split(_separatorKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
continue;
@ -173,8 +163,10 @@ namespace Catamaran.CollectionGenericObjects
}
}
}
_storages.Add(record[0], collection);
}
}
return true;
}