summaryrefslogtreecommitdiff
path: root/src/Storage.hs.old
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2017-01-08 22:46:29 +1100
committerJed Barber <jjbarber@y7mail.com>2017-01-08 22:46:29 +1100
commit1652e49e17e4f4dead4bd23694a2b99a06048023 (patch)
tree1fed8349ae18b5b61946195ae9cf48fbda0f9a05 /src/Storage.hs.old
parent6824f93b9b575622cd13aefa81b800eb56ce0e2f (diff)
Moved nonworking code to the side
Diffstat (limited to 'src/Storage.hs.old')
-rw-r--r--src/Storage.hs.old73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/Storage.hs.old b/src/Storage.hs.old
new file mode 100644
index 0000000..2697c39
--- /dev/null
+++ b/src/Storage.hs.old
@@ -0,0 +1,73 @@
+{-# LANGUAGE ForeignFunctionInterface #-}
+
+module Storage(
+ PrefStorage,
+ createStorage,
+ pokePref,
+ peekPref
+ ) where
+
+
+
+
+import Foreign
+import Foreign.C
+
+
+
+
+foreign import ccall "create_pref_array"
+ c_createPrefArray :: CInt -> CInt -> IO (Ptr PrefArray)
+
+foreign import ccall "free_pref_array"
+ c_freePrefArray :: CInt -> CInt -> Ptr PrefArray -> IO ()
+
+foreign import ccall "wrapper"
+ wrap :: (Ptr PrefArray -> IO ()) -> IO (FunPtr (Ptr PrefArray -> IO ()))
+
+foreign import ccall "poke_pref_array"
+ c_pokePrefArray :: CInt -> CInt -> Ptr PrefArray -> CInt -> CInt -> CInt -> IO ()
+
+foreign import ccall "peek_pref_array"
+ c_peekPrefArray :: CInt -> CInt -> Ptr PrefArray -> CInt -> CInt -> CInt -> IO CInt
+
+
+
+
+newtype PrefArray = PrefArray (Ptr PrefArray)
+
+data PrefStorage = PrefStorage { pointer :: ForeignPtr PrefArray
+ , numBallots :: Int
+ , sizeOfBallot :: Int }
+
+
+
+
+createStorage :: Int -> Int -> IO PrefStorage
+createStorage n s = do
+ x <- c_createPrefArray (fromIntegral n) (fromIntegral s)
+ f <- wrap (c_freePrefArray (fromIntegral n) (fromIntegral s))
+ y <- newForeignPtr f x
+ return (PrefStorage y n s)
+
+
+
+
+pokePref :: PrefStorage -> Int -> Int -> Int -> IO ()
+pokePref p n s r = do
+ let numBal = fromIntegral (numBallots p)
+ sizeBal = fromIntegral (sizeOfBallot p)
+ func a = c_pokePrefArray numBal sizeBal a (fromIntegral n) (fromIntegral s) (fromIntegral r)
+ withForeignPtr (pointer p) func
+
+
+
+
+peekPref :: PrefStorage -> Int -> Int -> Int -> IO Bool
+peekPref p n s r = do
+ let numBal = fromIntegral (numBallots p)
+ sizeBal = fromIntegral (sizeOfBallot p)
+ func a = c_peekPrefArray numBal sizeBal a (fromIntegral n) (fromIntegral s) (fromIntegral r)
+ result <- withForeignPtr (pointer p) func
+ return (result /= 0)
+