1
// This program is free software: you can redistribute it and/or modify
2
// it under the terms of the GNU General Public License as published by
3
// the Free Software Foundation, either version 3 of the License, or
4
// (at your option) any later version.
6
// This program is distributed in the hope that it will be useful,
7
// but WITHOUT ANY WARRANTY; without even the implied warranty of
8
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
// GNU General Public License for more details.
11
// You should have received a copy of the GNU General Public License
12
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
using System.Collections.Generic;
19
namespace SqueezeCenter
22
public static class Settings
25
public static void ReadSettings (string filename, ICollection<Setting> settings, bool storeMissingValues)
28
string line, key, val;
30
List<Setting> foundValues = new List<Setting> ();
31
StreamReader fileReader;
32
StreamWriter fileWriter;
34
if (File.Exists (filename)) {
37
using (fileReader = new StreamReader (filename)) {
39
while (null != (line = fileReader.ReadLine ())) {
42
if (line.Length == 0 || line.StartsWith ("#")) continue;
44
i = line.IndexOf ("=");
47
key = line.Substring (0, i).Trim ();
48
val = line.Substring (i+1, line.Length - i - 1).Trim ();
50
foreach (Setting setting in settings) {
51
if (string.Equals (key, setting.Name, System.StringComparison.OrdinalIgnoreCase)) {
53
foundValues.Add (setting);
60
catch (Exception ex) {
61
Console.WriteLine ("SqueezeCenter: Error reading configuration file \"{0}\". Message: {1}", filename, ex.ToString ());
67
if (storeMissingValues && foundValues.Count < settings.Count) {
68
using (fileWriter = new StreamWriter (filename, true)) {
69
foreach (Setting setting in settings) {
70
if (!foundValues.Contains (setting)) {
71
fileWriter.WriteLine ("# {0}", setting.Description);
72
fileWriter.WriteLine ("{0} = {1}", setting.Name, setting.DefaultValue.ToString ());
78
catch (Exception ex) {
79
Console.WriteLine ("SqueezeCenter: Error writing configuration file \"{0}\". Message: {1}", filename, ex.ToString ());
83
public static void SaveSettings (string filename, ICollection<Setting> settings)
85
StreamWriter fileWriter;
87
using (fileWriter = new StreamWriter (filename, false))
89
foreach (Setting setting in settings)
91
fileWriter.WriteLine ("# {0}", setting.Description);
92
fileWriter.WriteLine ("{0} = {1}", setting.Name, setting.Value.ToString ());
99
string name, description, val;
102
public Setting (string name, string description, object defaultVal)
105
this.description = description;
106
this.val = defaultVal.ToString ();
107
this.defaultVal = defaultVal;
112
get { return this.name; }
115
public string Description
117
get { return this.description; }
120
public object DefaultValue
122
get { return this.defaultVal; }
123
set { this.defaultVal = value; }
128
get { return this.val; }
129
set { this.val = value; }
132
public int ValueAsInt
136
if (!int.TryParse (this.val, out result))
137
result = (int)DefaultValue;
141
this.val = value.ToString ();
145
public bool ValueAsBool
149
if (!bool.TryParse (this.val, out result))
150
result = (bool)DefaultValue;
154
this.val = value.ToString ();
b'\\ No newline at end of file'