![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace YakaLib
{
public class Config
{
private Dictionary<string, string> _KeyValue = new Dictionary<string, string>();
private Dictionary<string, List<string>> _KeyList = new Dictionary<string, List<string>>();
private Dictionary<string, List<string>> _KeyRichList = new Dictionary<string, List<string>>();
private string configDir;
public Config(string strConfigfile)
{
configDir = strConfigfile;
StreamReader sr = new StreamReader(strConfigfile);
while (!sr.EndOfStream)
{
string strLine = sr.ReadLine();
strLine = strLine.Replace(" ", "");
if (strLine.Contains('='))
{
int nIndex = strLine.IndexOf('=');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex + 1)
{
_KeyValue[strLine.Substring(0, nIndex)] = strLine.Substring(nIndex + 1);
}
}
else if ((!strLine.StartsWith("#")) && strLine.Contains('{'))
{
List<string> list = new List<string>();
int nIndex = strLine.IndexOf('{');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex)
{
_KeyList[strLine.Substring(0, nIndex)] = list;
}
string item = "";
while (!sr.EndOfStream)
{
item = sr.ReadLine();
item = item.Replace(" ", "");
if (item.StartsWith("#") || item.Length == 0)
{
continue;
}
if ((!item.StartsWith("#")) && item.Contains('}'))
{
int itemsEnd = item.IndexOf('}');
item = item.Substring(0, itemsEnd);
if (item.Length != 0)
{
list.Add(item);
}
break;
}
list.Add(item);
}
}
}
sr.Close();
FillRichDict();
}
public Config(List<string> strConfigList)
{
foreach(string str in strConfigList)
{
string strLine = str.Replace(" ", "");
if (strLine.Contains('='))
{
int nIndex = strLine.IndexOf('=');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex + 1)
{
_KeyValue[strLine.Substring(0, nIndex)] = strLine.Substring(nIndex + 1);
}
}
}
}
public override string ToString()
{
string str = "";
StreamReader sr = new StreamReader(configDir);
while (!sr.EndOfStream)
{
str += sr.ReadLine();
str += "\r\n";
}
return str;
}
public string Get(string key)
{
if (_KeyValue.ContainsKey(key))
{
return _KeyValue[key];
}
else
{
return "";
}
}
public int Set(string key, string value)
{
int errorCode = -1;
if (_KeyValue.ContainsKey(key))
{
_KeyValue[key] = value;
RefreshKeyToFile(key);
errorCode = 0;
}
else
{
errorCode = -1;
}
return errorCode;
}
public void RefreshDictToFile()
{
}
public void RefreshKeyToFile(string key)
{
if (_KeyValue.ContainsKey(key))
{
List<string> fileStream = new List<string>();
//int KeyLineNumber = 0;
StreamReader sr = new StreamReader(configDir);
while (!sr.EndOfStream)
{
string strLine = sr.ReadLine();
if (strLine.Contains(key))
{
string[] strSplitedArr = strLine.Split('=');
if (strSplitedArr.Length == 2)
{
strSplitedArr[1] = " " + _KeyValue[key];
strLine = strSplitedArr[0] + "=" + strSplitedArr[1];
}
}
fileStream.Add(strLine);
}
sr.Close();
StreamWriter sw = new StreamWriter(configDir);
foreach (string item in fileStream)
{
sw.WriteLine(item);
sw.Flush();
}
sw.Close();
}
}
public List<string> GetList(string key)
{
if (_KeyList.ContainsKey(key))
{
return _KeyList[key];
}
else
{
return new List<string>();
}
}
private void FillRichDict()
{
StreamReader sr = new StreamReader(configDir);
while (!sr.EndOfStream)
{
string strLine = sr.ReadLine();
strLine = strLine.Replace(" ", "");
if (strLine.Contains('='))
{
int nIndex = strLine.IndexOf('=');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex + 1)
{
_KeyValue[strLine.Substring(0, nIndex)] = strLine.Substring(nIndex + 1);
}
}
else if ((!strLine.StartsWith("#")) && strLine.Contains('{'))
{
List<string> list = new List<string>();
int nIndex = strLine.IndexOf('{');
//if (strLine.StartsWith("#") && strLine.Contains("{"))
//{
// continue;
//}
if (nIndex > 0 && strLine.Length > nIndex)
{
_KeyRichList[strLine.Substring(0, nIndex)] = list;
}
string item = "";
while (!sr.EndOfStream)
{
item = sr.ReadLine();
//item = item.Replace(" ", "");
//if (item.StartsWith("#") || item.Length == 0)
//{
// continue;
//}
if ((!item.StartsWith("#")) && item.Contains('}'))
{
int itemsEnd = item.IndexOf('}');
item = item.Substring(0, itemsEnd);
if (item.Length != 0)
{
list.Add(item);
}
break;
}
list.Add(item);
}
}
}
sr.Close();
}
public Dictionary<string, List<string>> GetKeyRichListDict()
{
return _KeyRichList;
}
public Dictionary<string, string> GetKeyValueDict()
{
return _KeyValue;
}
public Dictionary<string, List<string>> GetKeyListDict()
{
return _KeyList;
}
public bool CheckLexical(ref string error)
{
StreamReader sr = new StreamReader(configDir);
UInt32 linenumber = 0;
int symbol = 0;
while (!sr.EndOfStream)
{
linenumber++;
string str = sr.ReadLine();
if (str.StartsWith("#"))
{
continue;
}
if (str.Contains('{'))
{
symbol++;
}
else if (str.Contains("}"))
{
if (symbol == 1)
{
symbol--;
}
else if (symbol == 0)
{
error = "Lost a { to match } in line " + linenumber;
return false;
}
}
}
if (symbol == 0)
{
return true;
}
else
{
error = "miss { or }";
return false;
}
}
public bool CheckLexical()
{
StreamReader sr = new StreamReader(configDir);
UInt32 linenumber = 0;
int symbol = 0;
while (!sr.EndOfStream)
{
linenumber++;
string str = sr.ReadLine();
if (str.StartsWith("#"))
{
continue;
}
if (str.Contains('{'))
{
symbol++;
}
else if (str.Contains("}"))
{
if (symbol == 1)
{
symbol--;
}
else if (symbol == 0)
{
//error = "Lost a { to match } in line " + linenumber;
return false;
}
}
}
if (symbol == 0)
{
return true;
}
else
{
//error = "miss { or }";
return false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace YakaLib
{
public class Config
{
private Dictionary<string, string> _KeyValue = new Dictionary<string, string>();
private Dictionary<string, List<string>> _KeyList = new Dictionary<string, List<string>>();
private Dictionary<string, List<string>> _KeyRichList = new Dictionary<string, List<string>>();
private string configDir;
public Config(string strConfigfile)
{
configDir = strConfigfile;
StreamReader sr = new StreamReader(strConfigfile);
while (!sr.EndOfStream)
{
string strLine = sr.ReadLine();
strLine = strLine.Replace(" ", "");
if (strLine.Contains('='))
{
int nIndex = strLine.IndexOf('=');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex + 1)
{
_KeyValue[strLine.Substring(0, nIndex)] = strLine.Substring(nIndex + 1);
}
}
else if ((!strLine.StartsWith("#")) && strLine.Contains('{'))
{
List<string> list = new List<string>();
int nIndex = strLine.IndexOf('{');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex)
{
_KeyList[strLine.Substring(0, nIndex)] = list;
}
string item = "";
while (!sr.EndOfStream)
{
item = sr.ReadLine();
item = item.Replace(" ", "");
if (item.StartsWith("#") || item.Length == 0)
{
continue;
}
if ((!item.StartsWith("#")) && item.Contains('}'))
{
int itemsEnd = item.IndexOf('}');
item = item.Substring(0, itemsEnd);
if (item.Length != 0)
{
list.Add(item);
}
break;
}
list.Add(item);
}
}
}
sr.Close();
FillRichDict();
}
public Config(List<string> strConfigList)
{
foreach(string str in strConfigList)
{
string strLine = str.Replace(" ", "");
if (strLine.Contains('='))
{
int nIndex = strLine.IndexOf('=');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex + 1)
{
_KeyValue[strLine.Substring(0, nIndex)] = strLine.Substring(nIndex + 1);
}
}
}
}
public override string ToString()
{
string str = "";
StreamReader sr = new StreamReader(configDir);
while (!sr.EndOfStream)
{
str += sr.ReadLine();
str += "\r\n";
}
return str;
}
public string Get(string key)
{
if (_KeyValue.ContainsKey(key))
{
return _KeyValue[key];
}
else
{
return "";
}
}
public int Set(string key, string value)
{
int errorCode = -1;
if (_KeyValue.ContainsKey(key))
{
_KeyValue[key] = value;
RefreshKeyToFile(key);
errorCode = 0;
}
else
{
errorCode = -1;
}
return errorCode;
}
public void RefreshDictToFile()
{
}
public void RefreshKeyToFile(string key)
{
if (_KeyValue.ContainsKey(key))
{
List<string> fileStream = new List<string>();
//int KeyLineNumber = 0;
StreamReader sr = new StreamReader(configDir);
while (!sr.EndOfStream)
{
string strLine = sr.ReadLine();
if (strLine.Contains(key))
{
string[] strSplitedArr = strLine.Split('=');
if (strSplitedArr.Length == 2)
{
strSplitedArr[1] = " " + _KeyValue[key];
strLine = strSplitedArr[0] + "=" + strSplitedArr[1];
}
}
fileStream.Add(strLine);
}
sr.Close();
StreamWriter sw = new StreamWriter(configDir);
foreach (string item in fileStream)
{
sw.WriteLine(item);
sw.Flush();
}
sw.Close();
}
}
public List<string> GetList(string key)
{
if (_KeyList.ContainsKey(key))
{
return _KeyList[key];
}
else
{
return new List<string>();
}
}
private void FillRichDict()
{
StreamReader sr = new StreamReader(configDir);
while (!sr.EndOfStream)
{
string strLine = sr.ReadLine();
strLine = strLine.Replace(" ", "");
if (strLine.Contains('='))
{
int nIndex = strLine.IndexOf('=');
if (strLine.StartsWith("#"))
{
continue;
}
if (nIndex > 0 && strLine.Length > nIndex + 1)
{
_KeyValue[strLine.Substring(0, nIndex)] = strLine.Substring(nIndex + 1);
}
}
else if ((!strLine.StartsWith("#")) && strLine.Contains('{'))
{
List<string> list = new List<string>();
int nIndex = strLine.IndexOf('{');
//if (strLine.StartsWith("#") && strLine.Contains("{"))
//{
// continue;
//}
if (nIndex > 0 && strLine.Length > nIndex)
{
_KeyRichList[strLine.Substring(0, nIndex)] = list;
}
string item = "";
while (!sr.EndOfStream)
{
item = sr.ReadLine();
//item = item.Replace(" ", "");
//if (item.StartsWith("#") || item.Length == 0)
//{
// continue;
//}
if ((!item.StartsWith("#")) && item.Contains('}'))
{
int itemsEnd = item.IndexOf('}');
item = item.Substring(0, itemsEnd);
if (item.Length != 0)
{
list.Add(item);
}
break;
}
list.Add(item);
}
}
}
sr.Close();
}
public Dictionary<string, List<string>> GetKeyRichListDict()
{
return _KeyRichList;
}
public Dictionary<string, string> GetKeyValueDict()
{
return _KeyValue;
}
public Dictionary<string, List<string>> GetKeyListDict()
{
return _KeyList;
}
public bool CheckLexical(ref string error)
{
StreamReader sr = new StreamReader(configDir);
UInt32 linenumber = 0;
int symbol = 0;
while (!sr.EndOfStream)
{
linenumber++;
string str = sr.ReadLine();
if (str.StartsWith("#"))
{
continue;
}
if (str.Contains('{'))
{
symbol++;
}
else if (str.Contains("}"))
{
if (symbol == 1)
{
symbol--;
}
else if (symbol == 0)
{
error = "Lost a { to match } in line " + linenumber;
return false;
}
}
}
if (symbol == 0)
{
return true;
}
else
{
error = "miss { or }";
return false;
}
}
public bool CheckLexical()
{
StreamReader sr = new StreamReader(configDir);
UInt32 linenumber = 0;
int symbol = 0;
while (!sr.EndOfStream)
{
linenumber++;
string str = sr.ReadLine();
if (str.StartsWith("#"))
{
continue;
}
if (str.Contains('{'))
{
symbol++;
}
else if (str.Contains("}"))
{
if (symbol == 1)
{
symbol--;
}
else if (symbol == 0)
{
//error = "Lost a { to match } in line " + linenumber;
return false;
}
}
}
if (symbol == 0)
{
return true;
}
else
{
//error = "miss { or }";
return false;
}
}
}
}