From e3b1101da18449e91070bd2bd5906429765112b1 Mon Sep 17 00:00:00 2001 From: ekallin Date: Thu, 16 Nov 2023 21:33:46 +0400 Subject: [PATCH] save data with stream writer --- .../ExtentionDrawningCar.cs | 2 +- .../FormLocomotiveCollections.Designer.cs | 2 + .../FormLocomotiveCollections.cs | 13 +++ .../FormLocomotiveConfig.Designer.cs | 18 ++-- .../FormLocomotiveConfig.cs | 2 - .../LocomotiveGenericStorage.cs | 82 ++++++++++++++++--- 6 files changed, 94 insertions(+), 25 deletions(-) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs index e9858b2..788893c 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs @@ -35,7 +35,7 @@ namespace ProjectElectricLocomotive height ); } - if (strs.Length == 7) + if (strs.Length == 6) { return new DrawingElectricLocomotive( Convert.ToInt32(strs[0]), diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs index 3ff35c4..29e2812 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs @@ -210,12 +210,14 @@ this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.SaveToolStripMenuItem.Text = "Сохранить"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.LoadToolStripMenuItem.Text = "Загрузить"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // // saveFileDialog // diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index 00ce712..d2a4f71 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -172,6 +172,19 @@ namespace ProjectElectricLocomotive private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { // 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); + + } + } } } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs index b305860..9f8b1fa 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs @@ -205,7 +205,7 @@ // panelPastelViolet // 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.Name = "panelPastelViolet"; this.panelPastelViolet.Size = new System.Drawing.Size(40, 40); @@ -214,7 +214,7 @@ // panelPastelLilac // 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.Name = "panelPastelLilac"; this.panelPastelLilac.Size = new System.Drawing.Size(40, 40); @@ -223,7 +223,7 @@ // panelSkyBlue // 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.Name = "panelSkyBlue"; this.panelSkyBlue.Size = new System.Drawing.Size(40, 40); @@ -232,7 +232,7 @@ // panelPastelGreen // 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.Name = "panelPastelGreen"; this.panelPastelGreen.Size = new System.Drawing.Size(40, 40); @@ -241,7 +241,7 @@ // panelPastelYellow // 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.Name = "panelPastelYellow"; this.panelPastelYellow.Size = new System.Drawing.Size(40, 40); @@ -250,7 +250,7 @@ // panelPastelOrange // 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.Name = "panelPastelOrange"; this.panelPastelOrange.Size = new System.Drawing.Size(40, 40); @@ -259,7 +259,7 @@ // panelPastelPink // 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.Name = "panelPastelPink"; this.panelPastelPink.Size = new System.Drawing.Size(40, 40); @@ -268,7 +268,7 @@ // panelPastelRed // 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.Name = "panelPastelRed"; this.panelPastelRed.Size = new System.Drawing.Size(40, 40); @@ -330,7 +330,7 @@ this.ButtonOk.TabIndex = 2; this.ButtonOk.Text = "Добавить"; this.ButtonOk.UseVisualStyleBackColor = true; - // this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); + this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); // // buttonCancel // diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs index ac78182..d1bd132 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs @@ -170,8 +170,6 @@ namespace ProjectElectricLocomotive } } - // TODO Реализовать логику смены цветов: основного и дополнительного (для продвинутого объекта) - /// /// Добавление loco /// diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 8c87e67..b0bf07c 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -118,10 +118,12 @@ namespace ProjectElectricLocomotive.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"LocomotiveStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using StreamWriter fs = new StreamWriter(filename); + + foreach(var str in _locomotivesStorage) + { + fs.WriteLine($"LocomotiveStorage{Environment.NewLine}{data}"); + } return true; } /// @@ -135,19 +137,73 @@ namespace ProjectElectricLocomotive.Generics { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + //string bufferTextFromFile = ""; + //using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = File.OpenText(filename)) + { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + /* byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true);*/ + /*while (fs.Read(b, 0, b.Length) > 0) { bufferTextFromFile += temp.GetString(b); + }*//* + + List strsh = new List(); + + 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; } + + if (!str.Contains("LocomotiveStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + + _locomotivesStorage.Clear(); + + foreach(string data in str) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotiveGenericCollection + 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; + } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + /*var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) + if (str == null || str.Length == 0) { return false; } @@ -157,7 +213,7 @@ namespace ProjectElectricLocomotive.Generics return false; } _locomotivesStorage.Clear(); - foreach (string data in strs) + foreach (string data in str) { string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); @@ -183,7 +239,7 @@ namespace ProjectElectricLocomotive.Generics } _locomotivesStorage.Add(record[0], collection); } - return true; + return true;*/ } } }