forked from microsoft/perfview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigData.cs
More file actions
173 lines (165 loc) · 5.68 KB
/
ConfigData.cs
File metadata and controls
173 lines (165 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright (c) Microsoft Corporation. All rights reserved. */
/* AUTHOR: Vance Morrison
* Date : 10/20/2007 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
namespace Utilities
{
/// <summary>
/// ConfigData is simply a Dictionary that knows how to read and write itself to XML efficiently.
/// </summary>
public class ConfigData : SortedDictionary<string, string>
{
public ConfigData() { m_elementName = "ConfigData"; }
public ConfigData(string xmlFileName, bool autoWrite = false, string elementName = "ConfigData")
{
m_elementName = elementName;
m_fileName = xmlFileName;
m_autoWrite = autoWrite;
Read(xmlFileName);
}
public ConfigData(Stream xmlDataStream) : this(xmlDataStream, "ConfigData") { }
public ConfigData(Stream xmlDataStream, string elementName)
{
m_elementName = elementName;
Read(xmlDataStream);
}
public new string this[string key]
{
get
{
string result = null;
base.TryGetValue(key, out result);
return result;
}
set
{
if (value == null)
{
base.Remove(key);
}
else
{
base[key] = value;
}
if (m_autoWrite && m_fileName != null)
{
Write(m_fileName);
}
}
}
public double GetDouble(string key, double defaultValue)
{
string value;
if (TryGetValue(key, out value))
{
double doubleValue;
if (double.TryParse(value, out doubleValue))
{
return doubleValue;
}
}
return defaultValue;
}
public string ElementName
{
get { return m_elementName; }
set { ElementName = value; }
}
public void Write(string xmlFileName)
{
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(xmlFileName)));
XmlWriterSettings settings = new XmlWriterSettings();
using (XmlWriter writer = XmlWriter.Create(xmlFileName, new XmlWriterSettings() { Indent = true, NewLineOnAttributes = true }))
{
WriteData(writer);
}
}
public void Write(Stream stream)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
writer.Close();
}
}
public void Read(string xmlFileName)
{
if (File.Exists(xmlFileName))
{
XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true, IgnoreComments = true };
using (XmlReader reader = XmlTextReader.Create(xmlFileName, settings))
{
Read(reader);
}
}
}
public void Read(Stream xmlDataStream)
{
XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true, IgnoreComments = true };
using (XmlReader reader = XmlTextReader.Create(xmlDataStream, settings))
{
Read(reader);
}
}
public void Read(XmlReader reader)
{
int entryDepth = reader.Depth;
try
{
reader.Read();
for (; ; )
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
// Console.WriteLine("Reader got element " + reader.Name);
if (reader.Depth > entryDepth)
{
string key = reader.Name;
string value = reader.ReadElementContentAsString();
Add(key, value);
continue;
}
else
{
if (reader.Name != m_elementName)
{
throw new Exception("Unexpected Element " + reader.Name + " expected " + m_elementName);
}
}
break;
case XmlNodeType.EndElement:
// Console.WriteLine("Reader got End element " + reader.Name);
return;
}
reader.Read();
}
}
catch (Exception)
{
Debug.WriteLine("Error reading configuration file. May have partial results");
}
}
#region Private
private void WriteData(XmlWriter writer)
{
writer.WriteStartElement(m_elementName);
foreach (KeyValuePair<string, string> entry in this)
{
// TODO worry about invalid historyInfo names
writer.WriteElementString(entry.Key, entry.Value);
}
writer.WriteEndElement();
}
private string m_elementName;
private string m_fileName;
private bool m_autoWrite;
#endregion
}
}