www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use C code in D

reply Dillen Meijboom <info dmeijboom.nl> writes:
Hi there,

I'm learning D for a while because it's really easy to use C-code 
in D.
The problem is that I don't really get how to deal with the data 
structures defined in C in D.

At one time for instance I've tried to get all environment 
variables on a POSIX system. I succeeded but I think it can be 
done way easier.

So my question basically is:
1. How can I learn to use C code in D? Is there any good tutorial 
or other packages that deal with this a lot?
2. Is the way I'm currently doing it okay or is it wrong and do I 
need to do something else?

As a reference, I'm currently using the following D code to get 
all environment variables:

```
import std.array;
import std.conv: to;
import std.string: fromStringz;

extern (C) extern const char** environ;

string[string] getenv() {
	string[string] env;
	char* line;

	for (auto data = cast(char**)environ; (line = *data) != null; 
++data) {
		auto keyval = to!string(fromStringz(line)).split('=');

		env[keyval[0]] = keyval[1];
	}

	return env;
}
```
Mar 23 2017
next sibling parent Laeeth Isharc <laeethnospam nospam.laeeth.com> writes:
On Thursday, 23 March 2017 at 18:10:20 UTC, Dillen Meijboom wrote:
 Hi there,

 I'm learning D for a while because it's really easy to use 
 C-code in D.
 The problem is that I don't really get how to deal with the 
 data structures defined in C in D.

 At one time for instance I've tried to get all environment 
 variables on a POSIX system. I succeeded but I think it can be 
 done way easier.

 So my question basically is:
 1. How can I learn to use C code in D? Is there any good 
 tutorial or other packages that deal with this a lot?
 2. Is the way I'm currently doing it okay or is it wrong and do 
 I need to do something else?

 As a reference, I'm currently using the following D code to get 
 all environment variables:

 ```
 import std.array;
 import std.conv: to;
 import std.string: fromStringz;

 extern (C) extern const char** environ;

 string[string] getenv() {
 	string[string] env;
 	char* line;

 	for (auto data = cast(char**)environ; (line = *data) != null; 
 ++data) {
 		auto keyval = to!string(fromStringz(line)).split('=');

 		env[keyval[0]] = keyval[1];
 	}

 	return env;
 }
 ```
https://dlang.org/phobos/std_process.html#.environment means it's already done for you. C global variables are __gshared. Reading other people's source code - starting with standard library and maybe look at some of the bindings and wrappers on code.dlang.org. IRC chat to get a quick answer most of the time.
Mar 23 2017
prev sibling next sibling parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 23 March 2017 at 18:10:20 UTC, Dillen Meijboom wrote:
 Hi there,

 I'm learning D for a while because it's really easy to use 
 C-code in D.
 The problem is that I don't really get how to deal with the 
 data structures defined in C in D.
D makes it easy to utilize C code, but there is no magic C to D wrapper. There are some functions in Phobos which help (e.g. toStringz). You can slice a C string to utilize it as a slice, but usually there are ownership issue with this approach. In my experience, you'll be writing some C style code to provide a D interface.
Mar 24 2017
prev sibling parent MGW <mgw yandex.ru> writes:
On Thursday, 23 March 2017 at 18:10:20 UTC, Dillen Meijboom wrote:
 Hi there,

 I'm learning D for a while because it's really easy to use 
 C-code in D.
 The problem is that I don't really get how to deal with the 
 data structures defined in C in D.
Perhaps, it will be interesting to you. I advise to look at https://www.youtube.com/watch?v=HTgJaRRfLPk
Mar 24 2017