AbstractFileBrowser.st
changeset 17744 8ce1acaa8fee
parent 17724 87f3d255c4e7
child 17746 7b5d6fc64fee
--- a/AbstractFileBrowser.st	Fri Nov 10 00:17:59 2017 +0100
+++ b/AbstractFileBrowser.st	Sun Nov 12 12:11:53 2017 +0100
@@ -2849,15 +2849,25 @@
 
 !AbstractFileBrowser class methodsFor:'utilities'!
 
-contentsOfBytesAsHexDump:data numberOfAddressDigits:addrDigits addressStart:virtualStart
-    "utility helper: generate a hexDump with addresses"
-
-    |lines nHexLines replacementForUnprintable|
+contentsOfBytesAsDump:data base:numberBase numberOfAddressDigits:addrDigits addressStart:virtualStart characterEncoding:characterEncodingSymbol
+    "utility helper: generate a hexDump with addresses;
+     characterEncodingSymbol determines how characters are to be shown in the right (character) columns.
+     By default, this is iso8859-1, but supported are also ebcdic and ascii7,
+     to support display of alien encoded binary data."
+
+    |lines nHexLines replacementForUnprintable colsPerByte characterDecoder|
+
+    (characterEncodingSymbol isNil or:[characterEncodingSymbol == #'iso8859-1']) ifFalse:[
+        characterDecoder := CharacterEncoder encoderToEncodeFrom:characterEncodingSymbol into:#unicode.
+    ].    
 
     "/ used to be:
     "/ replacementForUnprintable := $.
     replacementForUnprintable := Character value:16rB7. "/ a centered dot.
 
+    "/ cols needed per byte in the hex dump
+    colsPerByte := ((255 printStringRadix:numberBase) size).
+
     "generate a virtual collection which evaluates and returns lines on-demand"
 
     nHexLines := (data size + 15) // 16.
@@ -2866,7 +2876,8 @@
     lines setSize:nHexLines + (nHexLines // 16).
     lines 
         generator:[:lineNr |
-            |blockNr lineNrInBlock startOffset lineStream asciiLineStream byte line|
+            |blockNr lineNrInBlock startOffset lineStream asciiLineStream byte line
+             decodedChar charPrinted|
 
             blockNr := (lineNr - 1) // 17.
             lineNrInBlock := (lineNr - 1) \\ 17.
@@ -2875,6 +2886,8 @@
                 line := ''
             ] ifFalse:[
                 startOffset := ((blockNr * 16) + lineNrInBlock) * 16.
+
+                "/ two streams; one for the hex bytes, one for the ascii-interpretation
                 lineStream := String writeStream.
                 asciiLineStream := String writeStream.
 
@@ -2885,90 +2898,80 @@
 
                     (startOffset + i) > data size ifTrue:[
                         asciiLineStream nextPut:(Character space).
-                        lineStream nextPutAll:'  '.
+                        lineStream spaces:colsPerByte.
                     ] ifFalse:[
                         byte := data at:startOffset + i.
 
-                        lineStream nextPutAll:(byte hexPrintString:2).
+                        lineStream nextPutAll:(byte printStringRadix:numberBase padTo:colsPerByte).
+
                         
-                        (byte between:32 and:127) ifTrue:[
-                            asciiLineStream nextPut:(Character value:byte)
-                        ] ifFalse:[
-                            (byte between:0 and:31) ifTrue:[
-                                "/ the 'nul', 'soh' .. 'gs' chars in unicodePage 2400
-                                "/ are perfect here, but usually not available in the font
-                                "/ and we have currently no way of knowing if they are...
-                                "/ (could let the font draw into abitmap and check if there is something...)
-                                "/ therefore, for now, write a dot.·
-                                "/ asciiLineStream nextPut:(Character value:(byte + 16r2400))
-                                asciiLineStream nextPut:replacementForUnprintable.
+                        decodedChar := byte.
+                        characterDecoder notNil ifTrue:[
+                            decodedChar := characterDecoder encode:byte.
+                        ].    
+
+                        charPrinted := replacementForUnprintable.
+                        decodedChar notNil ifTrue:[
+                            ((decodedChar between:32 and:127) or:[(decodedChar between:(128+32) and:(128+127))]) ifTrue:[
+                                charPrinted := Character value:decodedChar.
                             ] ifFalse:[
-                                asciiLineStream nextPut:replacementForUnprintable.
+                                (decodedChar between:0 and:31) ifTrue:[
+                                    "/ the 'nul', 'soh' .. 'gs' chars in unicodePage 2400
+                                    "/ are perfect here, but usually not available in the font
+                                    "/ and we have currently no way of knowing if they are...
+                                    "/ (could let the font draw into a bitmap and check if there is something...)
+                                    "/ For now, write a dot.·
+                                    "/ charPrinted := (Character value:(byte + 16r2400))
+                                ].
                             ].
                         ].
+                        asciiLineStream nextPut:charPrinted.
                     ].
                 ].
-                line := (lineStream contents paddedTo:(addrDigits + 1 + (3*16)+ 8))
+                line := (lineStream contents paddedTo:(addrDigits + 1 + ((colsPerByte+1)*16) + 6))
                         , asciiLineStream contents.
             ].
             line
         ].
     ^ lines.
 
-"/    |offs 
-"/     col line lineStream asciiLineStream lines|
-"/
-"/    col := 1.
-"/    offs := virtualStart.
-"/    lines := StringCollection new.
-"/
-"/    lineStream := String writeStream.
-"/    asciiLineStream := String writeStream.
-"/
-"/    lineStream nextPutAll:(offs hexPrintString:addrDigits).
-"/    lineStream nextPutAll:': '.
-"/
-"/    data do:[:byte |
-"/        lineStream nextPutAll:(byte hexPrintString:2).
-"/        (byte between:32 and:127) ifTrue:[
-"/            asciiLineStream nextPut:(Character value:byte)
-"/        ] ifFalse:[
-"/            asciiLineStream nextPut:$.
-"/        ].
-"/
-"/        offs := offs + 1.
-"/        col := col + 1.
-"/        col > 16 ifTrue:[
-"/            lineStream nextPutAll:'        '.
-"/            lineStream nextPutAll:asciiLineStream contents.
-"/            lines add:(lineStream contents).
-"/            (offs bitAnd:16rFF) == 0 ifTrue:[
-"/                lines add:nil
-"/            ].
-"/            lineStream reset.
-"/            asciiLineStream reset.
-"/
-"/            lineStream nextPutAll:(offs hexPrintString:addrDigits).
-"/            lineStream nextPutAll:': '.
-"/            col := 1.
-"/        ] ifFalse:[
-"/            lineStream space
-"/        ]
-"/    ].
-"/    line := lineStream contents paddedTo:(3*16 + addrDigits + 1).
-"/    lines add:(line , '        ' , asciiLineStream contents).
-"/    ^ lines
+    "Created: / 12-11-2017 / 11:24:29 / cg"
+!
+
+contentsOfBytesAsHexDump:data numberOfAddressDigits:addrDigits addressStart:virtualStart 
+    "utility helper: generate a hexDump with addresses; the character columns at the right show
+     the iso8859-1 characters"
+    
+    ^ self 
+        contentsOfBytesAsDump:data base:16
+        numberOfAddressDigits:addrDigits
+        addressStart:virtualStart
+        characterEncoding:#'isco8859-1'
 
     "Created: / 13-02-2012 / 15:01:46 / cg"
-!
-
-contentsOfFileAsHexDump:f
-    ^ self contentsOfFileAsHexDump:f withLimit:nil lastPart:nil
-!
-
-contentsOfFileAsHexDump:f withLimit:limitOrNil lastPart:showLastPartOrNil
+    "Modified: / 12-11-2017 / 12:07:12 / cg"
+!
+
+contentsOfBytesAsHexDump:data numberOfAddressDigits:addrDigits addressStart:virtualStart characterEncoding:characterEncodingSymbol 
+    "utility helper: generate a hexDump with addresses;
+     characterEncodingSymbol determines how characters are to be shown in the right (character) columns.
+     By default, this is iso8859-1, but supported are also ebcdic and ascii7,
+     to support display of alien encoded binary data."
+    
+    ^ self 
+        contentsOfBytesAsDump:data base:16
+        numberOfAddressDigits:addrDigits
+        addressStart:virtualStart
+        characterEncoding:characterEncodingSymbol
+
+    "Created: / 12-11-2017 / 11:21:44 / cg"
+!
+
+contentsOfFileAsDump:f base:numberBase withLimit:limitOrNil lastPart:showLastPartOrNil characterEncoding:characterEncoding
+    "opens the file, get its contents and generates a dump for it"
+    
     |resources fileName stream data offs 
-     addrDigits col lines answer sizeLimit showLastPart|
+     addrDigits lines answer sizeLimit showLastPart|
 
     resources := self classResources.
 
@@ -3015,18 +3018,54 @@
     ].
     stream close.
 
-    col := 1.
     offs := 0.
     lines := StringCollection new.
 
     addrDigits := ((f fileSize + 1) log:16) truncated + 1.
 
     ^ self 
-        contentsOfBytesAsHexDump:data 
+        contentsOfBytesAsDump:data base:numberBase
         numberOfAddressDigits:addrDigits
         addressStart:0
-
-    "Modified: / 13-02-2012 / 15:01:19 / cg"
+        characterEncoding:characterEncoding
+
+    "Created: / 12-11-2017 / 12:08:10 / cg"
+!
+
+contentsOfFileAsHexDump:f 
+    "opens the file, get its contents and generates a dump for it"
+
+    ^ self 
+        contentsOfFileAsHexDump:f
+        withLimit:nil
+        lastPart:nil
+        characterEncoding:#'iso8859-1'
+
+    "Modified (comment): / 12-11-2017 / 12:09:30 / cg"
+!
+
+contentsOfFileAsHexDump:f withLimit:limitOrNil lastPart:showLastPartOrNil 
+    "opens the file, get its contents and generates a dump for it"
+
+    ^ self 
+        contentsOfFileAsHexDump:f
+        withLimit:limitOrNil
+        lastPart:showLastPartOrNil
+        characterEncoding:#'iso-8859-1'
+
+    "Modified (comment): / 12-11-2017 / 12:09:34 / cg"
+!
+
+contentsOfFileAsHexDump:f withLimit:limitOrNil lastPart:showLastPartOrNil characterEncoding:characterEncoding 
+    "opens the file, get its contents and generates a dump for it"
+
+    ^ self 
+        contentsOfFileAsDump:f base:16
+        withLimit:limitOrNil
+        lastPart:showLastPartOrNil
+        characterEncoding:characterEncoding
+
+    "Created: / 12-11-2017 / 12:02:05 / cg"
 ! !
 
 !AbstractFileBrowser methodsFor:'actions'!
@@ -7410,8 +7449,7 @@
 !
 
 fileHexDump
-
-    | file item|
+    |file item|
 
     file := self firstSelectedFile.
     file notNil ifTrue:[
@@ -7420,6 +7458,8 @@
             applicationNamed:#FileApplicationNoteBook
             ifPresentDo:[:appl | appl openTextEditorWithHexPresentationOn:item].
     ].
+
+    "Modified (format): / 12-11-2017 / 11:19:56 / cg"
 !
 
 fileIn:aFilename