[mercury-users] Filling Mercury strings in C

Ondrej Bojar oboj7042 at ss1000.ms.mff.cuni.cz
Wed Mar 26 00:06:24 AEDT 2003


Fergus Henderson answered the same question of me yesterday :-)

-------- Forwarded message ------
Date: Mon, 24 Mar 2003 15:05:10 +1100
From: Fergus Henderson <fjh at cs.mu.OZ.AU>
To: Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz>

On 23-Mar-2003, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> What is the correct approach to solve this?

In the current Mercury implementation, probably the best approach for this
is to call the macro MR_make_aligned_string_copy(<destination>, <source>).
The parameters both have type "char *"; the first one must be an lvalue.

This can only be called in places where the MR_hp register is valid,
i.e. from within a `pragma foreign_proc' code fragment declared
`will_not_call_mercury'.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.

On Tue, 25 Mar 2003, [iso-8859-2] Petr Nemec wrote:

> Hello people,
>  does anybody know (sure he does :),how to fill the Mercury strings
> in C properly? I use the attached code to read a line and to destroy
> ending \n in C. But it seems the memory for the allocated strings is
> not being freed at all during the computation. Do I allocate right :)
> ?
>
> Thanks
>  Petr
>
> :- pred creadstring(int,string).
> :- mode creadstring(in,out) is semidet.
>
> :-pragma
> c_code(creadstring(Pointer::in,String::out),[will_not_call_mercury],
> "
> char buffer[MAX_LINE];
> char* sDynamic;
>
> if(fgets(buffer,MAX_LINE,(FILE*)Pointer) != NULL)
> {
>  nLength = strlen(buffer);
>  buffer[nLength-1] = '\\0';
>  sDynamic = malloc(nLength);
>  strcpy(sDynamic,buffer);
>  String = sDynamic;
>   SUCCESS_INDICATOR = 1;
> }
> else
>   SUCCESS_INDICATOR = 0;
> ").
>
> ______________________________________________________________________
> Reklama:
> Vyzkousej zabavny test Travelmatch od KLM. Naleznes tak dovolenou svych
> snu a usetrĂ­s 30%! Hraj zde: http://ad2.seznam.cz/redir.cgi?instance=45492%26url=http://klm.mhc.hu/travelmatch/cz/tm_lp_cz_cz.htm
> --------------------------------------------------------------------------
> mercury-users mailing list
> post:  mercury-users at cs.mu.oz.au
> administrative address: owner-mercury-users at cs.mu.oz.au
> unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
> subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
> --------------------------------------------------------------------------
>

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list