CVS: python ihex2titext.py,1.2,1.3 titext2ihex.py,1.2,1.3

Chris Liechti <[email protected]>
Newsgroups gmane.comp.hardware.texas-instruments.msp430.gcc.cvs
Message-ID <[email protected]>
Update of /cvsroot/mspgcc/python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7576/python

Modified Files:
	ihex2titext.py titext2ihex.py 
Log Message:
add real command line options (-h, -o)

Index: ihex2titext.py
===================================================================
RCS file: /cvsroot/mspgcc/python/ihex2titext.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -d -r1.2 -r1.3
--- ihex2titext.py	28 Dec 2005 20:37:57 -0000	1.2
+++ ihex2titext.py	28 Dec 2005 22:02:27 -0000	1.3
@@ -1,25 +1,46 @@
 #!/usr/bin/env python
-# simple intel hex to TI text converter
+# simple intel hex (and elf) to TI text converter
 # data is read from stdin and output on stdout
 # usage: cat file.a43 | ihex2titext >out.txt
+# usage: ihex2titext file.a43 >out.txt
+# usage: ihex2titext file.a43 -o out.txt
 #
 # (C) 2005 Chris Liechti <[email protected]>
 # this is distributed under a free software license, see license.txt
 #
 # $Id$
 
-#~ from msp430.util import hexdump
 from msp430 import memory
 import sys
 
-data = memory.Memory()
-if len(sys.argv) > 1:
-    data.loadFile(sys.argv[1])
+from optparse import OptionParser
+
+parser = OptionParser(usage='usage: %prog [-o filename.txt|-] file.a43|-')
+parser.add_option("-o", "--output", dest="output",
+                  help="write result to given file", metavar="FILE")
+(options, args) = parser.parse_args()
+
+if not args:
+    parser.error('no input files given')
+
+#prepare output
+if options.output is None or options.output == '-':
+    out = sys.stdout
 else:
+    out = file(options.output, 'wb')
+
+#get input
+data = memory.Memory()
+
+for filename in args:
+    if filename == '-':
     data.loadIHex(sys.stdin)                    #intel-hex
+    else:
+        data.loadFile(filename)
+
+#output TI-Text
 for segment in data:
-    #~ hexdump((segment.startaddress, segment.data))            #print a hex display
-    sys.stdout.write("@%04x\n" % segment.startaddress)
+    out.write("@%04x\n" % segment.startaddress)
     for i in range(0, len(segment.data), 16):
-        sys.stdout.write("%s\n" % " ".join(["%02x" % ord(x) for x in segment.data[i:i+16]]))
-sys.stdout.write("q\n")
+        out.write("%s\n" % " ".join(["%02x" % ord(x) for x in segment.data[i:i+16]]))
+out.write("q\n")

Index: titext2ihex.py
===================================================================
RCS file: /cvsroot/mspgcc/python/titext2ihex.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -d -r1.2 -r1.3
--- titext2ihex.py	28 Dec 2005 20:37:57 -0000	1.2
+++ titext2ihex.py	28 Dec 2005 22:02:27 -0000	1.3
@@ -1,23 +1,45 @@
 #!/usr/bin/env python
 # simple TI text to intel hex converter
 # data is read from stdin and output on stdout
-# usage: cat file.txt | titext2ihex >out.a43
+# usage: cat file.txt | titext2ihex - >out.a43
+# usage: titext2ihex file.txt >out.a43
+# usage: titext2ihex file.txt -o out.a43
 #
 # (C) 2004 Chris Liechti <[email protected]>
 # this is distributed under a free software license, see license.txt
 #
 # $Id$
 
-from msp430.util import hexdump, makeihex, _ihexline
+from msp430.util import makeihex, _ihexline
 from msp430 import memory
 import sys
 
-data = memory.Memory()                      #prepare downloaded data
-if len(sys.argv) > 1:
-    data.loadFile(sys.argv[1])
+from optparse import OptionParser
+
+parser = OptionParser(usage='usage: %prog [-o filename.a43|-] file.txt|-')
+parser.add_option("-o", "--output", dest="output",
+                  help="write result to given file", metavar="FILE")
+(options, args) = parser.parse_args()
+
+if not args:
+    parser.error('no input files given')
+
+#prepare output
+if options.output is None or options.output == '-':
+    out = sys.stdout
 else:
+    out = file(options.output, 'wb')
+
+#get input
+data = memory.Memory()                      #prepare downloaded data
+
+for filename in args:
+    if filename == '-':
     data.loadTIText(sys.stdin)                  #TI's format
+    else:
+        data.loadFile(filename)
+
+#write ihex file
 for segment in data:
-    #~ hexdump((segment.startaddress, segment.data))            #print a hex display
-    makeihex((segment.startaddress, segment.data), eof=0)           #ouput a intel-hex file
-_ihexline(0, [], type=1)
+    makeihex((segment.startaddress, segment.data), eof=0, output=out)
+_ihexline(0, [], type=1, output=out)



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.