summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libtoprammer/util.py16
-rwxr-xr-xtoprammer23
-rwxr-xr-xtoprammer-gui29
3 files changed, 41 insertions, 27 deletions
diff --git a/libtoprammer/util.py b/libtoprammer/util.py
index 1d44bab..9ae8e86 100644
--- a/libtoprammer/util.py
+++ b/libtoprammer/util.py
@@ -140,7 +140,7 @@ def generateHexdump(mem):
def dumpMem(mem):
sys.stdout.write(generateHexdump(mem))
-class IO_ihex:
+class IO_ihex(object):
TYPE_DATA = 0
TYPE_EOF = 1
TYPE_ESAR = 2
@@ -236,7 +236,7 @@ class IO_ihex:
ihex.append(":00000001FF\n")
return "".join(ihex)
-class IO_hex:
+class IO_ahex(object):
def autodetect(self, data):
try:
self.toBinary(data)
@@ -250,7 +250,7 @@ class IO_hex:
def fromBinary(self, data):
return generateHexdump(data)
-class IO_binary:
+class IO_binary(object):
def autodetect(self, data):
return True
@@ -259,3 +259,13 @@ class IO_binary:
def fromBinary(self, data):
return data
+
+def IO_autodetect(data):
+ "Returns an IO_... object for the data."
+ if IO_ihex().autodetect(data):
+ return IO_ihex
+ elif IO_ahex().autodetect(data):
+ return IO_ahex
+ elif IO_binary().autodetect(data):
+ return IO_binary
+ assert(0) # Can't reach, because binary will always match.
diff --git a/toprammer b/toprammer
index 0c0563d..2c1c7f8 100755
--- a/toprammer
+++ b/toprammer
@@ -81,12 +81,12 @@ def usage():
print " auto Autodetect. (input only)"
print " bin Raw binary data"
print " ihex Intel hex"
- print " hex Human readable hex"
+ print " ahex Hex with ASCII dump"
IO_handlers = {
"bin" : IO_binary,
"ihex" : IO_ihex,
- "hex" : IO_hex,
+ "ahex" : IO_ahex,
}
def fileOut(filename, format, data):
@@ -97,23 +97,16 @@ def fileOut(filename, format, data):
else:
file(filename, "w+b").write(data)
-def fileIn(filename, format):
+def fileIn(filename, fmtString):
if filename == "-":
data = sys.stdin.read()
else:
data = file(filename, "rb").read()
- if format == "auto":
- if IO_ihex().autodetect(data):
- format = "ihex"
- elif IO_hex().autodetect(data):
- format = "hex"
- elif IO_binary().autodetect(data):
- format = "bin"
- else:
- assert(0)
- handler = IO_handlers[format]()
- data = handler.toBinary(data)
- return data
+ if fmtString == "auto":
+ handler = IO_autodetect(data)()
+ else:
+ handler = IO_handlers[fmtString]()
+ return handler.toBinary(data)
def main(argv):
opt_verbose = 1
diff --git a/toprammer-gui b/toprammer-gui
index 887aedf..5fe49f8 100755
--- a/toprammer-gui
+++ b/toprammer-gui
@@ -2235,8 +2235,10 @@ class MainWindow(QMainWindow):
(fn, selectedFilter) = QFileDialog.getOpenFileName(self,
"%s - open file" % bufWidget.getName(),
"",
- "Intel hex file (*.hex *.ihex);;"
- "Binary file (*)")
+ "Autodetect file format (*);;"
+ "Intel hex file (*.ihex *.hex);;"
+ "Hex file with ASCII dump (*.ahex);;"
+ "Binary file (*.bin)")
if not fn:
return
extensions = str(selectedFilter).split("(")[1].\
@@ -2249,10 +2251,15 @@ class MainWindow(QMainWindow):
(str(fn), str(e.strerror)))
return
try:
- if ".hex" in extensions or ".ihex" in extensions:
+ if ".bin" in extensions:
+ data = IO_binary().toBinary(data) # no-op
+ elif ".ihex" in extensions:
data = IO_ihex().toBinary(data)
- elif not extensions:
- data = IO_binary().toBinary(data)
+ elif ".ahex" in extensions:
+ data = IO_ahex().toBinary(data)
+ elif not extensions: # auto
+ handler = IO_autodetect(data)()
+ data = handler.toBinary(data)
else:
assert(0)
except (TOPException), e:
@@ -2273,7 +2280,8 @@ class MainWindow(QMainWindow):
(fn, selectedFilter) = QFileDialog.getSaveFileName(self,
"%s - save file" % bufWidget.getName(),
"",
- "Intel hex file (*.hex);;"
+ "Intel hex file (*.ihex *.hex);;"
+ "Hex file with ASCII dump (*.ahex);;"
"Binary file (*)")
if not fn:
return
@@ -2281,10 +2289,13 @@ class MainWindow(QMainWindow):
split(")")[0].replace("*", "").strip().split()
if not extensions:
extensions = [ "" ]
- if not fn.endswith(extensions[0]):
- fn += extensions[0]
- if ".hex" in extensions or ".ihex" in extensions:
+ if not '.' in fn or\
+ not '.' + fn.split('.')[-1] in extensions:
+ fn += extensions[0] # Default ext
+ if ".ihex" in extensions:
data = IO_ihex().fromBinary(data)
+ elif ".ahex" in extensions:
+ data = IO_ahex().fromBinary(data)
elif not extensions[0]:
data = IO_binary().fromBinary(data)
else:
bues.ch cgit interface