CVS: python/msp430 util.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/msp430
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9330/python/msp430

Modified Files:
	util.py 
Log Message:
- redirection support in util functions

Index: util.py
===================================================================
RCS file: /cvsroot/mspgcc/python/msp430/util.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -d -r1.2 -r1.3
--- util.py	9 Jul 2004 23:18:42 -0000	1.2
+++ util.py	14 Jun 2005 09:42:56 -0000	1.3
@@ -3,46 +3,46 @@
 import sys
 
 #for the use with memread
-def hexdump( (adr, memstr) ):
+def hexdump( (adr, memstr), output=sys.stdout ):
     """Print a hex dump of data collected with memread
     arg1: tuple with adress, memory
     return None"""
     count = 0
     ascii = ''
     for value in map(ord, memstr):
-        if not count: print "%04x: " % adr,
-        print "%02x" % value,
+        if not count: output.write("%04x:  " % adr)
+        output.write("%02x " % value)
         ascii += (32 <= value < 128) and chr(value) or '.'
         count += 1
         adr += 1
         if count == 16:
             count = 0
-            print "  ", ascii
+            output.write("   %s\n" % ascii)
             ascii = ''
-    if count < 16: print "   "*(16-count), " ", ascii
+    if count < 16: output.write("%s   %s\n" % ("   "*(16-count), ascii))
 
-def makeihex((address, data), eof=1):
+def makeihex((address, data), eof=1, output=sys.stdout):
     """work though the data and output lines in inzel hex format.
     and end tag is appended"""
     start = 0
     while start<len(data):
         end = start + 16
         if end > len(data): end = len(data)
-        _ihexline(address, [ord(x) for x in data[start:end]])
+        _ihexline(address, [ord(x) for x in data[start:end]], output=output)
         start += 16
         address += 16
     if eof:
-        _ihexline(address, [], type=1)   #append no data but an end line
+        _ihexline(address, [], type=1, output=output)   #append no data but an end line
 
-def _ihexline(address, buffer, type=0):
+def _ihexline(address, buffer, type=0, output=sys.stdout):
     """encode one line, output with checksum"""
-    sys.stdout.write( ':%02X%04X%02X' % (len(buffer), address & 0xffff, type) )
+    output.write( ':%02X%04X%02X' % (len(buffer), address & 0xffff, type) )
     sum = len(buffer) + ((address >> 8) & 255) + (address & 255) + (type&255)
     for b in buffer:
         if b == None: b = 0         #substitute nonexistent values with zero
-        sys.stdout.write('%02X' % (b & 255))
+        output.write('%02X' % (b & 255))
         sum += b & 255
-    sys.stdout.write('%02X\n' %( (-sum) & 255))
+    output.write('%02X\n' %( (-sum) & 255))
 
 #add some arguments to a function, but don't call it yet, instead return
 #a wrapper object for later invocation
@@ -70,4 +70,5 @@
             return "curry(%s, %r, %r)" % (self.fun, self.pending, self.kwargs)
 
 if __name__ == '__main__':
-    test()
+    hexdump((0x1000, '0123456789ABCDEF'*3))
+    hexdump((0x1000, 'abcdefg'))



-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games.  How far can you shotput
a projector? How fast can you ride your desk chair down the office luge track?
If you want to score the big prize, get to know the little guy.  
Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=20
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.