//----------------------------------------------------------------------- // // Copyright © 2012 Nils Hammar. All rights reserved. // //----------------------------------------------------------------------- /* * Software to access vehicle information via the OBD-II connector. * * Copyright © 2012 Nils Hammar * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Alternative licensing is possible, see the licensing document. * * The above text may not be removed or modified. */ namespace DataSource.FileAccess { using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Xml.Serialization; using global::SharedObjects; using global::SharedObjects.Misc; /// /// Access class for communication speeds file. /// public class CommspeedFileAccess : XmlParserHelper { /// /// Name of file. /// private string fileName; /// /// Gets List of supported communication speeds. /// private List commspeedsInt; /// /// Gets List of supported communication speeds. /// public IList commspeeds { get { return this.commspeedsInt; } } /// /// Initializes a new instance of the class. /// /// Logging interface. /// Name of file. /// Data file directory path. public CommspeedFileAccess(ILogging iLogging, string fileName, string dataFileDir) : base(iLogging, dataFileDir) { this.fileName = fileName; this.commspeedsInt = new List(); } /// /// Save the current list to file. /// /// 'true' when successful. public bool save() { bool success = false; XmlSerializer xms = new XmlSerializer(this.commspeedsInt.GetType()); xms.UnknownNode += new XmlNodeEventHandler(xms_UnknownNode); xms.UnknownAttribute += new XmlAttributeEventHandler(xms_UnknownAttribute); xms.UnknownElement += new XmlElementEventHandler(xms_UnknownElement); xms.UnreferencedObject += new UnreferencedObjectEventHandler(xms_UnreferencedObject); TextWriter writer = null; try { writer = new StreamWriter(this.dataFileDir + this.fileName); xms.Serialize(writer, this.commspeedsInt); success = true; } catch (Exception ex) { MessageBox.Show( ex.Message, "Save failed", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { if (writer != null) { writer.Close(); } } return success; } /// /// Load data from file into the current list. /// public void load() { this.iLogging.appendText("Loading Commspeeds." + "\r\n"); this.commspeedsInt = new List(); XmlSerializer xms = new XmlSerializer(this.commspeedsInt.GetType()); xms.UnknownNode += new XmlNodeEventHandler(xms_UnknownNode); xms.UnknownAttribute += new XmlAttributeEventHandler(xms_UnknownAttribute); xms.UnknownElement += new XmlElementEventHandler(xms_UnknownElement); xms.UnreferencedObject += new UnreferencedObjectEventHandler(xms_UnreferencedObject); StreamReader sr = null; try { sr = new StreamReader(this.dataFileDir + this.fileName); this.commspeedsInt = (List)xms.Deserialize(sr); } catch (Exception ex) { MessageBox.Show( ex.Message, "Load failed", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { if (sr != null) { sr.Close(); } } this.commspeedsInt.Sort(Utils.commspeed_sorter); this.iLogging.appendText(this.commspeedsInt.Count + " Commspeeds Loaded from XML file." + "\r\n"); } } }