Skip to content

Instantly share code, notes, and snippets.

@CyberShadow
Created September 8, 2011 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CyberShadow/1204580 to your computer and use it in GitHub Desktop.
Save CyberShadow/1204580 to your computer and use it in GitHub Desktop.
hvalue - show an integer constant from a C header file
// 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