Skip to content

Commit

Permalink
修正部分错误
Browse files Browse the repository at this point in the history
  • Loading branch information
BluePointLilac committed Oct 15, 2020
1 parent 44e7b23 commit c718d32
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions ContextMenuManager/BulePointLilac.Methods/IniReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ public class IniReader
{
public IniReader(StringBuilder sb)
{
if(string.IsNullOrWhiteSpace(sb.ToString())) return;
List<string> lines = sb.ToString().Split(new[] { Environment.NewLine },
if (string.IsNullOrWhiteSpace(sb.ToString())) return;
List<string> lines = sb.ToString().Split(new[] { "\r\n", "\n" },
StringSplitOptions.RemoveEmptyEntries).ToList();//拆分为行
lines.ForEach(line => line.Trim());
ReadLines(lines);
}

public IniReader(string filePath)
{
if(!File.Exists(filePath)) return;
if (!File.Exists(filePath)) return;
List<string> lines = new List<string>();
using(StreamReader reader = new StreamReader(filePath, EncodingType.GetType(filePath)))
while(!reader.EndOfStream)
using (StreamReader reader = new StreamReader(filePath, EncodingType.GetType(filePath)))
while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();
if(line != string.Empty) lines.Add(line);
if (line != string.Empty) lines.Add(line);
}
ReadLines(lines);
}
Expand All @@ -39,26 +39,26 @@ private void ReadLines(List<string> lines)
line => line.StartsWith(";")//移除注释
|| (!line.StartsWith("[") && !line.Contains("=")));//移除非section行且非key行

if(lines.Count == 0) return;
if (lines.Count == 0) return;

List<int> indexs = new List<int> { 0 };
for(int i = 1; i < lines.Count; i++)
for (int i = 1; i < lines.Count; i++)
{
if(lines[i].StartsWith("[")) indexs.Add(i);//获取section行号
if (lines[i].StartsWith("[")) indexs.Add(i);//获取section行号
}
indexs.Add(lines.Count);

for(int i = 0; i < indexs.Count - 1; i++)
for (int i = 0; i < indexs.Count - 1; i++)
{
string section = lines[indexs[i]];
int m = section.IndexOf(']') - 1;
if(m < 0) continue;
if (m < 0) continue;
section = section.Substring(1, m);
if(rootDic.ContainsKey(section)) continue;
if (rootDic.ContainsKey(section)) continue;
var keyValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
rootDic.Add(section, keyValues);

for(int j = indexs[i] + 1; j < indexs[i + 1]; j++)
for (int j = indexs[i] + 1; j < indexs[i + 1]; j++)
{
int k = lines[j].IndexOf('=');
string key = lines[j].Substring(0, k).TrimEnd();
Expand All @@ -70,8 +70,8 @@ private void ReadLines(List<string> lines)

public string GetValue(string section, string key)
{
if(rootDic.TryGetValue(section, out Dictionary<string, string> sectionDic))
if(sectionDic.TryGetValue(key, out string value))
if (rootDic.TryGetValue(section, out Dictionary<string, string> sectionDic))
if (sectionDic.TryGetValue(key, out string value))
return value;
return string.Empty;
}
Expand Down

0 comments on commit c718d32

Please sign in to comment.