|
using System; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using Aws.Diagnostics.Models.Settings; |
|
|
|
namespace TestApp.Services; |
|
|
|
public class PreferenceService : IPreferenceService |
|
{ |
|
public const string SettingsFile = "settings.ini"; |
|
|
|
private const char BracketClose = ']'; |
|
private const char BracketOpen = '['; |
|
private const char Equal = '='; |
|
private const char Hash = '#'; |
|
private const char Semi = ';'; |
|
|
|
private readonly Dictionary<string, Dictionary<string, string>> _iniData = |
|
new(StringComparer.OrdinalIgnoreCase); |
|
|
|
private string _filePath = string.Empty; |
|
|
|
public PreferenceService() |
|
{ |
|
_filePath = SettingsFile; |
|
Load(); |
|
} |
|
|
|
public bool AutoStartApp |
|
{ |
|
get => GetBool(Section.Settings, Key.AutoStartApp, false); |
|
set => SetBool(Section.Settings, Key.AutoStartApp, value); |
|
} |
|
|
|
public string FilePath { get => _filePath; set => _filePath = value; } |
|
|
|
public string NetworkHost1 |
|
{ |
|
get => GetValue(Section.Settings, Key.Ping1Host, DefaultLoopbackHost) ?? DefaultLoopbackHost; |
|
set => SetValue(Section.Settings, Key.Ping1Host, value); |
|
} |
|
|
|
public bool NetworkIsEnabled1 |
|
{ |
|
get => GetBool(Section.Settings, Key.Ping1Enabled, false); |
|
set => SetBool(Section.Settings, Key.Ping1Enabled, value); |
|
} |
|
|
|
public bool GetBool(string section, string key, bool defaultValue) |
|
{ |
|
var value = GetValue(section, key, defaultValue ? Value.True : Value.False); |
|
return value == Value.True; |
|
} |
|
|
|
public string? GetValue(string section, string key, string? defaultValue = null) |
|
{ |
|
if (_iniData.ContainsKey(section) && _iniData[section].ContainsKey(key)) |
|
return _iniData[section][key]; |
|
|
|
return defaultValue; |
|
} |
|
|
|
public int GetValue(string section, string key, int defaultValue = 0) |
|
{ |
|
// if (_iniData.ContainsKey(section) && _iniData[section].ContainsKey(key)) |
|
if (_iniData.TryGetValue(section, out Dictionary<string, string>? value) && value.ContainsKey(key)) |
|
{ |
|
string? item = _iniData[section][key]; |
|
if (int.TryParse(item, out int result)) |
|
return result; |
|
else |
|
return defaultValue; |
|
} |
|
|
|
return defaultValue; |
|
} |
|
|
|
public bool Load() |
|
{ |
|
_iniData.Clear(); |
|
|
|
if (!File.Exists(_filePath)) |
|
return false; |
|
|
|
string currentSection = string.Empty; |
|
foreach (string line in File.ReadAllLines(_filePath)) |
|
{ |
|
string trimmedLine = line.Trim(); |
|
|
|
// Skip empty lines and comments |
|
if (string.IsNullOrWhiteSpace(trimmedLine) || trimmedLine.StartsWith(Semi) || trimmedLine.StartsWith(Hash)) |
|
continue; |
|
|
|
if (trimmedLine.StartsWith(BracketOpen) && trimmedLine.EndsWith(BracketClose)) |
|
{ |
|
////currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2); |
|
currentSection = trimmedLine[1..^1]; |
|
if (!_iniData.ContainsKey(currentSection)) |
|
_iniData[currentSection] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
|
} |
|
else if (trimmedLine.Contains(Equal)) |
|
{ |
|
int equalsIndex = trimmedLine.IndexOf(Equal); |
|
|
|
////string key = trimmedLine.Substring(0, equalsIndex).Trim(); |
|
////string value = trimmedLine.Substring(equalsIndex + 1).Trim(); |
|
string key = trimmedLine[..equalsIndex].Trim(); |
|
string value = trimmedLine[(equalsIndex + 1)..].Trim(); |
|
|
|
if (!string.IsNullOrEmpty(currentSection) && _iniData.ContainsKey(currentSection)) |
|
_iniData[currentSection][key] = value; |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
|
|
public bool Save() |
|
{ |
|
try |
|
{ |
|
using StreamWriter writer = new(_filePath); |
|
foreach (var sectionEntry in _iniData) |
|
{ |
|
writer.WriteLine($"{BracketOpen}{sectionEntry.Key}{BracketClose}"); |
|
foreach (var keyValuePair in sectionEntry.Value) |
|
writer.WriteLine($"{keyValuePair.Key}{Equal}{keyValuePair.Value}"); |
|
|
|
// Add a blank line between sections for readability |
|
writer.WriteLine(); |
|
} |
|
} |
|
catch |
|
{ |
|
return false; |
|
} |
|
|
|
return true; |
|
} |
|
|
|
public void SetBool(string section, string key, bool value) => |
|
SetValue(section, key, value ? Value.True : Value.False); |
|
|
|
public void SetValue(string section, string key, string value) |
|
{ |
|
if (!_iniData.ContainsKey(section)) |
|
_iniData[section] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
|
|
|
_iniData[section][key] = value; |
|
|
|
Save(); |
|
} |
|
|
|
public void SetValue(string section, string key, int value) => |
|
SetValue(section, key, value.ToString()); |
|
} |