📄 csharp2vbconverter.cs
字号:
tcLine = tcLine.Replace("( ", "(");
tcLine = tcLine.Replace(" )", ")");
//Make sure that the next character after the open bracket is a valid variable beginner
if(IsNextCharValidVariable(tcLine, nStart) == true)
{
// we either have an _ or [A-Z] or [a-z]
//Now check if the closing bracket exists
int nCloseParen = tcLine.IndexOf(")", nStart + 1);
if (nCloseParen < 0)
break;
//refine the position of nStart based on the position of the closing bracket
nStart = tcLine.Substring(0, nCloseParen).LastIndexOf("(");
npos = nStart;
//Get the position of the first space and see if the space is not before the close paren
int nSpacePos = tcLine.IndexOf(" ", nStart + 1);
if((nSpacePos == -1) || (nSpacePos > nCloseParen))
{
if(IsNextCharValidVariable(tcLine, nCloseParen) == true)
{
string lcVariable = "";
int nEndLocation = -1;
//bool lLoopUntilClose = false;
bool lfound = false;
int nOpenBr = 0, nClosedBr = 0;
//new logic to determine end point in a cast
for(int i=nCloseParen + 1; i<tcLine.Length; i++)
{
switch(tcLine[i])
{
case ';':
{
//if we encounter a semicolon we are done
lfound = true;
break;
}
case ')':
{
nClosedBr++;
break;
}
case '(':
{
nOpenBr++;
break;
}
}
//Check the status
if(lfound == false)
{
//check the brackt counts and update the flag
if(nClosedBr > nOpenBr)
{
lfound = true;
}
else
continue;
}
if(lfound)
{
// we hit the mark
nEndLocation = i - 1;
break;
}
}
if(lfound == true)
{
lcVariable = tcLine.Substring(nCloseParen +1, (nEndLocation - nCloseParen )).Trim();
string cCandidate = tcLine;
string one = tcLine.Substring(0, npos) ;
string two = "CType(" + lcVariable + ", " ;
string three = tcLine.Substring(npos + 1, (nCloseParen - (npos+1)));
if((three.Trim().StartsWith("(")) && (three.Trim().EndsWith(")")))
{
three = three.TrimStart().Substring(1) ;
}
else
{
three += ")";
}
string four = tcLine.Substring(nEndLocation + 1).TrimStart();
// [Leave these comments here for now]
//cCandidate = tcLine.Substring(0, npos) +
// "CType(" + lcVariable + ", " + tcLine.Substring(npos + 1, (nCloseParen - npos -1)) + " )" +
// tcLine.Substring(nEndLocation) ;
/*if(one.TrimEnd().EndsWith("(") && three.StartsWith(")"))
{
one = one.Substring(0, one.Length -1);
three = three.Substring(1);
}*/
cCandidate = one + two + three + four;
tcLine = cCandidate ;
}
}
}
else
{
break;
}
}
} while(true);
}
/// <summary>
/// Handles all the calls for for blocks
/// </summary>
/// <param name="tcText"></param>
/// <returns></returns>
private string HandleForBlocks(string tcText)
{
string[] aText = tcText.Split('\r');
//Loop through the array and look for for block
int i;
for(i=0; i< aText.Length; i++)
{
string lcCurrent = aText[i];
int nEnd = i;
bool lHandled = false;
StringBuilder sb = new StringBuilder();
string cBlock = "";
int j = 0;
string cForLine = "";
if(lcCurrent.Trim().StartsWith("for"))
{
int nOpenedBr = 0;
int nClosedBr = 0;
cForLine = lcCurrent;
int nBrPos = 0;
bool ColonChecked = false;
int nColonPos;
int nRightPos = 0;
//Loop through the block and locate the first {
for(j=i+1; j<aText.Length; j++)
{
if(this.IsHandled(aText[j]))
{
sb.Append(aText[j] + "\n");
continue;
}
//locate the position of a semicolon
if(ColonChecked == false)
{
if(aText[j].StartsWith("for"))
{
nRightPos = aText[j].LastIndexOf(")");
nColonPos = aText[j].IndexOf(";", nRightPos);
nBrPos = aText[j].IndexOf("{", nRightPos);
}
else
{
nColonPos = aText[j].IndexOf(";");
nBrPos = aText[j].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
sb.Append(aText[j]);
lHandled = true;
nEnd = j;
cBlock = sb.ToString();
break;
}
else
{
//nothing
}
}
if(lHandled == false)
{
//locate the ending of the block
if(aText[j].IndexOf("{") > 0)
nOpenedBr += 1;
if(aText[j].IndexOf("}") > 0)
nClosedBr += 1;
sb.Append(aText[j]);
if(nOpenedBr != 0 && nClosedBr!=0)
{
if(nOpenedBr-nClosedBr == 0)
{
nEnd = j;
cBlock = sb.ToString();
break;
}
}
}
}
ForBlockManager fbm = new ForBlockManager();
string cRetVal = fbm.GetForBlock(this, cForLine, cBlock);
this.UpdateIfBlock(ref aText, cRetVal, i, j);
}
}
return this.Build(ref aText);
}
protected string Build(ref string[] taText)
{
StringBuilder s = new StringBuilder();
for(int i=0; i<taText.Length; i++)
{
s.Append(taText[i]);
}
return s.ToString();
}
protected string GetErrLine()
{
return "'----- Error occured when converting -----'\n";
}
/// <summary>
/// Returns a bool indicating if the current line is a method or not
/// </summary>
/// <param name="tcLine"></param>
/// <returns></returns>
private bool IsMethod(string tcLine)
{
//Logic to determine if the line is a method beginner
// Rule 0. If the item is an indexer return back
// Rule 1. The last character of the method is a closing parenthesis
// Rule 2. Currently we use that the method HAS to begin with one of the method modifier
// Rule 3. Check if [ is before ( because chances are that it is an array declaration
tcLine = ReplaceManager.GetSingledSpacedString(tcLine);
if((tcLine.StartsWith("Default ")) || (tcLine.StartsWith("While ")) || (tcLine.StartsWith("Select ")))
return false;
bool llRetVal = false;
//Discard simple method calls e.g. "MyMethod.DoMethod(Para1, Para2);" as they will have semicolon as the last character
//e.g. Object o = new Object();
if(tcLine.EndsWith(")"))
{
//Check if [ exists
if(tcLine.IndexOf("[") >= 0)
{
//different check
if(tcLine.IndexOf("(") < tcLine.IndexOf("["))
{
llRetVal = true;
}
}
else
{
llRetVal = true;
}
}
return llRetVal;
}
/// <summary>
/// Handles the calls for all methods
/// </summary>
/// <param name="tcText"></param>
/// <returns></returns>
private string HandleMethod(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 method
string lcCurrent = aText[i];
if(this.IsHandled(lcCurrent))
continue;
//Unline others it is difficult to determine if the line is a method or not
//IsMethod() determines that process
if(this.IsMethod(lcCurrent))
{
int nStartBr = 0;
int nEndBr = 0;
int j;
int nEnd = i;
string cRetVal = "";
StringBuilder sb = new StringBuilder();
bool lHandled = false;
for(j= i; j<aText.Length; j++)
{
sb.Append(aText[j]);
if(this.IsHandled(aText[j]))
{
continue;
}
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;
lHandled = true;
break;
}
}
}
if(lHandled == true)
{
//Pass the call to the appropriate manager to handle it
MethodBlockManager mbm = new MethodBlockManager();
cRetVal = mbm.GetMethodBlock(lcCurrent, sb.ToString());
//Update the string
this.UpdateIfBlock(ref aText, cRetVal, i, nEnd);
}
}
}
return this.Build(ref aText);
}
/// <summary>
/// Returns a bool indicating if the current line is a property or not
/// </summary>
/// <param name="tcLine"></param>
/// <returns></returns>
private bool IsProperty(string tcLine)
{
//Logic to determine if the line is a method beginner
// Rule 0. If the line begins with "Default " then it is an indexer
// Rule 1. The last character of the property is NOT a closing parenthesis
// Rule 2. Currently we use that the property HAS to begin with one of the method modifier
// Rule 3. Check for properties after methods are checked so that way properties are handled properly
tcLine = ReplaceManager.GetSingledSpacedString(tcLine);
if(tcLine.StartsWith("Default "))
return true;
//Discard simple method calls e.g. "MyMethod.DoMethod(Para1, Para2);" as they will have semicolon as the last character
//e.g. Object o = new Object();
if(tcLine.EndsWith(")") == false && tcLine.EndsWith(";") == false && tcLine.EndsWith("=") == false)
{
for(int i=0; i < this.MethodModifiers.Length; i++)
{
if(tcLine.StartsWith(this.MethodModifiers[i]))
{
if(tcLine.IndexOf("enum") <0 && tcLine.IndexOf("interface") <0 && tcLine.IndexOf("struct") < 0 && tcLine.IndexOf("class") <0)
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// Handles all the properties
/// </summary>
/// <param name="tcText"></param>
/// <returns></returns>
private string HandleProperties(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 method
string lcCurrent = aText[i];
//Unline others it is difficult to determine if the line is a method or not
//IsMethod() determines that process
if(this.IsProperty(lcCurrent))
{
int nStartBr = 0;
int nEndBr = 0;
int j;
int nEnd = i;
string cRetVal = "";
StringBuilder sb = new StringBuilder();
for(j= i; j<aText.Length; j++)
{
sb.Append(aText[j] + "\r\n");
if(this.IsHandled(aText[j]))
{
continue;
}
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
PropertyManager pm = new PropertyManager();
cRetVal = pm.GetBlock(lcCurrent, sb.ToString());
//Update the string
this.UpdateIfBlock(ref aText, cRetVal, i, nEnd);
}
}
return this.Build(ref aText);
}
/// <summary>
/// Receives a string, search value and mode to search for as parameters and
/// returns a bool specifying if the condition was successful
/// </summary>
/// <param name="tcStr"></param>
/// <param name="tcSearch"></param>
/// <param name="tnType"></param>
/// <returns></returns>
protected bool CheckIfFound(string tcStr, string tcSearch, int tnType)
{
if(tnType == 0)
{
//If type is 0 check for starts with
return tcStr.Trim().StartsWith(tcSearch);
}
else
{
//search for a contains in the string
return tcStr.Trim().IndexOf(tcSearch) >=0 ;
}
}
/// <summary>
/// Common handler that receives the string to handle and handles it the generic
/// way. Supports a third parameter tnType that specifies if the string should be
/// searched for "begins with " or "contains"
/// </summary>
/// <param name="tcText"></param>
/// <param name="cSearch"></param>
/// <param name="tnType"></param>
/// <returns></returns>
private string CommonHandler(string tcText, string cSearch, int tnType)
{
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];
if(this.IsHandled(lcCurrent))
continue;
//Check if the line does not have any brackets
//if(lcCurrent.Trim().IndexOf(cSearch) >= 0)
if(this.CheckIfFound(lcCurrent, cSearch, tnType))
{
int nStartBr = 0;
int nEndBr = 0;
int j;
int nEnd = i;
string cRetVal = "";
StringBuilder sb = new StringBuilder();
bool ColonChecked = false;
int nColonPos = 0;
int nBrPos = 0;
for(j= i; j<aText.Length; j++)
{
sb.Append(aText[j]);
if(this.IsHandled(aText[j]))
{
continue;
}
//kamal
//locate the position of a semicolon
if(ColonChecked == false)
{
nColonPos = aText[j].IndexOf(";");
nBrPos = aText[j].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
nEnd = j;
break;
}
else
{
//nothing;
}
//make sure that we check this out
if(aText[j].IndexOf("{") > 0)
nStartBr++;
if(aText[j].IndexOf("}") > 0)
nEndBr++;
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -