using System; | |
using System.IO; | |
namespace headfirst.hexdmper | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
var filename = Get_filename_from_commandline (args); | |
Check_if_file_exists (filename); | |
Read_blocks_from_file (filename, | |
block => { | |
var formattedBlock = Format_block(block); | |
Print_block(formattedBlock); | |
}); | |
} | |
static string Get_filename_from_commandline(string[] args) { | |
if (args.Length != 1) { | |
Console.Error.WriteLine ("Verwendung: hexdmper Dateiname"); | |
Environment.Exit (1); | |
} | |
return args [0]; | |
} | |
static void Check_if_file_exists(string filename) { | |
if (!File.Exists (filename)) { | |
Console.Error.WriteLine ("Keine derartige Datei: {0}", filename); | |
Environment.Exit (2); | |
} | |
} | |
static void Read_blocks_from_file(string filename, Action<Block> onBlock) { | |
using (Stream file = File.OpenRead (filename)) { | |
var buffer = new byte[16]; | |
var bytesRead = 0; | |
while(true) { | |
var block = new Block () { Position = file.Position }; | |
bytesRead = file.Read (buffer, 0, buffer.Length); | |
if (bytesRead == 0) break; | |
block.Bytes = new byte[bytesRead]; | |
Array.Copy(buffer, block.Bytes, bytesRead); | |
onBlock(block); | |
}; | |
} | |
} | |
static FormattedBlock Format_block(Block block) { | |
var formattedBlock = new FormattedBlock () { Position = block.Position }; | |
formattedBlock.HexBytes = Format_block_as_hexbytes (block.Bytes); | |
formattedBlock.ASCIIBytes = Format_block_as_ASCIIbytes (block.Bytes); | |
return formattedBlock; | |
} | |
static string[] Format_block_as_hexbytes(byte[] bytes) { | |
var hexbytes = new string[16]; | |
for (var i = 0; i < 16; i++) { | |
if (i >= bytes.Length) | |
hexbytes [i] = " "; | |
else | |
hexbytes [i] = bytes [i].ToString ("x2"); | |
} | |
return hexbytes; | |
} | |
static string[] Format_block_as_ASCIIbytes(byte[] bytes) { | |
var asciiBytes = new string[16]; | |
for (var i = 0; i < 16; i++) { | |
if (i >= bytes.Length) | |
asciiBytes [i] = "."; | |
else | |
asciiBytes [i] = bytes [i] >= 32 && bytes [i] <= 250 | |
? char.ConvertFromUtf32(bytes[i]) | |
: "."; | |
} | |
return asciiBytes; | |
} | |
static void Print_block(FormattedBlock block) { | |
Console.Write ("{0:00000000}: ", block.Position); | |
foreach (var hb in block.HexBytes) | |
Console.Write ("{0} ", hb); | |
foreach (var ab in block.ASCIIBytes) | |
Console.Write ("{0}", ab); | |
Console.WriteLine (); | |
} | |
class Block { | |
public long Position; | |
public byte[] Bytes; | |
} | |
class FormattedBlock { | |
public long Position; | |
public string[] HexBytes; | |
public string[] ASCIIBytes; | |
} | |
} | |
} |
using System; | |
using System.IO; | |
namespace headfirst.hexdmper | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
var cmd = new CommandlineProvider (); | |
var filesys = new FilesystemProvider (); | |
var format = new Formatter (); | |
var console = new ConsoleProvider (); | |
var filename = cmd.Get_filename (args); | |
filesys.Check_if_file_exists (filename); | |
filesys.Read_blocks_from_file (filename, | |
block => { | |
var formattedBlock = format.Format_block(block); | |
console.Print_block(formattedBlock); | |
}); | |
} | |
} | |
class CommandlineProvider { | |
public string Get_filename(string[] args) { | |
if (args.Length != 1) { | |
Console.Error.WriteLine ("Verwendung: hexdmper Dateiname"); | |
Environment.Exit (1); | |
} | |
return args [0]; | |
} | |
} | |
class FilesystemProvider { | |
public void Check_if_file_exists(string filename) { | |
if (!File.Exists (filename)) { | |
Console.Error.WriteLine ("Keine derartige Datei: {0}", filename); | |
Environment.Exit (2); | |
} | |
} | |
public void Read_blocks_from_file(string filename, Action<Block> onBlock) { | |
using (Stream file = File.OpenRead (filename)) { | |
var buffer = new byte[16]; | |
var bytesRead = 0; | |
while(true) { | |
var block = new Block () { Position = file.Position }; | |
bytesRead = file.Read (buffer, 0, buffer.Length); | |
if (bytesRead == 0) break; | |
block.Bytes = new byte[bytesRead]; | |
Array.Copy(buffer, block.Bytes, bytesRead); | |
onBlock(block); | |
}; | |
} | |
} | |
} | |
class Block { | |
public long Position; | |
public byte[] Bytes; | |
} | |
class Formatter { | |
public FormattedBlock Format_block(Block block) { | |
var formattedBlock = new FormattedBlock () { Position = block.Position }; | |
formattedBlock.HexBytes = Format_block_as_hexbytes (block.Bytes); | |
formattedBlock.ASCIIBytes = Format_block_as_ASCIIbytes (block.Bytes); | |
return formattedBlock; | |
} | |
private string[] Format_block_as_hexbytes(byte[] bytes) { | |
var hexbytes = new string[16]; | |
for (var i = 0; i < 16; i++) { | |
if (i >= bytes.Length) | |
hexbytes [i] = " "; | |
else | |
hexbytes [i] = bytes [i].ToString ("x2"); | |
} | |
return hexbytes; | |
} | |
private string[] Format_block_as_ASCIIbytes(byte[] bytes) { | |
var asciiBytes = new string[16]; | |
for (var i = 0; i < 16; i++) { | |
if (i >= bytes.Length) | |
asciiBytes [i] = "."; | |
else | |
asciiBytes [i] = bytes [i] >= 32 && bytes [i] <= 250 | |
? char.ConvertFromUtf32(bytes[i]) | |
: "."; | |
} | |
return asciiBytes; | |
} | |
} | |
class FormattedBlock { | |
public long Position; | |
public string[] HexBytes; | |
public string[] ASCIIBytes; | |
} | |
class ConsoleProvider { | |
public void Print_block(FormattedBlock block) { | |
Console.Write ("{0:00000000}: ", block.Position); | |
foreach (var hb in block.HexBytes) | |
Console.Write ("{0} ", hb); | |
foreach (var ab in block.ASCIIBytes) | |
Console.Write ("{0}", ab); | |
Console.WriteLine (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment