August 10, 2005 Improvement for std.c.stdio / std.c.stdlib | ||||
---|---|---|---|---|
| ||||
I was implementing a POSIX interface for my library, and i discovered this solution to the stdin/errno problem that i find quite elegant: * Like Walter already did for errno, write a little C file that accesses all C globals that are difficult to get otherwise: ##### #include <errno.h> #include <stdio.h> int indigo_getErrno() { return errno; } void indigo_setErrno(int value) { errno = value; } FILE* indigo_getStdin() { return stdin; } FILE* indigo_getStdout() { return stdout; } FILE* indigo_getStderr() { return stderr; } ##### * Link the object into Phobos/Ares, of course. * Provide these declarations in std.c.stdio / std.c.stdlib: ##### extern (C) { private int indigo_getErrno(); private void indigo_setErrno(int value); private FILE* indigo_getStdin(); private FILE* indigo_getStdout(); private FILE* indigo_getStderr(); public alias indigo_getErrno errno; public alias indigo_setErrno errno; public alias indigo_getStdin stdin; public alias indigo_getStdout stdout; public alias indigo_getStderr stderr; } ##### * Now what's the effect of it? You can write things like this: ##### private import std.c.stdio; private import std.c.stdlib; private import std.string; int main() { errno = 5; fprintf(stderr, "Error %i is: %s\n", errno, strerror(errno)); } ##### Just like you would do in C! Ciao uwe |
Copyright © 1999-2021 by the D Language Foundation