~haakan/do-plugins/sshfix

« back to all changes in this revision

Viewing changes to SqueezeCenter/src/Settings.cs

  • Committer: Jason Smith
  • Date: 2008-12-24 04:37:17 UTC
  • mto: This revision was merged to the branch mainline in revision 337.
  • Revision ID: jassmith@gmail.com-20081224043717-9yq3uhajlmnyyg5k
Merge community into official

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
5
//
 
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.
 
10
//
 
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/>.
 
13
 
 
14
using System;
 
15
using System.Collections.Generic;
 
16
using System.IO;
 
17
using Do;
 
18
 
 
19
namespace SqueezeCenter
 
20
{
 
21
        
 
22
        public static class Settings
 
23
        {
 
24
                
 
25
                public static void ReadSettings (string filename, ICollection<Setting> settings, bool storeMissingValues) 
 
26
                {
 
27
 
 
28
                        string line, key, val;
 
29
                        int i;
 
30
                        List<Setting> foundValues = new List<Setting> ();
 
31
                        StreamReader fileReader;
 
32
                        StreamWriter fileWriter;
 
33
                                                
 
34
                        if (File.Exists (filename)) {
 
35
                                
 
36
                                try {
 
37
                                        using (fileReader = new StreamReader (filename)) {
 
38
                                        
 
39
                                                while (null != (line = fileReader.ReadLine ())) {
 
40
                                                
 
41
                                                        line = line.Trim ();
 
42
                                                        if (line.Length == 0 || line.StartsWith ("#")) continue;
 
43
                                                        
 
44
                                                        i = line.IndexOf ("=");
 
45
                                                        if (i <= 0) continue;
 
46
                                                        
 
47
                                                        key = line.Substring (0, i).Trim ();
 
48
                                                        val = line.Substring (i+1, line.Length - i - 1).Trim ();
 
49
                                                        
 
50
                                                        foreach (Setting setting in settings) {
 
51
                                                                if (string.Equals (key, setting.Name, System.StringComparison.OrdinalIgnoreCase)) {
 
52
                                                                        setting.Value = val;
 
53
                                                                        foundValues.Add (setting);
 
54
                                                                        break;
 
55
                                                                }
 
56
                                                        }
 
57
                                                }
 
58
                                        }
 
59
                                }
 
60
                                catch (Exception ex) {
 
61
                                        Console.WriteLine ("SqueezeCenter: Error reading configuration file \"{0}\". Message: {1}", filename, ex.ToString ());
 
62
                                        return; 
 
63
                                }
 
64
                        }
 
65
 
 
66
                        try {
 
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 ());
 
73
                                                        }
 
74
                                                }
 
75
                                        }
 
76
                                }
 
77
                        }
 
78
                        catch (Exception ex) {
 
79
                                Console.WriteLine ("SqueezeCenter: Error writing configuration file \"{0}\". Message: {1}", filename, ex.ToString ());
 
80
                        }
 
81
                }
 
82
                
 
83
                public static void SaveSettings (string filename, ICollection<Setting> settings) 
 
84
                {
 
85
                        StreamWriter fileWriter;                                                
 
86
                        
 
87
                        using (fileWriter = new StreamWriter (filename, false)) 
 
88
                        {
 
89
                                foreach (Setting setting in settings) 
 
90
                                {                                       
 
91
                                        fileWriter.WriteLine ("# {0}", setting.Description);
 
92
                                        fileWriter.WriteLine ("{0} = {1}", setting.Name, setting.Value.ToString ());                                    
 
93
                                }
 
94
                        }
 
95
                }
 
96
 
 
97
                public class Setting
 
98
                {
 
99
                        string name, description, val;
 
100
                        object defaultVal;
 
101
                        
 
102
                        public Setting (string name, string description, object defaultVal)
 
103
                        {
 
104
                                this.name = name;
 
105
                                this.description = description;
 
106
                                this.val = defaultVal.ToString ();
 
107
                                this.defaultVal = defaultVal;
 
108
                        }
 
109
                                                                
 
110
                        public string Name
 
111
                        {
 
112
                                get { return this.name; }
 
113
                        }
 
114
                                
 
115
                        public string Description
 
116
                        {
 
117
                                get { return this.description; }
 
118
                        }
 
119
                                                                
 
120
                        public object DefaultValue
 
121
                        {
 
122
                                get { return this.defaultVal; }
 
123
                                set { this.defaultVal = value; }
 
124
                        }
 
125
                        
 
126
                        public string Value
 
127
                        {
 
128
                                get { return this.val; }
 
129
                                set { this.val = value; }
 
130
                        }
 
131
                        
 
132
                        public int ValueAsInt
 
133
                        {
 
134
                                get {
 
135
                                        int result;
 
136
                                        if (!int.TryParse (this.val, out result))
 
137
                                                result = (int)DefaultValue;
 
138
                                        return result;
 
139
                                }
 
140
                                set {
 
141
                                        this.val = value.ToString ();
 
142
                                }
 
143
                        }
 
144
                                
 
145
                        public bool ValueAsBool
 
146
                        {
 
147
                                get {
 
148
                                        bool result;
 
149
                                        if (!bool.TryParse (this.val, out result))
 
150
                                                result = (bool)DefaultValue;
 
151
                                        return result;
 
152
                                }
 
153
                                set {
 
154
                                        this.val = value.ToString ();
 
155
                                }
 
156
                        }
 
157
                }
 
158
        }
 
159
}
 
 
b'\\ No newline at end of file'