BUG-2037: Added a small fix to improve the ini stability, also added some other small changes which were pending.

This commit is contained in:
Robin 2016-09-29 18:10:32 +02:00
commit 3b214133e4
5 changed files with 57 additions and 46 deletions

View file

@ -24,40 +24,51 @@ using System.IO;
using System.Text;
namespace Greenshot.IniFile {
/// <summary>
///
/// </summary>
public static class IniReader {
private const string SECTION_START = "[";
private const string SECTION_END = "]";
private const string COMMENT = ";";
private static readonly char[] ASSIGNMENT = new[] { '=' };
private const string SectionStart = "[";
private const string SectionEnd = "]";
private const string Comment = ";";
private static readonly char[] Assignment = { '=' };
/**
* Read an ini file to a Dictionary, each key is a section and the value is a Dictionary with name and values.
*/
public static Dictionary<string, Dictionary<string, string>> read(string path, Encoding encoding) {
Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>();
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024)) {
using (StreamReader reader = new StreamReader(fileStream, encoding)) {
Dictionary<string, string> nameValues = new Dictionary<string, string>();
while (!reader.EndOfStream) {
string line = reader.ReadLine();
if (line != null) {
string cleanLine = line.Trim();
if (cleanLine.Length == 0 || cleanLine.StartsWith(COMMENT)) {
continue;
}
if (cleanLine.StartsWith(SECTION_START)) {
string section = line.Replace(SECTION_START, "").Replace(SECTION_END, "").Trim();
/// <summary>
/// Read an ini file to a Dictionary, each key is a section and the value is a Dictionary with name and values.
/// </summary>
/// <param name="path"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static IDictionary<string, IDictionary<string, string>> Read(string path, Encoding encoding) {
var ini = new Dictionary<string, IDictionary<string, string>>();
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024)) {
using (var streamReader = new StreamReader(fileStream, encoding)) {
IDictionary<string, string> nameValues = new Dictionary<string, string>();
while (!streamReader.EndOfStream) {
string line = streamReader.ReadLine();
if (line == null)
{
continue;
}
string cleanLine = line.Trim();
if (cleanLine.Length == 0 || cleanLine.StartsWith(Comment)) {
continue;
}
if (cleanLine.StartsWith(SectionStart)) {
string section = line.Replace(SectionStart, string.Empty).Replace(SectionEnd, string.Empty).Trim();
if (!ini.TryGetValue(section, out nameValues))
{
nameValues = new Dictionary<string, string>();
ini.Add(section, nameValues);
}
} else {
string[] keyvalueSplitter = line.Split(Assignment, 2);
string name = keyvalueSplitter[0];
string inivalue = keyvalueSplitter.Length > 1 ? keyvalueSplitter[1] : null;
if (nameValues.ContainsKey(name)) {
nameValues[name] = inivalue;
} else {
string[] keyvalueSplitter = line.Split(ASSIGNMENT, 2);
string name = keyvalueSplitter[0];
string inivalue = keyvalueSplitter.Length > 1 ? keyvalueSplitter[1] : null;
if (nameValues.ContainsKey(name)) {
nameValues[name] = inivalue;
} else {
nameValues.Add(name, inivalue);
}
nameValues.Add(name, inivalue);
}
}
}