add a null check that could potentially crash the emulator in the HowMany() function I wrote for path and watch functions. No functional change probably. Also clean up the organization of this method

This commit is contained in:
andres.delikat 2012-09-04 20:40:39 +00:00
parent eb67d2b666
commit 71e07d0493
4 changed files with 45 additions and 38 deletions

View File

@ -170,8 +170,8 @@ namespace BizHawk.MultiClient
int x = NumParentDirectories(path);
if (x > 0)
{
int y = HowMany(path, "..\\");
int z = HowMany(workingpath, "\\");
int y = StringHelpers.HowMany(path, "..\\");
int z = StringHelpers.HowMany(workingpath, "\\");
if (y >= z)
{
//Return drive letter only, working path must be absolute?
@ -184,36 +184,14 @@ namespace BizHawk.MultiClient
public static int NumParentDirectories(string path)
{
//determine the number of parent directories in path and return result
int x = HowMany(path, '\\');
int x = StringHelpers.HowMany(path, '\\');
if (x > 0)
{
return HowMany(path, "..\\");
return StringHelpers.HowMany(path, "..\\");
}
return 0;
}
public static int HowMany(string str, string s)
{
int count = 0;
for (int x = 0; x < (str.Length - s.Length); x++)
{
if (str.Substring(x, s.Length) == s)
count++;
}
return count;
}
public static int HowMany(string str, char c)
{
int count = 0;
for (int x = 0; x < str.Length; x++)
{
if (str[x] == c)
count++;
}
return count;
}
public static bool IsRecent(string path)
{
if (path == "%recent%")

View File

@ -76,7 +76,7 @@ namespace BizHawk.MultiClient
if (s.Length >= 8 && s.Substring(0, 8) == "SystemID")
continue;
z = HowMany(s, '\t');
z = StringHelpers.HowMany(s, '\t');
if (z == 5)
{
//If 5, then this is a .wch file format made from another emulator, the first column (watch position) is not needed here
@ -131,17 +131,6 @@ namespace BizHawk.MultiClient
return true;
}
public static int HowMany(string str, char c)
{
int count = 0;
for (int x = 0; x < str.Length; x++)
{
if (str[x] == c)
count++;
}
return count;
}
public static int GetDomainPos(string name)
{
//Attempts to find the memory domain by name, if it fails, it defaults to index 0

View File

@ -115,6 +115,7 @@
</Compile>
<Compile Include="InputValidate.cs" />
<Compile Include="KeyTurbo.cs" />
<Compile Include="StringHelpers.cs" />
<Compile Include="MiscControls.cs">
<SubType>Component</SubType>
</Compile>

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BizHawk
{
public static class StringHelpers
{
public static int HowMany(string str, char c)
{
if (!String.IsNullOrEmpty(str))
{
int count = 0;
for (int x = 0; x < str.Length; x++)
{
if (str[x] == c)
count++;
}
return count;
}
else
{
return 0;
}
}
public static int HowMany(string str, string s)
{
int count = 0;
for (int x = 0; x < (str.Length - s.Length); x++)
{
if (str.Substring(x, s.Length) == s)
count++;
}
return count;
}
}
}