Thread overview
Interfacing C programs: Pass D string to C function
Dec 13, 2013
Dfr
Dec 13, 2013
Ithy
Dec 13, 2013
Jonathan M Davis
Dec 13, 2013
Mike Parker
Dec 14, 2013
Jonathan M Davis
December 13, 2013
Hello

I trying to write simple wrapper around pcre and have problem passing strings to it. As i understood, the best way is  std.string.toStringZ.

So, my code look like:

string pattern = "....";
pcre_compile2( toStringz(pattern), options, &errcode, &errmsg, &erroffset, cast(char*)null);

This gives me error:

Error: function pcre_compile2 (char*, int, int*, char**, int*, char*) is not callable using argument types (immutable(char)*, int, int*, char**, int*, char*)

Any ideas for better way to do this task ?
December 13, 2013
On Friday, 13 December 2013 at 08:00:21 UTC, Dfr wrote:
> Hello
>
> I trying to write simple wrapper around pcre and have problem passing strings to it. As i understood, the best way is  std.string.toStringZ.
>
> So, my code look like:
>
> string pattern = "....";
> pcre_compile2( toStringz(pattern), options, &errcode, &errmsg, &erroffset, cast(char*)null);
>
> This gives me error:
>
> Error: function pcre_compile2 (char*, int, int*, char**, int*, char*) is not callable using argument types (immutable(char)*, int, int*, char**, int*, char*)
>
> Any ideas for better way to do this task ?

Try using cast(char*)pattern
December 13, 2013
On Friday, December 13, 2013 09:00:20 Dfr wrote:
> Hello
> 
> I trying to write simple wrapper around pcre and have problem passing strings to it. As i understood, the best way is std.string.toStringZ.
> 
> So, my code look like:
> 
> string pattern = "....";
> pcre_compile2( toStringz(pattern), options, &errcode, &errmsg,
> &erroffset, cast(char*)null);
> 
> This gives me error:
> 
> Error: function pcre_compile2 (char*, int, int*, char**, int*,
> char*) is not callable using argument types (immutable(char)*,
> int, int*, char**, int*, char*)
> 
> Any ideas for better way to do this task ?

The problem is that toStringz returns immutable(char)* (because string is immutable(string)[]), and your function is taking char*. It will work to pass the result of toStringz to a C function which takes const char* (since immutable will implicitly convert to const), but immutable doesn't implicitly convert to mutable, and casting immutable mutable is almost always a bad idea.

The easiest solution would be to use std.utf.toUTFz. e.g.

auto cstr = str.toUTFz!(char*)();

or you could do something like

auto cstr = str.dup.ptr;

In either case, what you're doing is allocating a new char[] which holds the same elements as the original string and getting its ptr property so that you have a char*.

- Jonathan M Davis
December 13, 2013
On 12/13/2013 5:00 PM, Dfr wrote:
> Hello
>
> I trying to write simple wrapper around pcre and have problem passing
> strings to it. As i understood, the best way is std.string.toStringZ.
>
> So, my code look like:
>
> string pattern = "....";
> pcre_compile2( toStringz(pattern), options, &errcode, &errmsg,
> &erroffset, cast(char*)null);
>
> This gives me error:
>
> Error: function pcre_compile2 (char*, int, int*, char**, int*, char*) is
> not callable using argument types (immutable(char)*, int, int*, char**,
> int*, char*)
>
> Any ideas for better way to do this task ?

Declare the function to like so:

pcre* pcre_compile2( const(char)*, int, int*, const(char)**, int*, const(ubyte)*);

This matches the C declaration according to [1]. immutable args can be passed to const params without seeing your error. Also, note the last paramters. In C, it's declared to be const unsigned char* and the documentation suggest it's intended to be treated as a byte array rather than a string. In that case, const(ubyte)* is the appropriate choice. D declarations of C functions should generally match the C version as closely as possible, including use of const, but with intent taken into account as well (e.g. byte array vs string).

[1] https://code.google.com/p/wiimc/source/browse/trunk/libs/pcre/doc/html/pcre_compile2.html?r=423
December 14, 2013
On Friday, December 13, 2013 01:17:41 Jonathan M Davis wrote:
> or you could do something like
> 
> auto cstr = str.dup.ptr;

Wait. That was stupid of me. Don't do this. It'll give you a char*, but it won't be null-terminated. If you need char*, then use toUTFz!(char*) - though from what Mike's saying, it sounds like the problem was really your function prototype, which was supposed to take const char* rather than char*.

- Jonathan M Davis