Commit 079364c3 authored by Vincent van Beveren's avatar Vincent van Beveren

improved export

parent c7acd993
......@@ -71,7 +71,7 @@ public final class Utils {
}
public static void dumpHex(PrintWriter pw, byte[] data, boolean extended) {
public static void dumpHex(PrintWriter pw, byte[] data, boolean extended, int offset) {
StringBuilder sbC = new StringBuilder();
StringBuilder sbH = new StringBuilder();
......@@ -79,7 +79,7 @@ public final class Utils {
if (i % 16 == 0 && extended) {
pw.printf("%04x: ", i);
pw.printf("%04x: ", i + offset);
}
char c = (char)data[i];
......
......@@ -9,7 +9,11 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.swing.Action;
import javax.swing.BorderFactory;
......@@ -17,6 +21,7 @@ import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
......@@ -55,9 +60,9 @@ public class ModuleManager implements BaySelectionListener
_ddmi = ddmiLoader.getDDMI();
ButtonGroup bg = new ButtonGroup();
_binary = new JRadioButton("Binary");
_plainhex = new JRadioButton("Plain Hex");
_extendedhex = new JRadioButton("Extended Hex");
_binary = new JRadioButton("Binary (multiple files)");
_plainhex = new JRadioButton("Plain Hex (multiple files)");
_extendedhex = new JRadioButton("Extended Hex (single file)");
bg.add(_plainhex);
bg.add(_extendedhex);
bg.add(_binary);
......@@ -108,61 +113,126 @@ public class ModuleManager implements BaySelectionListener
String filebasebase =String.format("sfp_%s_%s", _moduleName, _fileDateFormat.format(new Date()));
for (DataSource ds : _ddmi.getSources())
{
if (!ds.isValid(_ctx)) continue;
System.out.println("Exporting datasource " + ds);
List<String> filesWritten = new ArrayList<String>();
List<DataSource> orderedSources = new ArrayList<DataSource>();
orderedSources.addAll(_ddmi.getSources());
Comparator<DataSource> comparator = new Comparator<DataSource>() {
@Override
public int compare(DataSource left, DataSource right) {
return left.getPath().compareTo(right.getPath());
}
};
Collections.sort(orderedSources, comparator);
if (_extendedhex.isSelected()) {
String filename = filebasebase + ".txt";
try {
PrintWriter pw = new PrintWriter(new FileWriter(new File(f, filename)));
byte[] data = ds.read(_ctx, ds.start, ds.size());
String filebase = String.format("sfp_%s_%s_%s",
_fileDateFormat.format(new Date()),
_moduleName,
ds.getPath());
if (_plainhex.isSelected()) {
exportHex(f, filebase, data, false);
}
if (_binary.isSelected()) {
exportBinary(f, filebase, data);
}
if (_extendedhex.isSelected()) {
exportHex(f, filebase, data, true);
try {
for (DataSource ds : orderedSources)
{
if (!ds.isValid(_ctx)) continue;
System.out.println("Exporting datasource " + ds);
try {
byte[] data = ds.read(_ctx, ds.start, ds.size());
pw.println("Section: " + ds.getPath());
exportHex(f, pw, data, true, ds.start);
pw.println();
} catch (IOException e) {
e.printStackTrace();
continue;
}
}
} finally {
filesWritten.add(filename);
try {
pw.close();
} catch (Exception e) {};
}
} catch (IOException e) {
e.printStackTrace();
continue;
} catch (IOException io) {
}
} else {
for (DataSource ds : orderedSources)
{
if (!ds.isValid(_ctx)) continue;
System.out.println("Exporting datasource " + ds);
try {
byte[] data = ds.read(_ctx, ds.start, ds.size());
String filebase = filebasebase + "_" + ds.getPath();
if (_plainhex.isSelected()) {
exportHex(filesWritten, f, filebase, data, false, 0);
}
if (_binary.isSelected()) {
exportBinary(filesWritten, f, filebase, data);
}
} catch (IOException e) {
e.printStackTrace();
continue;
}
}
}
StringBuilder sb = new StringBuilder();
sb.append("Following file(s) have been written:\n");
for (String s : filesWritten) {
sb.append(" * " + s + "\n");
}
sb.append("In directory: \n");
sb.append(" " + f.getPath());
JOptionPane.showMessageDialog(parent, sb.toString(), "Export success!", JOptionPane.INFORMATION_MESSAGE);
}
private void exportHex(File f, String filebase, byte[] content, boolean extended) throws IOException
private void exportHex(List<String> filesWritten, File f, String filebase, byte[] content, boolean extended, int offset) throws IOException
{
PrintWriter pw = new PrintWriter(new FileWriter(new File(f, filebase + (extended ? ".txt" : ".hex"))));
try {
Utils.dumpHex(pw, content, extended);
exportHex(f, pw, content, extended, offset);
filesWritten.add(filebase + (extended ? ".txt" : ".hex"));
} finally {
pw.close();
}
}
private void exportBinary(File f, String filebase, byte[] content) throws IOException
private void exportHex(File f, PrintWriter pw, byte[] content, boolean extended, int offset) throws IOException
{
Utils.dumpHex(pw, content, extended, offset);
}
private void exportBinary(List<String> filesWritten, File f, String filebase, byte[] content) throws IOException
{
FileOutputStream fos = new FileOutputStream(new File(f, filebase + ".bin"));
try {
fos.write(content);
filesWritten.add(filebase + ".bin");
} finally {
fos.close();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment