Text list representation import/export for array types

This commit is contained in:
Justin Aquadro 2013-05-25 17:12:37 -04:00
parent e319c519dc
commit 563ca5b0a5

View file

@ -3,6 +3,8 @@ using System.Windows.Forms;
using Be.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace NBTExplorer.Windows
{
@ -105,6 +107,80 @@ namespace NBTExplorer.Windows
Close();
}
private String RawToText (byte[] data)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < data.Length; i += _bytesPerElem) {
if (data.Length - i < _bytesPerElem)
break;
switch (_bytesPerElem) {
case 1:
builder.AppendLine(((sbyte)data[i]).ToString());
break;
case 2:
builder.AppendLine(BitConverter.ToInt16(data, i).ToString());
break;
case 4:
builder.AppendLine(BitConverter.ToInt32(data, i).ToString());
break;
case 8:
builder.AppendLine(BitConverter.ToInt64(data, i).ToString());
break;
}
}
return builder.ToString();
}
private byte[] TextToRaw (string text)
{
string[] items = text.Split(null as char[], StringSplitOptions.RemoveEmptyEntries);
byte[] data = new byte[_bytesPerElem * items.Length];
for (int i = 0; i < items.Length; i++) {
int index = i * _bytesPerElem;
switch (_bytesPerElem) {
case 1:
sbyte val1;
if (sbyte.TryParse(items[i], out val1))
data[index] = (byte)val1;
break;
case 2:
short val2;
if (short.TryParse(items[i], out val2)) {
byte[] buffer = BitConverter.GetBytes(val2);
Array.Copy(buffer, 0, data, index, 2);
}
break;
case 4:
int val4;
if (int.TryParse(items[i], out val4)) {
byte[] buffer = BitConverter.GetBytes(val4);
Array.Copy(buffer, 0, data, index, 4);
}
break;
case 8:
long val8;
if (long.TryParse(items[i], out val8)) {
byte[] buffer = BitConverter.GetBytes(val8);
Array.Copy(buffer, 0, data, index, 8);
}
break;
}
}
return data;
}
private void ImportRaw (string path)
{
try {
@ -124,6 +200,28 @@ namespace NBTExplorer.Windows
}
}
private void ImportText (string path)
{
try {
using (FileStream fstr = File.OpenRead(path)) {
byte[] raw = new byte[fstr.Length];
fstr.Read(raw, 0, (int)fstr.Length);
string text = System.Text.Encoding.UTF8.GetString(raw, 0, raw.Length);
_data = TextToRaw(text);
_byteProvider = new DynamicByteProvider(_data);
_byteProvider.Changed += (o, e) => { _modified = true; };
hexBox1.ByteProvider = _byteProvider;
_modified = true;
}
}
catch (Exception e) {
MessageBox.Show("Failed to import data from \"" + path + "\"\n\nException: " + e.Message);
}
}
private void ExportRaw (string path)
{
try {
@ -137,6 +235,20 @@ namespace NBTExplorer.Windows
}
}
private void ExportText (string path)
{
try {
using (FileStream fstr = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None)) {
string text = RawToText(_byteProvider.Bytes.ToArray());
byte[] data = System.Text.Encoding.UTF8.GetBytes(text);
fstr.Write(data, 0, data.Length);
}
}
catch (Exception e) {
MessageBox.Show("Failed to export data to \"" + path + "\"\n\nException: " + e.Message);
}
}
private void _buttonOK_Click (object sender, EventArgs e)
{
Apply();
@ -147,12 +259,12 @@ namespace NBTExplorer.Windows
using (OpenFileDialog ofd = new OpenFileDialog()) {
ofd.RestoreDirectory = true;
ofd.Multiselect = false;
ofd.Filter = "Binary Data|*|Text List (*.txt)|*.txt";
ofd.Filter = "Binary Data|*|Text Data (*.txt)|*.txt";
ofd.FilterIndex = 0;
if (ofd.ShowDialog() == DialogResult.OK) {
if (Path.GetExtension(ofd.FileName) == "txt")
return;
if (Path.GetExtension(ofd.FileName) == ".txt")
ImportText(ofd.FileName);
else
ImportRaw(ofd.FileName);
}
@ -163,13 +275,13 @@ namespace NBTExplorer.Windows
{
using (SaveFileDialog sfd = new SaveFileDialog()) {
sfd.RestoreDirectory = true;
sfd.Filter = "Binary Data|*|Text List (*.txt)|*.txt";
sfd.Filter = "Binary Data|*|Text Data (*.txt)|*.txt";
sfd.FilterIndex = 0;
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() == DialogResult.OK) {
if (Path.GetExtension(sfd.FileName) == "txt")
return;
if (Path.GetExtension(sfd.FileName) == ".txt")
ExportText(sfd.FileName);
else
ExportRaw(sfd.FileName);
}