X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=glafp-utils%2Fnofib-analyse%2FPrintf.lhs;h=33b5290e074ca16abd6a547f536424832dfe82a2;hb=21044c2db566270297baef26c0a8d9228e66af7c;hp=1fdc8c9c08dd84281db16bcd0f95c02b2183773f;hpb=b7081c5f00ef3947dbb39f942b4c5edaf524b3b5;p=ghc-hetmet.git diff --git a/glafp-utils/nofib-analyse/Printf.lhs b/glafp-utils/nofib-analyse/Printf.lhs index 1fdc8c9..33b5290 100644 --- a/glafp-utils/nofib-analyse/Printf.lhs +++ b/glafp-utils/nofib-analyse/Printf.lhs @@ -1,13 +1,17 @@ ----------------------------------------------------------------------------- --- $Id: Printf.lhs,v 1.1 1999/11/12 11:54:17 simonmar Exp $ +-- $Id: Printf.lhs,v 1.5 2002/03/14 17:09:46 simonmar Exp $ --- (c) Simon Marlow 1997-1999 +-- (c) Simon Marlow 1997-2001 ----------------------------------------------------------------------------- > module Printf (showFloat, showFloat') where -> import GlaExts -> import PrelPack (unpackCString) +> import Foreign +> import CTypes +> import CTypesISO +> import CString +> import IOExts +> import ByteArray > showFloat > :: Bool -- Always print decimal point @@ -23,18 +27,31 @@ > bUFSIZE = 512 :: Int > showFloat alt left sign blank zero width prec num = -> unsafePerformPrimIO ( do -> buf <- _ccall_ malloc bUFSIZE :: IO Addr -> _ccall_ snprintf buf bUFSIZE format num +> unsafePerformIO $ do + +#if __GLASGOW_HASKELL__ < 500 + +> buf <- malloc bUFSIZE +> snprintf buf (fromIntegral bUFSIZE) (packString format) +> (realToFrac num) > let s = unpackCString buf > length s `seq` -- urk! need to force the string before we > -- free the buffer. A better solution would > -- be to use foreign objects and finalisers, > -- but that's just too heavyweight. -> _ccall_ free buf +> free buf > return s -> ) -> + +#else + +> allocaBytes bUFSIZE $ \buf -> +> withCString format $ \cformat -> do +> snprintf buf (fromIntegral bUFSIZE) cformat +> (realToFrac num) +> peekCString buf + +#endif + > where > format = '%' : > if_bool alt "#" ++ @@ -54,3 +71,14 @@ > if_maybe Nothing f = [] > if_maybe (Just s) f = f s + +#if __GLASGOW_HASKELL__ < 500 + +> type PackedString = ByteArray Int +> foreign import unsafe snprintf :: Addr -> CSize -> PackedString -> Double -> IO () + +#else + +> foreign import unsafe snprintf :: CString -> CSize -> CString -> Double -> IO () + +#endif