AbstractFileBrowser.st
changeset 17744 8ce1acaa8fee
parent 17724 87f3d255c4e7
child 17746 7b5d6fc64fee
equal deleted inserted replaced
17743:b8e718389cc7 17744:8ce1acaa8fee
  2847     ^ FileBrowser classResources
  2847     ^ FileBrowser classResources
  2848 ! !
  2848 ! !
  2849 
  2849 
  2850 !AbstractFileBrowser class methodsFor:'utilities'!
  2850 !AbstractFileBrowser class methodsFor:'utilities'!
  2851 
  2851 
  2852 contentsOfBytesAsHexDump:data numberOfAddressDigits:addrDigits addressStart:virtualStart
  2852 contentsOfBytesAsDump:data base:numberBase numberOfAddressDigits:addrDigits addressStart:virtualStart characterEncoding:characterEncodingSymbol
  2853     "utility helper: generate a hexDump with addresses"
  2853     "utility helper: generate a hexDump with addresses;
  2854 
  2854      characterEncodingSymbol determines how characters are to be shown in the right (character) columns.
  2855     |lines nHexLines replacementForUnprintable|
  2855      By default, this is iso8859-1, but supported are also ebcdic and ascii7,
       
  2856      to support display of alien encoded binary data."
       
  2857 
       
  2858     |lines nHexLines replacementForUnprintable colsPerByte characterDecoder|
       
  2859 
       
  2860     (characterEncodingSymbol isNil or:[characterEncodingSymbol == #'iso8859-1']) ifFalse:[
       
  2861         characterDecoder := CharacterEncoder encoderToEncodeFrom:characterEncodingSymbol into:#unicode.
       
  2862     ].    
  2856 
  2863 
  2857     "/ used to be:
  2864     "/ used to be:
  2858     "/ replacementForUnprintable := $.
  2865     "/ replacementForUnprintable := $.
  2859     replacementForUnprintable := Character value:16rB7. "/ a centered dot.
  2866     replacementForUnprintable := Character value:16rB7. "/ a centered dot.
       
  2867 
       
  2868     "/ cols needed per byte in the hex dump
       
  2869     colsPerByte := ((255 printStringRadix:numberBase) size).
  2860 
  2870 
  2861     "generate a virtual collection which evaluates and returns lines on-demand"
  2871     "generate a virtual collection which evaluates and returns lines on-demand"
  2862 
  2872 
  2863     nHexLines := (data size + 15) // 16.
  2873     nHexLines := (data size + 15) // 16.
  2864 
  2874 
  2865     lines := VirtualArray new.
  2875     lines := VirtualArray new.
  2866     lines setSize:nHexLines + (nHexLines // 16).
  2876     lines setSize:nHexLines + (nHexLines // 16).
  2867     lines 
  2877     lines 
  2868         generator:[:lineNr |
  2878         generator:[:lineNr |
  2869             |blockNr lineNrInBlock startOffset lineStream asciiLineStream byte line|
  2879             |blockNr lineNrInBlock startOffset lineStream asciiLineStream byte line
       
  2880              decodedChar charPrinted|
  2870 
  2881 
  2871             blockNr := (lineNr - 1) // 17.
  2882             blockNr := (lineNr - 1) // 17.
  2872             lineNrInBlock := (lineNr - 1) \\ 17.
  2883             lineNrInBlock := (lineNr - 1) \\ 17.
  2873 
  2884 
  2874             lineNrInBlock == 16 ifTrue:[
  2885             lineNrInBlock == 16 ifTrue:[
  2875                 line := ''
  2886                 line := ''
  2876             ] ifFalse:[
  2887             ] ifFalse:[
  2877                 startOffset := ((blockNr * 16) + lineNrInBlock) * 16.
  2888                 startOffset := ((blockNr * 16) + lineNrInBlock) * 16.
       
  2889 
       
  2890                 "/ two streams; one for the hex bytes, one for the ascii-interpretation
  2878                 lineStream := String writeStream.
  2891                 lineStream := String writeStream.
  2879                 asciiLineStream := String writeStream.
  2892                 asciiLineStream := String writeStream.
  2880 
  2893 
  2881                 lineStream nextPutAll:((startOffset+virtualStart) hexPrintString:addrDigits).
  2894                 lineStream nextPutAll:((startOffset+virtualStart) hexPrintString:addrDigits).
  2882                 lineStream nextPutAll:': '.
  2895                 lineStream nextPutAll:': '.
  2883                 1 to:16 do:[:i |
  2896                 1 to:16 do:[:i |
  2884                     i ~~ 1 ifTrue:[ lineStream space ].
  2897                     i ~~ 1 ifTrue:[ lineStream space ].
  2885 
  2898 
  2886                     (startOffset + i) > data size ifTrue:[
  2899                     (startOffset + i) > data size ifTrue:[
  2887                         asciiLineStream nextPut:(Character space).
  2900                         asciiLineStream nextPut:(Character space).
  2888                         lineStream nextPutAll:'  '.
  2901                         lineStream spaces:colsPerByte.
  2889                     ] ifFalse:[
  2902                     ] ifFalse:[
  2890                         byte := data at:startOffset + i.
  2903                         byte := data at:startOffset + i.
  2891 
  2904 
  2892                         lineStream nextPutAll:(byte hexPrintString:2).
  2905                         lineStream nextPutAll:(byte printStringRadix:numberBase padTo:colsPerByte).
       
  2906 
  2893                         
  2907                         
  2894                         (byte between:32 and:127) ifTrue:[
  2908                         decodedChar := byte.
  2895                             asciiLineStream nextPut:(Character value:byte)
  2909                         characterDecoder notNil ifTrue:[
  2896                         ] ifFalse:[
  2910                             decodedChar := characterDecoder encode:byte.
  2897                             (byte between:0 and:31) ifTrue:[
  2911                         ].    
  2898                                 "/ the 'nul', 'soh' .. 'gs' chars in unicodePage 2400
  2912 
  2899                                 "/ are perfect here, but usually not available in the font
  2913                         charPrinted := replacementForUnprintable.
  2900                                 "/ and we have currently no way of knowing if they are...
  2914                         decodedChar notNil ifTrue:[
  2901                                 "/ (could let the font draw into abitmap and check if there is something...)
  2915                             ((decodedChar between:32 and:127) or:[(decodedChar between:(128+32) and:(128+127))]) ifTrue:[
  2902                                 "/ therefore, for now, write a dot.·
  2916                                 charPrinted := Character value:decodedChar.
  2903                                 "/ asciiLineStream nextPut:(Character value:(byte + 16r2400))
       
  2904                                 asciiLineStream nextPut:replacementForUnprintable.
       
  2905                             ] ifFalse:[
  2917                             ] ifFalse:[
  2906                                 asciiLineStream nextPut:replacementForUnprintable.
  2918                                 (decodedChar between:0 and:31) ifTrue:[
       
  2919                                     "/ the 'nul', 'soh' .. 'gs' chars in unicodePage 2400
       
  2920                                     "/ are perfect here, but usually not available in the font
       
  2921                                     "/ and we have currently no way of knowing if they are...
       
  2922                                     "/ (could let the font draw into a bitmap and check if there is something...)
       
  2923                                     "/ For now, write a dot.·
       
  2924                                     "/ charPrinted := (Character value:(byte + 16r2400))
       
  2925                                 ].
  2907                             ].
  2926                             ].
  2908                         ].
  2927                         ].
       
  2928                         asciiLineStream nextPut:charPrinted.
  2909                     ].
  2929                     ].
  2910                 ].
  2930                 ].
  2911                 line := (lineStream contents paddedTo:(addrDigits + 1 + (3*16)+ 8))
  2931                 line := (lineStream contents paddedTo:(addrDigits + 1 + ((colsPerByte+1)*16) + 6))
  2912                         , asciiLineStream contents.
  2932                         , asciiLineStream contents.
  2913             ].
  2933             ].
  2914             line
  2934             line
  2915         ].
  2935         ].
  2916     ^ lines.
  2936     ^ lines.
  2917 
  2937 
  2918 "/    |offs 
  2938     "Created: / 12-11-2017 / 11:24:29 / cg"
  2919 "/     col line lineStream asciiLineStream lines|
  2939 !
  2920 "/
  2940 
  2921 "/    col := 1.
  2941 contentsOfBytesAsHexDump:data numberOfAddressDigits:addrDigits addressStart:virtualStart 
  2922 "/    offs := virtualStart.
  2942     "utility helper: generate a hexDump with addresses; the character columns at the right show
  2923 "/    lines := StringCollection new.
  2943      the iso8859-1 characters"
  2924 "/
  2944     
  2925 "/    lineStream := String writeStream.
  2945     ^ self 
  2926 "/    asciiLineStream := String writeStream.
  2946         contentsOfBytesAsDump:data base:16
  2927 "/
  2947         numberOfAddressDigits:addrDigits
  2928 "/    lineStream nextPutAll:(offs hexPrintString:addrDigits).
  2948         addressStart:virtualStart
  2929 "/    lineStream nextPutAll:': '.
  2949         characterEncoding:#'isco8859-1'
  2930 "/
       
  2931 "/    data do:[:byte |
       
  2932 "/        lineStream nextPutAll:(byte hexPrintString:2).
       
  2933 "/        (byte between:32 and:127) ifTrue:[
       
  2934 "/            asciiLineStream nextPut:(Character value:byte)
       
  2935 "/        ] ifFalse:[
       
  2936 "/            asciiLineStream nextPut:$.
       
  2937 "/        ].
       
  2938 "/
       
  2939 "/        offs := offs + 1.
       
  2940 "/        col := col + 1.
       
  2941 "/        col > 16 ifTrue:[
       
  2942 "/            lineStream nextPutAll:'        '.
       
  2943 "/            lineStream nextPutAll:asciiLineStream contents.
       
  2944 "/            lines add:(lineStream contents).
       
  2945 "/            (offs bitAnd:16rFF) == 0 ifTrue:[
       
  2946 "/                lines add:nil
       
  2947 "/            ].
       
  2948 "/            lineStream reset.
       
  2949 "/            asciiLineStream reset.
       
  2950 "/
       
  2951 "/            lineStream nextPutAll:(offs hexPrintString:addrDigits).
       
  2952 "/            lineStream nextPutAll:': '.
       
  2953 "/            col := 1.
       
  2954 "/        ] ifFalse:[
       
  2955 "/            lineStream space
       
  2956 "/        ]
       
  2957 "/    ].
       
  2958 "/    line := lineStream contents paddedTo:(3*16 + addrDigits + 1).
       
  2959 "/    lines add:(line , '        ' , asciiLineStream contents).
       
  2960 "/    ^ lines
       
  2961 
  2950 
  2962     "Created: / 13-02-2012 / 15:01:46 / cg"
  2951     "Created: / 13-02-2012 / 15:01:46 / cg"
  2963 !
  2952     "Modified: / 12-11-2017 / 12:07:12 / cg"
  2964 
  2953 !
  2965 contentsOfFileAsHexDump:f
  2954 
  2966     ^ self contentsOfFileAsHexDump:f withLimit:nil lastPart:nil
  2955 contentsOfBytesAsHexDump:data numberOfAddressDigits:addrDigits addressStart:virtualStart characterEncoding:characterEncodingSymbol 
  2967 !
  2956     "utility helper: generate a hexDump with addresses;
  2968 
  2957      characterEncodingSymbol determines how characters are to be shown in the right (character) columns.
  2969 contentsOfFileAsHexDump:f withLimit:limitOrNil lastPart:showLastPartOrNil
  2958      By default, this is iso8859-1, but supported are also ebcdic and ascii7,
       
  2959      to support display of alien encoded binary data."
       
  2960     
       
  2961     ^ self 
       
  2962         contentsOfBytesAsDump:data base:16
       
  2963         numberOfAddressDigits:addrDigits
       
  2964         addressStart:virtualStart
       
  2965         characterEncoding:characterEncodingSymbol
       
  2966 
       
  2967     "Created: / 12-11-2017 / 11:21:44 / cg"
       
  2968 !
       
  2969 
       
  2970 contentsOfFileAsDump:f base:numberBase withLimit:limitOrNil lastPart:showLastPartOrNil characterEncoding:characterEncoding
       
  2971     "opens the file, get its contents and generates a dump for it"
       
  2972     
  2970     |resources fileName stream data offs 
  2973     |resources fileName stream data offs 
  2971      addrDigits col lines answer sizeLimit showLastPart|
  2974      addrDigits lines answer sizeLimit showLastPart|
  2972 
  2975 
  2973     resources := self classResources.
  2976     resources := self classResources.
  2974 
  2977 
  2975     fileName := f baseName.
  2978     fileName := f baseName.
  2976     f isDirectory ifTrue:[
  2979     f isDirectory ifTrue:[
  3013     ] ifFalse:[
  3016     ] ifFalse:[
  3014         data := stream contents.
  3017         data := stream contents.
  3015     ].
  3018     ].
  3016     stream close.
  3019     stream close.
  3017 
  3020 
  3018     col := 1.
       
  3019     offs := 0.
  3021     offs := 0.
  3020     lines := StringCollection new.
  3022     lines := StringCollection new.
  3021 
  3023 
  3022     addrDigits := ((f fileSize + 1) log:16) truncated + 1.
  3024     addrDigits := ((f fileSize + 1) log:16) truncated + 1.
  3023 
  3025 
  3024     ^ self 
  3026     ^ self 
  3025         contentsOfBytesAsHexDump:data 
  3027         contentsOfBytesAsDump:data base:numberBase
  3026         numberOfAddressDigits:addrDigits
  3028         numberOfAddressDigits:addrDigits
  3027         addressStart:0
  3029         addressStart:0
  3028 
  3030         characterEncoding:characterEncoding
  3029     "Modified: / 13-02-2012 / 15:01:19 / cg"
  3031 
       
  3032     "Created: / 12-11-2017 / 12:08:10 / cg"
       
  3033 !
       
  3034 
       
  3035 contentsOfFileAsHexDump:f 
       
  3036     "opens the file, get its contents and generates a dump for it"
       
  3037 
       
  3038     ^ self 
       
  3039         contentsOfFileAsHexDump:f
       
  3040         withLimit:nil
       
  3041         lastPart:nil
       
  3042         characterEncoding:#'iso8859-1'
       
  3043 
       
  3044     "Modified (comment): / 12-11-2017 / 12:09:30 / cg"
       
  3045 !
       
  3046 
       
  3047 contentsOfFileAsHexDump:f withLimit:limitOrNil lastPart:showLastPartOrNil 
       
  3048     "opens the file, get its contents and generates a dump for it"
       
  3049 
       
  3050     ^ self 
       
  3051         contentsOfFileAsHexDump:f
       
  3052         withLimit:limitOrNil
       
  3053         lastPart:showLastPartOrNil
       
  3054         characterEncoding:#'iso-8859-1'
       
  3055 
       
  3056     "Modified (comment): / 12-11-2017 / 12:09:34 / cg"
       
  3057 !
       
  3058 
       
  3059 contentsOfFileAsHexDump:f withLimit:limitOrNil lastPart:showLastPartOrNil characterEncoding:characterEncoding 
       
  3060     "opens the file, get its contents and generates a dump for it"
       
  3061 
       
  3062     ^ self 
       
  3063         contentsOfFileAsDump:f base:16
       
  3064         withLimit:limitOrNil
       
  3065         lastPart:showLastPartOrNil
       
  3066         characterEncoding:characterEncoding
       
  3067 
       
  3068     "Created: / 12-11-2017 / 12:02:05 / cg"
  3030 ! !
  3069 ! !
  3031 
  3070 
  3032 !AbstractFileBrowser methodsFor:'actions'!
  3071 !AbstractFileBrowser methodsFor:'actions'!
  3033 
  3072 
  3034 askForCommandFor:fileName thenDo:aBlock
  3073 askForCommandFor:fileName thenDo:aBlock
  7408 
  7447 
  7409     "Modified: / 25-07-2006 / 09:07:25 / cg"
  7448     "Modified: / 25-07-2006 / 09:07:25 / cg"
  7410 !
  7449 !
  7411 
  7450 
  7412 fileHexDump
  7451 fileHexDump
  7413 
  7452     |file item|
  7414     | file item|
       
  7415 
  7453 
  7416     file := self firstSelectedFile.
  7454     file := self firstSelectedFile.
  7417     file notNil ifTrue:[
  7455     file notNil ifTrue:[
  7418         item := DirectoryContentsBrowser itemClass fileName:file.
  7456         item := DirectoryContentsBrowser itemClass fileName:file.
  7419         self 
  7457         self 
  7420             applicationNamed:#FileApplicationNoteBook
  7458             applicationNamed:#FileApplicationNoteBook
  7421             ifPresentDo:[:appl | appl openTextEditorWithHexPresentationOn:item].
  7459             ifPresentDo:[:appl | appl openTextEditorWithHexPresentationOn:item].
  7422     ].
  7460     ].
       
  7461 
       
  7462     "Modified (format): / 12-11-2017 / 11:19:56 / cg"
  7423 !
  7463 !
  7424 
  7464 
  7425 fileIn:aFilename
  7465 fileIn:aFilename
  7426     "fileIn a file"
  7466     "fileIn a file"
  7427 
  7467