Re: Exporting a String

2001-09-28 Thread Marcin 'Qrczak' Kowalczyk

Fri, 28 Sep 2001 08:20:08 +0200, Sven Eric Panitz <[EMAIL PROTECTED]> pisze:

> xs <- peekCString cxs
> ys <- peekCString cys
> erg <- return (xs++ys)

The last line can be written:
  let erg = xs++ys
and you can also directly write (xs++ys) instead of erg below.

> newCString erg

All OK, except that you leak memory. This string is allocated using
malloc and should be freed using free when no longer needed (either
C free or Haskell free).

>//calling Haskell function
>test=cAppend("the world"," is my oyster"); 
> 
>//printing its result
>printf("%s\n", test);

And then: free(test);

BTW, there is less boilerplate (no startupHaskell etc.) when the main
program is in Haskell instead of C (of course sometimes this is not
what we want).

-- 
 __("<  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



Re: Exporting a String

2001-09-27 Thread Sven Eric Panitz


I am no expert in these things, but
as a matter of fact I just did some quite similar and found
the help in the mailing list archive. (at least I did not find
a cookbook for this in the documentation)

My Haskell module:
==

module Mini where
import CString

foreign export stdcall cAppend :: CString -> CString -> IO CString

cAppend :: CString -> CString -> IO CString
cAppend cxs cys
 = do
xs <- peekCString cxs
ys <- peekCString cys
erg <- return (xs++ys)
newCString erg


And my C program Main.c calling this function:
==

#include 
#include "Mini_stub.h"
#include "RtsAPI.h" 

extern void __stginit_Mini ( void ); 

int main(void)
{
   char* bogusFlags[1] = { "\0" }; 
   char* test;   

   //starting up Haskell runtime
   startupHaskell(0, bogusFlags, __stginit_Mini);

   //calling Haskell function
   test=cAppend("the world"," is my oyster"); 

   //printing its result
   printf("%s\n", test);

   //say bye bye to Haskell
   shutdownHaskell();

   return 0;
}


and my way to build this thing:
===
ghc -fglasgow-exts -package text -c Mini.hs
gcc -I. -I'e:/Program Files/ghc/ghc-5.02/include' -I'E:\Program 
Files\ghc\ghc-5.02\include\mingw' -c Main.c
ghc -fglasgow-exts -no-hs-main -package text -static Main.o Mini.o Mini_stub.o -o 
Test.exe




Please some expert correct me, if this is not the right way to
do this.

Sven Eric


-- 
__
Sven Eric Panitz Uhlandstr. 12   D-64297 Darmstadt
Software AG   [EMAIL PROTECTED] (+49)6151-92-1426 
---  when I cannot sing my heart, I can only speak my mind ---


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



Exporting a String

2001-09-27 Thread Juan Ignacio García García

hello,

Is there any way to export a value of type String?
I have tried it as follows

Haskell code
 foreign export f::Int -> IO (StablePtr String)
 f:: Int -> IO (StablePtr String)
 f n = newStablePtr ("hola")

C code
 char *cad;
 cad=f(5);
 printf("%s",cad);

but it does not go (segmentation fault).

Thanks in advance,

J. Ignacio García

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