d

how to convert a c string to a d string?

拟墨画扇 提交于 2021-02-07 11:46:11
问题 This is so simple I'm embarrassed to ask, but how do you convert a c string to a d string in D2? I've got two use cases. string convert( const(char)* c_str ); string convert( const(char)* c_str, size_t length ); 回答1: Use std.string.toString(char*) (D1/Phobos) or std.conv.to!(string) (D2): // D1 import std.string; ... string s = toString(c_str); // D2 import std.conv; ... string s = to!(string)(c_str); Slice the pointer: string s = c_str[0..len]; (you can't use "length" because it has a

What is the D way to write this?

血红的双手。 提交于 2021-01-27 06:19:27
问题 I wrote this program in C and also in erlang To practice I tried to rewrite in D. A friend also wrote it in D but wrote it differently The steps are simple. Pseudocode: While not end of file: X = Read ulong from file and covert to little endian Y = Read X bytes from file into ubyte array subtract 1 from each byte in Y save Y as an ogg file My D attempt: import std.file, std.stdio, std.bitmanip, std.conv, core.stdc.stdio : fread; void main(){ auto file = File("./sounds.pk", "r+"); auto fp =

Binary file I/O

喜夏-厌秋 提交于 2021-01-26 23:51:54
问题 How to read and write to binary files in D language? In C would be: FILE *fp = fopen("/home/peu/Desktop/bla.bin", "wb"); char x[4] = "RIFF"; fwrite(x, sizeof(char), 4, fp); I found rawWrite at D docs, but I don't know the usage, nor if does what I think. fread is from C: T[] rawRead(T)(T[] buffer); If the file is not opened, throws an exception. Otherwise, calls fread for the file handle and throws on error. rawRead always read in binary mode on Windows. 回答1: rawRead and rawWrite should

Binary file I/O

流过昼夜 提交于 2021-01-26 23:51:35
问题 How to read and write to binary files in D language? In C would be: FILE *fp = fopen("/home/peu/Desktop/bla.bin", "wb"); char x[4] = "RIFF"; fwrite(x, sizeof(char), 4, fp); I found rawWrite at D docs, but I don't know the usage, nor if does what I think. fread is from C: T[] rawRead(T)(T[] buffer); If the file is not opened, throws an exception. Otherwise, calls fread for the file handle and throws on error. rawRead always read in binary mode on Windows. 回答1: rawRead and rawWrite should

Meaning of “scope” in D (for a parameter)

删除回忆录丶 提交于 2020-12-29 09:14:07
问题 What does scope in void foo(scope void* p) { } mean? (I'm not talking about scope(exit) or scope int x = 5; , but about scope as used inside a parameter list.) 回答1: There are 3 uses for scope in D. scope statements. This is when you use scope(success) , scope(failure) , or scope(exit) . The statements in the block that follows are run when exiting the scope that the scope statement is in if no exception is thrown, if an exception is thrown, or regardless of whether an exception is thrown for

Super-weird issue triggering “Segmentation Fault”

柔情痞子 提交于 2020-03-03 04:57:08
问题 I won't go very deep into the issue (the codebase is already thousands of lines and quite complex), so I'll try to miniminise the... "window" to what I've spotted. Here's the routine triggering the "Segmentation Fault" : extern (C) { void* Statements_new() { return cast(void*)(new Statements()); } void Statements_add(Statements s, Statement st) { //writeln("In here"); if (s is null) writeln("StatemenTS are null"); else writeln("not null : "~ typeid(s).name); if (st is null) writeln("statement

How to pass any memory to a function through another function

廉价感情. 提交于 2020-01-25 08:22:06
问题 I'm making an event dispatch system where functions are registered to a handler which then calls them as it sees fit. The delegates are in an associative array of dynamic arrays which maps an integer "key" to a number of delegates. This setup is necessary so that the handler is extendable with other delegate function types. class Handler { void addDelegate(void delegate(...) del, int event) nothrow @safe { _tick[event] ~= del; } void callEvent(int event, ...) { runDelegates(event, _argptr); }

CMake or Waf for D project

江枫思渺然 提交于 2020-01-22 17:34:46
问题 We are looking for adequate build tool for a desktop GUI application to be written in D (using Qt toolkit), consisting of several native libraries, using 3rd party C-lib(s). It has to build on Linux (native development) and Mac as well on Windows. We might adopt Code::Blocks as IDE. Waf already has support for D language, while CMake is just receiving it cmaked2. CMake uses special language, while Waf is pure Python...Otoh, CMake, via CPack, can produce packages in several formats as well as

Call C++(C) from D language

无人久伴 提交于 2020-01-21 01:44:08
问题 How to call C++ function from D program? I still can't understand how to do it. What commands do I need to execute? I use dmd in Fedora. 回答1: Simplest example I can think of, if you're calling C functions: $ cat a.c int f(int a, int b){ return a + b + 42; } $ cat a.di extern (C): int f(int, int); $ cat b.d import std.stdio; import a; void main(){ writeln( f( 100, 1000) ); } $ gcc -c a.c $ dmd b.d a.o $ ./b 1142 $ If you're using shared objects, you could so something like: $ cat sdltest.di

How can get all non-static members of a type?

天涯浪子 提交于 2020-01-15 05:01:09
问题 __traits(allMembers, T) returns both instance and static members. How can I filter out the static members? I'd like this to work for both fields and methods. 回答1: Sure you can do this. D's introspection power is immense, in your case Filter from std.meta is your friend ;-) struct Lion { static maxSpeed = 100; string name; bool isDangerous() { return true; } static bool isAlive(uint meat) { return meat > 100; } } template FilterMembers(alias T, bool filterStatic = true) { import std.meta :