Sicherung

This commit is contained in:
Maier Stephan SI
2023-01-02 04:33:49 +01:00
parent bea46135fd
commit d01747f75a
284 changed files with 6106 additions and 65112 deletions

View File

@@ -0,0 +1,21 @@
namespace Config.Net.Stores.Formats.Ini
{
class IniComment : IniEntity
{
public const string CommentSeparator = ";";
public IniComment(string value)
{
Value = value;
}
public string Value { get; set; }
public string EscapedValue
{
get { return Value.Replace("\r", @"\r").Replace("\n", @"\n"); }
}
public override string ToString() => Value;
}
}

View File

@@ -0,0 +1,6 @@
namespace Config.Net.Stores.Formats.Ini
{
abstract class IniEntity
{
}
}

View File

@@ -0,0 +1,72 @@
using System;
namespace Config.Net.Stores.Formats.Ini
{
internal class IniKeyValue : IniEntity
{
public const string KeyValueSeparator = "=";
public IniKeyValue(string key, string value, string? comment)
{
if(key == null) throw new ArgumentNullException(nameof(key));
Key = key;
Value = value;
Comment = comment == null ? null : new IniComment(comment);
}
public string Key { get; }
public string Value { get; set; }
public string EscapedKey
{
get { return Key.Replace("\r", @"\r").Replace("\n", @"\n"); }
}
public string EscapedValue
{
get { return Value.Replace("\r", @"\r").Replace("\n", @"\n"); }
}
public IniComment? Comment { get; }
public static IniKeyValue? FromLine(string line, bool parseInlineComments, bool unescapeNewLines = false)
{
int idx = line.IndexOf(KeyValueSeparator, StringComparison.CurrentCulture);
if(idx == -1) return null;
string key = line.Substring(0, idx).Trim();
string value = line.Substring(idx + 1).Trim();
string? comment = null;
if (parseInlineComments)
{
idx = value.LastIndexOf(IniComment.CommentSeparator, StringComparison.CurrentCulture);
if (idx != -1)
{
comment = value.Substring(idx + 1).Trim();
value = value.Substring(0, idx).Trim();
}
}
if(unescapeNewLines)
{
key = UnescapeString(key);
value = UnescapeString(value);
comment = (comment != null) ? UnescapeString(comment) : null;
}
return new IniKeyValue(key, value, comment);
}
private static string UnescapeString(string key)
{
return key.Replace(@"\r", "\r").Replace(@"\n", "\n");
}
public override string ToString()
{
return $"{Value}";
}
}
}

View File

@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace Config.Net.Stores.Formats.Ini
{
class IniSection
{
public const string SectionKeySeparator = ".";
private readonly List<IniEntity> _entities = new List<IniEntity>();
private readonly Dictionary<string, IniKeyValue> _keyToValue = new Dictionary<string, IniKeyValue>();
/// <summary>
/// Section name
/// </summary>
public string? Name { get; set; }
/// <summary>
///
/// </summary>
/// <param name="name">Pass null to work with global section</param>
public IniSection(string? name)
{
if(name != null)
{
if (name.StartsWith("[")) name = name.Substring(1);
if (name.EndsWith("]")) name = name.Substring(0, name.Length - 1);
}
Name = name;
}
public void Add(IniEntity entity)
{
_entities.Add(entity);
IniKeyValue? ikv = entity as IniKeyValue;
if(ikv != null)
{
_keyToValue[ikv.Key] = ikv;
}
}
public IniKeyValue? Set(string key, string? value)
{
if(value == null)
{
IniKeyValue? ikv;
if(_keyToValue.TryGetValue(key, out ikv))
{
_keyToValue.Remove(key);
return ikv;
}
return null;
}
else
{
IniKeyValue? ikv;
if(_keyToValue.TryGetValue(key, out ikv))
{
ikv.Value = value;
}
else
{
ikv = new IniKeyValue(key, value, null);
Add(ikv);
}
return ikv;
}
}
public static void SplitKey(string fullKey, out string? sectionName, out string keyName)
{
int idx = fullKey.IndexOf(SectionKeySeparator, StringComparison.CurrentCulture);
if(idx == -1)
{
sectionName = null;
keyName = fullKey;
}
else
{
sectionName = fullKey.Substring(0, idx);
keyName = fullKey.Substring(idx + 1);
}
}
public void WriteTo(StreamWriter writer)
{
foreach(IniEntity entity in _entities)
{
IniKeyValue? ikv = entity as IniKeyValue;
if(ikv != null)
{
writer.Write($"{ikv.EscapedKey}{IniKeyValue.KeyValueSeparator}{ikv.EscapedValue}");
if(ikv.Comment != null)
{
writer.Write(" ");
writer.Write(IniComment.CommentSeparator);
writer.Write(ikv.Comment.EscapedValue);
}
writer.WriteLine();
continue;
}
IniComment? comment = entity as IniComment;
if(comment != null)
{
writer.Write(IniComment.CommentSeparator);
writer.WriteLine(comment.Value);
}
}
}
public override string ToString()
{
return Name ?? string.Empty;
}
}
}

View File

@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Config.Net.Stores.Formats.Ini
{
class StructuredIniFile
{
private const string _sectionBegin = "[";
private const string _sectionEnd = "]";
private static readonly char[] _sectionTrims = {'[', ']'};
private readonly IniSection _globalSection;
private readonly List<IniSection> _sections = new List<IniSection>();
private readonly Dictionary<string, IniKeyValue> _fullKeyNameToValue = new Dictionary<string, IniKeyValue>(StringComparer.InvariantCultureIgnoreCase);
public StructuredIniFile()
{
_globalSection = new IniSection(null);
_sections.Add(_globalSection);
}
public string? this[string key]
{
get
{
if(key == null) return null;
IniKeyValue? value;
return !_fullKeyNameToValue.TryGetValue(key, out value) ? null : value.Value;
}
set
{
if(key == null) return;
IniSection.SplitKey(key, out string? sectionName, out string keyName);
IniSection? section = sectionName == null
? _globalSection
: _sections.FirstOrDefault(s => s.Name == sectionName);
if(section == null)
{
section = new IniSection(sectionName);
_sections.Add(section);
}
IniKeyValue? ikv = section.Set(keyName, value);
//update the local cache
if(ikv != null)
{
if(value == null)
{
_fullKeyNameToValue.Remove(key);
}
else
{
_fullKeyNameToValue[key] = ikv;
}
}
}
}
public static StructuredIniFile ReadFrom(Stream inputStream, bool parseInlineComments, bool unescapeNewLines = false)
{
if(inputStream == null) throw new ArgumentNullException(nameof(inputStream));
var file = new StructuredIniFile();
using(var reader = new StreamReader(inputStream))
{
IniSection section = file._globalSection;
string? line;
while((line = reader.ReadLine()) != null)
{
line = line.Trim();
if(line.StartsWith(_sectionBegin))
{
//start new section
line = line.Trim();
section = new IniSection(line);
file._sections.Add(section);
}
else if(line.StartsWith(IniComment.CommentSeparator))
{
//whole line is a comment
string comment = line.Substring(1).Trim();
section.Add(new IniComment(comment));
}
else
{
IniKeyValue? ikv = IniKeyValue.FromLine(line, parseInlineComments, unescapeNewLines);
if(ikv == null) continue;
section.Add(ikv);
string fullKey = section.Name == null
? ikv.Key
: $"{section.Name}{IniSection.SectionKeySeparator}{ikv.Key}";
file._fullKeyNameToValue[fullKey] = ikv;
}
}
}
return file;
}
public void WriteTo(Stream outputStream)
{
if(outputStream == null) throw new ArgumentNullException(nameof(outputStream));
using(var writer = new StreamWriter(outputStream))
{
foreach(IniSection section in _sections)
{
if(section.Name != null)
{
writer.WriteLine();
writer.WriteLine($"{_sectionBegin}{section.Name}{_sectionEnd}");
}
section.WriteTo(writer);
}
}
}
//private static
}
}