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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Globalization;
using KeePassLib.Utility;
namespace KeePass.App.Configuration
{
public sealed class AceKvp
{
private string m_strKey = null;
public string Key
{
get { return m_strKey; }
set { m_strKey = value; }
}
private string m_strValue = null;
public string Value
{
get { return m_strValue; }
set { m_strValue = value; }
}
public AceKvp()
{
}
public AceKvp(string strKey, string strValue)
{
m_strKey = strKey;
m_strValue = strValue;
}
}
public sealed class AceCustomConfig
{
private Dictionary<string, string> m_vItems = new Dictionary<string, string>();
public AceCustomConfig()
{
}
internal AceKvp[] Serialize()
{
List<AceKvp> v = new List<AceKvp>();
foreach(KeyValuePair<string, string> kvp in m_vItems)
v.Add(new AceKvp(kvp.Key, kvp.Value));
return v.ToArray();
}
internal void Deserialize(AceKvp[] v)
{
if(v == null) throw new ArgumentNullException("v");
m_vItems.Clear();
foreach(AceKvp kvp in v)
m_vItems[kvp.Key] = kvp.Value;
}
/// <summary>
/// Set a configuration item's value.
/// </summary>
/// <param name="strID">ID of the configuration item. This identifier
/// should consist only of English characters (a-z, A-Z, 0-9, '.',
/// ',', '-', '_') and should be unique -- for example (without quotes):
/// "PluginName.YourConfigGroupName.ItemName". Use upper camel
/// case as naming convention.</param>
/// <param name="strValue">New value of the configuration item.</param>
public void SetString(string strID, string strValue)
{
if(strID == null) throw new ArgumentNullException("strID");
if(strID.Length == 0) throw new ArgumentException();
if(strValue == null) m_vItems.Remove(strID);
else m_vItems[strID] = strValue;
}
/// <summary>
/// Set a configuration item's value.
/// </summary>
/// <param name="strID">ID of the configuration item. This identifier
/// should consist only of English characters (a-z, A-Z, 0-9, '.',
/// ',', '-', '_') and should be unique -- for example (without quotes):
/// "PluginName.YourConfigGroupName.ItemName". Use upper camel
/// case as naming convention.</param>
/// <param name="bValue">New value of the configuration item.</param>
public void SetBool(string strID, bool bValue)
{
SetString(strID, StrUtil.BoolToString(bValue));
}
/// <summary>
/// Set a configuration item's value.
/// </summary>
/// <param name="strID">ID of the configuration item. This identifier
/// should consist only of English characters (a-z, A-Z, 0-9, '.',
/// ',', '-', '_') and should be unique -- for example (without quotes):
/// "PluginName.YourConfigGroupName.ItemName". Use upper camel
/// case as naming convention.</param>
/// <param name="lValue">New value of the configuration item.</param>
public void SetLong(string strID, long lValue)
{
SetString(strID, lValue.ToString(NumberFormatInfo.InvariantInfo));
}
/// <summary>
/// Set a configuration item's value.
/// </summary>
/// <param name="strID">ID of the configuration item. This identifier
/// should consist only of English characters (a-z, A-Z, 0-9, '.',
/// ',', '-', '_') and should be unique -- for example (without quotes):
/// "PluginName.YourConfigGroupName.ItemName". Use upper camel
/// case as naming convention.</param>
/// <param name="uValue">New value of the configuration item.</param>
public void SetULong(string strID, ulong uValue)
{
SetString(strID, uValue.ToString(NumberFormatInfo.InvariantInfo));
}
public string GetString(string strID)
{
return GetString(strID, null);
}
/// <summary>
/// Get the current value of a custom configuration string.
/// </summary>
/// <param name="strID">ID of the configuration item.</param>
/// <param name="strDefault">Default value that is returned if
/// the specified configuration does not exist.</param>
/// <returns>Value of the configuration item.</returns>
public string GetString(string strID, string strDefault)
{
if(strID == null) throw new ArgumentNullException("strID");
if(strID.Length == 0) throw new ArgumentException();
string strValue;
if(m_vItems.TryGetValue(strID, out strValue)) return strValue;
return strDefault;
}
public bool GetBool(string strID, bool bDefault)
{
string strValue = GetString(strID, null);
if(string.IsNullOrEmpty(strValue)) return bDefault;
return StrUtil.StringToBool(strValue);
}
public long GetLong(string strID, long lDefault)
{
string strValue = GetString(strID, null);
if(string.IsNullOrEmpty(strValue)) return lDefault;
long lValue;
if(StrUtil.TryParseLongInvariant(strValue, out lValue))
return lValue;
return lDefault;
}
public ulong GetULong(string strID, ulong uDefault)
{
string strValue = GetString(strID, null);
if(string.IsNullOrEmpty(strValue)) return uDefault;
ulong uValue;
if(StrUtil.TryParseULongInvariant(strValue, out uValue))
return uValue;
return uDefault;
}
}
}
|