From b8a632a357400ac6d989147607e24e4efff7e602 Mon Sep 17 00:00:00 2001 From: Atakan Date: Sun, 21 Oct 2018 23:22:08 +0300 Subject: [PATCH] Opening project from single file is possible. --- BigBug/BigBug.csproj | 1 + BigBug/MainForm.Designer.cs | 46 +++++++------ BigBug/MainForm.cs | 88 +++++++++---------------- BigBug/MainForm.resx | 10 +-- BigBug/ProjectScanner.cs | 126 ++++++++++++++++++++++++++++++++++++ BigBug/Visuals.cs | 10 +-- 6 files changed, 187 insertions(+), 94 deletions(-) create mode 100644 BigBug/ProjectScanner.cs diff --git a/BigBug/BigBug.csproj b/BigBug/BigBug.csproj index 7f917cc..55f4df0 100644 --- a/BigBug/BigBug.csproj +++ b/BigBug/BigBug.csproj @@ -98,6 +98,7 @@ + diff --git a/BigBug/MainForm.Designer.cs b/BigBug/MainForm.Designer.cs index 980dc5b..b452c7f 100644 --- a/BigBug/MainForm.Designer.cs +++ b/BigBug/MainForm.Designer.cs @@ -31,7 +31,6 @@ private void InitializeComponent() this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); this.buttonNewProject = new System.Windows.Forms.Button(); - this.openFile = new System.Windows.Forms.OpenFileDialog(); this.buttonPortOpenClose = new System.Windows.Forms.Button(); this.comboPorts = new System.Windows.Forms.ComboBox(); this.timerSerialPortListUpdate = new System.Windows.Forms.Timer(this.components); @@ -46,9 +45,10 @@ private void InitializeComponent() this.saveFile = new System.Windows.Forms.SaveFileDialog(); this.textFilter = new System.Windows.Forms.TextBox(); this.labelFilter = new System.Windows.Forms.Label(); - this.buttonRefreshProject = new System.Windows.Forms.Button(); + this.buttonNewSingleFileProject = new System.Windows.Forms.Button(); this.dataGrid = new System.Windows.Forms.DataGridView(); this.labelClearFilter = new System.Windows.Forms.Label(); + this.browserOpenSingleFile = new System.Windows.Forms.OpenFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit(); this.SuspendLayout(); // @@ -63,11 +63,6 @@ private void InitializeComponent() this.buttonNewProject.UseVisualStyleBackColor = true; this.buttonNewProject.Click += new System.EventHandler(this.buttonNewProject_Click); // - // openFile - // - this.openFile.FileName = "openFile"; - this.openFile.Multiselect = true; - // // buttonPortOpenClose // this.buttonPortOpenClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); @@ -85,7 +80,7 @@ private void InitializeComponent() this.comboPorts.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.comboPorts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboPorts.FormattingEnabled = true; - this.comboPorts.Location = new System.Drawing.Point(464, 16); + this.comboPorts.Location = new System.Drawing.Point(467, 18); this.comboPorts.Margin = new System.Windows.Forms.Padding(4); this.comboPorts.Name = "comboPorts"; this.comboPorts.Size = new System.Drawing.Size(160, 24); @@ -102,7 +97,7 @@ private void InitializeComponent() // this.textBaud.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.textBaud.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162))); - this.textBaud.Location = new System.Drawing.Point(633, 17); + this.textBaud.Location = new System.Drawing.Point(635, 18); this.textBaud.Margin = new System.Windows.Forms.Padding(4); this.textBaud.Name = "textBaud"; this.textBaud.Size = new System.Drawing.Size(73, 23); @@ -143,7 +138,7 @@ private void InitializeComponent() this.labelProjectName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.labelProjectName.AutoSize = true; - this.labelProjectName.Location = new System.Drawing.Point(232, 21); + this.labelProjectName.Location = new System.Drawing.Point(302, 21); this.labelProjectName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.labelProjectName.Name = "labelProjectName"; this.labelProjectName.Size = new System.Drawing.Size(0, 17); @@ -186,17 +181,16 @@ private void InitializeComponent() this.labelFilter.TabIndex = 14; this.labelFilter.Text = "Filter"; // - // buttonRefreshProject + // buttonNewSingleFileProject // - this.buttonRefreshProject.Location = new System.Drawing.Point(124, 15); - this.buttonRefreshProject.Margin = new System.Windows.Forms.Padding(4); - this.buttonRefreshProject.Name = "buttonRefreshProject"; - this.buttonRefreshProject.Size = new System.Drawing.Size(100, 28); - this.buttonRefreshProject.TabIndex = 15; - this.buttonRefreshProject.Text = "Refresh"; - this.buttonRefreshProject.UseVisualStyleBackColor = true; - this.buttonRefreshProject.Visible = false; - this.buttonRefreshProject.Click += new System.EventHandler(this.buttonRefreshProject_Click); + this.buttonNewSingleFileProject.Location = new System.Drawing.Point(124, 15); + this.buttonNewSingleFileProject.Margin = new System.Windows.Forms.Padding(4); + this.buttonNewSingleFileProject.Name = "buttonNewSingleFileProject"; + this.buttonNewSingleFileProject.Size = new System.Drawing.Size(170, 28); + this.buttonNewSingleFileProject.TabIndex = 15; + this.buttonNewSingleFileProject.Text = "New Single File Project"; + this.buttonNewSingleFileProject.UseVisualStyleBackColor = true; + this.buttonNewSingleFileProject.Click += new System.EventHandler(this.buttonNewSingleFileProject_Click); // // dataGrid // @@ -239,6 +233,10 @@ private void InitializeComponent() this.labelClearFilter.Visible = false; this.labelClearFilter.Click += new System.EventHandler(this.labelClearFilter_Click); // + // browserOpenSingleFile + // + this.browserOpenSingleFile.FileName = "browserOpenSingleFile"; + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); @@ -247,7 +245,7 @@ private void InitializeComponent() this.Controls.Add(this.labelClearFilter); this.Controls.Add(this.dataGrid); this.Controls.Add(this.labelFilter); - this.Controls.Add(this.buttonRefreshProject); + this.Controls.Add(this.buttonNewSingleFileProject); this.Controls.Add(this.textFilter); this.Controls.Add(this.labelAuthor); this.Controls.Add(this.labelProjectName); @@ -262,7 +260,7 @@ private void InitializeComponent() this.MinimumSize = new System.Drawing.Size(847, 580); this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "BigBug v3.0.1"; + this.Text = "BigBug v3.1.0"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -272,7 +270,6 @@ private void InitializeComponent() #endregion private System.Windows.Forms.Button buttonNewProject; - private System.Windows.Forms.OpenFileDialog openFile; private System.Windows.Forms.Button buttonPortOpenClose; private System.Windows.Forms.ComboBox comboPorts; private System.Windows.Forms.Timer timerSerialPortListUpdate; @@ -287,9 +284,10 @@ private void InitializeComponent() private System.Windows.Forms.SaveFileDialog saveFile; private System.Windows.Forms.TextBox textFilter; private System.Windows.Forms.Label labelFilter; - private System.Windows.Forms.Button buttonRefreshProject; + private System.Windows.Forms.Button buttonNewSingleFileProject; private System.Windows.Forms.DataGridView dataGrid; private System.Windows.Forms.Label labelClearFilter; + private System.Windows.Forms.OpenFileDialog browserOpenSingleFile; } } diff --git a/BigBug/MainForm.cs b/BigBug/MainForm.cs index a86cbd0..d3f8bdd 100644 --- a/BigBug/MainForm.cs +++ b/BigBug/MainForm.cs @@ -59,7 +59,7 @@ public MainForm() // Restore Baud rate and MaxDataRows from the settings. textBaud.Text = (settings.LoadSetting("General.Baud") != "") ? settings.LoadSetting("General.Baud") : "9600"; int maxDataRowsSetting = 0; - if (int.TryParse(settings.LoadSetting("General.MaxDataRows"), out maxDataRowsSetting)) + if (int.TryParse(settings.LoadSetting("User.MaxDataRows"), out maxDataRowsSetting)) maxDataRows = maxDataRowsSetting; selectedPort = settings.LoadSetting("General.Port"); @@ -76,13 +76,29 @@ public MainForm() private void buttonNewProject_Click(object sender, EventArgs e) { // Load Recent opened folder and set it to the folder browser initial path. - browserNewProject.SelectedPath = settings.LoadSetting("General.Recent"); + browserNewProject.SelectedPath = settings.LoadSetting("General.RecentProject"); // Show browser. if (browserNewProject.ShowDialog() == System.Windows.Forms.DialogResult.OK) { - // If a folder selected, send a click event to RefreshProject button. - buttonRefreshProject_Click(null, null); + // Get supported file extensions. + string supportedFileExtensionsString = (settings.LoadSetting("User.FileExtensions") != "") ? settings.LoadSetting("User.FileExtensions") : Properties.Resources.supportedFileExtensions; + string[] supportedFileExtensions = supportedFileExtensionsString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + // Use ProjectScanner to scan all files. + if (!ProjectScanner.Scan(browserNewProject.SelectedPath, supportedFileExtensions, ref bbCodeBase)) + { + MessageBox.Show("No BBCode Descriptor has been found under the selected path."); + } + + // Remember the project name including the number of recognized BBCode entries. + projectName = Path.GetFileName(browserNewProject.SelectedPath) + " #" + bbCodeBase.GetCodeCount(); + + // Set visuals for ProjectOpen state. + SetVisuals(GetVisuals() | VisualStates.ProjectOpen); + + // Save teh successfuly opened project path. + settings.SaveSetting("General.RecentProject", browserNewProject.SelectedPath); } } @@ -224,74 +240,32 @@ private void buttonSave_Click(object sender, EventArgs e) } /** - * @brief Opens or re-opens a project from the selected location and scans all BBCode descriptors to fill the BBCodeBase. + * @brief Opens a single-file project from the selected location and scans all BBCode descriptors to fill the BBCodeBase. * @param sender * @param e */ - private void buttonRefreshProject_Click(object sender, EventArgs e) + private void buttonNewSingleFileProject_Click(object sender, EventArgs e) { - string selectedProjectPath = ""; - string[] fileList = { }; - - try - { - // Open the - selectedProjectPath = Path.GetFileName(browserNewProject.SelectedPath); - fileList = Directory.GetFiles(browserNewProject.SelectedPath, "*.*", SearchOption.AllDirectories); - } - catch { }; + // Load Recent opened folder and set it to the folder browser initial path. + browserOpenSingleFile.FileName = settings.LoadSetting("General.RecentSingleFile"); - // Get the selected path and make sure that its not empty. - if (selectedProjectPath != "") + // Show browser. + if (browserOpenSingleFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { - // Get supported file extensions. - string supportedFileExtensionsString = (settings.LoadSetting("General.FileExtensions") != "") ? settings.LoadSetting("General.FileExtensions") : Properties.Resources.supportedFileExtensions; - string[] supportedFileExtensions = supportedFileExtensionsString.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries); - - // Create an empty BBCode database. - bbCodeBase = new BBCodeBase(); - - // Iterate all files under the selected path. - foreach (string fileName in fileList) + // Use ProjectScanner to scan the selected file. + if (!ProjectScanner.Scan(browserOpenSingleFile.FileName, new string[] { }, ref bbCodeBase)) { - // Check the file extension for allowed extensions. - if (File.Exists(fileName) && supportedFileExtensions.Contains(Path.GetExtension(fileName).TrimStart('.'))) - { - // Open the file as text. - StreamReader sr = File.OpenText(fileName); - - // Get pure, relative file name. - string sourceFile = fileName.Replace(browserNewProject.SelectedPath, ""); - - // Iterate all lines until the end. - string lineText; - int lineNumber = 0; - while (!sr.EndOfStream) - { - // Read single line. - lineText = sr.ReadLine(); - lineNumber++; - - // Try to parse the line with descriptor parser. Result is not null if successful. - BBCodeObject bbCode = BBCodeDescriptorParser.Parse(lineText, sourceFile, lineNumber); - - // Add to the database (only if not null). - bbCodeBase.AddBBCode(bbCode); - } - - // Close the file. - sr.Close(); - } + MessageBox.Show("No BBCode Descriptor has been found under the selected path."); } // Remember the project name including the number of recognized BBCode entries. - projectName = Path.GetFileName(browserNewProject.SelectedPath) + " #" + bbCodeBase.GetCodeCount(); + projectName = Path.GetFileName(browserOpenSingleFile.FileName) + " #" + bbCodeBase.GetCodeCount(); // Set visuals for ProjectOpen state. SetVisuals(GetVisuals() | VisualStates.ProjectOpen); // Save teh successfuly opened project path. - settings.SaveSetting("General.Recent", browserNewProject.SelectedPath); + settings.SaveSetting("General.RecentSingleFile", browserOpenSingleFile.FileName); } } diff --git a/BigBug/MainForm.resx b/BigBug/MainForm.resx index 5ca5edd..7484ce2 100644 --- a/BigBug/MainForm.resx +++ b/BigBug/MainForm.resx @@ -117,9 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 664, 17 - 434, 17 @@ -130,10 +127,13 @@ 206, 17 - 779, 17 + 664, 17 - 851, 17 + 736, 17 + + + 845, 17 76 diff --git a/BigBug/ProjectScanner.cs b/BigBug/ProjectScanner.cs new file mode 100644 index 0000000..5ff9a5b --- /dev/null +++ b/BigBug/ProjectScanner.cs @@ -0,0 +1,126 @@ +/** + * @file ProjectScanner.cs + * @author Atakan S. + * @date 01/01/2018 + * @version 1.0 + * @brief Scans all files in a directory or a single file for BBCode. + * + * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +using System.IO; +using System.Linq; + +namespace BigBug +{ + class ProjectScanner + { + /** + * @brief Scans a project location of file and fills the BBCodeBase. + * @param projectPath is path to a directory or a single file. + * @param supportedFileExtensions string array of extensions. + * @param bbCodeBase clears all entries in this object and adds the new entries. + * @return true if entries are found. + */ + public static bool Scan(string projectPath, string[] supportedFileExtensions, ref BBCodeBase bbCodeBase) + { + // Create an empty BBCode database. + bbCodeBase = new BBCodeBase(); + + // Check if the given is a file or directory. + if (File.Exists(projectPath)) + { + // Scan single file. + ScanSingleFile(projectPath, ref bbCodeBase); + } + else + { + // Create empty file list. + string[] fileList = { }; + + // Try to get file names under the selected path. + try + { + fileList = Directory.GetFiles(projectPath, "*.*", SearchOption.AllDirectories); + } + catch { }; + + // Get the selected path and make sure that its not empty. + if (fileList.Length > 0) + { + // Iterate all files under the selected path. + foreach (string fileName in fileList) + { + // Check the file extension for allowed extensions. + if (File.Exists(fileName) && ((fileList.Length == 1) || supportedFileExtensions.Contains(Path.GetExtension(fileName).TrimStart('.')))) + { + ScanSingleFile(fileName, ref bbCodeBase); + } + } + } + } + + // Success if descriptors are found. + return (bbCodeBase.GetCodeCount() > 0); + } + + /** + * @brief Scans a project location of file and fills the BBCodeBase. + * @param fileName is path to a directory or a single file. + * @param bbCodeBase clears all entries in this object and adds the new entries. + * @return true on successful scan. + */ + private static bool ScanSingleFile(string fileName, ref BBCodeBase bbCodeBase) + { + try + { + // Open the file as text. + StreamReader sr = File.OpenText(fileName); + + // Iterate all lines until the end. + string lineText; + int lineNumber = 0; + while (!sr.EndOfStream) + { + // Read single line. + lineText = sr.ReadLine(); + lineNumber++; + + // Try to parse the line with descriptor parser. Result is not null if successful. + BBCodeObject bbCode = BBCodeDescriptorParser.Parse(lineText, fileName, lineNumber); + + // Add to the database (only if not null). + bbCodeBase.AddBBCode(bbCode); + } + + // Close the file. + sr.Close(); + + // Everything is OK. + return true; + } + catch { } + + // Problem occured. + return false; + } + } +} diff --git a/BigBug/Visuals.cs b/BigBug/Visuals.cs index 2d4ef61..7e8564a 100644 --- a/BigBug/Visuals.cs +++ b/BigBug/Visuals.cs @@ -80,7 +80,7 @@ private void SetVisuals(VisualStates state) comboPorts.Enabled = false; textBaud.Enabled = false; buttonNewProject.Enabled = false; - buttonRefreshProject.Enabled = false; + buttonNewSingleFileProject.Enabled = false; buttonSave.Enabled = false; // Enable receiption timer. @@ -95,7 +95,7 @@ private void SetVisuals(VisualStates state) comboPorts.Enabled = true; textBaud.Enabled = true; buttonNewProject.Enabled = true; - buttonRefreshProject.Enabled = true; + buttonNewSingleFileProject.Enabled = true; buttonSave.Enabled = true; // Disable receiption timer. @@ -110,17 +110,11 @@ private void SetVisuals(VisualStates state) { // Show opened projecst name/description. labelProjectName.Text = projectName; - - // Show refresh button. - buttonRefreshProject.Visible = true; } else { // Hide opened projecst name/description. labelProjectName.Text = ""; - - // Hide refresh button. - buttonRefreshProject.Visible = false; } // Filter clear button (label) is only visible when filter text is entered.