一个INI解析器的C#实现

虽然在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

  1<mc.count;i++) =').length="" break;="" cm.withoutcontainer();="" cnt="0;" cnt++;="" else="" for(int="" getsubkeynames(string="" i="0;i&lt;strB.Length;i++)" if(!iniprocerror){="" if((i+1)!="mc.Count)" if(caseproc(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim())="mainkey){" if(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim().replace("\ua1a1","").length!="0)" if(strb[i].trim().replace("\ua1a1","").length!="0)" if(strb[i].trim().replace("\ua1a1","").split('=').Length&gt;1)   
  2length++;   
  3strBB=new String[length];   
  4length=0;   
  5for(int i=0;i&lt;strB.Length;i++)   
  6if(strB[i].Trim().Replace("\ua1a1","").Length!=0)   
  7if(strB[i].Trim().Replace("\ua1a1","").Split(' if(testmainkeyexist(src_ini,mainkey)){="" int="" length="0;" mainkey="CaseProc(mainkey.Trim());" mainkey){="" mc="regex.Matches(strA);" names="" ok.");="" public="" regex="new" regex(@"(\r\n)?\\[[\x20\ua1a1]*\s+[\x20\ua1a1]*\\]\s*(\r\n)?");="" return="" sendokmsg("get="" sloc="mc[i].Index+mc[i].Value.Length;" src_ini,string="" stra='cm.EmptyComments(strAA,"\"","\"");' straa="CleanComments(src_ini);" strb="strAA.Substring(sloc,length).Replace(iniEOL,&quot;\n&quot;).Split('\n');" strb;="" strb[cnt++]='mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim();' string[]="" string[]{""};="" string[cnt];="" }="">1)   
  8strBB[length++]=strB[i].Split('=')[0].Trim();   
  9SendOkMsg("Get subkey names OK.");   
 10return strBB;   
 11}   
 12else return strB;   
 13}   
 14else return strB;   
 15}   
 16public bool TestKeyExist(string src_ini,string mainkey,string subkey){   
 17if(mainkey.Trim().Length==0&amp;&amp;subkey.Trim().Length==0){   
 18SendErrorMsg("Test key exist exception:must be specify the mainkey &amp; subkey names.");   
 19return false;   
 20}   
 21else{   
 22if(subkey.Trim().Length==0) return TestMainkeyExist(src_ini,mainkey);   
 23else return TestSubkeyExist(src_ini,mainkey,subkey);   
 24}   
 25}   
 26public string GetKeyValue(string src_ini,string mainkey,string subkey){   
 27if(TestSubkeyExist(src_ini,mainkey,subkey)){   
 28strAA=CleanComments(src_ini);   
 29cm.WithoutContainer();   
 30strA=cm.EmptyComments(strAA,"\"","\"");   
 31if(iniProcError) return "";   
 32mainkey=CaseProc(mainkey.Trim());   
 33subkey=CaseProc(subkey.Trim());   
 34regex=new Regex(@"(\r\n)?\\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\\]\s*(\r\n)?");   
 35mc=regex.Matches(strA);   
 36int sloc=0,length=0;   
 37for(int i=0;i<mc.count;i++) "";="" "+stra+".");="" 0;="" ;="" =')[0].trim())="subkey)" break;="" cm.withoutcontainer();="" else="" for(int="" getkeyvaluelong(string="" i="0;i&lt;mc.Count;i++)" if(!iniprocerror&&stra.trim().length!="0){" if((i+1)!="mc.Count)" if(caseproc(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim())="mainkey){" if(mc.count="0)" if(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim().replace("\ua1a1","").length="" if(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim().replace("\ua1a1","").length!="0)" if(strb[i].trim().replace("\ua1a1","").length!="0)" if(strb[i].trim().replace("\ua1a1","").split('=").Length&gt;1)   
 38if(CaseProc(strB[i].Split(" if(testsubkeyexist(src_ini,mainkey,subkey)){="" int="" int64.parse(stra.trim());="" is="" key:\""+mainkey.trim()+"."+subkey+"\"="" length="strA.Substring(sloc).Length;" long="" mainkey="CaseProc(mainkey.Trim());" mainkey,string="" mc="regex.Matches(strA);" of="" public="" regex="new" regex(@"(\r\n)?\\[[\x20\ua1a1]*\s+[\x20\ua1a1]*\\]\s*(\r\n)?");="" regex(@"[^0-9]*");="" return="" sendokmsg("the="" setkeyvalue(string="" sloc="0,length=0;" src_ini,string="" stra='cm.EmptyComments(strAA,"\"","\"");' stra;="" straa="EmptyComments(src_ini);" strb="strAA.Substring(sloc,length).Replace(iniEOL,&quot;\n&quot;).Split('\n');" string="" subkey="CaseProc(subkey.Trim());" subkey)="" subkey,string="" val){="" value="" {="" }="" }else="">0)   
 39if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){   
 40sloc=mc[i].Index+mc[i].Value.Length;   
 41if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;   
 42else length=strA.Substring(sloc).Length;   
 43break;   
 44}   
 45strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split('\n');   
 46strBB=src_ini.Substring(sloc,length).Replace(iniEOL,"\n").Split('\n');   
 47strA="";   
 48for(int i=0;i<strb.length;i++) ")!="-1){" "))="" ){="" ;="" =')[0].trim())="subkey){" \""+mainkey.trim()+"\"="" \""+val.trim()+"\".");="" autosetkeyvalue(string="" cleancomments(string="" cleaning="" cleaningspiltheol(src_ini);="" cleaningspiltheol(stra);="" cm.cleanspiltheol="iniCleanSpilthEOL;" cm.eol="iniEOL;" cm.withoutcontainer("\"","\"","\'","\'");="" cm.withoutcontainer();="" comments="" comments.");="" createmainkey(string="" createsubkey(src_ini,mainkey,subkey,val);="" createsubkey(string="" deletekey(string="" deletemainkey(src_ini,mainkey);="" deletesubkey(src_ini,mainkey,subkey);="" else="" else{="" empty="" error,must="" exception="" for(int="" functions="" i="0;i&lt;mc.Count;i++)" if(!testmainkeyexist(src_ini,mainkey))src_ini="CreateMainkey(src_ini,mainkey);" if(mainkey.trim().length="0){" if(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim().replace("\ua1a1","").length="" if(src_ini.trim()!="" if(strb[i].trim().replace("\ua1a1","").length!="0){" if(strb[i].trim().split('=").Length&gt;1){   
 49if(CaseProc(strB[i].Split(" if(strbb[i].indexof(@"="" if(strbb[i].indexof(@";")!="-1){" if(strbb[i].indexof(@";")<strbb[i].indexof(@"="" if(subkey.trim().length="0)" if(testmainkeyexist(src_ini,mainkey))src_ini="CleaningSpilthEOL(DeleteMainkey(src_ini,mainkey));" if(testmainkeyexist(src_ini,mainkey)){="" if(testsubkeyexist(src_ini,mainkey,subkey))="" ini="" inicasesensitive="true;" inicleanspiltheol="true;" inieol="\r\n" iniprocerror="false;" int="" internal="" key:\""+mainkey.trim()+"."+subkey+"\"="" mainkey="CaseProc(mainkey.Trim());" mainkey){="" mainkey,string="" mc="regex.Matches(strA);" name.");="" of="" ok.");="" protected="" public="" regex="new" regex(@"(\r\n)?\\[[\x20\ua1a1]*\s+[\x20\ua1a1]*\\]\s*(\r\n)?");="" resetdefaultoptions(){="" return="" senderrormsg("source="" senderrormsg(@"internal:create="" sendokmsg("clean="" sendokmsg("internal:create="" sendokmsg("set="" setkeyvalue(src_ini,mainkey,subkey,val);="" setkeyvalue(src_ini,mainkey,subkey,val.tostring());="" setkeyvaluelong(string="" sloc="0,length=0;" specify="" src_ini){="" src_ini+='iniEOL+"["+mainkey.Trim()+"]"+iniEOL;' src_ini,string="" src_ini;="" stra='cm.EmptyComments(strAA,"\"","\"");' stra+="strBB[i]+iniEOL;" straa="EmptyComments(src_ini);" string="" subkey){="" subkey,long="" subkey,string="" the="" to="" val){="" value="" void="" when="" }="" }else="">0)   
 50if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){   
 51sloc=mc[i].Index+mc[i].Value.Length;   
 52if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;   
 53else length=strA.Substring(sloc).Length;   
 54break;   
 55}   
 56strA=src_ini.Substring(0,sloc+length)+iniEOL+subkey+"="+val.Trim()+iniEOL+src_ini.Substring(sloc+length);   
 57SendOkMsg("Create the subkey:\""+mainkey.Trim()+"."+subkey.Trim()+"\" OK.");   
 58return CleaningSpilthEOL(strA);   
 59}   
 60else return src_ini;   
 61}   
 62protected string DeleteMainkey(string src_ini,string mainkey){   
 63if(TestMainkeyExist(src_ini,mainkey)){   
 64strAA=EmptyComments(src_ini);   
 65cm.WithoutContainer();   
 66strA=cm.EmptyComments(strAA,"\"","\"");   
 67mainkey=CaseProc(mainkey.Trim());   
 68regex=new Regex(@"(\r\n)?\\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\\]\s*(\r\n)?");   
 69mc=regex.Matches(strA);   
 70int sloc=0,length=0;   
 71for(int i=0;i<mc.count;i++) if(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim().replace("\ua1a1","").length="">0)   
 72if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){   
 73sloc=mc[i].Index;   
 74if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;   
 75else length=strA.Substring(sloc).Length;   
 76break;   
 77}   
 78strA=src_ini.Substring(0,sloc)+iniEOL+src_ini.Substring(sloc+length);   
 79SendOkMsg("Delete the mainkey:\""+mainkey.Trim()+"\" OK.");   
 80return CleaningSpilthEOL(strA);   
 81}   
 82else return src_ini;   
 83}   
 84protected string DeleteSubkey(string src_ini,string mainkey,string subkey){   
 85if(TestSubkeyExist(src_ini,mainkey,subkey)){   
 86strAA=EmptyComments(src_ini);   
 87cm.WithoutContainer();   
 88strA=cm.EmptyComments(strAA,"\"","\"");   
 89mainkey=CaseProc(mainkey.Trim());   
 90subkey=CaseProc(subkey.Trim());   
 91regex=new Regex(@"(\r\n)?\\[[\x20\ua1a1]*\S+[\x20\ua1a1]*\\]\s*(\r\n)?");   
 92mc=regex.Matches(strA);   
 93int sloc=0,length=0;   
 94for(int i=0;i<mc.count;i++) if(mc[i].value.replace(inieol,"").replace("[","").replace("]","").trim().replace("\ua1a1","").length="">0)   
 95if(CaseProc(mc[i].Value.Replace(iniEOL,"").Replace("[","").Replace("]","").Trim())==mainkey){   
 96sloc=mc[i].Index+mc[i].Value.Length;   
 97if((i+1)!=mc.Count)length=mc[i+1].Index-sloc;   
 98else length=strA.Substring(sloc).Length;   
 99break;   
100}   
101strB=strAA.Substring(sloc,length).Replace(iniEOL,"\n").Split('\n');   
102strBB=src_ini.Substring(sloc,length).Replace(iniEOL,"\n").Split('\n');   
103strA="";   
104for(int i=0;i<strb.length;i++) =')[0].trim())!="subkey)strA+=strBB[i]+iniEOL;" be="" bool="" cleaningspiltheol(stra);="" cm.withoutcontainer("\"","\"","\'","\'");="" comments="" else="" else{="" empty="" emptycomments(string="" exception.");="" exception:must="" exist="" exist.");="" false;="" for(int="" i="0;i&lt;strB.Length;i++)" if(!iniprocerror){="" if(caseproc(strb[i])="mainkey){" if(mainkey.length="0){" if(src_ini.trim().length!="0){" if(strb.length="" if(strb[i].trim().replace("\ua1a1","").length!="0){" if(strb[i].trim().replace("\ua1a1","").split('=").Length&gt;1){   
105if(CaseProc(strB[i].Split(" if(subkey.length="0){" if(testmainkeyexist(src_ini,mainkey)){="" ini="" key:\""+mainkey+"\"="" main="" mainkey="" mainkey){="" mainkey,string="" name.");="" not="" ok.");="" protected="" return="" senderrormsg("test="" senderrormsg(@"internal:source="" sendokmsg("delete="" sendokmsg("the="" sendokmsg(@"internal:empty="" specify="" src_ini){="" src_ini,string="" src_ini;="" stra='cm.EmptyComments(src_ini,@"/*",@"*/",@"//","",@";","");' stra+="strBB[i]+iniEOL;" stra;="" strb="GetSubkeyNames(src_ini,mainkey);" string="" string[]{""};="" subkey="" subkey){="" subkey:\""+mainkey.trim()+"."+subkey+"\"="" testmainkeyexist(string="" testsubkeyexist(string="" the="" to="" true;="" }="" }else="">0){   
106for(int i=0;i<strb.length;i++) attributes="" be="" bool="" caseproc(string="" casesensitive{="" class="" cleaningspiltheol(string="" cleanspiltheol{="" cm.cleanspiltheol="iniCleanSpilthEOL;" cm.eol="iniEOL;" convert{="" decodestring(string="" else="" eol{="" error,string="" error{="" exist.");="" false;="" for(int="" function="" get{="" i="0;i&lt;3;i++)" if(caseproc(strb[i])="subkey){" if(inicasesensitive)="" if(inicleanspiltheol){="" if(src.trim().length="" inicasesensitive="value;" inicasesensitive;="" inicleanspiltheol="value;" inicleanspiltheol;="" inieol="value;" inieol;="" iniprocerror="true;" iniprocerror;="" key:\""+mainkey.trim()+"."+subkey+"\"="" msg){="" not="" ondebug(iniprocerror,msg);="" ondefaultdebugmessage(bool="" private="" public="" return="" senderrormsg(string="" sendokmsg("the="" sendokmsg(string="" set{="" src){="" src_ini){="" str){="" str.tolower();="" str;="" stra="strA.Substring(iniEOL.Length);" stra;="" string="" to="" true;="" void="" while(stra.indexof(inieol)="0){" }="">0){   
107src=src.Replace(@"\a","\a");   
108src=src.Replace(@"\b","\b");   
109src=src.Replace(@"\t","\t");   
110src=src.Replace(@"\r","\r");   
111src=src.Replace(@"\v","\v");   
112src=src.Replace(@"\f","\f");   
113src=src.Replace(@"\n","\n");   
114src=src.Replace(@"\d","\x22");   
115src=src.Replace(@"\s","\x27");   
116src=src.Replace(@"\m","\\\");   
117int loc=0,begin=0;   
118string temp="";   
119while(loc<src.length&&src.indexof(@"\",loc)>-1){   
120loc=src.IndexOf(@"\",loc);   
121for(int i=begin;i<loc;i++) begin="loc;" if(loc="" loc);="" temp+="StringToValue(src,ref">=src.Length)break;   
122}   
123if(loc&lt;=src.Length-1){   
124for(int i=loc;i&lt;=src.Length-1;i++)   
125temp+=src[i].ToString();   
126}   
127return temp;   
128}   
129else return src;   
130}   
131public string EncodeString(string src){   
132if(src.Trim().Length&gt;0){   
133src=src.Replace(" \\\",@"\m ");   
134src=src.Replace("\a",@"\a");   
135src=src.Replace("\b",@"\b");   
136src=src.Replace("\t",@"\t");   
137src=src.Replace("\r",@"\r");   
138src=src.Replace("\v",@"\v");   
139src=src.Replace("\f",@"\f");   
140src=src.Replace("\n",@"\n");   
141src=src.Replace("\x22",@"\d");   
142src=src.Replace("\x27",@"\s");   
143string temp="";   
144for(int i=0;i<src.length;i++){ if(src[i]<="\x1f" ||(src[i]="">='\x7f'&amp;&amp;src[i]&lt;='\xff')) temp+=ToHexString(src[i]);   
145else temp+=src[i].ToString();   
146}   
147return temp;   
148}   
149else return src;   
150}   
151public string StringToValue(string src,ref int loc){   
152string temp;   
153if(loc+1<src.length){ ){="" a="0,b=0,c=0;" if(loc+1<src.length-1){="" if(temp[0]="0" if(temp[1]="" temp="src.Substring(loc+1);" ushort="">='0'&amp;&amp;temp[1]&lt;='7'){   
154a=(ushort)(temp[1]-'0');   
155if(temp.Length&gt;=3){   
156if(temp[2]&gt;='0'&amp;&amp;temp[2]&lt;='7'){   
157b=(ushort)(temp[2]-'0');   
158if(temp.Length&gt;=4){   
159if(temp[3]&gt;='0'&amp;&amp;temp[3]&lt;='7'){   
160c=(ushort)(temp[3]-'0');   
161loc+=5;   
162return ((char)(a*64+b*8+c)).ToString();   
163}   
164else{   
165loc+=4;   
166return ((char)(a*8+b)).ToString();   
167}   
168}   
169else{   
170loc+=4;   
171return ((char)(a*8+b)).ToString();   
172}   
173}   
174else{   
175loc+=3;   
176return ((char)a).ToString();   
177}   
178}   
179else{   
180loc+=3;   
181return ((char)a).ToString();   
182}   
183}   
184else{   
185loc+=2;   
186return @"\0";   
187}   
188}   
189else if(temp[0].ToString().ToLower()=="x"){   
190if( (temp[1]&gt;='0'&amp;&amp;temp[1]&lt;='9') || ( (temp[1]|0x20) &gt;='a' &amp;&amp; (temp[1]|0x20) &lt;='f') ){   
191if(temp[1]&lt;='9'){   
192a=(ushort)(temp[1]-'0');   
193}   
194else{   
195a=(ushort)((temp[1]|0x20)-'a'+10);   
196}   
197if(temp.Length&gt;=3){   
198if( (temp[2]&gt;='0'&amp;&amp;temp[2]&lt;='9') || ( (temp[2]|0x20) &gt;='a' &amp;&amp; (temp[2]|0x20) &lt;='f') ){   
199if(temp[2]&lt;='9'){   
200b=(ushort)(temp[2]-'0');   
201}   
202else{   
203b=(ushort)((temp[2]|0x20)-'a'+10);   
204}   
205loc+=4;   
206return ((char)(a*16+b)).ToString();   
207}   
208else{   
209loc+=3;   
210return ((char)a).ToString();   
211}   
212}   
213else{   
214loc+=3;   
215return ((char)a).ToString();   
216}   
217}   
218else{   
219loc+=2;   
220return @"\"+temp[0].ToString();   
221}   
222}   
223else{   
224loc++;   
225return @"\";   
226}   
227}   
228else{   
229loc+=2;   
230return @"\"+src[loc-1].ToString();   
231}   
232}   
233else{   
234loc++;   
235return @"\";   
236}   
237}   
238protected string ToHexString(char val){   
239ushort baseradix=16;   
240return @"\x"+HexCode((ushort)(val/baseradix))+HexCode((ushort)(val%baseradix));   
241}   
242protected string ToOctString(char val){   
243int baseradix=8,a=0,b=0,c=0,v;   
244v=(int)val;   
245if(v&gt;baseradix*baseradix)   
246a=v/(baseradix*baseradix);   
247if(v&gt;baseradix)   
248b=(v-a*baseradix*baseradix)/baseradix;   
249c=(ushort)(v-a*baseradix*baseradix-b*baseradix);   
250return @"\0"+a.ToString()+b.ToString()+c.ToString();   
251}   
252private string HexCode(ushort val){   
253val=(ushort)(val%16);   
254string temp=val.ToString();   
255if(val&gt;9)   
256{   
257switch(val){   
258case 10:   
259temp="a";   
260break;   
261case 11:   
262temp="b";   
263break;   
264case 12:   
265temp="c";   
266break;   
267case 13:   
268temp="d";   
269break;   
270case 14:   
271temp="e";   
272break;   
273case 15:   
274temp="f";   
275break;   
276}   
277}   
278return temp;   
279}   
280}   
281public class Comment{   
282private string comEOL,strA;   
283private string [] strB;   
284private const char splitor=',';   
285//   
286private int [,] info;   
287private string[,] without;   
288private bool comCleanSpilthEOL;   
289//   
290public Comment(){   
291ResetDefault();   
292}   
293public void ResetDefault(){   
294comEOL="\r\n";   
295info=new int[0,0];   
296without=new string[0,0];   
297comCleanSpilthEOL=true;   
298}   
299///////////////////////////////////// Public Functions /////////////////////////////////////   
300public void WithoutContainer(params string [] para){   
301if(para.GetUpperBound(0)==-1){   
302without=new string[0,0];   
303}   
304else{   
305int count=para.GetUpperBound(0)+1;   
306int cnt=count/2;   
307if(count%2==1)cnt++;   
308without=new string[cnt,2];   
309for(int i=0,j=0;i<cnt;i++){ if(j+1="" without[i,0]="para[j++].Trim();">=count)   
310without[i,1]="";   
311else   
312without[i,1]=para[j++].Trim();   
313}   
314}   
315info=new int[0,0];   
316}   
317public void ProcessComments(string src,params string[] para){   
318if(src==null)src="";   
319strA="";   
320info=new int[0,0];   
321if(src.Trim().Length&gt;0&amp;&amp;para.GetUpperBound(0)!=-1) {   
322int count=para.GetUpperBound(0)+1;   
323int cnt=count/2;   
324if(count%2==1)cnt++;   
325string [,]keyword=new String[cnt,3];   
326for(int i=0,j=0;i<cnt;i++){ if(j+1="" keyword[i,0]="i.ToString();" keyword[i,1]="para[j++].Trim();">=count)   
327keyword[i,2]="";   
328else   
329keyword[i,2]=para[j++].Trim();   
330}   
331//check double first   
332int loc,end,begin;   
333string temp;   
334bool prev=false;   
335strA=src;   
336string al="";   
337for(int z=0;z&lt;2;z++){   
338for(int i=0;i<cnt;i++){ continue;}="" else="" if(z="0){if(keyword[i,2].Length==0)" {if(keyword[i,2].length="">0) continue;}   
339loc=0;   
340while(strA.IndexOf(keyword[i,1],loc)!=-1&amp;&amp;loc<stra.length){ begin);="" break;="" checking="" continue;="" else{="" end="loc+keyword[i,1].Length;" for(int="" if="" if(!onthecontainer(temp,temp.indexof(keyword[j,1]))){="" if(!prev){="" if(end="" if(j!="i){" if(onthecontainer(temp,loc-begin)){="" if(temp.indexof(keyword[j,1])!="-1){" if(temp.indexof(keyword[j,1])<loc-begin){="" j="0;j&lt;cnt;j++){" loc="strA.IndexOf(keyword[i,1],loc);" loc+="keyword[i,1].Length;" on="" prefore="" prev="true;" temp="LineOfIndex(strA,loc,out" }="">=strA.Length-1)   
341end=strA.Length-1;   
342if(z==0){   
343end=strA.IndexOf(keyword[i,2],end);   
344if(end==-1){   
345end=strA.Length-1;   
346begin=end+1;   
347}   
348else{   
349end+=keyword[i,2].Length-1;   
350begin=end+1;   
351}   
352}   
353else {   
354end=strA.IndexOf(comEOL,end);   
355if(end==-1){   
356end=strA.Length-1;   
357begin=end+1;   
358}   
359else{   
360end+=comEOL.Length-1;   
361begin=end+1;   
362}   
363}   
364strA=MakeBlank(strA,loc,end);   
365if(al.Length!=0)al+=splitor.ToString();   
366al+=i.ToString()+splitor.ToString()+loc.ToString()+splitor.ToString()+end.ToString();   
367loc=begin;   
368if(begin&gt;=strA.Length)break;   
369}   
370else{   
371loc+=keyword[i,1].Length;   
372}   
373}//else   
374}//while   
375}//for   
376}//for   
377strB=al.Split(splitor);   
378count=strB.Length/3;   
379if(count!=0){   
380info=new int[count,3];   
381for(cnt=0,begin=0;cnt<count;cnt++){ ;="" cleancomments(string="" emptycomments(string="" for(loc="0;loc&lt;3;loc++)" if(src='null)src="";' if(strb.length="" info[cnt,loc]="(int)Int32.Parse(strB[begin++]);" para){="" processcomments(src,para);="" public="" return="" src,params="" stra="" stra;="" strb="Split(strA,comEOL);" string="" string[]="" }="">1){   
382strA="";   
383for(int i=0;i<strb.length;i++){ []="" cleaningspiltheol(stra);="" cleaningspiltheol(strb[0].trim());="" else="" if(src='null)src="";' if(src.indexof(separator)="" if(src.trim().length!="0){" public="" return="" separator){="" split(string="" src,string="" static="" str="new" stra+="strB[i].Trim()+comEOL;" string="" string[1]{src};="" string[]="" }="">-1){   
384int cnt=0,loc=0,begin=0;   
385while(loc<src.length&&src.indexof(separator,loc)>-1){   
386cnt++;   
387loc=src.IndexOf(separator,loc)+separator.Length;   
388if(loc&gt;=src.Length) break;   
389}   
390if(loc&lt;=src.Length-1)cnt++;   
391str=new string[cnt];   
392loc=0;   
393cnt=0;   
394while(loc<src.length&&src.indexof(separator,loc)>-1){   
395loc=src.IndexOf(separator,loc);   
396str[cnt]="";   
397for(int i=begin;i<loc;i++) begin="loc;" if(loc="" loc+="separator.Length;" str[cnt]+="src[i].ToString();">=src.Length)break;   
398cnt++;   
399}   
400if(loc&lt;=src.Length-1){   
401for(int i=loc;i&lt;=src.Length-1;i++) str[cnt]+=src[i].ToString();   
402}   
403return str;   
404}   
405else return str;   
406}   
407else return str;   
408}   
409///////////////////////////////////// Internal functions /////////////////////////////////////   
410protected string MakeBlank(string src,int start,int end){   
411if(start==end||src.Trim().Length==0) return src;   
412else{   
413string temp,tempA;   
414tempA=src.Substring(start,end-start+1);   
415if(tempA.IndexOf(comEOL)==-1){   
416tempA="";   
417for(int i=0;i<end-start+1;i++) ;="" at="tempA.IndexOf(comEOL,at);" at+="comEOL.Length;" begin="at;" begin){="" bool="" else{="" equ="false;" for(int="" i="at;i&lt;=tempA.Length-1;i++)" idx,out="" if(at<="tempA.Length-1){" int="" lineofindex(string="" loc="idx;loc" protected="" return="" src,int="" src.substring(0,start)+tempa+src.substring(end+1);="" start="0;" string="" temp="" temp+=" " tempa="temp;" tempa+=" " while(tempa.indexof(comeol,at)!="-1&amp;&amp;at&lt;tempA.Length){" }="">=0;loc--){   
418if(src[loc]==comEOL[comEOL.Length-1]){   
419equ=false;   
420if(comEOL.Length&gt;1){   
421if(loc-1&gt;=comEOL.Length-2){   
422for(int j=0;j&lt;=comEOL.Length-2;j++){   
423if(src[loc-j-1]==comEOL[comEOL.Length-j-2])equ=true;   
424else equ=false;   
425}   
426}   
427else equ=false;   
428}else equ=true;   
429if(equ){   
430start=loc+1;   
431break;   
432}   
433}   
434}   
435begin=start;   
436if(src.IndexOf(comEOL,start)==-1) return src.Substring(start);   
437else return src.Substring(start,src.IndexOf(comEOL,start)-start);   
438}   
439protected bool OnTheContainer(string str,int idx){   
440if(without.GetUpperBound(0)!=-1&amp;&amp;without.GetUpperBound(1)!=-1){   
441int cnt=without.GetUpperBound(0)+1;   
442int left=0,right=0;   
443for(int i=0;i<cnt;i++){ else="" if(idx="" if(right="-1)break;" if(without[i,1].length="0)" left="str.IndexOf(without[i,0],right+1);" right="str.IndexOf(without[i,1],left+without[i,0].Length);" while(str.indexof(without[i,0],right+1)!="-1&amp;&amp;right+1&lt;=str.Length){">left&amp;&amp;idx&lt;right)return true;   
444}   
445}   
446return false;   
447}   
448else return false;   
449}   
450protected string CleaningSpilthEOL(string src_ini){   
451strA=src_ini;   
452if(comCleanSpilthEOL){   
453for(int i=0;i&lt;3;i++) strA=strA.Replace(comEOL+comEOL,comEOL);   
454while(true){   
455if(strA.IndexOf(comEOL)==0) strA=strA.Substring(comEOL.Length);   
456else break;   
457}   
458}   
459return strA;   
460}   
461///////////////////////////////////// public attrib /////////////////////////////////////   
462public string EOL{   
463get{   
464return comEOL;   
465}   
466set{   
467comEOL=value;   
468}   
469}   
470public bool CleanSpilthEOL{   
471get{   
472return comCleanSpilthEOL;   
473}   
474set{   
475comCleanSpilthEOL=value;   
476}   
477}   
478public int Count{   
479get{   
480if(info.GetUpperBound(0)!=-1&amp;&amp;info.GetUpperBound(1)!=-1){   
481return info.GetUpperBound(0)+1;   
482}   
483else return 0;   
484}   
485}   
486public void GetInfo(int index,out int start,out int end,out int type){   
487if(index&lt;this.Count){   
488start=info[index,1];   
489end=info[index,2];   
490type=info[index,0];   
491}   
492else{   
493start=0;   
494end=0;   
495type=0;   
496}   
497}   
498}   
499}</cnt;i++){></end-start+1;i++)></loc;i++)></src.length&&src.indexof(separator,loc)></src.length&&src.indexof(separator,loc)></strb.length;i++){></count;cnt++){></stra.length){></cnt;i++){></cnt;i++){></cnt;i++){></src.length){></src.length;i++){></loc;i++)></src.length&&src.indexof(@"\",loc)></strb.length;i++)></strb.length;i++)></mc.count;i++)></mc.count;i++)></strb.length;i++)></mc.count;i++)></mc.count;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus