-----------------------------------------------------------------------------
--- $Id: Printf.lhs,v 1.2 2001/02/21 16:24:34 simonmar Exp $
+-- $Id: Printf.lhs,v 1.5 2002/03/14 17:09:46 simonmar Exp $
-- (c) Simon Marlow 1997-2001
-----------------------------------------------------------------------------
> import CString
> import IOExts
> import ByteArray
-> import PrelPack (unpackCString)
> showFloat
> :: Bool -- Always print decimal point
> bUFSIZE = 512 :: Int
> showFloat alt left sign blank zero width prec num =
-> unsafePerformIO ( do
+> unsafePerformIO $ do
+
+#if __GLASGOW_HASKELL__ < 500
+
> buf <- malloc bUFSIZE
-> snprintf buf (fromIntegral bUFSIZE) (packString format) num
+> 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
> -- but that's just too heavyweight.
> 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 "#" ++
> 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 -> Float -> IO ()
+> foreign import unsafe snprintf :: Addr -> CSize -> PackedString -> Double -> IO ()
+
+#else
+
+> foreign import unsafe snprintf :: CString -> CSize -> CString -> Double -> IO ()
+
+#endif