推荐文章:WSDL: 描述你的Web服务
推荐文章简介:
WSDL: 描述你的Web服务柴晓路2001-8-13 本文最初由 IBM developerWorks 中国网站发表,其网址是http://www.ibm.com/developerWorks/cn/Web Service “Stack”在我的先前的文章中,我已经介绍过Web服务的整个技术体系
for(int i=0;i<src.Length;i++){
if(src[i]<=''\x1f''||(src[i]>=''\x7f''&&src[i]<=''\xff'')) temp+=ToHexString(src[i]);
else temp+=src[i].ToString();
}
return temp;
}
else return src;
}
public string StringToValue(string src,ref int loc){
string temp;
if(loc+1<src.Length){
if(loc+1<src.Length-1){
temp=src.Substring(loc+1);
ushort a=0,b=0,c=0;
if(temp[0]==''0''){
if(temp[1]>=''0''&&temp[1]<=''7''){
a=(ushort)(temp[1]-''0'');
if(temp.Length>=3){
if(temp[2]>=''0''&&temp[2]<=''7''){
b=(ushort)(temp[2]-''0'');
if(temp.Length>=4){
if(temp[3]>=''0''&&temp[3]<=''7''){
c=(ushort)(temp[3]-''0'');
loc+=5;
return ((char)(a*64+b*8+c)).ToString();
}
else{
loc+=4;
return ((char)(a*8+b)).ToString();
}
}
else{
loc+=4;
return ((char)(a*8+b)).ToString();
}
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=2;
return @"\0";
}
}
else if(temp[0].ToString().ToLower()=="x"){
if( (temp[1]>=''0''&&temp[1]<=''9'') || ( (temp[1]|0x20) >=''a'' && (temp[1]|0x20) <=''f'') ){
if(temp[1]<=''9''){
a=(ushort)(temp[1]-''0'');
}
else{
a=(ushort)((temp[1]|0x20)-''a''+10);
}
if(temp.Length>=3){
if( (temp[2]>=''0''&&temp[2]<=''9'') || ( (temp[2]|0x20) >=''a'' && (temp[2]|0x20) <=''f'') ){
if(temp[2]<=''9''){
b=(ushort)(temp[2]-''0'');
}
else{
b=(ushort)((temp[2]|0x20)-''a''+10);
}
loc+=4;
return ((char)(a*16+b)).ToString();
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=2;
return @"\"+temp[0].ToString();
}
}
else{
loc++;
return @"\";
}
}
else{
loc+=2;
return @"\"+src[loc-1].ToString();
}
}
else{
loc++;
return @"\";
}
}
protected string ToHexString(char val){
ushort baseradix=16;
return @"\x"+HexCode((ushort)(val/baseradix))+HexCode((ushort)(val%baseradix));
}
protected string ToOctString(char val){
int baseradix=8,a=0,b=0,c=0,v;
v=(int)val;
if(v>baseradix*baseradix)
a=v/(baseradix*baseradix);
if(v>baseradix)
b=(v-a*baseradix*baseradix)/baseradix;
c=(ushort)(v-a*baseradix*baseradix-b*baseradix);
return @"\0"+a.ToString()+b.ToString()+c.ToString();
}
private string HexCode(ushort val){
val=(ushort)(val%16);
string temp=val.ToString();
if(val>9)
{
switch(val){
case 10:
temp="a";
break;
case 11:
temp="b";
break;
case 12:
temp="c";
break;
case 13:
temp="d";
break;
case 14:
temp="e";
break;
case 15:
temp="f";
break;
}
}
return temp;
}
}
public class Comment{
private string comEOL,strA;
private string [] strB;
private const char splitor='','';
//
private int [,] info;
private string[,] without;
private bool comCleanSpilthEOL;
//
public Comment(){
ResetDefault();
}
public void ResetDefault(){
comEOL="\r\n";
info=new int[0,0];
without=new string[0,0];
comCleanSpilthEOL=true;
}
///////////////////////////////////// Public Functions /////////////////////////////////////
public void WithoutContainer(params string [] para){
if(para.GetUpperBound(0)==-1){
without=new string[0,0];
}
else{
int count=para.GetUpperBound(0)+1;
int cnt=count/2;
if(count%2==1)cnt++;
without=new string[cnt,2];
for(int i=0,j=0;i<cnt;i++){
without[i,0]=para[j++].Trim();
if(j+1>=count)
without[i,1]="";
else
without[i,1]=para[j++].Trim();
}
}
info=new int[0,0];
}
public void ProcessComments(string src,params string[] para){
if(src==null)src="";
strA="";
info=new int[0,0];
if(src.Trim().Length>0&¶.GetUpperBound(0)!=-1) {
int count=para.GetUpperBound(0)+1;
int cnt=count/2;
if(count%2==1)cnt++;
string [,]keyword=new String[cnt,3];
for(int i=0,j=0;i<cnt;i++){
keyword[i,0]=i.ToString();
keyword[i,1]=para[j++].Trim();
if(j+1>=count)
keyword[i,2]="";
else
keyword[i,2]=para[j++].Trim();
}
//check double first
int loc,end,begin;
string temp;
bool prev=false;
strA=src;
string al="";
for(int z=0;z<2;z++){
for(int i=0;i<cnt;i++){
if(z==0){if(keyword[i,2].Length==0) continue;}
else {if(keyword[i,2].Length>0) continue;}
loc=0;
while(strA.IndexOf(keyword[i,1],loc)!=-1&&loc<strA.Length){
loc=strA.IndexOf(keyword[i,1],loc);
temp=LineOfIndex(strA,loc,out begin);
if(OnTheContainer(temp,loc-begin)){
loc+=keyword[i,1].Length;
continue;
}
else{ //checking if on prefore
prev=false;
for(int j=0;j<cnt;j++){
if(j!=i){
if(temp.IndexOf(keyword[j,1])!=-1){
if(!OnTheContainer(temp,temp.IndexOf(keyword[j,1]))){
if(temp.IndexOf(keyword[j,1])<loc-begin){
prev=true;
break;
}
}
}
}
}
if(!prev){
end=loc+keyword[i,1].Length;
if(end>=strA.Length-1)
end=strA.Length-1;
if(z==0){
end=strA.IndexOf(keyword[i,2],end);
if(end==-1){
end=strA.Length-1;
begin=end+1;
}
else{
end+=keyword[i,2].Length-1;
begin=end+1;
}
}
else {
end=strA.IndexOf(comEOL,end);
if(end==-1){
end=strA.Length-1;
begin=end+1;
}
else{
end+=comEOL.Length-1;
begin=end+1;
}
}
strA=MakeBlank(strA,loc,end);
if(al.Length!=0)al+=splitor.ToString();
al+=i.ToString()+splitor.ToString()+loc.ToString()+splitor.ToString()+end.ToString();
loc=begin;
if(begin>=strA.Length)break;
}
else{
loc+=keyword[i,1].Length;
}
}//else
}//while
}//for
}//for
strB=al.Split(splitor);
count=strB.Length/3;
if(count!=0){
info=new int[count,3];
for(cnt=0,begin=0;cnt<count;cnt++){
for(loc=0;loc<3;loc++)
info[cnt,loc]=(int)Int32.Parse(strB[begin++]);
}
}
}
}
public string EmptyComments(string src,params string[] para){
if(src==null)src="";
strA="";
ProcessComments(src,para);
return strA;
}
public string CleanComments(string src,params string[] para){
if(src==null)src="";
strA="";
ProcessComments(src,para);
strB=Split(strA,comEOL);
if(strB.Length>1){
strA="";
for(int i=0;i<strB.Length;i++){
strA+=strB[i].Trim()+comEOL;
}
return CleaningSpilthEOL(strA);
}
else return CleaningSpilthEOL(strB[0].Trim());
}
public static string[] Split(string src,string separator){
if(src==null)src="";
string [] str=new string[1]{src};
if(src.Trim().Length!=0){
if(src.IndexOf(separator)>-1){
int cnt=0,loc=0,begin=0;
while(loc<src.Length&&src.IndexOf(separator,loc)>-1){
cnt++;
loc=src.IndexOf(separator,loc)+separator.Length;
if(loc>=src.Length) break;
}
if(loc<=src.Length-1)cnt++;
str=new string[cnt];
loc=0;
cnt=0;
while(loc<src.Length&&src.IndexOf(separator,loc)>-1){
loc=src.IndexOf(separator,loc);
str[cnt]="";
for(int i=begin;i<loc;i++) str[cnt]+=src[i].ToString();
loc+=separator.Length;
begin=loc;
if(loc>=src.Length)break;
cnt++;
}
if(loc<=src.Length-1){
for(int i=loc;i<=src.Length-1;i++) str[cnt]+=src[i].ToString();
}
return str;
}
else return str;
}
else return str;
}
///////////////////////////////////// Internal functions /////////////////////////////////////
protected string MakeBlank(string src,int start,int end){
if(start==end||src.Trim().Length==0) return src;
else{
string temp,tempA;
tempA=src.Substring(start,end-start+1);
if(tempA.IndexOf(comEOL)==-1){
tempA="";
for(int i=0;i<end-start+1;i++) tempA+=" ";
}
else{
int at=0,begin=0;
temp="";
while(tempA.IndexOf(comEOL,at)!=-1&&at<tempA.Length){
at=tempA.IndexOf(comEOL,at);
for(int i=begin;i<at;i++) temp+=" ";
temp+=comEOL;
at+=comEOL.Length;
begin=at;
}
if(at<=tempA.Length-1){
for(int i=at;i<=tempA.Length-1;i++) temp+=" ";
}
tempA=temp;
}
return src.Substring(0,start)+tempA+src.Substring(end+1);
}
}
protected string LineOfIndex(string src,int idx,out int begin){
int start=0;
bool equ=false;
for(int loc=idx;loc>=0;loc--){
if(src[loc]==comEOL[comEOL.Length-1]){
equ=false;
if(comEOL.Length>1){
if(loc-1>=comEOL.Length-2){
for(int j=0;j<=comEOL.Length-2;j++){
if(src[loc-j-1]==comEOL[comEOL.Length-j-2])equ=true;
else equ=false;
}
}
else equ=false;
}else equ=true;
if(equ){
start=loc+1;
break;
}
}
}
begin=start;
if(src.IndexOf(comEOL,start)==-1) return src.Substring(start);
else return src.Substring(start,src.IndexOf(comEOL,start)-start);
}
protected bool OnTheContainer(string str,int idx){
if(without.GetUpperBound(0)!=-1&&without.GetUpperBound(1)!=-1){
int cnt=without.GetUpperBound(0)+1;
int left=0,right=0;
for(int i=0;i<cnt;i++){
right=-1;
while(str.IndexOf(without[i,0],right+1)!=-1&&right+1<=str.Length){
left=str.IndexOf(without[i,0],right+1);
if(without[i,1].Length==0)
right=str.IndexOf(without[i,0],left+without[i,0].Length);
else
right=str.IndexOf(without[i,1],left+without[i,0].Length);
if(right==-1)break;
else if(idx>left&&idx<right)return true;
}
}
return false;
}
else return false;
}
protected string CleaningSpilthEOL(string src_ini){
strA=src_ini;
if(comCleanSpilthEOL){
for(int i=0;i<3;i++) strA=strA.Replace(comEOL+comEOL,comEOL);
while(true){
if(strA.IndexOf(comEOL)==0) strA=strA.Substring(comEOL.Length);
else break;
}
}
return strA;
}
///////////////////////////////////// public attrib /////////////////////////////////////
public string EOL{
get{
return comEOL;
}
set{
comEOL=value;
}
}
public bool CleanSpilthEOL{
get{
return comCleanSpilthEOL;
}
set{
comCleanSpilthEOL=value;
}
}
public int Count{
get{
if(info.GetUpperBound(0)!=-1&&info.GetUpperBound(1)!=-1){
return info.GetUpperBound(0)+1;
}
else return 0;
}
}
public void GetInfo(int index,out int start,out int end,out int type){
if(index<this.Count){
start=info[index,1];
end=info[index,2];
type=info[index,0];
}
else{
start=0;
end=0;
type=0;
}
}
}
}
推荐文章简介:
WSDL: 描述你的Web服务柴晓路2001-8-13 本文最初由 IBM developerWorks 中国网站发表,其网址是http://www.ibm.com/developerWorks/cn/Web Service “Stack”在我的先前的文章中,我已经介绍过Web服务的整个技术体系
虽然在Windows里面操作INI并不是一件难事,但为了使用更强大的功能,就需要编写自己的INI解析程序,这是DWG用C#实现的一个INI解析器
using System;
using System.Text.RegularExpressions;
using System.Collections;
/*********************************************************************
DWG INI Parser 1.0
作者:DWG
Copyright(C)2002-2003
作者网站:http://asp.6to23.com/mulansystem
版权声明:
(1)版权归“DWG”所有,任何个人或企业未经许可不得用于商业用途;
(2)个人可免费使用本程序,但不得去除版权信息,修改后的版本不得去除原作者信息;
若要发布修改版本,应该先给作者发一份修改后的源程序;
(3)本源程序发布在http://asp.6to23.com/mulansystem 技术/原创作品展示
使用方法:
请参考http://asp.6to23.com/mulansystem 技术/原创作品展示内的相关文档;
*********************************************************************/
namespace StrProcessor
{
public class Ini
{
public delegate void OnDebugMessage(bool errorflag,string msg);
public event OnDebugMessage OnDebug;
private Regex regex;
private MatchCollection mc;
private Comment cm;
private Convert cv;
// Temporary swap variable
private string strA,strAA;
private string [] strB,strBB;
// Control flags
private string iniEOL;
private bool iniCleanSpilthEOL,iniProcError,iniCaseSensitive;
//
public Ini()
{
cm=new Comment();
cv=new Convert();
ResetDefaultOptions();
OnDebug+=new OnDebugMessage(OnDefaultDebugMessage);
}
///////////////////////////// Interface Functions /////////////////////////////////
public string SetString(string src_ini,string mainkey,string subkey,string val){
string temp=cv.EncodeString(val);
if(temp.Length==src_ini.Length) return AutoSetKeyValue(src_ini,mainkey,subkey,temp);
else return AutoSetKeyValue(src_ini,mainkey,subkey,"\""+temp+"\"");
}
public string GetString(string src_ini,string mainkey,string subkey){
string temp=GetKeyValue(src_ini,mainkey,subkey);
if(!iniProcError){
if(temp.IndexOf("\"")==0&&temp.IndexOf("\"",temp.Length-1)==temp.Length-1&&temp.Length>2)
return cv.DecodeString(temp.Substring(1,temp.Length-2));
else
return temp;
}
else return "";
}
public string[] GetMainkeyNames(string src_ini){
strB=new string[]{""};
strAA=CleanComments(src_ini);
if(!iniProcError){
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc = regex.Matches(strA);
int cnt=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
cnt++;
strB=new string[cnt];
cnt=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
strB[cnt++]=mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim();
SendOkMsg("Get mainkey names OK.");
return strB;
}
else return strB;
}
public string[] GetSubkeyNames(string src_ini,string mainkey){
strB=new string[]{""};
if(TestMainkeyExist(src_ini,mainkey)){
strB=new string[]{""};
strAA=CleanComments(src_ini);
if(!iniProcError){
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
mainkey=CaseProc(mainkey.Trim());
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc=regex.Matches(strA);
int sloc=0,length=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
sloc=mc[i].Index+mc[i].Value.Length;
if((i+1)!=mc.Count) length=mc[i+1].Index-sloc;
else length=strA.Substring(sloc).Length;
break;
}
strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
length=0;
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0)
if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1)
length++;
strBB=new String[length];
length=0;
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0)
if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1)
strBB[length++]=strB[i].Split(''='')[0].Trim();
SendOkMsg("Get subkey names OK.");
return strBB;
}
else return strB;
}
else return strB;
}
public bool TestKeyExist(string src_ini,string mainkey,string subkey){
if(mainkey.Trim().Length==0&&subkey.Trim().Length==0){
SendErrorMsg("Test key exist exception:must be specify the mainkey & subkey names.");
return false;
}
else{
if(subkey.Trim().Length==0) return TestMainkeyExist(src_ini,mainkey);
else return TestSubkeyExist(src_ini,mainkey,subkey);
}
}
public string GetKeyValue(string src_ini,string mainkey,string subkey){
if(TestSubkeyExist(src_ini,mainkey,subkey)){
strAA=CleanComments(src_ini);
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
if(iniProcError) return "";
mainkey=CaseProc(mainkey.Trim());
subkey=CaseProc(subkey.Trim());
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc=regex.Matches(strA);
int sloc=0,length=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
sloc=mc[i].Index+mc[i].Value.Length;
if((i+1)!=mc.Count) length=mc[i+1].Index-sloc;
else length=strA.Substring(sloc).Length;
break;
}
strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
strA="";
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0)
if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1)
if(CaseProc(strB[i].Split(''='')[0].Trim())==subkey)
strA=strB[i].Substring(strB[i].IndexOf("=")+1).Trim();
SendOkMsg("The value of key:\""+mainkey.Trim()+"."+subkey+"\" is "+strA+".");
return strA;
}else return "";
}
public long GetKeyValueLong(string src_ini,string mainkey,string subkey)
{
strA=GetKeyValue(src_ini,mainkey,subkey);
if(!iniProcError&&strA.Trim().Length!=0){
regex=new Regex(@"[^0-9]*");
mc=regex.Matches(strA);
if(mc.Count==0) return Int64.Parse(strA.Trim());
else return 0;
}
else return 0;
}
public string SetKeyValue(string src_ini,string mainkey,string subkey,string val){
if(TestSubkeyExist(src_ini,mainkey,subkey)){
strAA=EmptyComments(src_ini);
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
mainkey=CaseProc(mainkey.Trim());
subkey=CaseProc(subkey.Trim());
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc=regex.Matches(strA);
int sloc=0,length=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
sloc=mc[i].Index+mc[i].Value.Length;
if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
else length=strA.Substring(sloc).Length;
break;
}
strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
strBB=src_ini.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
strA="";
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0){
if(strB[i].Trim().Split(''='').Length>1){
if(CaseProc(strB[i].Split(''='')[0].Trim())==subkey){
strA+=strB[i].Trim().Split(''='')[0].Trim()+"="+val.Trim();
if(strBB[i].IndexOf(@";")!=-1&&strBB[i].IndexOf(@"//")!=-1){
if(strBB[i].IndexOf(@";")<strBB[i].IndexOf(@"//"))
strA+=strBB[i].Substring(strBB[i].IndexOf(@";"));
else
strA+=strBB[i].Substring(strBB[i].IndexOf(@"//"));
}
else if(strBB[i].IndexOf(@"//")!=-1){
strA+=strBB[i].Substring(strBB[i].IndexOf("//"));
}
else if(strBB[i].IndexOf(@";")!=-1){
strA+=strBB[i].Substring(strBB[i].IndexOf(@";"));
}
strA+=iniEOL;
}
else strA+=strBB[i]+iniEOL;
}else strA+=strBB[i]+iniEOL;
}else strA+=strBB[i]+iniEOL;
SendOkMsg("Set the value of key:\""+mainkey.Trim()+"."+subkey+"\" to \""+val.Trim()+"\".");
strA=src_ini.Substring(0,sloc)+strA+src_ini.Substring(sloc+length);
return CleaningSpilthEOL(strA);
}
else return src_ini;
}
public string SetKeyValueLong(string src_ini,string mainkey,string subkey,long val){
return SetKeyValue(src_ini,mainkey,subkey,val.ToString());
}
public string AutoSetKeyValue(string src_ini,string mainkey,string subkey,string val){
if(!TestMainkeyExist(src_ini,mainkey))src_ini=CreateMainkey(src_ini,mainkey);
return CreateSubkey(src_ini,mainkey,subkey,val);
}
public string CleanComments(string src_ini){
if(src_ini.Trim()!=""){
cm.WithoutContainer("\"","\"","\''","\''");
strA=cm.CleanComments(src_ini,@"/*",@"*/",@"//","",@";","");
SendOkMsg("Clean comments OK.");
using System.Text.RegularExpressions;
using System.Collections;
/*********************************************************************
DWG INI Parser 1.0
作者:DWG
Copyright(C)2002-2003
作者网站:http://asp.6to23.com/mulansystem
版权声明:
(1)版权归“DWG”所有,任何个人或企业未经许可不得用于商业用途;
(2)个人可免费使用本程序,但不得去除版权信息,修改后的版本不得去除原作者信息;
若要发布修改版本,应该先给作者发一份修改后的源程序;
(3)本源程序发布在http://asp.6to23.com/mulansystem 技术/原创作品展示
使用方法:
请参考http://asp.6to23.com/mulansystem 技术/原创作品展示内的相关文档;
*********************************************************************/
namespace StrProcessor
{
public class Ini
{
public delegate void OnDebugMessage(bool errorflag,string msg);
public event OnDebugMessage OnDebug;
private Regex regex;
private MatchCollection mc;
private Comment cm;
private Convert cv;
// Temporary swap variable
private string strA,strAA;
private string [] strB,strBB;
// Control flags
private string iniEOL;
private bool iniCleanSpilthEOL,iniProcError,iniCaseSensitive;
//
public Ini()
{
cm=new Comment();
cv=new Convert();
ResetDefaultOptions();
OnDebug+=new OnDebugMessage(OnDefaultDebugMessage);
}
///////////////////////////// Interface Functions /////////////////////////////////
public string SetString(string src_ini,string mainkey,string subkey,string val){
string temp=cv.EncodeString(val);
if(temp.Length==src_ini.Length) return AutoSetKeyValue(src_ini,mainkey,subkey,temp);
else return AutoSetKeyValue(src_ini,mainkey,subkey,"\""+temp+"\"");
}
public string GetString(string src_ini,string mainkey,string subkey){
string temp=GetKeyValue(src_ini,mainkey,subkey);
if(!iniProcError){
if(temp.IndexOf("\"")==0&&temp.IndexOf("\"",temp.Length-1)==temp.Length-1&&temp.Length>2)
return cv.DecodeString(temp.Substring(1,temp.Length-2));
else
return temp;
}
else return "";
}
public string[] GetMainkeyNames(string src_ini){
strB=new string[]{""};
strAA=CleanComments(src_ini);
if(!iniProcError){
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc = regex.Matches(strA);
int cnt=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
cnt++;
strB=new string[cnt];
cnt=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
strB[cnt++]=mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim();
SendOkMsg("Get mainkey names OK.");
return strB;
}
else return strB;
}
public string[] GetSubkeyNames(string src_ini,string mainkey){
strB=new string[]{""};
if(TestMainkeyExist(src_ini,mainkey)){
strB=new string[]{""};
strAA=CleanComments(src_ini);
if(!iniProcError){
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
mainkey=CaseProc(mainkey.Trim());
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc=regex.Matches(strA);
int sloc=0,length=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
sloc=mc[i].Index+mc[i].Value.Length;
if((i+1)!=mc.Count) length=mc[i+1].Index-sloc;
else length=strA.Substring(sloc).Length;
break;
}
strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
length=0;
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0)
if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1)
length++;
strBB=new String[length];
length=0;
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0)
if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1)
strBB[length++]=strB[i].Split(''='')[0].Trim();
SendOkMsg("Get subkey names OK.");
return strBB;
}
else return strB;
}
else return strB;
}
public bool TestKeyExist(string src_ini,string mainkey,string subkey){
if(mainkey.Trim().Length==0&&subkey.Trim().Length==0){
SendErrorMsg("Test key exist exception:must be specify the mainkey & subkey names.");
return false;
}
else{
if(subkey.Trim().Length==0) return TestMainkeyExist(src_ini,mainkey);
else return TestSubkeyExist(src_ini,mainkey,subkey);
}
}
public string GetKeyValue(string src_ini,string mainkey,string subkey){
if(TestSubkeyExist(src_ini,mainkey,subkey)){
strAA=CleanComments(src_ini);
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
if(iniProcError) return "";
mainkey=CaseProc(mainkey.Trim());
subkey=CaseProc(subkey.Trim());
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc=regex.Matches(strA);
int sloc=0,length=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length!=0)
if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
sloc=mc[i].Index+mc[i].Value.Length;
if((i+1)!=mc.Count) length=mc[i+1].Index-sloc;
else length=strA.Substring(sloc).Length;
break;
}
strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
strA="";
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0)
if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1)
if(CaseProc(strB[i].Split(''='')[0].Trim())==subkey)
strA=strB[i].Substring(strB[i].IndexOf("=")+1).Trim();
SendOkMsg("The value of key:\""+mainkey.Trim()+"."+subkey+"\" is "+strA+".");
return strA;
}else return "";
}
public long GetKeyValueLong(string src_ini,string mainkey,string subkey)
{
strA=GetKeyValue(src_ini,mainkey,subkey);
if(!iniProcError&&strA.Trim().Length!=0){
regex=new Regex(@"[^0-9]*");
mc=regex.Matches(strA);
if(mc.Count==0) return Int64.Parse(strA.Trim());
else return 0;
}
else return 0;
}
public string SetKeyValue(string src_ini,string mainkey,string subkey,string val){
if(TestSubkeyExist(src_ini,mainkey,subkey)){
strAA=EmptyComments(src_ini);
cm.WithoutContainer();
strA=cm.EmptyComments(strAA,"\"","\"");
mainkey=CaseProc(mainkey.Trim());
subkey=CaseProc(subkey.Trim());
regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
mc=regex.Matches(strA);
int sloc=0,length=0;
for(int i=0;i<mc.Count;i++)
if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
sloc=mc[i].Index+mc[i].Value.Length;
if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
else length=strA.Substring(sloc).Length;
break;
}
strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
strBB=src_ini.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
strA="";
for(int i=0;i<strB.Length;i++)
if(strB[i].Trim().Replace("\ua1a1","").Length!=0){
if(strB[i].Trim().Split(''='').Length>1){
if(CaseProc(strB[i].Split(''='')[0].Trim())==subkey){
strA+=strB[i].Trim().Split(''='')[0].Trim()+"="+val.Trim();
if(strBB[i].IndexOf(@";")!=-1&&strBB[i].IndexOf(@"//")!=-1){
if(strBB[i].IndexOf(@";")<strBB[i].IndexOf(@"//"))
strA+=strBB[i].Substring(strBB[i].IndexOf(@";"));
else
strA+=strBB[i].Substring(strBB[i].IndexOf(@"//"));
}
else if(strBB[i].IndexOf(@"//")!=-1){
strA+=strBB[i].Substring(strBB[i].IndexOf("//"));
}
else if(strBB[i].IndexOf(@";")!=-1){
strA+=strBB[i].Substring(strBB[i].IndexOf(@";"));
}
strA+=iniEOL;
}
else strA+=strBB[i]+iniEOL;
}else strA+=strBB[i]+iniEOL;
}else strA+=strBB[i]+iniEOL;
SendOkMsg("Set the value of key:\""+mainkey.Trim()+"."+subkey+"\" to \""+val.Trim()+"\".");
strA=src_ini.Substring(0,sloc)+strA+src_ini.Substring(sloc+length);
return CleaningSpilthEOL(strA);
}
else return src_ini;
}
public string SetKeyValueLong(string src_ini,string mainkey,string subkey,long val){
return SetKeyValue(src_ini,mainkey,subkey,val.ToString());
}
public string AutoSetKeyValue(string src_ini,string mainkey,string subkey,string val){
if(!TestMainkeyExist(src_ini,mainkey))src_ini=CreateMainkey(src_ini,mainkey);
return CreateSubkey(src_ini,mainkey,subkey,val);
}
public string CleanComments(string src_ini){
if(src_ini.Trim()!=""){
cm.WithoutContainer("\"","\"","\''","\''");
strA=cm.CleanComments(src_ini,@"/*",@"*/",@"//","",@";","");
SendOkMsg("Clean comments OK.");
1return CleaningSpilthEOL(strA);
2 }
3 else{
4 SendErrorMsg("Source ini string empty exception when cleaning comments.");
5 return src_ini;
6 }
7 }
8 public void ResetDefaultOptions(){
9 iniEOL="\r\n";
10 iniCleanSpilthEOL=true;
11 iniProcError=false;
12 iniCaseSensitive=true;
13 cm.EOL=iniEOL;
14 cm.CleanSpilthEOL=iniCleanSpilthEOL;
15 }
16 public string DeleteKey(string src_ini,string mainkey,string subkey){
17 if(subkey.Trim().Length==0) return DeleteMainkey(src_ini,mainkey);
18 else return DeleteSubkey(src_ini,mainkey,subkey);
19 }
20 /////////////////////////////// Internal functions ////////////////////////////////
21 protected string CreateMainkey(string src_ini,string mainkey){
22 if(mainkey.Trim().Length==0){
23 SendErrorMsg(@"Internal:create mainkey error,must specify the mainkey name.");
24 return src_ini;
25 }
26 else{
27 if(TestMainkeyExist(src_ini,mainkey))src_ini=CleaningSpilthEOL(DeleteMainkey(src_ini,mainkey));
28 src_ini+=iniEOL+"["+mainkey.Trim()+"]"+iniEOL;
29 SendOkMsg("Internal:create mainkey \""+mainkey.Trim()+"\" OK.");
30 return CleaningSpilthEOL(src_ini);
31 }
32 }
33 protected string CreateSubkey(string src_ini,string mainkey,string subkey,string val){
34 if(TestMainkeyExist(src_ini,mainkey)){
35 if(TestSubkeyExist(src_ini,mainkey,subkey)) return SetKeyValue(src_ini,mainkey,subkey,val);
36 strAA=EmptyComments(src_ini);
37 cm.WithoutContainer();
38 strA=cm.EmptyComments(strAA,"\"","\"");
39 mainkey=CaseProc(mainkey.Trim());
40 regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
41 mc=regex.Matches(strA);
42 int sloc=0,length=0;
43 for(int i=0;i<mc.Count;i++)
44 if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
45 if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
46 sloc=mc[i].Index+mc[i].Value.Length;
47 if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
48 else length=strA.Substring(sloc).Length;
49 break;
50 }
51 strA=src_ini.Substring(0,sloc+length)+iniEOL+subkey+"="+val.Trim()+iniEOL+src_ini.Substring(sloc+length);
52 SendOkMsg("Create the subkey:\""+mainkey.Trim()+"."+subkey.Trim()+"\" OK.");
53 return CleaningSpilthEOL(strA);
54 }
55 else return src_ini;
56 }
57 protected string DeleteMainkey(string src_ini,string mainkey){
58 if(TestMainkeyExist(src_ini,mainkey)){
59 strAA=EmptyComments(src_ini);
60 cm.WithoutContainer();
61 strA=cm.EmptyComments(strAA,"\"","\"");
62 mainkey=CaseProc(mainkey.Trim());
63 regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
64 mc=regex.Matches(strA);
65 int sloc=0,length=0;
66 for(int i=0;i<mc.Count;i++)
67 if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
68 if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
69 sloc=mc[i].Index;
70 if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
71 else length=strA.Substring(sloc).Length;
72 break;
73 }
74 strA=src_ini.Substring(0,sloc)+iniEOL+src_ini.Substring(sloc+length);
75 SendOkMsg("Delete the mainkey:\""+mainkey.Trim()+"\" OK.");
76 return CleaningSpilthEOL(strA);
77 }
78 else return src_ini;
79 }
80 protected string DeleteSubkey(string src_ini,string mainkey,string subkey){
81 if(TestSubkeyExist(src_ini,mainkey,subkey)){
82 strAA=EmptyComments(src_ini);
83 cm.WithoutContainer();
84 strA=cm.EmptyComments(strAA,"\"","\"");
85 mainkey=CaseProc(mainkey.Trim());
86 subkey=CaseProc(subkey.Trim());
87 regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
88 mc=regex.Matches(strA);
89 int sloc=0,length=0;
90 for(int i=0;i<mc.Count;i++)
91 if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
92 if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
93 sloc=mc[i].Index+mc[i].Value.Length;
94 if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
95 else length=strA.Substring(sloc).Length;
96 break;
97 }
98 strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
99 strBB=src_ini.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
100 strA="";
101 for(int i=0;i<strB.Length;i++)
102 if(strB[i].Trim().Replace("\ua1a1","").Length!=0){
103 if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1){
104 if(CaseProc(strB[i].Split(''='')[0].Trim())!=subkey)strA+=strBB[i]+iniEOL;
105 }else strA+=strBB[i]+iniEOL;
106 }else strA+=strBB[i]+iniEOL;
107 SendOkMsg("Delete the subkey:\""+mainkey.Trim()+"."+subkey+"\" OK.");
108 strA=src_ini.Substring(0,sloc)+strA+src_ini.Substring(sloc+length);
109 return CleaningSpilthEOL(strA);
110 }
111 else return src_ini;
112 }
113 protected string EmptyComments(string src_ini){
114 if(src_ini.Trim().Length!=0){
115 cm.WithoutContainer("\"","\"","\''","\''");
116 strA=cm.EmptyComments(src_ini,@"/*",@"*/",@"//","",@";","");
117 SendOkMsg(@"Internal:empty comments OK.");
118 return strA;
119 }
120 else{
121 SendErrorMsg(@"Internal:source ini empty exception.");
122 return src_ini;
123 }
124 }
125 protected bool TestMainkeyExist(string src_ini,string mainkey){
126 strB=GetMainkeyNames(src_ini);
127 if(!iniProcError){
128 mainkey=CaseProc(mainkey.Trim());
129 if(mainkey.Length==0){
130 SendErrorMsg("Test mainkey exist exception:must specify the mainkey name.");
131 return false;
132 }
133 for(int i=0;i<strB.Length;i++)
134 if(CaseProc(strB[i])==mainkey){
135 SendOkMsg("The main key:\""+mainkey+"\" to be exist.");
136 return true;
137 }
138 SendOkMsg("The main key:\""+mainkey+"\" not exist.");
139 return false;
140 }
141 else return false;
142 }
143 protected bool TestSubkeyExist(string src_ini,string mainkey,string subkey){
144 if(TestMainkeyExist(src_ini,mainkey)){
145 strB=new string[]{""};
146 strB=GetSubkeyNames(src_ini,mainkey);
147 if(!iniProcError){
148 subkey=CaseProc(subkey.Trim());
149 if(subkey.Length==0){
150 SendErrorMsg("Test subkey exist exception:must specify the subkey name.");
151 return false;
152 }
153 if(strB.Length>0){
154 for(int i=0;i<strB.Length;i++)
155 if(CaseProc(strB[i])==subkey){
156 SendOkMsg("The key:\""+mainkey.Trim()+"."+subkey+"\" to be exist.");
157 return true;
158 }
159 }
160 SendOkMsg("The key:\""+mainkey.Trim()+"."+subkey+"\" not exist.");
161 return false;
162 }
163 else return false;
164 }
165 else return false;
166 }
167 private string CleaningSpilthEOL(string src_ini){
168 strA=src_ini;
169 if(iniCleanSpilthEOL){
170 for(int i=0;i<3;i++) strA=strA.Replace(iniEOL+iniEOL,iniEOL);
171 while(strA.IndexOf(iniEOL)==0){
172 strA=strA.Substring(iniEOL.Length);
173 }
174 }
175 return strA;
176 }
177 private void SendOkMsg(string msg){
178 iniProcError=false;
179 OnDebug(iniProcError,msg);
180 }
181 private void SendErrorMsg(string msg){
182 iniProcError=true;
183 OnDebug(iniProcError,msg);
184 }
185 private void OnDefaultDebugMessage(bool error,string msg){
186 }
187 private string CaseProc(string str){
188 if(iniCaseSensitive) return str;
189 else return str.ToLower();
190 }
191 /////////////////////////////// Attributes function ///////////////////////////////
192 public string EOL{
193 get{
194 return iniEOL;
195 }
196 set{
197 iniEOL=value;
198 cm.EOL=iniEOL;
199 }
200 }
201 public bool CleanSpilthEOL{
202 get{
203 return iniCleanSpilthEOL;
204 }
205 set{
206 iniCleanSpilthEOL=value;
207 cm.CleanSpilthEOL=iniCleanSpilthEOL;
208 }
209 }
210 public bool CaseSensitive{
211 get{
212 return iniCaseSensitive;
213 }
214 set{
215 iniCaseSensitive=value;
216 }
217 }
218 public bool Error{
219 get{
220 return iniProcError;
221 }
222 }
223 }
224 public class Convert{
225 public string DecodeString(string src){
226 if(src.Trim().Length>0){
227 src=src.Replace(@"\a","\a");
228 src=src.Replace(@"\b","\b");
229 src=src.Replace(@"\t","\t");
230 src=src.Replace(@"\r","\r");
231 src=src.Replace(@"\v","\v");
232 src=src.Replace(@"\f","\f");
233 src=src.Replace(@"\n","\n");
234 src=src.Replace(@"\d","\x22");
235 src=src.Replace(@"\s","\x27");
236 src=src.Replace(@"\m","\\");
237 int loc=0,begin=0;
238 string temp="";
239 while(loc<src.Length&&src.IndexOf(@"\",loc)>-1){
240 loc=src.IndexOf(@"\",loc);
241 for(int i=begin;i<loc;i++) temp+=src[i].ToString();
242 temp+=StringToValue(src,ref loc);
243 begin=loc;
244 if(loc>=src.Length)break;
245 }
246 if(loc<=src.Length-1){
247 for(int i=loc;i<=src.Length-1;i++)
248 temp+=src[i].ToString();
249 }
250 return temp;
251 }
252 else return src;
253 }
254 public string EncodeString(string src){
255 if(src.Trim().Length>0){
256 src=src.Replace("\\",@"\m");
257 src=src.Replace("\a",@"\a");
258 src=src.Replace("\b",@"\b");
259 src=src.Replace("\t",@"\t");
260 src=src.Replace("\r",@"\r");
261 src=src.Replace("\v",@"\v");
262 src=src.Replace("\f",@"\f");
263 src=src.Replace("\n",@"\n");
264 src=src.Replace("\x22",@"\d");
265 src=src.Replace("\x27",@"\s");
266 &nb
sp;string temp="";2 }
3 else{
4 SendErrorMsg("Source ini string empty exception when cleaning comments.");
5 return src_ini;
6 }
7 }
8 public void ResetDefaultOptions(){
9 iniEOL="\r\n";
10 iniCleanSpilthEOL=true;
11 iniProcError=false;
12 iniCaseSensitive=true;
13 cm.EOL=iniEOL;
14 cm.CleanSpilthEOL=iniCleanSpilthEOL;
15 }
16 public string DeleteKey(string src_ini,string mainkey,string subkey){
17 if(subkey.Trim().Length==0) return DeleteMainkey(src_ini,mainkey);
18 else return DeleteSubkey(src_ini,mainkey,subkey);
19 }
20 /////////////////////////////// Internal functions ////////////////////////////////
21 protected string CreateMainkey(string src_ini,string mainkey){
22 if(mainkey.Trim().Length==0){
23 SendErrorMsg(@"Internal:create mainkey error,must specify the mainkey name.");
24 return src_ini;
25 }
26 else{
27 if(TestMainkeyExist(src_ini,mainkey))src_ini=CleaningSpilthEOL(DeleteMainkey(src_ini,mainkey));
28 src_ini+=iniEOL+"["+mainkey.Trim()+"]"+iniEOL;
29 SendOkMsg("Internal:create mainkey \""+mainkey.Trim()+"\" OK.");
30 return CleaningSpilthEOL(src_ini);
31 }
32 }
33 protected string CreateSubkey(string src_ini,string mainkey,string subkey,string val){
34 if(TestMainkeyExist(src_ini,mainkey)){
35 if(TestSubkeyExist(src_ini,mainkey,subkey)) return SetKeyValue(src_ini,mainkey,subkey,val);
36 strAA=EmptyComments(src_ini);
37 cm.WithoutContainer();
38 strA=cm.EmptyComments(strAA,"\"","\"");
39 mainkey=CaseProc(mainkey.Trim());
40 regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
41 mc=regex.Matches(strA);
42 int sloc=0,length=0;
43 for(int i=0;i<mc.Count;i++)
44 if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
45 if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
46 sloc=mc[i].Index+mc[i].Value.Length;
47 if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
48 else length=strA.Substring(sloc).Length;
49 break;
50 }
51 strA=src_ini.Substring(0,sloc+length)+iniEOL+subkey+"="+val.Trim()+iniEOL+src_ini.Substring(sloc+length);
52 SendOkMsg("Create the subkey:\""+mainkey.Trim()+"."+subkey.Trim()+"\" OK.");
53 return CleaningSpilthEOL(strA);
54 }
55 else return src_ini;
56 }
57 protected string DeleteMainkey(string src_ini,string mainkey){
58 if(TestMainkeyExist(src_ini,mainkey)){
59 strAA=EmptyComments(src_ini);
60 cm.WithoutContainer();
61 strA=cm.EmptyComments(strAA,"\"","\"");
62 mainkey=CaseProc(mainkey.Trim());
63 regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
64 mc=regex.Matches(strA);
65 int sloc=0,length=0;
66 for(int i=0;i<mc.Count;i++)
67 if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
68 if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
69 sloc=mc[i].Index;
70 if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
71 else length=strA.Substring(sloc).Length;
72 break;
73 }
74 strA=src_ini.Substring(0,sloc)+iniEOL+src_ini.Substring(sloc+length);
75 SendOkMsg("Delete the mainkey:\""+mainkey.Trim()+"\" OK.");
76 return CleaningSpilthEOL(strA);
77 }
78 else return src_ini;
79 }
80 protected string DeleteSubkey(string src_ini,string mainkey,string subkey){
81 if(TestSubkeyExist(src_ini,mainkey,subkey)){
82 strAA=EmptyComments(src_ini);
83 cm.WithoutContainer();
84 strA=cm.EmptyComments(strAA,"\"","\"");
85 mainkey=CaseProc(mainkey.Trim());
86 subkey=CaseProc(subkey.Trim());
87 regex=new Regex(@"(\r\n)?\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\]\s*(\r\n)?");
88 mc=regex.Matches(strA);
89 int sloc=0,length=0;
90 for(int i=0;i<mc.Count;i++)
91 if(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim().Replace("\ua1a1","").Length>0)
92 if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){
93 sloc=mc[i].Index+mc[i].Value.Length;
94 if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;
95 else length=strA.Substring(sloc).Length;
96 break;
97 }
98 strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
99 strBB=src_ini.Substring(sloc,length).Replace(iniEOL,"\n").Split(''\n'');
100 strA="";
101 for(int i=0;i<strB.Length;i++)
102 if(strB[i].Trim().Replace("\ua1a1","").Length!=0){
103 if(strB[i].Trim().Replace("\ua1a1","").Split(''='').Length>1){
104 if(CaseProc(strB[i].Split(''='')[0].Trim())!=subkey)strA+=strBB[i]+iniEOL;
105 }else strA+=strBB[i]+iniEOL;
106 }else strA+=strBB[i]+iniEOL;
107 SendOkMsg("Delete the subkey:\""+mainkey.Trim()+"."+subkey+"\" OK.");
108 strA=src_ini.Substring(0,sloc)+strA+src_ini.Substring(sloc+length);
109 return CleaningSpilthEOL(strA);
110 }
111 else return src_ini;
112 }
113 protected string EmptyComments(string src_ini){
114 if(src_ini.Trim().Length!=0){
115 cm.WithoutContainer("\"","\"","\''","\''");
116 strA=cm.EmptyComments(src_ini,@"/*",@"*/",@"//","",@";","");
117 SendOkMsg(@"Internal:empty comments OK.");
118 return strA;
119 }
120 else{
121 SendErrorMsg(@"Internal:source ini empty exception.");
122 return src_ini;
123 }
124 }
125 protected bool TestMainkeyExist(string src_ini,string mainkey){
126 strB=GetMainkeyNames(src_ini);
127 if(!iniProcError){
128 mainkey=CaseProc(mainkey.Trim());
129 if(mainkey.Length==0){
130 SendErrorMsg("Test mainkey exist exception:must specify the mainkey name.");
131 return false;
132 }
133 for(int i=0;i<strB.Length;i++)
134 if(CaseProc(strB[i])==mainkey){
135 SendOkMsg("The main key:\""+mainkey+"\" to be exist.");
136 return true;
137 }
138 SendOkMsg("The main key:\""+mainkey+"\" not exist.");
139 return false;
140 }
141 else return false;
142 }
143 protected bool TestSubkeyExist(string src_ini,string mainkey,string subkey){
144 if(TestMainkeyExist(src_ini,mainkey)){
145 strB=new string[]{""};
146 strB=GetSubkeyNames(src_ini,mainkey);
147 if(!iniProcError){
148 subkey=CaseProc(subkey.Trim());
149 if(subkey.Length==0){
150 SendErrorMsg("Test subkey exist exception:must specify the subkey name.");
151 return false;
152 }
153 if(strB.Length>0){
154 for(int i=0;i<strB.Length;i++)
155 if(CaseProc(strB[i])==subkey){
156 SendOkMsg("The key:\""+mainkey.Trim()+"."+subkey+"\" to be exist.");
157 return true;
158 }
159 }
160 SendOkMsg("The key:\""+mainkey.Trim()+"."+subkey+"\" not exist.");
161 return false;
162 }
163 else return false;
164 }
165 else return false;
166 }
167 private string CleaningSpilthEOL(string src_ini){
168 strA=src_ini;
169 if(iniCleanSpilthEOL){
170 for(int i=0;i<3;i++) strA=strA.Replace(iniEOL+iniEOL,iniEOL);
171 while(strA.IndexOf(iniEOL)==0){
172 strA=strA.Substring(iniEOL.Length);
173 }
174 }
175 return strA;
176 }
177 private void SendOkMsg(string msg){
178 iniProcError=false;
179 OnDebug(iniProcError,msg);
180 }
181 private void SendErrorMsg(string msg){
182 iniProcError=true;
183 OnDebug(iniProcError,msg);
184 }
185 private void OnDefaultDebugMessage(bool error,string msg){
186 }
187 private string CaseProc(string str){
188 if(iniCaseSensitive) return str;
189 else return str.ToLower();
190 }
191 /////////////////////////////// Attributes function ///////////////////////////////
192 public string EOL{
193 get{
194 return iniEOL;
195 }
196 set{
197 iniEOL=value;
198 cm.EOL=iniEOL;
199 }
200 }
201 public bool CleanSpilthEOL{
202 get{
203 return iniCleanSpilthEOL;
204 }
205 set{
206 iniCleanSpilthEOL=value;
207 cm.CleanSpilthEOL=iniCleanSpilthEOL;
208 }
209 }
210 public bool CaseSensitive{
211 get{
212 return iniCaseSensitive;
213 }
214 set{
215 iniCaseSensitive=value;
216 }
217 }
218 public bool Error{
219 get{
220 return iniProcError;
221 }
222 }
223 }
224 public class Convert{
225 public string DecodeString(string src){
226 if(src.Trim().Length>0){
227 src=src.Replace(@"\a","\a");
228 src=src.Replace(@"\b","\b");
229 src=src.Replace(@"\t","\t");
230 src=src.Replace(@"\r","\r");
231 src=src.Replace(@"\v","\v");
232 src=src.Replace(@"\f","\f");
233 src=src.Replace(@"\n","\n");
234 src=src.Replace(@"\d","\x22");
235 src=src.Replace(@"\s","\x27");
236 src=src.Replace(@"\m","\\");
237 int loc=0,begin=0;
238 string temp="";
239 while(loc<src.Length&&src.IndexOf(@"\",loc)>-1){
240 loc=src.IndexOf(@"\",loc);
241 for(int i=begin;i<loc;i++) temp+=src[i].ToString();
242 temp+=StringToValue(src,ref loc);
243 begin=loc;
244 if(loc>=src.Length)break;
245 }
246 if(loc<=src.Length-1){
247 for(int i=loc;i<=src.Length-1;i++)
248 temp+=src[i].ToString();
249 }
250 return temp;
251 }
252 else return src;
253 }
254 public string EncodeString(string src){
255 if(src.Trim().Length>0){
256 src=src.Replace("\\",@"\m");
257 src=src.Replace("\a",@"\a");
258 src=src.Replace("\b",@"\b");
259 src=src.Replace("\t",@"\t");
260 src=src.Replace("\r",@"\r");
261 src=src.Replace("\v",@"\v");
262 src=src.Replace("\f",@"\f");
263 src=src.Replace("\n",@"\n");
264 src=src.Replace("\x22",@"\d");
265 src=src.Replace("\x27",@"\s");
266 &nb
for(int i=0;i<src.Length;i++){
if(src[i]<=''\x1f''||(src[i]>=''\x7f''&&src[i]<=''\xff'')) temp+=ToHexString(src[i]);
else temp+=src[i].ToString();
}
return temp;
}
else return src;
}
public string StringToValue(string src,ref int loc){
string temp;
if(loc+1<src.Length){
if(loc+1<src.Length-1){
temp=src.Substring(loc+1);
ushort a=0,b=0,c=0;
if(temp[0]==''0''){
if(temp[1]>=''0''&&temp[1]<=''7''){
a=(ushort)(temp[1]-''0'');
if(temp.Length>=3){
if(temp[2]>=''0''&&temp[2]<=''7''){
b=(ushort)(temp[2]-''0'');
if(temp.Length>=4){
if(temp[3]>=''0''&&temp[3]<=''7''){
c=(ushort)(temp[3]-''0'');
loc+=5;
return ((char)(a*64+b*8+c)).ToString();
}
else{
loc+=4;
return ((char)(a*8+b)).ToString();
}
}
else{
loc+=4;
return ((char)(a*8+b)).ToString();
}
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=2;
return @"\0";
}
}
else if(temp[0].ToString().ToLower()=="x"){
if( (temp[1]>=''0''&&temp[1]<=''9'') || ( (temp[1]|0x20) >=''a'' && (temp[1]|0x20) <=''f'') ){
if(temp[1]<=''9''){
a=(ushort)(temp[1]-''0'');
}
else{
a=(ushort)((temp[1]|0x20)-''a''+10);
}
if(temp.Length>=3){
if( (temp[2]>=''0''&&temp[2]<=''9'') || ( (temp[2]|0x20) >=''a'' && (temp[2]|0x20) <=''f'') ){
if(temp[2]<=''9''){
b=(ushort)(temp[2]-''0'');
}
else{
b=(ushort)((temp[2]|0x20)-''a''+10);
}
loc+=4;
return ((char)(a*16+b)).ToString();
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=3;
return ((char)a).ToString();
}
}
else{
loc+=2;
return @"\"+temp[0].ToString();
}
}
else{
loc++;
return @"\";
}
}
else{
loc+=2;
return @"\"+src[loc-1].ToString();
}
}
else{
loc++;
return @"\";
}
}
protected string ToHexString(char val){
ushort baseradix=16;
return @"\x"+HexCode((ushort)(val/baseradix))+HexCode((ushort)(val%baseradix));
}
protected string ToOctString(char val){
int baseradix=8,a=0,b=0,c=0,v;
v=(int)val;
if(v>baseradix*baseradix)
a=v/(baseradix*baseradix);
if(v>baseradix)
b=(v-a*baseradix*baseradix)/baseradix;
c=(ushort)(v-a*baseradix*baseradix-b*baseradix);
return @"\0"+a.ToString()+b.ToString()+c.ToString();
}
private string HexCode(ushort val){
val=(ushort)(val%16);
string temp=val.ToString();
if(val>9)
{
switch(val){
case 10:
temp="a";
break;
case 11:
temp="b";
break;
case 12:
temp="c";
break;
case 13:
temp="d";
break;
case 14:
temp="e";
break;
case 15:
temp="f";
break;
}
}
return temp;
}
}
public class Comment{
private string comEOL,strA;
private string [] strB;
private const char splitor='','';
//
private int [,] info;
private string[,] without;
private bool comCleanSpilthEOL;
//
public Comment(){
ResetDefault();
}
public void ResetDefault(){
comEOL="\r\n";
info=new int[0,0];
without=new string[0,0];
comCleanSpilthEOL=true;
}
///////////////////////////////////// Public Functions /////////////////////////////////////
public void WithoutContainer(params string [] para){
if(para.GetUpperBound(0)==-1){
without=new string[0,0];
}
else{
int count=para.GetUpperBound(0)+1;
int cnt=count/2;
if(count%2==1)cnt++;
without=new string[cnt,2];
for(int i=0,j=0;i<cnt;i++){
without[i,0]=para[j++].Trim();
if(j+1>=count)
without[i,1]="";
else
without[i,1]=para[j++].Trim();
}
}
info=new int[0,0];
}
public void ProcessComments(string src,params string[] para){
if(src==null)src="";
strA="";
info=new int[0,0];
if(src.Trim().Length>0&¶.GetUpperBound(0)!=-1) {
int count=para.GetUpperBound(0)+1;
int cnt=count/2;
if(count%2==1)cnt++;
string [,]keyword=new String[cnt,3];
for(int i=0,j=0;i<cnt;i++){
keyword[i,0]=i.ToString();
keyword[i,1]=para[j++].Trim();
if(j+1>=count)
keyword[i,2]="";
else
keyword[i,2]=para[j++].Trim();
}
//check double first
int loc,end,begin;
string temp;
bool prev=false;
strA=src;
string al="";
for(int z=0;z<2;z++){
for(int i=0;i<cnt;i++){
if(z==0){if(keyword[i,2].Length==0) continue;}
else {if(keyword[i,2].Length>0) continue;}
loc=0;
while(strA.IndexOf(keyword[i,1],loc)!=-1&&loc<strA.Length){
loc=strA.IndexOf(keyword[i,1],loc);
temp=LineOfIndex(strA,loc,out begin);
if(OnTheContainer(temp,loc-begin)){
loc+=keyword[i,1].Length;
continue;
}
else{ //checking if on prefore
prev=false;
for(int j=0;j<cnt;j++){
if(j!=i){
if(temp.IndexOf(keyword[j,1])!=-1){
if(!OnTheContainer(temp,temp.IndexOf(keyword[j,1]))){
if(temp.IndexOf(keyword[j,1])<loc-begin){
prev=true;
break;
}
}
}
}
}
if(!prev){
end=loc+keyword[i,1].Length;
if(end>=strA.Length-1)
end=strA.Length-1;
if(z==0){
end=strA.IndexOf(keyword[i,2],end);
if(end==-1){
end=strA.Length-1;
begin=end+1;
}
else{
end+=keyword[i,2].Length-1;
begin=end+1;
}
}
else {
end=strA.IndexOf(comEOL,end);
if(end==-1){
end=strA.Length-1;
begin=end+1;
}
else{
end+=comEOL.Length-1;
begin=end+1;
}
}
strA=MakeBlank(strA,loc,end);
if(al.Length!=0)al+=splitor.ToString();
al+=i.ToString()+splitor.ToString()+loc.ToString()+splitor.ToString()+end.ToString();
loc=begin;
if(begin>=strA.Length)break;
}
else{
loc+=keyword[i,1].Length;
}
}//else
}//while
}//for
}//for
strB=al.Split(splitor);
count=strB.Length/3;
if(count!=0){
info=new int[count,3];
for(cnt=0,begin=0;cnt<count;cnt++){
for(loc=0;loc<3;loc++)
info[cnt,loc]=(int)Int32.Parse(strB[begin++]);
}
}
}
}
public string EmptyComments(string src,params string[] para){
if(src==null)src="";
strA="";
ProcessComments(src,para);
return strA;
}
public string CleanComments(string src,params string[] para){
if(src==null)src="";
strA="";
ProcessComments(src,para);
strB=Split(strA,comEOL);
if(strB.Length>1){
strA="";
for(int i=0;i<strB.Length;i++){
strA+=strB[i].Trim()+comEOL;
}
return CleaningSpilthEOL(strA);
}
else return CleaningSpilthEOL(strB[0].Trim());
}
public static string[] Split(string src,string separator){
if(src==null)src="";
string [] str=new string[1]{src};
if(src.Trim().Length!=0){
if(src.IndexOf(separator)>-1){
int cnt=0,loc=0,begin=0;
while(loc<src.Length&&src.IndexOf(separator,loc)>-1){
cnt++;
loc=src.IndexOf(separator,loc)+separator.Length;
if(loc>=src.Length) break;
}
if(loc<=src.Length-1)cnt++;
str=new string[cnt];
loc=0;
cnt=0;
while(loc<src.Length&&src.IndexOf(separator,loc)>-1){
loc=src.IndexOf(separator,loc);
str[cnt]="";
for(int i=begin;i<loc;i++) str[cnt]+=src[i].ToString();
loc+=separator.Length;
begin=loc;
if(loc>=src.Length)break;
cnt++;
}
if(loc<=src.Length-1){
for(int i=loc;i<=src.Length-1;i++) str[cnt]+=src[i].ToString();
}
return str;
}
else return str;
}
else return str;
}
///////////////////////////////////// Internal functions /////////////////////////////////////
protected string MakeBlank(string src,int start,int end){
if(start==end||src.Trim().Length==0) return src;
else{
string temp,tempA;
tempA=src.Substring(start,end-start+1);
if(tempA.IndexOf(comEOL)==-1){
tempA="";
for(int i=0;i<end-start+1;i++) tempA+=" ";
}
else{
int at=0,begin=0;
temp="";
while(tempA.IndexOf(comEOL,at)!=-1&&at<tempA.Length){
at=tempA.IndexOf(comEOL,at);
for(int i=begin;i<at;i++) temp+=" ";
temp+=comEOL;
at+=comEOL.Length;
begin=at;
}
if(at<=tempA.Length-1){
for(int i=at;i<=tempA.Length-1;i++) temp+=" ";
}
tempA=temp;
}
return src.Substring(0,start)+tempA+src.Substring(end+1);
}
}
protected string LineOfIndex(string src,int idx,out int begin){
int start=0;
bool equ=false;
for(int loc=idx;loc>=0;loc--){
if(src[loc]==comEOL[comEOL.Length-1]){
equ=false;
if(comEOL.Length>1){
if(loc-1>=comEOL.Length-2){
for(int j=0;j<=comEOL.Length-2;j++){
if(src[loc-j-1]==comEOL[comEOL.Length-j-2])equ=true;
else equ=false;
}
}
else equ=false;
}else equ=true;
if(equ){
start=loc+1;
break;
}
}
}
begin=start;
if(src.IndexOf(comEOL,start)==-1) return src.Substring(start);
else return src.Substring(start,src.IndexOf(comEOL,start)-start);
}
protected bool OnTheContainer(string str,int idx){
if(without.GetUpperBound(0)!=-1&&without.GetUpperBound(1)!=-1){
int cnt=without.GetUpperBound(0)+1;
int left=0,right=0;
for(int i=0;i<cnt;i++){
right=-1;
while(str.IndexOf(without[i,0],right+1)!=-1&&right+1<=str.Length){
left=str.IndexOf(without[i,0],right+1);
if(without[i,1].Length==0)
right=str.IndexOf(without[i,0],left+without[i,0].Length);
else
right=str.IndexOf(without[i,1],left+without[i,0].Length);
if(right==-1)break;
else if(idx>left&&idx<right)return true;
}
}
return false;
}
else return false;
}
protected string CleaningSpilthEOL(string src_ini){
strA=src_ini;
if(comCleanSpilthEOL){
for(int i=0;i<3;i++) strA=strA.Replace(comEOL+comEOL,comEOL);
while(true){
if(strA.IndexOf(comEOL)==0) strA=strA.Substring(comEOL.Length);
else break;
}
}
return strA;
}
///////////////////////////////////// public attrib /////////////////////////////////////
public string EOL{
get{
return comEOL;
}
set{
comEOL=value;
}
}
public bool CleanSpilthEOL{
get{
return comCleanSpilthEOL;
}
set{
comCleanSpilthEOL=value;
}
}
public int Count{
get{
if(info.GetUpperBound(0)!=-1&&info.GetUpperBound(1)!=-1){
return info.GetUpperBound(0)+1;
}
else return 0;
}
}
public void GetInfo(int index,out int start,out int end,out int type){
if(index<this.Count){
start=info[index,1];
end=info[index,2];
type=info[index,0];
}
else{
start=0;
end=0;
type=0;
}
}
}
}