bindings-common 0.2.2 → 0.2.3
raw patch · 4 files changed
+54/−44 lines, 4 files
Files
- .hg_archival.txt +1/−1
- .hgtags +2/−0
- bindings-common.cabal +8/−5
- src/Bindings.hs +43/−38
.hg_archival.txt view
@@ -1,2 +1,2 @@ repo: db8906263ec6e2f02cd51ed9b583ad96027042e9-node: c2c4a362ae898822a6fcbc3096e88a6586d87d2e+node: a52c81536a1cb35fdee2743e24ad64d166c38722
.hgtags view
@@ -17,3 +17,5 @@ e4b2035d9ac09075748f164694a67bc066c7f5ea libusb-0.0.5 720960ad84b9248da6470fa90908a539c1c439bd posix-0.0.1 c000d3f4d858be75a9714d4cc930458c529876e4 0.2.1+c2c4a362ae898822a6fcbc3096e88a6586d87d2e 0.2.2+dae097e9f947f25b42e3776d67baafbfeb735491 0.2.3
bindings-common.cabal view
@@ -1,12 +1,15 @@ cabal-version: >= 1.2.3 name: bindings-common-homepage: http://bitbucket.org/mauricio/bindings+homepage: http://bitbucket.org/mauricio/bindings-common synopsis:- Low-level library bindings, base package.+ Macros and modules to facilitate writing library+ bindings. description: - Facilities to wrap foreign C libraries. Used as basis in- @bindings-*@ packages.-version: 0.2.2+ This package contains @hsc2hs@ macros and Haskell+ modules that can be used to write C library+ bindings according to a well defined standard. See+ "Bindings" module documentation for details.+version: 0.2.3 license: BSD3 license-file: LICENSE maintainer: Maurício C. Antunes <mauricio.antunes@gmail.com>
src/Bindings.hs view
@@ -10,24 +10,24 @@ module Bindings ( -- * Code facilities-+-- -- | See documentation for module "Bindings.Utilities".-+-- -- * How to wrap a library using this package-+-- -- | If you want to write a comprehensive binding -- to your favorite library, and you want to try -- this package to see if it suits your needs, you -- can look at this documentation and then at the -- source code for "Bindings.C", which tries to wrap -- the full standard C library.-+-- -- * Macros-+-- -- | Starting from version 0.2, package @bindings-common@ -- provides many @hsc2hs@ macros to easy C binding. -- Here we list the most important.-+-- -- [@#bindings_num@] Makes a C value into a Haskell -- name with type @(Num a) => a@. Used mostly to -- copy pre-processor macros. Note that here, as in@@ -37,20 +37,20 @@ -- @ -- #bindings_num MY_MACRO -- @-+-- -- [@#bindings_int@] Like @#bindings_num@, but values -- are typed as @CInt@.-+-- -- [@#bindings_frac@] Like @#bindings_num@, but works -- with floating point numbers. Values will have type -- @(Fractional a) => a@.-+-- -- [@#bindings_function@] Wrap C functions. Usage: -- -- @ -- #bindings_function function_name , CInt -> CString -> IO () -- @-+-- -- [@#bindings_startype , #bindings_stoptype@] Declare a -- Haskell @data@ type after a C type. You can wrap @struct@s, -- @union@s and types named with C @typedef@. Note that@@ -67,7 +67,7 @@ -- when your type is defined with @typedef@. Note that -- the @_@ after @#bindings_stoptype@ is needed since -- @hsc2hs@ doesn't accept macros with no parameters.-+-- -- [@#bindings_field , #bindings_array_field@] Describe fields -- inside types. Supose you have a @struct@ like this: --@@ -101,7 +101,7 @@ -- This is necessary to avoid name clashes since Haskell -- would not allow many types with similar records, as -- is common practice in C.-+-- -- [@#bindings_equivalent_integer@] This gives you a Haskell -- integer type that is the same size as a C type. Usage: --@@ -112,7 +112,7 @@ -- This is actually equivalent to @hsc2hs@ -- @#type@, except that it is safe to use on pointers -- (but not on floating point types).-+-- -- [@#bindings_globalvar@] Wraps a global variable, using -- 'Bindings.Utilities.GlobalVariable'. Usage: --@@ -125,10 +125,10 @@ -- allowed to change its value. When touching it -- using 'Bindings.Utilities.writeGlobalVariable' -- this is invisible to you.--+--+-- -- * Example-+-- -- | We'll take a small piece of C code and wrap it -- using @hsc2hs@ macros available in @bindings-common@. -- Our intention is to show that we can write Haskell@@ -139,12 +139,15 @@ -- opinion, the style shown here is easier to write -- and give results that are more confortable to use -- in Haskell.-+-- -- ** C API-+-- -- | This is a small (artificial, naive and ugly) API -- for UTF-8 coding of characters. Most APIs have better -- design, but we just want to show how to deal with it.+-- In real world, if we wanted, it would obviously be+-- easier to write a UTF-8 handler in Haskell than+-- this interface. -- -- @ -- #define UNICODE_2_UTF8 1@@ -163,11 +166,12 @@ -- number, and then calling @translate@ with @UNICODE_2_UTF8@; -- or filling @eight_bits@ and calling @translate@ with -- @UTF8_2_UNICODE@.-+-- -- ** Haskell low level binding-+-- -- | Now we make use of @hsc2hs@ macros inside Haskell. --+-- @ -- #bindings_num UNICODE_2_UTF8 -- #bindings_num UTF8_2_UNICODE --@@ -178,6 +182,7 @@ -- #bindings_stoptype -- -- #bindings_function translate , CInt -> Ptr Unicode_translator -> IO ()+-- @ -- -- This gives us a set of declarations as below. --@@ -193,9 +198,9 @@ -- -- translate :: CInt -> Ptr Unicode_translator -> IO () -- @---- ** Clean Haskell code-+--+-- ** Cleaner Haskell interface+-- -- | Now we declare a few Haskell utilities that -- better fit Haskell programming. --@@ -209,11 +214,11 @@ -- unicodeToUtf string = liftM concat $ alloca $ \ptrUt -> -- (flip mapM) string $ \char -> do -- ut <- peek ptrUt--- poke ptrUt (ut {unicode_translator'unicode = toChar char})--- translate _UNICODE_2_UTF8 ptrUt+-- poke ptrUt (ut {/unicode_translator'unicode/ = toChar char})+-- /translate _UNICODE_2_UTF8/ ptrUt -- ut <- peek ptrUt--- let nChars = fromIntegral $ unicode_translator'nchars ut--- let eightBits = unicode_translator'eight_bits ut+-- let nChars = fromIntegral $ /unicode_translator'nchars/ ut+-- let eightBits = /unicode_translator'eight_bits/ ut -- return $ (map toChar) $ reverse $ take nChars eightBits -- -- utfToUnicode :: String -> IO String@@ -221,14 +226,14 @@ -- (. (map fromChar)) $ -- (. splitCodes) $ -- mapM $ \c -> do--- let ut = Unicode_translator {--- unicode_translator'nchars = fromIntegral $ length c,--- unicode_translator'eight_bits = reverse $ map fromChar c,--- unicode_translator'unicode = 0+-- let ut = /Unicode_translator/ {+-- /unicode_translator'nchars/ = fromIntegral $ length c,+-- /unicode_translator'eight_bits/ = reverse $ map fromChar c,+-- /unicode_translator'unicode/ = 0 -- }--- unicode <- with ut $ \ptr -> do--- translate _UTF8_2_UNICODE ptr--- liftM unicode_translator'unicode $ peek ptr+-- unicode \<- with ut $ \ptr -\> do+-- /translate _UTF8_2_UNICODE/ ptr+-- liftM /unicode_translator'unicode/ $ peek ptr -- return $ toChar $ unicode -- where -- splitCodes :: [Word8] -> [[Word8]]@@ -237,16 +242,16 @@ -- then -- [a]:(splitCodes t) -- else--- let i = findIndex (\c -> c < 0x80 || c > 0xBF) t+-- let i = findIndex (\c -\> c \< 0x80 || c \> 0xBF) t -- (t1,t2) = maybe ([],t) (flip splitAt t) i -- in (a:t1):(splitCodes t2) -- @ -- -- @unicodeToUtf@ and @utfToUnicode@ now use Haskell -- day-to-day types.-+-- -- ** Better interface-+-- -- | Our functions are effect-free. -- -- @@@ -264,7 +269,7 @@ -- printAsInt s = putStrLn $ show $ map fromEnum s -- -- main = do--- let a = "Exceção"+-- let a = \"Exceção\" -- printAsInt a -- printAsInt $ toUtf8 a -- printAsInt $ fromUtf8 $ toUtf8 a