public static void Initialize(string file) { m_file = file; m_values = new Dictionary <string, object>(); if (File.Exists(file)) { // load settings var reader = XmlReader.Create(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); try { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "setting") { reader.MoveToAttribute("key"); string key = reader.Value; reader.MoveToAttribute("type"); string type = reader.Value; if (type == "string") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, value); } } else if (type == "int") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, int.Parse(value)); } } else if (type == "bool") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, bool.Parse(value)); } } else if (type == "string-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); if (!m_values.ContainsKey(key)) { m_values.Add(key, values); } } else if (type == "double") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, double.Parse(value)); } } else if (type == "int-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); int[] parsedValues = new int[values.Length]; for (int i = 0; i < values.Length; i++) { parsedValues[i] = int.Parse(values[i]); } if (!m_values.ContainsKey(key)) { m_values.Add(key, parsedValues); } } else if (type == "double-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); double[] parsedValues = new double[values.Length]; for (int i = 0; i < values.Length; i++) { parsedValues[i] = double.Parse(values[i]); } if (!m_values.ContainsKey(key)) { m_values.Add(key, parsedValues); } } else if (type == "DateTime") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { DateTime outValue; if (DateTime.TryParse(value, out outValue)) { m_values.Add(key, outValue); } } } } } } catch (XmlException) { // invalid xml } } else { // create new settings file var directory = Path.GetDirectoryName(m_file); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using (var sw = File.CreateText(m_file)) { sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf - 8\"?>"); sw.WriteLine("<settings>"); sw.WriteLine("</settings>"); } } Serializer = new SettingsSerializer(); }
/// <summary> /// Shows the save file dialog and then saves the document. /// </summary> /// <param name="saved">Whether the document was saved.</param> private void SaveDocumentAs(out bool saved) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Save As"; sfd.Filter = "Circuit Diagram Document (*.cddx)|*.cddx|XML Files (*.xml)|*.xml"; if (sfd.ShowDialog() == true) { string extension = Path.GetExtension(sfd.FileName); IDictionary<IOComponentType, EmbedComponentData> embedComponents; IODocument ioDocument = circuitDisplay.Document.ToIODocument(out embedComponents); if (extension == ".cddx") { bool doSave = false; // Load default save options SettingsManager.SettingsSerializer serializer = new SettingsManager.SettingsSerializer(); serializer.Category = "CDDX"; CircuitDiagram.IO.CDDX.CDDXSaveOptions saveOptions = new CircuitDiagram.IO.CDDX.CDDXSaveOptions(); saveOptions.Deserialize(serializer); if (!SettingsManager.Settings.ReadBool("CDDX.AlwaysUseSettings")) { winSaveOptions saveOptionsWindow = new winSaveOptions(new CDDXSaveOptionsEditor(), saveOptions); saveOptionsWindow.Owner = this; if (saveOptionsWindow.ShowDialog() == true) { saveOptions = saveOptionsWindow.Options as CircuitDiagram.IO.CDDX.CDDXSaveOptions; if (saveOptionsWindow.AlwaysUseSettings) { saveOptions.Serialize(serializer); SettingsManager.Settings.Write("CDDX.AlwaysUseSettings", true); } doSave = true; } } else doSave = true; if (doSave) { IO.CDDX.CDDXWriter writer = new IO.CDDX.CDDXWriter(); writer.Document = ioDocument; writer.Options = saveOptions; writer.EmbedComponents = embedComponents; writer.Begin(); if (writer.RenderContext != null) { writer.RenderContext.Begin(); circuitDisplay.Document.Render(writer.RenderContext); writer.RenderContext.End(); } using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write, FileShare.Read)) { writer.Write(fs); } writer.End(); m_docPath = sfd.FileName; m_documentTitle = System.IO.Path.GetFileNameWithoutExtension(sfd.FileName); this.Title = m_documentTitle + " - Circuit Diagram"; m_undoManager.SetSaveIndex(); AddRecentFile(m_docPath); m_lastSaveOptions = saveOptions; saved = true; } else saved = false; } else { IO.Xml.XmlWriter writer = new IO.Xml.XmlWriter(); writer.Document = ioDocument; writer.Begin(); using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write, FileShare.Read)) { writer.Write(fs); } writer.End(); m_docPath = sfd.FileName; m_documentTitle = System.IO.Path.GetFileNameWithoutExtension(sfd.FileName); this.Title = m_documentTitle + " - Circuit Diagram"; m_undoManager.SetSaveIndex(); AddRecentFile(m_docPath); saved = true; } } else saved = false; }
public static void Initialize(string file) { m_file = file; m_values = new Dictionary<string, object>(); if (File.Exists(file)) { // load settings XmlTextReader reader = new XmlTextReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); try { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "setting") { reader.MoveToAttribute("key"); string key = reader.Value; reader.MoveToAttribute("type"); string type = reader.Value; if (type == "string") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) m_values.Add(key, value); } else if (type == "int") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) m_values.Add(key, int.Parse(value)); } else if (type == "bool") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) m_values.Add(key, bool.Parse(value)); } else if (type == "string-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); if (!m_values.ContainsKey(key)) m_values.Add(key, values); } else if (type == "double") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) m_values.Add(key, double.Parse(value)); } else if (type == "int-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); int[] parsedValues = new int[values.Length]; for (int i = 0; i < values.Length; i++) parsedValues[i] = int.Parse(values[i]); if (!m_values.ContainsKey(key)) m_values.Add(key, parsedValues); } else if (type == "double-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); double[] parsedValues = new double[values.Length]; for (int i = 0; i < values.Length; i++) parsedValues[i] = double.Parse(values[i]); if (!m_values.ContainsKey(key)) m_values.Add(key, parsedValues); } else if (type == "DateTime") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { DateTime outValue; if (DateTime.TryParse(value, out outValue)) m_values.Add(key, outValue); } } } } } catch (XmlException) { // invalid xml } } else { // create new settings file var directory = Path.GetDirectoryName(m_file); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); using (var sw = File.CreateText(m_file)) { sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf - 8\"?>"); sw.WriteLine("<settings>"); sw.WriteLine("</settings>"); } } Serializer = new SettingsSerializer(); }
/// <summary> /// Attempts to save the document, prompting for a file name if it has not previously been saved. /// </summary> /// <param name="saved">Whether the document was saved.</param> private void SaveDocument(out bool saved) { if (!String.IsNullOrEmpty(m_docPath)) { IDictionary<IOComponentType, EmbedComponentData> embedComponents; IODocument ioDocument = circuitDisplay.Document.ToIODocument(out embedComponents); string extension = Path.GetExtension(m_docPath); if (extension == ".cddx") { // Load default save options if no previous options (e.g. if file opened) SettingsManager.SettingsSerializer serializer = new SettingsManager.SettingsSerializer(); serializer.Category = "CDDX"; m_lastSaveOptions = new IO.CDDX.CDDXSaveOptions(); m_lastSaveOptions.Deserialize(serializer); IO.CDDX.CDDXWriter writer = new IO.CDDX.CDDXWriter(); writer.Document = ioDocument; writer.Options = m_lastSaveOptions; writer.EmbedComponents = embedComponents; writer.Begin(); if (writer.RenderContext != null) { writer.RenderContext.Begin(); circuitDisplay.Document.Render(writer.RenderContext); writer.RenderContext.End(); } using (FileStream fs = new FileStream(m_docPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { writer.Write(fs); } writer.End(); } else { IO.Xml.XmlWriter writer = new IO.Xml.XmlWriter(); writer.Document = ioDocument; writer.Begin(); using (FileStream fs = new FileStream(m_docPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { writer.Write(fs); } writer.End(); } this.Title = System.IO.Path.GetFileNameWithoutExtension(m_docPath) + " - Circuit Diagram"; UndoManager.SetSaveIndex(); saved = true; } else SaveDocumentAs(out saved); }
public static void Initialize(string file) { m_file = file; m_values = new Dictionary <string, object>(); if (File.Exists(file)) { // load settings XmlTextReader reader = new XmlTextReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); try { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "setting") { reader.MoveToAttribute("key"); string key = reader.Value; reader.MoveToAttribute("type"); string type = reader.Value; if (type == "string") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, value); } } else if (type == "int") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, int.Parse(value)); } } else if (type == "bool") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, bool.Parse(value)); } } else if (type == "string-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); if (!m_values.ContainsKey(key)) { m_values.Add(key, values); } } else if (type == "double") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { m_values.Add(key, double.Parse(value)); } } else if (type == "int-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); int[] parsedValues = new int[values.Length]; for (int i = 0; i < values.Length; i++) { parsedValues[i] = int.Parse(values[i]); } if (!m_values.ContainsKey(key)) { m_values.Add(key, parsedValues); } } else if (type == "double-array") { reader.MoveToAttribute("value"); string value = reader.Value; string[] values = value.Split(','); double[] parsedValues = new double[values.Length]; for (int i = 0; i < values.Length; i++) { parsedValues[i] = double.Parse(values[i]); } if (!m_values.ContainsKey(key)) { m_values.Add(key, parsedValues); } } else if (type == "DateTime") { reader.MoveToAttribute("value"); string value = reader.Value; if (!m_values.ContainsKey(key)) { DateTime outValue; if (DateTime.TryParse(value, out outValue)) { m_values.Add(key, outValue); } } } } } } catch (XmlException) { // invalid xml } } else { // create new settings file } Serializer = new SettingsSerializer(); }