Guest User

Untitled

a guest
May 19th, 2010
147
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ================ CInterface.hsc =====================
  2. {-# LANGUAGE ForeignFunctionInterface #-}
  3.  
  4. module CInterface where
  5.  
  6. import Foreign.C
  7. import Foreign.Storable
  8. import Foreign.Ptr
  9. import Data.Bits
  10.  
  11. foreign export ccall printInput :: Ptr Input -> IO ()
  12.  
  13.  
  14. data Input = Input { alpha :: Bool
  15. , beta :: Bool } deriving (Eq, Show)
  16.  
  17. -- from http://www.haskell.org/haskellwiki/FFI_cook_book
  18. #let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
  19.  
  20. #def typedef struct {
  21. union {
  22. struct {
  23. unsigned char alpha : 1;
  24. unsigned char beta : 1;
  25. unsigned char reserved : 6;
  26. } baz;
  27. unsigned char baz_all;
  28. };
  29. } input_t;
  30.  
  31.  
  32. instance Storable Input where
  33. alignment _ = #{alignment input_t}
  34. sizeOf _ = #{size input_t}
  35. peek ptr = do
  36. b <- ( #{peek input_t, baz_all} ptr )::IO CUChar
  37. return Input { alpha = testBit b 0
  38. , beta = testBit b 1 }
  39. poke ptr inp = undefined
  40.  
  41.  
  42. printInput :: Ptr Input -> IO ()
  43. printInput pInp = do
  44. inp <- peek pInp
  45. putStrLn $ show inp
  46.  
  47. ================ ctest.c =====================
  48. #include "HsFFI.h"
  49. #include "CInterface_stub.h"
  50. #include "CInterface_hsc.h"
  51. #include "dll_init.h"
  52.  
  53. int main(int argc, char* argv[]) {
  54. sp_init(&argc,&argv);
  55.  
  56. ////////////////////////////////////////
  57. input_t inp = {0};
  58. inp.baz.alpha = 1;
  59. inp.baz.beta = 0;
  60.  
  61. printInput( &inp );
  62. ////////////////////////////////////////
  63.  
  64. sp_exit();
  65. return 0;
  66. }
  67.  
  68. ================ dll_init.h =====================
  69. #pragma once
  70.  
  71. extern void __stginit_CInterface(void);
  72.  
  73. extern void sp_init(int*, char***);
  74. extern void sp_exit(void);
  75.  
  76. ================ dll_init.c =====================
  77. #include "HsFFI.h"
  78. #include "dll_init.h"
  79.  
  80. void sp_init(int* p_argc, char*** p_argv) {
  81. hs_init(p_argc, p_argv);
  82. hs_add_root(__stginit_CInterface);
  83. }
  84.  
  85. void sp_exit(void) {
  86. hs_exit();
  87. }
  88.  
  89. =================== test.cabal =====================
  90. name: test
  91. version: 0.0
  92. cabal-version: >1.2
  93. synopsis: <Project description>
  94. description: <Project description>
  95. category: Codec
  96. license: BSD3
  97. license-file: LICENSE
  98. author: # example, `Fred Bloggs
  99. maintainer: fred@example.net
  100. build-depends: base >=4 && <5
  101. build-type: Simple
  102.  
  103.  
  104. library
  105. build-depends: base >=4 && <5
  106. exposed-modules: CInterface
  107. c-sources: dll_init.c
  108. ghc-options: -threaded
  109.  
  110. ====================== LICENSE ===============
  111.  
  112. ====================== Setup.lhs ==============
  113. #!/usr/bin/env runhaskell
  114. > import Distribution.Simple
  115. > main = defaultMain
RAW Paste Data