@@ -372,17 +372,46 @@ Msgtable[] msgtable =
372372 { " udaSelector" , " selector" },
373373];
374374
375+ // Copied from std.string
376+ immutable (char )* toStringz (in string s) @trusted pure nothrow
377+ {
378+ if (s.length == 0 ) return " " .ptr;
379+
380+ immutable p = s.ptr + s.length;
381+
382+ if ((cast (size_t ) p & 3 ) && * p == 0 )
383+ return &s[0 ];
384+
385+ auto copy = new char [s.length + 1 ];
386+ copy[0 .. s.length] = s[];
387+ copy[s.length] = 0 ;
375388
376- int main ()
389+ return &(cast (immutable (char )* )copy)[0 ];
390+ }
391+
392+ int main (string [] args)
377393{
394+ // If an argument is given it's expected to be the full path of the project
395+ // root directory with a trailing slash. The argument is used when building
396+ // with Dub.
397+ immutable workingDirectory = args.length == 2 ? args[1 ] ~ " src/" : " ./" ;
398+
399+ FILE * openFile (string path)
378400 {
379- auto fp = fopen(" ddmd/id.h" ," wb" );
401+ immutable fullPath = toStringz(workingDirectory ~ path);
402+ auto fp = fopen(fullPath," wb" );
380403 if (! fp)
381404 {
382- printf(" can't open ddmd/id.h \n " );
405+ printf(" can't open %s \n " , fullPath );
383406 exit(EXIT_FAILURE );
384407 }
385408
409+ return fp;
410+ }
411+
412+ {
413+ auto fp = openFile(" ddmd/id.h" );
414+
386415 fprintf(fp, " // File generated by idgen.d\n " );
387416 fprintf(fp, " #ifndef DMD_ID_H\n " );
388417 fprintf(fp, " #define DMD_ID_H 1\n " );
@@ -404,12 +433,7 @@ int main()
404433 }
405434
406435 {
407- auto fp = fopen(" ddmd/id.d" ," wb" );
408- if (! fp)
409- {
410- printf(" can't open ddmd.id.d\n " );
411- exit(EXIT_FAILURE );
412- }
436+ auto fp = openFile(" ddmd/id.d" );
413437
414438 fprintf(fp, " // File generated by idgen.d\n " );
415439 fprintf(fp, " \n " );
0 commit comments