Utilities/PersistentSettings.cs
author moel.mich
Thu, 14 Oct 2010 17:30:51 +0000
changeset 222 ba64bb91ebe4
parent 166 fa9dfbfc4145
child 344 3145aadca3d2
permissions -rw-r--r--
Improved the invariant TSC frequency estimation code to ignore readings with a large error.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System.Collections.Generic;
    39 using System.Globalization;
    40 using System.Drawing;
    41 using System.Xml;
    42 using OpenHardwareMonitor.Hardware;
    43 
    44 namespace OpenHardwareMonitor {
    45   public class PersistentSettings : ISettings {
    46 
    47     private IDictionary<string, string> settings = 
    48       new Dictionary<string, string>();
    49 
    50     public void Load(string fileName) {
    51       XmlDocument doc = new XmlDocument();
    52       try {
    53         doc.Load(fileName);
    54       } catch {
    55         return;
    56       }
    57       XmlNodeList list = doc.GetElementsByTagName("appSettings");
    58       foreach (XmlNode node in list) {
    59         XmlNode parent = node.ParentNode;
    60         if (parent != null && parent.Name == "configuration" && 
    61           parent.ParentNode is XmlDocument) {
    62           foreach (XmlNode child in node.ChildNodes) {
    63             if (child.Name == "add") {
    64               XmlAttributeCollection attributes = child.Attributes;
    65               XmlAttribute keyAttribute = attributes["key"];
    66               XmlAttribute valueAttribute = attributes["value"];
    67               if (keyAttribute != null && valueAttribute != null && 
    68                 keyAttribute.Value != null) {
    69                 settings.Add(keyAttribute.Value, valueAttribute.Value);
    70               }
    71             }
    72           }
    73         }
    74       }      
    75     }
    76 
    77     public void Save(string fileName) {
    78       XmlDocument doc = new XmlDocument();
    79       doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
    80       XmlElement configuration = doc.CreateElement("configuration");
    81       doc.AppendChild(configuration);
    82       XmlElement appSettings = doc.CreateElement("appSettings");
    83       configuration.AppendChild(appSettings);
    84       foreach (KeyValuePair<string, string> keyValuePair in settings) {
    85         XmlElement add = doc.CreateElement("add");
    86         add.SetAttribute("key", keyValuePair.Key);
    87         add.SetAttribute("value", keyValuePair.Value);
    88         appSettings.AppendChild(add);
    89       }
    90       doc.Save(fileName);
    91     }
    92 
    93     public bool Contains(string name) {
    94       return settings.ContainsKey(name);
    95     }
    96 
    97     public void SetValue(string name, string value) {
    98       settings[name] = value;
    99     }
   100 
   101     public string GetValue(string name, string value) {
   102       string result;
   103       if (settings.TryGetValue(name, out result))
   104         return result;
   105       else
   106         return value;
   107     }
   108 
   109     public void Remove(string name) {
   110       settings.Remove(name);
   111     }
   112 
   113     public void SetValue(string name, int value) {
   114       settings[name] = value.ToString();
   115     }
   116 
   117     public int GetValue(string name, int value) {
   118       string str;
   119       if (settings.TryGetValue(name, out str)) {
   120         int parsedValue;
   121         if (int.TryParse(str, out parsedValue))
   122           return parsedValue;
   123         else
   124           return value;
   125       } else {
   126         return value;
   127       }
   128     }
   129 
   130     public void SetValue(string name, float value) {
   131       settings[name] = value.ToString(CultureInfo.InvariantCulture);
   132     }
   133 
   134     public float GetValue(string name, float value) {
   135       string str;
   136       if (settings.TryGetValue(name, out str)) {
   137         float parsedValue;
   138         if (float.TryParse(str, NumberStyles.Float, 
   139           CultureInfo.InvariantCulture, out parsedValue))
   140           return parsedValue;
   141         else
   142           return value;
   143       } else {
   144         return value;
   145       }
   146     }
   147 
   148     public void SetValue(string name, bool value) {
   149       settings[name] = value ? "true" : "false";
   150     }
   151 
   152     public bool GetValue(string name, bool value) {
   153       string str;
   154       if (settings.TryGetValue(name, out str)) {
   155         return str == "true";
   156       } else {
   157         return value;
   158       }
   159     }
   160 
   161     public void SetValue(string name, Color color) {
   162       settings[name] = color.ToArgb().ToString("X8");
   163     }
   164 
   165     public Color GetValue(string name, Color value) {
   166       string str;
   167       if (settings.TryGetValue(name, out str)) {
   168         int parsedValue;
   169         if (int.TryParse(str, NumberStyles.HexNumber,
   170           CultureInfo.InvariantCulture, out parsedValue))
   171           return Color.FromArgb(parsedValue);
   172         else
   173           return value;
   174       } else {
   175         return value;
   176       }
   177     }
   178   }
   179 }