Re: Converting [Word8] to CString

2001-09-15 Thread Ashley Yakeley

At 2001-09-15 02:15, Marcin 'Qrczak' Kowalczyk wrote:

>Fri, 14 Sep 2001 23:40:42 -0700, Ashley Yakeley <[EMAIL PROTECTED]> pisze:
>
>> I'm looking for a function that will convert a [Word8] byte-array
>> to a CString (i.e. a C byte array) for the purposes of FFI.
>
>You can use newArray which allocates the C byte array using malloc
>so it must be freed by free, and cast the resulting pointer from
>Ptr Word8 to CString using castPtr.

Thank you. I ended up doing this and simply using Ptr Word8 as the FFI 
type.

Curiously, the GHC user's guide sec. 8.2.4.1 seems to suggest that Ptr 
types are illegal as arguments in foreign import function declarations. I 
think this is the source of my confusion.
http://www.haskell.org/ghc/docs/latest/set/sec-primitive.html#SEC-PRIM-TYPE
S


-- 
Ashley Yakeley, Seattle WA


___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



Re: Converting [Word8] to CString

2001-09-15 Thread Marcin 'Qrczak' Kowalczyk

Fri, 14 Sep 2001 23:40:42 -0700, Ashley Yakeley <[EMAIL PROTECTED]> pisze:

> I'm looking for a function that will convert a [Word8] byte-array
> to a CString (i.e. a C byte array) for the purposes of FFI.

You can use newArray which allocates the C byte array using malloc
so it must be freed by free, and cast the resulting pointer from
Ptr Word8 to CString using castPtr.

When the array is temporary, it's better (and faster) to use

withArray list_of_Word8 $ \word8Ptr ->
let cstring = castPtr word8Ptr in
...

> And it has to be the _same_ list of bytes that comes through on
> the native side.

I don't unserstand this.

-- 
 __("<  Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/
  ^^  SYGNATURA ZASTÊPCZA
QRCZAK


___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



Converting [Word8] to CString

2001-09-14 Thread Ashley Yakeley

I'm looking for a function that will convert a [Word8] byte-array to a 
CString (i.e. a C byte array) for the purposes of FFI.

 foreign import "JVMBridge" "JVMBridge_FindClass" rawFindClass ::
  JavaVM -> CString -> IO JVMObjectRef;

 makeCString :: [Word8] -> IO CString;

It has to work correctly with _any_ list of bytes. And it has to be the 
_same_ list of bytes that comes through on the native side.

Note that we are only dealing with bytes here, this has nothing whatever 
to "characters", "text", "charsets", "Unicode", "encodings" or any of 
that nonsense.

If this really can't be done, is there another type I can use? In some 
cases, the external function may call back into Haskell.

-- 
Ashley Yakeley, Seattle WA


___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users