![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
namespaceYakaLib
{
publicclassConfig
{
privateDictionarystring,string_KeyValue=newDictionarystring,string();
privateDictionarystring,Liststring_KeyList=newDictionarystring,Liststring();
privateDictionarystring,Liststring_KeyRichList=newDictionarystring,Liststring();
privatestringconfigDir;
publicConfig(stringstrConfigfile)
{
configDir=strConfigfile;
StreamReadersr=newStreamReader(strConfigfile);
while(!sr.EndOfStream)
{
stringstrLine=sr.ReadLine();
strLine=strLine.Replace("","");
if(strLine.Contains('='))
{
intnIndex=strLine.IndexOf('=');
if(strLine.StartsWith("#"))
{
continue;
}
if(nIndex0strLine.LengthnIndex+1)
{
_KeyValue[strLine.Substring(0,nIndex)]=strLine.Substring(nIndex+1);
}
}
elseif((!strLine.StartsWith("#"))strLine.Contains('{'))
{
Liststringlist=newListstring();
intnIndex=strLine.IndexOf('{');
if(strLine.StartsWith("#"))
{
continue;
}
if(nIndex0strLine.LengthnIndex)
{
_KeyList[strLine.Substring(0,nIndex)]=list;
}
stringitem="";
while(!sr.EndOfStream)
{
item=sr.ReadLine();
item=item.Replace("","");
if(item.StartsWith("#")||item.Length==0)
{
continue;
}
if((!item.StartsWith("#"))item.Contains('}'))
{
intitemsEnd=item.IndexOf('}');
item=item.Substring(0,itemsEnd);
if(item.Length!=0)
{
list.Add(item);
}
break;
}
list.Add(item);
}
}
}
sr.Close();
FillRichDict();
}
publicConfig(ListstringstrConfigList)
{
foreach(stringstrinstrConfigList)
{
stringstrLine=str.Replace("","");
if(strLine.Contains('='))
{
intnIndex=strLine.IndexOf('=');
if(strLine.StartsWith("#"))
{
continue;
}
if(nIndex0strLine.LengthnIndex+1)
{
_KeyValue[strLine.Substring(0,nIndex)]=strLine.Substring(nIndex+1);
}
}
}
}
publicoverridestringToString()
{
stringstr="";
StreamReadersr=newStreamReader(configDir);
while(!sr.EndOfStream)
{
str+=sr.ReadLine();
str+="\r\n";
}
returnstr;
}
publicstringGet(stringkey)
{
if(_KeyValue.ContainsKey(key))
{
return_KeyValue[key];
}
else
{
return"";
}
}
publicintSet(stringkey,stringvalue)
{
interrorCode=-1;
if(_KeyValue.ContainsKey(key))
{
_KeyValue[key]=value;
RefreshKeyToFile(key);
errorCode=0;
}
else
{
errorCode=-1;
}
returnerrorCode;
}
publicvoidRefreshDictToFile()
{
}
publicvoidRefreshKeyToFile(stringkey)
{
if(_KeyValue.ContainsKey(key))
{
ListstringfileStream=newListstring();
//intKeyLineNumber=0;
StreamReadersr=newStreamReader(configDir);
while(!sr.EndOfStream)
{
stringstrLine=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();
StreamWritersw=newStreamWriter(configDir);
foreach(stringiteminfileStream)
{
sw.WriteLine(item);
sw.Flush();
}
sw.Close();
}
}
publicListstringGetList(stringkey)
{
if(_KeyList.ContainsKey(key))
{
return_KeyList[key];
}
else
{
returnnewListstring();
}
}
privatevoidFillRichDict()
{
StreamReadersr=newStreamReader(configDir);
while(!sr.EndOfStream)
{
stringstrLine=sr.ReadLine();
strLine=strLine.Replace("","");
if(strLine.Contains('='))
{
intnIndex=strLine.IndexOf('=');
if(strLine.StartsWith("#"))
{
continue;
}
if(nIndex0strLine.LengthnIndex+1)
{
_KeyValue[strLine.Substring(0,nIndex)]=strLine.Substring(nIndex+1);
}
}
elseif((!strLine.StartsWith("#"))strLine.Contains('{'))
{
Liststringlist=newListstring();
intnIndex=strLine.IndexOf('{');
//if(strLine.StartsWith("#")strLine.Contains("{"))
//{
//continue;
//}
if(nIndex0strLine.LengthnIndex)
{
_KeyRichList[strLine.Substring(0,nIndex)]=list;
}
stringitem="";
while(!sr.EndOfStream)
{
item=sr.ReadLine();
//item=item.Replace("","");
//if(item.StartsWith("#")||item.Length==0)
//{
//continue;
//}
if((!item.StartsWith("#"))item.Contains('}'))
{
intitemsEnd=item.IndexOf('}');
item=item.Substring(0,itemsEnd);
if(item.Length!=0)
{
list.Add(item);
}
break;
}
list.Add(item);
}
}
}
sr.Close();
}
publicDictionarystring,ListstringGetKeyRichListDict()
{
return_KeyRichList;
}
publicDictionarystring,stringGetKeyValueDict()
{
return_KeyValue;
}
publicDictionarystring,ListstringGetKeyListDict()
{
return_KeyList;
}
publicboolCheckLexical(refstringerror)
{
StreamReadersr=newStreamReader(configDir);
UInt32linenumber=0;
intsymbol=0;
while(!sr.EndOfStream)
{
linenumber++;
stringstr=sr.ReadLine();
if(str.StartsWith("#"))
{
continue;
}
if(str.Contains('{'))
{
symbol++;
}
elseif(str.Contains("}"))
{
if(symbol==1)
{
symbol--;
}
elseif(symbol==0)
{
error="Losta{tomatch}inline"+linenumber;
returnfalse;
}
}
}
if(symbol==0)
{
returntrue;
}
else
{
error="miss{or}";
returnfalse;
}
}
publicboolCheckLexical()
{
StreamReadersr=newStreamReader(configDir);
UInt32linenumber=0;
intsymbol=0;
while(!sr.EndOfStream)
{
linenumber++;
stringstr=sr.ReadLine();
if(str.StartsWith("#"))
{
continue;
}
if(str.Contains('{'))
{
symbol++;
}
elseif(str.Contains("}"))
{
if(symbol==1)
{
symbol--;
}
elseif(symbol==0)
{
//error="Losta{tomatch}inline"+linenumber;
returnfalse;
}
}
}
if(symbol==0)
{
returntrue;
}
else
{
//error="miss{or}";
returnfalse;
}
}
}
}
转载于:https://blog.51cto.com/gnhao/1117969