module Algebra.Geometric.Polygon.File (readPolygon, writePolygon) where
import Foreign
import Foreign.C
import Algebra.Geometric.Polygon
type CPolygon = Ptr (Polygon)
readPolygon :: FilePath -> Bool -> IO (Maybe Polygon)
readPolygon fileName hole =
alloca $
\cResult ->
do
cFileName <- newCString fileName
mode <- newCString "r"
file <- fopen cFileName mode
let free_ = free cFileName >> free mode
if file == nullPtr
then free_ >> return Nothing
else
do
gpc_read_polygon file (fromBool hole) cResult
fclose file
result <- peek cResult
free_
gpc_free_polygon cResult
return $ Just result
writePolygon :: String -> Bool -> Polygon -> IO Bool
writePolygon fileName hole polygon =
alloca $
\cPolygon ->
do
poke cPolygon polygon
cFileName <- newCString fileName
mode <- newCString "w"
file <- fopen cFileName mode
let free_ = free cFileName >> free mode
if file == nullPtr
then free_ >> return False
else
gpc_write_polygon file (fromBool hole) cPolygon >>
fclose file >>
free_ >> gpc_free_polygon cPolygon >>
return True
foreign import ccall unsafe "Algebra/Geometric/Polygon/File.chs.h fopen"
fopen :: ((Ptr CChar) -> ((Ptr CChar) -> (IO (Ptr ()))))
foreign import ccall unsafe "Algebra/Geometric/Polygon/File.chs.h gpc_read_polygon"
gpc_read_polygon :: ((Ptr ()) -> (CInt -> ((CPolygon) -> (IO ()))))
foreign import ccall unsafe "Algebra/Geometric/Polygon/File.chs.h fclose"
fclose :: ((Ptr ()) -> (IO CInt))
foreign import ccall unsafe "Algebra/Geometric/Polygon/File.chs.h gpc_free_polygon"
gpc_free_polygon :: ((CPolygon) -> (IO ()))
foreign import ccall unsafe "Algebra/Geometric/Polygon/File.chs.h gpc_write_polygon"
gpc_write_polygon :: ((Ptr ()) -> (CInt -> ((CPolygon) -> (IO ()))))