-hGetStringBuffer :: Bool -> FilePath -> IO StringBuffer
-hGetStringBuffer expand_tabs fname = do
- (a, read) <- if expand_tabs
- then slurpFileExpandTabs fname
- else slurpFile fname
-
- let (A# a#) = a; (I# read#) = read
-
- -- add sentinel '\NUL'
- _casm_ `` ((char *)%0)[(int)%1]=(char)0; '' (A# a#) (I# (read# -# 1#))
- return (StringBuffer a# read# 0# 0#)
-
-#if __GLASGOW_HASKELL__ < 303
-slurpFile fname =
- openFile fname ReadMode >>= \ hndl ->
- hFileSize hndl >>= \ len ->
- let len_i = fromInteger len in
- -- Allocate an array for system call to store its bytes into.
- -- ToDo: make it robust
--- trace (show ((len_i::Int)+1)) $
- _casm_ `` %r=(char *)malloc(sizeof(char)*(int)%0); '' (len_i::Int) >>= \ arr@(A# a#) ->
- if addr2Int# a# ==# 0# then
- fail (userError ("hGetStringBuffer: Could not allocate "++show len_i ++ " bytes"))
- else
- readHandle hndl >>= \ hndl_ ->
- writeHandle hndl hndl_ >>
- let ptr = filePtr hndl_ in
-#if __GLASGOW_HASKELL__ <= 302
- _ccall_ fread arr (1::Int) len_i (ptr::ForeignObj) >>= \ (I# read#) ->
+hGetStringBuffer :: FilePath -> IO StringBuffer
+hGetStringBuffer fname = do
+ (a, read) <- slurpFileExpandTabs fname
+
+ -- urk! slurpFile gives us a buffer that doesn't have room for
+ -- the sentinel. Assume it has a final newline for now, and overwrite
+ -- that with the sentinel. slurpFileExpandTabs (below) leaves room
+ -- for the sentinel.
+ let (Ptr a#) = a;
+ (I# read#) = read;
+ end# = read# -# 1#
+
+ -- add sentinel '\NUL'
+ writeCharOffPtr a (I# end#) '\0'
+
+ return (StringBuffer a# end# 0# 0#)
+\end{code}
+
+-----------------------------------------------------------------------------
+-- Turn a String into a StringBuffer
+
+\begin{code}
+stringToStringBuffer :: String -> IO StringBuffer
+freeStringBuffer :: StringBuffer -> IO ()
+
+#if __GLASGOW_HASKELL__ >= 502
+stringToStringBuffer str = do
+ let sz@(I# sz#) = length str
+ Ptr a# <- newCString str
+ return (StringBuffer a# sz# 0# 0#)
+
+freeStringBuffer (StringBuffer a# _ _ _) = Foreign.free (Ptr a#)