code cleanup

address a comment on a method called "ascii" that actually used utf8
This commit is contained in:
nattthebear 2020-05-28 07:38:09 -04:00
parent 12227f4836
commit 04f86c2843
5 changed files with 20 additions and 11 deletions

View File

@ -25,15 +25,24 @@ namespace BizHawk.Common.IOExtensions
return outStream.ToArray(); return outStream.ToArray();
} }
// Read bytes from a BinaryReader and translate them into the UTF-8 string they represent. /// <summary>
// WHAT? WHY IS THIS NAMED ASCII BUT USING UTF8 /// Read a string from a binary reader using utf8 encoding and known byte length
public static string ReadStringFixedAscii(this BinaryReader r, int bytes) /// </summary>
/// <param name="r"></param>
/// <param name="bytes">exact number of bytes to read</param>
/// <returns></returns>
public static string ReadStringFixedUtf8(this BinaryReader r, int bytes)
{ {
var read = new byte[bytes]; var read = new byte[bytes];
r.Read(read, 0, bytes); r.Read(read, 0, bytes);
return Encoding.UTF8.GetString(read); return Encoding.UTF8.GetString(read);
} }
/// <summary>
/// Read a null terminated string from a binary reader using utf8 encoding
/// </summary>
/// <param name="br"></param>
/// <returns></returns>
public static string ReadStringUtf8NullTerminated(this BinaryReader br) public static string ReadStringUtf8NullTerminated(this BinaryReader br)
{ {
using var ms = new MemoryStream(); using var ms = new MemoryStream();

View File

@ -62,9 +62,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
LoadAddress = br.ReadUInt16(); LoadAddress = br.ReadUInt16();
InitAddress = br.ReadUInt16(); InitAddress = br.ReadUInt16();
PlayAddress = br.ReadUInt16(); PlayAddress = br.ReadUInt16();
SongName = br.ReadStringFixedAscii(32); SongName = br.ReadStringFixedUtf8(32);
ArtistName = br.ReadStringFixedAscii(32); ArtistName = br.ReadStringFixedUtf8(32);
CopyrightHolder = br.ReadStringFixedAscii(32); CopyrightHolder = br.ReadStringFixedUtf8(32);
SpeedNTSC = br.ReadUInt16(); SpeedNTSC = br.ReadUInt16();
br.Read(BankswitchInitValues, 0, 8); br.Read(BankswitchInitValues, 0, 8);
SpeedPAL = br.ReadUInt16(); SpeedPAL = br.ReadUInt16();

View File

@ -20,7 +20,7 @@ namespace BizHawk.Emulation.Cores.Sony.PSX
{ {
//not endian safe //not endian safe
var br = new BinaryReader(fs); var br = new BinaryReader(fs);
var sig = br.ReadStringFixedAscii(4); var sig = br.ReadStringFixedUtf8(4);
if (sig != "PSF\x1") if (sig != "PSF\x1")
return false; return false;
@ -37,9 +37,9 @@ namespace BizHawk.Emulation.Cores.Sony.PSX
} }
else else
{ {
if (br.ReadStringFixedAscii(5) == "[TAG]") if (br.ReadStringFixedUtf8(5) == "[TAG]")
{ {
var tagstring = br.ReadStringFixedAscii((int)(fs.Length - fs.Position)).Replace("\r\n", "\n"); var tagstring = br.ReadStringFixedUtf8((int)(fs.Length - fs.Position)).Replace("\r\n", "\n");
foreach (var tag in tagstring.Split('\n', '\x0')) foreach (var tag in tagstring.Split('\n', '\x0'))
{ {
if (tag.Trim() == "") if (tag.Trim() == "")

View File

@ -37,7 +37,7 @@ namespace BizHawk.Emulation.DiscSystem.SBI
using (var fs = File.OpenRead(path)) using (var fs = File.OpenRead(path))
{ {
BinaryReader br = new BinaryReader(fs); BinaryReader br = new BinaryReader(fs);
string sig = br.ReadStringFixedAscii(4); string sig = br.ReadStringFixedUtf8(4);
if (sig != "SBI\0") if (sig != "SBI\0")
return false; return false;
} }

View File

@ -25,7 +25,7 @@ namespace BizHawk.Emulation.DiscSystem.SBI
{ {
using var fs = File.OpenRead(IN_Path); using var fs = File.OpenRead(IN_Path);
BinaryReader br = new BinaryReader(fs); BinaryReader br = new BinaryReader(fs);
string sig = br.ReadStringFixedAscii(4); string sig = br.ReadStringFixedUtf8(4);
if (sig != "SBI\0") if (sig != "SBI\0")
throw new SBIParseException("Missing magic number"); throw new SBIParseException("Missing magic number");