@@ -532,14 +532,29 @@ def __len__(self):
532532 """Return count of bytes with real values."""
533533 return len (dict_keys (self ._buf ))
534534
535- def write_hex_file (self , f , write_start_addr = True ):
535+ def _get_eol_textfile (eolstyle , platform ):
536+ if eolstyle == 'native' :
537+ return '\n '
538+ elif eolstyle == 'CRLF' :
539+ if platform != 'win32' :
540+ return '\r \n '
541+ else :
542+ return '\n '
543+ else :
544+ raise ValueError ("wrong eolstyle %s" % repr (eolstyle ))
545+ _get_eol_textfile = staticmethod (_get_eol_textfile )
546+
547+ def write_hex_file (self , f , write_start_addr = True , eolstyle = 'native' ):
536548 """Write data to file f in HEX format.
537549
538550 @param f filename or file-like object for writing
539551 @param write_start_addr enable or disable writing start address
540552 record to file (enabled by default).
541553 If there is no start address in obj, nothing
542554 will be written regardless of this setting.
555+ @param eolstyle can be used to force CRLF line-endings
556+ for output file on different platforms.
557+ Supported eol styles: 'native', 'CRLF'.
543558 """
544559 fwrite = getattr (f , "write" , None )
545560 if fwrite :
@@ -550,6 +565,8 @@ def write_hex_file(self, f, write_start_addr=True):
550565 fwrite = fobj .write
551566 fclose = fobj .close
552567
568+ eol = IntelHex ._get_eol_textfile (eolstyle , sys .platform )
569+
553570 # Translation table for uppercasing hex ascii string.
554571 # timeit shows that using hexstr.translate(table)
555572 # is faster than hexstr.upper():
@@ -581,7 +598,7 @@ def write_hex_file(self, f, write_start_addr=True):
581598 bin [8 ] = (- sum (bin )) & 0x0FF # chksum
582599 fwrite (':' +
583600 asstr (hexlify (array_tobytes (bin )).translate (table )) +
584- ' \n ' )
601+ eol )
585602 elif keys == ['EIP' ]:
586603 # Start Linear Address Record
587604 bin [0 ] = 4 # reclen
@@ -596,7 +613,7 @@ def write_hex_file(self, f, write_start_addr=True):
596613 bin [8 ] = (- sum (bin )) & 0x0FF # chksum
597614 fwrite (':' +
598615 asstr (hexlify (array_tobytes (bin )).translate (table )) +
599- ' \n ' )
616+ eol )
600617 else :
601618 if fclose :
602619 fclose ()
@@ -633,7 +650,7 @@ def write_hex_file(self, f, write_start_addr=True):
633650 bin [6 ] = (- sum (bin )) & 0x0FF # chksum
634651 fwrite (':' +
635652 asstr (hexlify (array_tobytes (bin )).translate (table )) +
636- ' \n ' )
653+ eol )
637654
638655 while True :
639656 # produce one record
@@ -671,7 +688,7 @@ def write_hex_file(self, f, write_start_addr=True):
671688 bin [4 + chain_len ] = (- sum (bin )) & 0x0FF # chksum
672689 fwrite (':' +
673690 asstr (hexlify (array_tobytes (bin )).translate (table )) +
674- ' \n ' )
691+ eol )
675692
676693 # adjust cur_addr/cur_ix
677694 cur_ix += chain_len
@@ -685,7 +702,7 @@ def write_hex_file(self, f, write_start_addr=True):
685702 break
686703
687704 # end-of-file record
688- fwrite (":00000001FF\n " )
705+ fwrite (":00000001FF" + eol )
689706 if fclose :
690707 fclose ()
691708
0 commit comments