Created
September 8, 2011 20:30
-
-
Save CyberShadow/1204580 to your computer and use it in GitHub Desktop.
hvalue - show an integer constant from a C header file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rm hvalue* ; wget --no-check-certificate https://raw.github.com/gist/1204580/hvalue.d ; dmd hvalue.d | |
import std.stdio, std.file, std.process, std.exception; | |
void cleanup() | |
{ | |
try | |
foreach (fn; listDir("", "hvalue-test*")) | |
remove(fn); | |
catch {} | |
} | |
void test(string header, string value) | |
{ | |
std.file.write("hvalue-test.c", | |
`#include <`~header~`> | |
#include <stdio.h> | |
int main() | |
{ | |
printf("%d (0x%x)\n", (unsigned int)`~value~`, (unsigned int)`~value~`); | |
return 0; | |
} | |
`); | |
scope(exit) cleanup(); | |
auto cc = getenv("CC"); | |
if (cc is null) | |
cc = "gcc"; | |
enforce(system(cc ~ " hvalue-test.c -o hvalue-test")==0, "Compilation failed"); | |
version(Windows) | |
enforce(system("hvalue-test")==0, "Execution failed"); | |
else | |
enforce(system("./hvalue-test")==0, "Execution failed"); | |
} | |
int main(string[] args) | |
{ | |
if (args.length != 3) | |
return stderr.writefln("Usage: %s header.h VALUE", args[0]), 1; | |
try | |
test(args[1], args[2]); | |
catch (Exception e) | |
return stderr.writeln(e.msg), 1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment