📄 csharp2vbconverter.cs
字号:
{
if(aText[j].IndexOf("{") > 0)
nStartBr++;
if(aText[j].IndexOf("}") > 0)
nEndBr++;
if(nStartBr !=0 && nEndBr !=0)
{
if(nStartBr-nEndBr == 0)
{
//we reached the end
nEnd = j;
break;
}
}
}
}
//Pass the call to the appropriate manager to handle it
//Cheat for now instead of using a delegate
switch(cSearch.Trim())
{
case "class":
{
ClassBlockManager bm = new ClassBlockManager();
cRetVal = bm.GetClassBlock(lcCurrent, sb.ToString());
break;
}
case "struct":
{
StructureBlockManager sm = new StructureBlockManager();
cRetVal = sm.GetBlock(lcCurrent, sb.ToString());
break;
}
case "interface":
{
InterfaceBlockManager im = new InterfaceBlockManager();
cRetVal = im.GetBlock(lcCurrent, sb.ToString());
break;
}
case "namespace":
{
NameSpaceManager bm = new NameSpaceManager();
cRetVal = bm.GetNameSpaceBlock(lcCurrent, sb.ToString());
break;
}
case "foreach":
{
ForEachManager bm = new ForEachManager();
cRetVal = bm.GetBlock(lcCurrent, sb.ToString());
break;
}
case "do":
{
if(!ReplaceManager.IsNextCharValid(lcCurrent, "do"))
continue;
DoWhileManager bm = new DoWhileManager();
cRetVal = bm.GetBlock(this, lcCurrent, sb.ToString());
break;
}
case "while":
{
WhileManager bm = new WhileManager();
cRetVal = bm.GetBlock(this, lcCurrent, sb.ToString());
break;
}
case "switch":
{
SwitchManager sm = new SwitchManager();
cRetVal = sm.GetBlock(lcCurrent, sb.ToString());
break;
}
case "case":
{
CaseManager cm = new CaseManager();
cRetVal = cm.GetBlock(lcCurrent, sb.ToString());
break;
}
case "catch":
{
CatchManager cm = new CatchManager();
cRetVal = cm.GetBlock(lcCurrent, sb.ToString());
break;
}
case "enum":
{
EnumManager em = new EnumManager();
cRetVal = em.GetBlock(lcCurrent, sb.ToString());
break;
}
default:
break;
}
//Update the string
this.UpdateIfBlock(ref aText, cRetVal, i, nEnd);
}
}
return this.Build(ref aText);
}
/// <summary>
/// Designed for common tasks and returns the first available block of code
/// Used heavily by try-case-finally, if-endif, switch-case
/// </summary>
/// <param name="aText"></param>
/// <param name="nCurrent"></param>
/// <param name="cSearchFor"></param>
/// <param name="nFoundAt"></param>
/// <returns></returns>
private string GetBlock(ref string[] aText, ref int nCurrent, string cSearchFor, ref int nFoundAt, ref string tcNextStartWith)
{
StringBuilder sb = new StringBuilder();
//scan further to check if there is a catch block
for(int k=nCurrent; k<aText.Length; k++)
{
if(this.IsHandled(aText[k]))
{
continue;
}
int nStartBr = 0;
int nEndBr = 0;
int nColonPos = 0;
int nBrPos = 0;
bool ColonChecked = false;
if(aText[k].Trim().StartsWith(cSearchFor))
{
nFoundAt = k;
for(int l=k; l<aText.Length; l++)
{
sb.Append(aText[l] + "\r\n");
//Removed this line because if a throw was on the same line it broke
//if(this.IsHandled(aText[l]))
if(aText[l].Trim().StartsWith("//") == true)
{
continue;
}
//locate the position of a semicolon
if(ColonChecked == false)
{
nColonPos = aText[l].IndexOf(";");
nBrPos = aText[l].IndexOf("{");
if(nBrPos>nColonPos)
{
//update the flag and don't come in this loop again
ColonChecked = true;
}
else if(nColonPos > nBrPos)
{
//Get the value from here itself and break out of here
tcNextStartWith = ";";
nCurrent = l;
return sb.ToString();
}
else
{
//nothing;
}
}
if(aText[l].IndexOf("{") >= 0)
nStartBr++;
if(aText[l].IndexOf("}") >= 0)
nEndBr++;
if(nStartBr !=0 && nEndBr !=0)
{
if(nStartBr-nEndBr == 0)
{
nCurrent = l;
tcNextStartWith = "}";
return sb.ToString();
}
}
}
//
}
}
return "";
}
/// <summary>
/// Returns a bool indicating if a specified word is following another word.
/// Used to determine patterns in code blocks
/// </summary>
/// <param name="tcSearchIn"></param>
/// <param name="nStart"></param>
/// <param name="tcAfter"></param>
/// <param name="tcSearchFor"></param>
/// <returns></returns>
private bool IsImmediatelyAfter(ref string[] tcSearchIn, ref int nStart, string tcAfter, string tcSearchFor)
{
for(int i= nStart; i<tcSearchIn.Length; i++)
{
//Ignore blank and comment lines
if(tcSearchIn[i].Trim().Length == 0)
continue;
//Locate the startpoint and if unless we find it continue
if(tcSearchIn[i].Trim().StartsWith("//") == false)
{
//locate the position of the tcAfter string
int nStartAfter = tcSearchIn[i].IndexOf(tcAfter);
if(nStartAfter < 0)
break;
//Just in case the starting next search is on the same line
if(tcSearchIn[i].IndexOf(tcSearchFor,nStartAfter) > 0)
{
nStart = i;
return true;
}
for(int j= i+1; j < tcSearchIn.Length; j++)
{
//Ignore blank and comment lines
if(tcSearchIn[j].Trim().Length == 0)
continue;
if(tcSearchIn[j].Trim().StartsWith(this.BlankLineToken) == true)
continue;
//Locate the startpoint and if unless we find it continue
if(tcSearchIn[j].Trim().StartsWith("//") == false)
{
bool lFound = tcSearchIn[j].Trim().StartsWith(tcSearchFor);
if(lFound == true)
nStart = j;
return lFound;
}
}
}
}
return false;
}
/// <summary>
/// Handles all the if blocks
/// </summary>
/// <param name="tcText"></param>
/// <returns></returns>
private string HandleIfBlock(string tcText)
{
string[] aText = tcText.Split('\r');
int i;
for(i = 0; i < aText.Length; i++)
{
string lcCurrent = aText[i];
string cRetVal = "";
if(this.IsHandled(lcCurrent))
continue;
//Check if the line does not have any brackets
if(lcCurrent.Trim().StartsWith("if"))
{
string cIfBlock = "";
ArrayList aElseBlock = new ArrayList();
string cIfLine = "";
int nStartPos = i;
int nFoundAt = 0;
int nCurrent = i;
string cElseBlock = "";
string cElseLine = "";
string cNextStartWith = "";
cIfBlock = this.GetBlock(ref aText, ref nCurrent, "if", ref nFoundAt, ref cNextStartWith);
cIfLine = aText[nFoundAt];
do
{
if(this.IsImmediatelyAfter(ref aText, ref nCurrent, cNextStartWith, "else"))
{
cElseBlock = this.GetBlock(ref aText, ref nCurrent, "else", ref nFoundAt, ref cNextStartWith);
cElseLine = aText[nFoundAt];
ElseBlockToken o = new ElseBlockToken();
o.ElseLine = cElseLine;
o.ElseBlock = cElseBlock;
aElseBlock.Add(o);
}
else
{
break;
}
}while(true);
IfBlockManager tm = new IfBlockManager();
cRetVal = tm.GetIfBlock(this, cIfBlock, cIfLine, aElseBlock);
//Update the string
this.UpdateIfBlock(ref aText, cRetVal, nStartPos, nCurrent);
}
}
return this.Build(ref aText);
}
/// <summary>
/// Handles ann try-catch-finally blocks
/// </summary>
/// <param name="tcText"></param>
/// <returns></returns>
private string HandleTryCatchFinally(string tcText)
{
string[] aText = tcText.Split('\r');
//Loop through each line of the string and check if the string is a class
int i;
for(i = 0; i < aText.Length; i++)
{
//stop only when we encounter an a class
string lcCurrent = aText[i];
string cRetVal = "";
if(this.IsHandled(lcCurrent))
continue;
//Check if the line does not have any brackets
if(lcCurrent.Trim().StartsWith("try"))
{
string cTryBlock = "";
string cFinallyBlock = "";
string cCatchBlock = "";
string cCatchLine = "";
int nStartPos = i;
int nFoundAt = 0;
int nCurrent = i;
string cNextStartWith = "";
cTryBlock = this.GetBlock(ref aText, ref nCurrent, "try", ref nFoundAt, ref cNextStartWith);
do
{
if(this.IsImmediatelyAfter(ref aText, ref nCurrent, cNextStartWith, "catch"))
{
cCatchBlock += this.GetBlock(ref aText, ref nCurrent, "catch", ref nFoundAt, ref cNextStartWith);
cCatchLine = aText[nFoundAt];
}
else
{
break;
}
} while(true);
if(this.IsImmediatelyAfter(ref aText, ref nCurrent, cNextStartWith, "finally"))
{
cFinallyBlock = this.GetBlock(ref aText, ref nCurrent, "finally", ref nFoundAt, ref cNextStartWith);
}
TryCatchFinallyManager tm = new TryCatchFinallyManager();
//cRetVal = tm.GetBlock(cTryBlock, cCatchLine, cCatchBlock, cFinallyBlock);
cRetVal = tm.GetBlock(cTryBlock, cCatchBlock, cFinallyBlock);
//Update the string
this.UpdateIfBlock(ref aText, cRetVal, nStartPos, nCurrent );
}
}
return this.Build(ref aText);
}
/// <summary>
/// Common method that is used by all the Handlers to update the block
/// </summary>
/// <param name="aDest"></param>
/// <param name="cSource"></param>
/// <param name="nCurrent"></param>
/// <param name="nEnd"></param>
private void UpdateIfBlock(ref string[] aDest, string cSource, int nCurrent, int nEnd)
{
//Change the contents of aText
string[] aRetVal = cSource.Split('\n');
for(int i=nCurrent; i <= nEnd; i++)
{
try
{
aDest[i] = "";
}
catch
{
// ignore since we are simply blanking the items. We WILL hit
// this place if the source code has compilation problems due to
// incorrect number of open and closed braces
}
}
int r;
int nCount = 0;
for(r=0; r< aRetVal.Length; r++)
{
if(aRetVal[r].Trim().Length > 0)
{
//Check if we have hit the max limit
if(nCount == (nEnd - nCurrent + 1))
{
//keep adding it on the same line
aDest[nCurrent + nCount -1] = aDest[nCurrent + nCount -1] + "\n" + aRetVal[r];
}
else
{
aDest[nCurrent + nCount] = "\n" + aRetVal[r];
nCount++;
}
}
}
}
/// <summary>
/// Returns a bool to the handlers specifing if the line is handled or not
/// </summary>
/// <param name="tcLine"></param>
/// <returns></returns>
private bool IsHandled(string tcLine)
{
//return false;
bool lHandled = false;
//If the line is empty ignore it
if(tcLine.Trim().Length == 0)
{
lHandled = true;
}
//If the line is a comment then handle it
if(tcLine.Trim().StartsWith("//"))
{
lHandled = true;
}
if(tcLine.Trim().StartsWith("using"))
lHandled = true;
if(tcLine.Trim().StartsWith("throw"))
lHandled = true;
//if(tcLine.Trim() == this.BlankLineToken)
// lHandled = true;
return lHandled;
}
/// <summary>
/// Handles when the appropriate declarations should be called
/// </summary>
/// <param name="tcText"></param>
/// <returns></returns>
private string HandleDeclaration(string tcText)
{
StringBuilder sb = new StringBuilder();
string[] aText = tcText.Split('\n');
bool llHandled;
//Loop through each line of the string and check if the left of the string is a datatype
for(int i = 0; i < aText.Length; i++)
{
llHandled = false;
string lcCurrent = aText[i];
//special condition if the line has a += then ignore conversion
if((this.IsHandled(lcCurrent) == false) && lcCurrent.IndexOf("+=") < 0)
{
//Check if the line does not have any brackets
if((lcCurrent.IndexOf(";") > 0) || lcCurrent.Trim().EndsWith("]") || ((lcCurrent.IndexOf("[") > 0) && (lcCurrent.Trim().EndsWith("="))))
{
//Fixed the converter so there is only one method
if(llHandled == false)
{
string lcStr = lcCurrent.Trim();
string lcStr1 = ReplaceManager.GetSingledSpacedString(lcStr);
//handle array assignmenets
if((lcStr1.IndexOf("new ") > 0) && (lcStr1.EndsWith("];")))
{
lcCurrent = lcCurrent.Replace(";", " {};");
lcStr = lcCurrent;
}
int nPos = lcStr.IndexOf('=');
//extract everything before an equals sign
if(nPos > 0)
{
lcStr = lcStr.Substring(0, nPos).Trim();
}
//Split the string with a space
//lcStr = lcStr.Replace("=", " = ");
lcStr = ReplaceManager.GetSingledSpacedString(lcStr);
//Remove the last semicolon and trim up the string
if(lcStr.EndsWith(";"))
{
lcStr = lcStr.Substring(0, lcStr.Length-1).Trim();
}
//remove any modifiers from the string
for(int q=0; q<ReplaceManager.Modifiers.Length; q++)
{
if(lcStr.StartsWith(ReplaceManager.Modifiers[q]))
{
lcStr = lcStr.Substring(ReplaceManager.Modifiers[q].Length + 1);
q=0;
}
}
lcStr = lcStr.Trim();
string[] acheck = lcStr.Split(' ');
if(acheck.Length == 2)
{
//we found a match
llHandled = true;
FieldManager fm = new FieldManager();
lcCurrent = fm.GetConvertedExpression(lcCurrent);
}
}
}
}
sb.Append(lcCurrent + "\n");
}
return sb.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -