uhexdump 0.1 → 0.2
raw patch · 4 files changed
+13/−50 lines, 4 filesdep +bytestring
Dependencies added: bytestring
Files
- FileBytes.hs +0/−33
- Makefile +2/−4
- uhexdump.cabal +2/−2
- uhexdump.hs +9/−11
− FileBytes.hs
@@ -1,33 +0,0 @@-module FileBytes where--import Data.Word (Word8)-import Foreign.Marshal.Array (allocaArray, peekArray, pokeArray)-import System.IO (hFileSize, Handle, hGetBuf, hPutBuf, openBinaryFile, hClose,- IOMode(ReadMode, WriteMode))---- ------------------------------------------------------------------------- supporting utf-8 stuff--- ------------------------------------------------------------------------readFileBytes :: FilePath -> IO [Word8]-readFileBytes f =- do h <- openBinaryFile f ReadMode- hsize <- fromIntegral `fmap` hFileSize h- hGetBytes h hsize--writeFileBytes :: FilePath -> [Word8] -> IO ()-writeFileBytes f ws =- do h <- openBinaryFile f WriteMode- hPutBytes h (length ws) ws- hClose h--hGetBytes :: Handle -> Int -> IO [Word8]-hGetBytes h c = allocaArray c $ \p ->- do c' <- hGetBuf h p c- peekArray c' p--hPutBytes :: Handle -> Int -> [Word8] -> IO ()-hPutBytes h c ws = allocaArray c $ \p ->- do pokeArray p ws- hPutBuf h p c-
Makefile view
@@ -1,4 +1,2 @@-PROGRAMS := hexdump-utf8--$(PROGRAMS): % : %.hs- ghc --make -Wall $@+all:+ runhaskell Setup build
uhexdump.cabal view
@@ -1,5 +1,5 @@ name: uhexdump-version: 0.1+version: 0.2 synopsis: hex dumper for UTF-8 text description: hex dumper for UTF-8 text category: Text@@ -7,7 +7,7 @@ license-file: LICENSE author: Eric Kow maintainer: <E.Y.Kow@brighton.ac.uk>-build-Depends: base, utf8-string+build-Depends: base, bytestring, utf8-string build-type: Simple executable: uhexdump
uhexdump.hs view
@@ -3,25 +3,24 @@ -- TODO -- ---- -- o figure out how to use utf8-string for the decoding with--- errors [this lets us get rid of our FileBytes and UTF8--- modules+-- errors [this lets us get rid of the UTF8 module] -- o colourise badly encoded characters (switch to something -- easier to type than guillmets) -- o fancy flags import Control.Monad (forM_) import Data.Bits+import qualified Data.ByteString as B import Data.Char (isPrint) import Data.List (intersperse, inits) import Data.Word (Word8) import System.Environment (getArgs)-import System.IO (stdin, hFileSize, openBinaryFile, hClose, IOMode(ReadMode))+import System.IO (stdin, openBinaryFile, hClose, IOMode(ReadMode)) import qualified Numeric as N import Prelude hiding (putStrLn, putStr) import System.IO.UTF8 import UTF8 hiding (decode)-import FileBytes main :: IO () main =@@ -30,18 +29,17 @@ [] -> return [stdin] _ -> mapM (\f -> openBinaryFile f ReadMode) args forM_ hs $ \h ->- do hsize <- fromIntegral `fmap` hFileSize h- bs <- hGetBytes h hsize+ do bs <- B.unpack `fmap` B.hGetContents h putStr $ unlines $ map (dump h_size) $ clump c_size $ decode bs hClose h where- h_size = 60+ h_size = 55 c_size = 16 dump :: Int -> [HexChar] -> String dump hex_sz hs =- hex_part ++ padding ++ char_part -- hex_part ++ char_part+ hex_part ++ padding ++ char_part where hex_part = (foldr (.) id $ intersperse (showChar ' ') $@@ -66,9 +64,9 @@ where iter acc [] = reverse acc iter acc cs =- case break toobig (inits cs) of- ([[]],_) -> next 1 -- all too big- (_ ,[]) -> iter (cs:acc) [] -- none too big+ case break toobig (drop 1 $ inits cs) of+ ([],_) -> next 1 -- first too big+ (_,[]) -> iter (cs:acc) [] -- none too big (_,(x:_)) -> next (length x) where next n = iter (take n cs : acc) (drop n cs) toobig x = (sum . intersperse sep . map f) x > l