переделал StreamWriter и StreamReader

This commit is contained in:
Kirill 2023-12-25 21:36:16 +04:00
parent e785755a98
commit 39e4865cc7
6 changed files with 62 additions and 73 deletions

View File

@ -59,8 +59,8 @@ namespace ProjectTractor.DrawningObjects
{
return str;
}
return
$"{str}{separatorForObject}{bulldoser.AdditionalColor.Name}{separatorForObject}{bulldoser.WheelsOrnament}{separatorForObject}{bulldoser.Blade}";
str = $"{str}{separatorForObject}{bulldoser.AdditionalColor.Name}{separatorForObject}{bulldoser.WheelsOrnament}{separatorForObject}{bulldoser.Blade}";
return str;
}
}
}

View File

@ -261,7 +261,7 @@ namespace ProjectTractor
this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Left;
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(620, 532);
this.pictureBoxCollection.Size = new System.Drawing.Size(643, 532);
this.pictureBoxCollection.TabIndex = 1;
this.pictureBoxCollection.TabStop = false;
//
@ -273,7 +273,7 @@ namespace ProjectTractor
this.groupBox.Controls.Add(this.ButtonAddTractor);
this.groupBox.Controls.Add(this.maskedTextBoxNumber);
this.groupBox.Controls.Add(this.file);
this.groupBox.Location = new System.Drawing.Point(626, 12);
this.groupBox.Location = new System.Drawing.Point(649, 12);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(255, 524);
this.groupBox.TabIndex = 2;
@ -404,7 +404,7 @@ namespace ProjectTractor
//
// FormTractorCollection
//
this.ClientSize = new System.Drawing.Size(893, 532);
this.ClientSize = new System.Drawing.Size(916, 532);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.pictureBoxCollection);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

View File

@ -166,7 +166,7 @@
// panelBlue
//
this.panelBlue.AllowDrop = true;
this.panelBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
this.panelBlue.BackColor = System.Drawing.Color.Blue;
this.panelBlue.Location = new System.Drawing.Point(98, 23);
this.panelBlue.Name = "panelBlue";
this.panelBlue.Size = new System.Drawing.Size(40, 40);
@ -188,7 +188,7 @@
// panelGreen
//
this.panelGreen.AllowDrop = true;
this.panelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
this.panelGreen.BackColor = System.Drawing.Color.Green;
this.panelGreen.Location = new System.Drawing.Point(52, 23);
this.panelGreen.Name = "panelGreen";
this.panelGreen.Size = new System.Drawing.Size(40, 40);

View File

@ -142,7 +142,7 @@ namespace ProjectTractor.Generics
if (tractor != null)
{
int countRows = _pictureWidth / _placeSizeWidth;
tractor.SetPosition(_pictureWidth - _placeSizeWidth * 2 - (i % countRows * _placeSizeWidth) + 20, _pictureHeight - i / countRows * _placeSizeHeight - 160);
tractor.SetPosition(_pictureWidth - _placeSizeWidth * 2 - (i % countRows * _placeSizeWidth) + 210, _pictureHeight - i / countRows * _placeSizeHeight - 160);
tractor.DrawTransport(g);
}
i++;

View File

@ -90,40 +90,38 @@ namespace ProjectTractor
}
}
/// <summary>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
TractorsGenericCollection<DrawningTractor, DrawningObjectTractor>> record in _tractorStorages)
{
StringBuilder records = new();
foreach (DrawningTractor? elem in record.Value.GetTractors)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string,
TractorsGenericCollection<DrawningTractor, DrawningObjectTractor>> record in _tractorStorages)
{
StringBuilder records = new();
foreach (DrawningTractor? elem in record.Value.GetTractors)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new
UTF8Encoding(true).GetBytes($"TractorStorage{Environment.NewLine}{data}");
fs.Write(info, 0, info.Length);
return true;
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
return false;
}
using (StreamWriter streamWriter = new(filename))
{
streamWriter.WriteLine($"TractorStorage{Environment.NewLine}{data}");
}
return true;
}
/// <summary>
/// Загрузка информации по автомобилям в хранилище из файла
/// </summary>
@ -135,53 +133,43 @@ namespace ProjectTractor
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader streamReader = new(filename))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
string str = streamReader.ReadLine();
string[] strings = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (strings == null || strings.Length == 0)
{
bufferTextFromFile += temp.GetString(b);
return false;
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("TractorStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_tractorStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
if (!strings[0].StartsWith("TractorStorage"))
{
continue;
return false;
}
TractorsGenericCollection<DrawningTractor, DrawningObjectTractor>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
_tractorStorages.Clear();
do
{
DrawningTractor? tractor =
elem?.CreateDrawningTractor(_separatorForObject, _pictureWidth, _pictureHeight);
if (tractor != null)
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
if (!(collection + tractor))
str = streamReader.ReadLine();
continue;
}
TractorsGenericCollection<DrawningTractor, DrawningObjectTractor> collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningTractor? bus = elem?.CreateDrawningTractor(_separatorForObject, _pictureWidth, _pictureHeight);
if (bus != null)
{
return false;
if (!(collection + bus))
{
return false;
}
}
}
}
_tractorStorages.Add(record[0], collection);
_tractorStorages.Add(record[0], collection);
str = streamReader.ReadLine();
} while (str != null);
}
return true;
}

View File

@ -1,3 +1,4 @@
TractorStorage
апр|100:100:White;100:100:ff0000c0:Yellow:True:True;
fff|100:100:White:Yellow:False:True;100:100:White:Black:False:True;
gg|100:100:Yellow;100:100:Blue:Green:True:True;
ii|100:100:Purple:Green:True:True;100:100:Red:Yellow:True:True;