Changes between Version 19 and Version 20 of HashSourceCodes


Ignore:
Timestamp:
Nov 21, 2009, 8:28:34 AM (15 years ago)
Author:
os
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v19 v20  
    754754
    755755}}}
     756
     757== Haskell ==
     758{{{
     759import IO(bracket)
     760import System.Environment(getArgs)
     761import System.IO(openBinaryFile,hClose,hFileSize,hSeek,IOMode(ReadMode),SeekMode(AbsoluteSeek,SeekFromEnd))
     762import qualified Data.ByteString.Lazy as L(hGet,unpack)
     763import Data.Binary.Get(runGet,getWord64le)
     764import Data.Binary.Put(runPut,putWord64le)
     765import Data.Word(Word64)
     766import Control.Monad(foldM)
     767import Data.Bits.Utils(w82s)
     768import Data.Hex(hex)
     769
     770shortsum :: FilePath -> IO Word64
     771shortsum filename = bracket (openBinaryFile filename ReadMode) hClose $ \h -> do
     772  fs <- hFileSize h
     773  hSeek h AbsoluteSeek 0 ; begin <- L.hGet h chunksize
     774  hSeek h SeekFromEnd (-(toInteger chunksize)) ; end <- L.hGet h chunksize
     775  return $ (flip runGet $ begin) $ chunksum $ (flip runGet $ end) (chunksum . fromInteger $ fs)
     776  where
     777    chunksize = 0x10000
     778    chunksum n = foldM (\a _ -> getWord64le >>= return . (+a)) n [1..(chunksize`div`8)]
     779
     780main :: IO ()
     781main = do
     782  args <- getArgs
     783  let fn = head $ args
     784  p <- shortsum fn
     785  putStrLn $ "The hash of file " ++ fn ++ ": " ++ (hex $ w82s $ reverse (L.unpack $ runPut $ putWord64le p))
     786}}}