save data with stream writer

This commit is contained in:
ekallin 2023-11-16 21:33:46 +04:00
parent aee4ec8c0e
commit e3b1101da1
6 changed files with 94 additions and 25 deletions

View File

@ -35,7 +35,7 @@ namespace ProjectElectricLocomotive
height height
); );
} }
if (strs.Length == 7) if (strs.Length == 6)
{ {
return new DrawingElectricLocomotive( return new DrawingElectricLocomotive(
Convert.ToInt32(strs[0]), Convert.ToInt32(strs[0]),

View File

@ -210,12 +210,14 @@
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.SaveToolStripMenuItem.Text = "Сохранить"; this.SaveToolStripMenuItem.Text = "Сохранить";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.LoadToolStripMenuItem.Text = "Загрузить"; this.LoadToolStripMenuItem.Text = "Загрузить";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
// saveFileDialog // saveFileDialog
// //

View File

@ -172,6 +172,19 @@ namespace ProjectElectricLocomotive
private void LoadToolStripMenuItem_Click(object sender, EventArgs e) private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{ {
// TODO продумать логику // TODO продумать логику
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Load is done!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadObjects();
}
else
{
MessageBox.Show("Load is fail!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} }
} }
} }

View File

@ -205,7 +205,7 @@
// panelPastelViolet // panelPastelViolet
// //
this.panelPastelViolet.AllowDrop = true; this.panelPastelViolet.AllowDrop = true;
this.panelPastelViolet.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); this.panelPastelViolet.BackColor = System.Drawing.Color.Plum;
this.panelPastelViolet.Location = new System.Drawing.Point(281, 110); this.panelPastelViolet.Location = new System.Drawing.Point(281, 110);
this.panelPastelViolet.Name = "panelPastelViolet"; this.panelPastelViolet.Name = "panelPastelViolet";
this.panelPastelViolet.Size = new System.Drawing.Size(40, 40); this.panelPastelViolet.Size = new System.Drawing.Size(40, 40);
@ -214,7 +214,7 @@
// panelPastelLilac // panelPastelLilac
// //
this.panelPastelLilac.AllowDrop = true; this.panelPastelLilac.AllowDrop = true;
this.panelPastelLilac.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); this.panelPastelLilac.BackColor = System.Drawing.Color.RoyalBlue;
this.panelPastelLilac.Location = new System.Drawing.Point(207, 110); this.panelPastelLilac.Location = new System.Drawing.Point(207, 110);
this.panelPastelLilac.Name = "panelPastelLilac"; this.panelPastelLilac.Name = "panelPastelLilac";
this.panelPastelLilac.Size = new System.Drawing.Size(40, 40); this.panelPastelLilac.Size = new System.Drawing.Size(40, 40);
@ -223,7 +223,7 @@
// panelSkyBlue // panelSkyBlue
// //
this.panelSkyBlue.AllowDrop = true; this.panelSkyBlue.AllowDrop = true;
this.panelSkyBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); this.panelSkyBlue.BackColor = System.Drawing.Color.Turquoise;
this.panelSkyBlue.Location = new System.Drawing.Point(132, 110); this.panelSkyBlue.Location = new System.Drawing.Point(132, 110);
this.panelSkyBlue.Name = "panelSkyBlue"; this.panelSkyBlue.Name = "panelSkyBlue";
this.panelSkyBlue.Size = new System.Drawing.Size(40, 40); this.panelSkyBlue.Size = new System.Drawing.Size(40, 40);
@ -232,7 +232,7 @@
// panelPastelGreen // panelPastelGreen
// //
this.panelPastelGreen.AllowDrop = true; this.panelPastelGreen.AllowDrop = true;
this.panelPastelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.panelPastelGreen.BackColor = System.Drawing.Color.PaleGreen;
this.panelPastelGreen.Location = new System.Drawing.Point(55, 110); this.panelPastelGreen.Location = new System.Drawing.Point(55, 110);
this.panelPastelGreen.Name = "panelPastelGreen"; this.panelPastelGreen.Name = "panelPastelGreen";
this.panelPastelGreen.Size = new System.Drawing.Size(40, 40); this.panelPastelGreen.Size = new System.Drawing.Size(40, 40);
@ -241,7 +241,7 @@
// panelPastelYellow // panelPastelYellow
// //
this.panelPastelYellow.AllowDrop = true; this.panelPastelYellow.AllowDrop = true;
this.panelPastelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.panelPastelYellow.BackColor = System.Drawing.Color.LightYellow;
this.panelPastelYellow.Location = new System.Drawing.Point(281, 35); this.panelPastelYellow.Location = new System.Drawing.Point(281, 35);
this.panelPastelYellow.Name = "panelPastelYellow"; this.panelPastelYellow.Name = "panelPastelYellow";
this.panelPastelYellow.Size = new System.Drawing.Size(40, 40); this.panelPastelYellow.Size = new System.Drawing.Size(40, 40);
@ -250,7 +250,7 @@
// panelPastelOrange // panelPastelOrange
// //
this.panelPastelOrange.AllowDrop = true; this.panelPastelOrange.AllowDrop = true;
this.panelPastelOrange.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192))))); this.panelPastelOrange.BackColor = System.Drawing.Color.PeachPuff;
this.panelPastelOrange.Location = new System.Drawing.Point(207, 35); this.panelPastelOrange.Location = new System.Drawing.Point(207, 35);
this.panelPastelOrange.Name = "panelPastelOrange"; this.panelPastelOrange.Name = "panelPastelOrange";
this.panelPastelOrange.Size = new System.Drawing.Size(40, 40); this.panelPastelOrange.Size = new System.Drawing.Size(40, 40);
@ -259,7 +259,7 @@
// panelPastelPink // panelPastelPink
// //
this.panelPastelPink.AllowDrop = true; this.panelPastelPink.AllowDrop = true;
this.panelPastelPink.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); this.panelPastelPink.BackColor = System.Drawing.Color.LightSalmon;
this.panelPastelPink.Location = new System.Drawing.Point(132, 35); this.panelPastelPink.Location = new System.Drawing.Point(132, 35);
this.panelPastelPink.Name = "panelPastelPink"; this.panelPastelPink.Name = "panelPastelPink";
this.panelPastelPink.Size = new System.Drawing.Size(40, 40); this.panelPastelPink.Size = new System.Drawing.Size(40, 40);
@ -268,7 +268,7 @@
// panelPastelRed // panelPastelRed
// //
this.panelPastelRed.AllowDrop = true; this.panelPastelRed.AllowDrop = true;
this.panelPastelRed.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128))))); this.panelPastelRed.BackColor = System.Drawing.Color.Red;
this.panelPastelRed.Location = new System.Drawing.Point(55, 35); this.panelPastelRed.Location = new System.Drawing.Point(55, 35);
this.panelPastelRed.Name = "panelPastelRed"; this.panelPastelRed.Name = "panelPastelRed";
this.panelPastelRed.Size = new System.Drawing.Size(40, 40); this.panelPastelRed.Size = new System.Drawing.Size(40, 40);
@ -330,7 +330,7 @@
this.ButtonOk.TabIndex = 2; this.ButtonOk.TabIndex = 2;
this.ButtonOk.Text = "Добавить"; this.ButtonOk.Text = "Добавить";
this.ButtonOk.UseVisualStyleBackColor = true; this.ButtonOk.UseVisualStyleBackColor = true;
// this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click);
// //
// buttonCancel // buttonCancel
// //

View File

@ -170,8 +170,6 @@ namespace ProjectElectricLocomotive
} }
} }
// TODO Реализовать логику смены цветов: основного и дополнительного (для продвинутого объекта)
/// <summary> /// <summary>
/// Добавление loco /// Добавление loco
/// </summary> /// </summary>

View File

@ -118,10 +118,12 @@ namespace ProjectElectricLocomotive.Generics
{ {
return false; return false;
} }
using FileStream fs = new(filename, FileMode.Create); using StreamWriter fs = new StreamWriter(filename);
byte[] info = new
UTF8Encoding(true).GetBytes($"LocomotiveStorage{Environment.NewLine}{data}"); foreach(var str in _locomotivesStorage)
fs.Write(info, 0, info.Length); {
fs.WriteLine($"LocomotiveStorage{Environment.NewLine}{data}");
}
return true; return true;
} }
/// <summary> /// <summary>
@ -135,29 +137,42 @@ namespace ProjectElectricLocomotive.Generics
{ {
return false; return false;
} }
string bufferTextFromFile = ""; //string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) //using (FileStream fs = new(filename, FileMode.Open))
using (StreamReader fs = File.OpenText(filename))
{ {
byte[] b = new byte[fs.Length]; /* byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true); UTF8Encoding temp = new(true);*/
while (fs.Read(b, 0, b.Length) > 0) /*while (fs.Read(b, 0, b.Length) > 0)
{ {
bufferTextFromFile += temp.GetString(b); bufferTextFromFile += temp.GetString(b);
} }*//*
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, List<string> strsh = new List<string>();
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0) string str;
while ((str = fs.ReadLine()) != null)
{
Console.WriteLine(str);
strsh.Add(str);
}*/
string? str = fs.ReadLine();
if (str == null || str.Length == 0)
{ {
return false; return false;
} }
if (!strs[0].StartsWith("LocomotiveStorage"))
if (!str.Contains("LocomotiveStorage"))
{ {
//если нет такой записи, то это не те данные //если нет такой записи, то это не те данные
return false; return false;
} }
_locomotivesStorage.Clear(); _locomotivesStorage.Clear();
foreach (string data in strs)
foreach(string data in str)
{ {
string[] record = data.Split(_separatorForKeyValue, string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
@ -184,6 +199,47 @@ namespace ProjectElectricLocomotive.Generics
_locomotivesStorage.Add(record[0], collection); _locomotivesStorage.Add(record[0], collection);
} }
return true; return true;
}
/*var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (str == null || str.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("LocomotiveStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_locomotivesStorage.Clear();
foreach (string data in str)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
continue;
}
LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingLocomotive? loco =
elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight);
if (loco != null)
{
if ((collection + loco) != -1) // or vice versa
{
return false;
}
}
}
_locomotivesStorage.Add(record[0], collection);
}
return true;*/
} }
} }
} }