1616
1717#include < fstream>
1818
19- #if defined(__linux__) || \
20- defined (__FreeBSD_kernel__) || \
21- defined(__GNU__) || \
22- defined(__unix__) || \
23- defined(__CYGWIN__) || \
24- defined(__MACH__)
25- #include < unistd.h>
26- #endif
27-
2819// / quote a string for bash and CMD
2920static std::string shell_quote (const std::string &src)
3021{
@@ -325,11 +316,11 @@ bool c_preprocess_visual_studio(
325316
326317 // use Visual Studio's CL
327318
328- std::string stderr_file= get_temporary_file (" tmp.stderr" , " " );
329- std::string command_file_name= get_temporary_file (" tmp.cl-cmd" , " " );
319+ temporary_filet stderr_file (" tmp.stderr" , " " );
320+ temporary_filet command_file_name (" tmp.cl-cmd" , " " );
330321
331322 {
332- std::ofstream command_file (command_file_name);
323+ std::ofstream command_file (command_file_name () );
333324
334325 // This marks the command file as UTF-8, which Visual Studio
335326 // understands.
@@ -385,23 +376,20 @@ bool c_preprocess_visual_studio(
385376 command_file << shell_quote (file) << " \n " ;
386377 }
387378
388- std::string tmpi= get_temporary_file (" tmp.cl" , " " );
379+ temporary_filet tmpi (" tmp.cl" , " " );
389380
390- std::string command= " CL @\" " + command_file_name+ " \" " ;
391- command+= " > \" " + tmpi+ " \" " ;
392- command+= " 2> \" " + stderr_file+ " \" " ;
381+ std::string command = " CL @\" " + command_file_name () + " \" " ;
382+ command += " > \" " + tmpi () + " \" " ;
383+ command += " 2> \" " + stderr_file () + " \" " ;
393384
394385 // _popen isn't very reliable on WIN32
395386 // that's why we use system()
396387 int result=system (command.c_str ());
397388
398- std::ifstream instream (tmpi);
389+ std::ifstream instream (tmpi () );
399390
400391 if (!instream)
401392 {
402- unlink (tmpi.c_str ());
403- unlink (stderr_file.c_str ());
404- unlink (command_file_name.c_str ());
405393 message.error () << " CL Preprocessing failed (open failed)"
406394 << messaget::eom;
407395 return true ;
@@ -410,15 +398,11 @@ bool c_preprocess_visual_studio(
410398 outstream << instream.rdbuf (); // copy
411399
412400 instream.close ();
413- unlink (tmpi.c_str ());
414- unlink (command_file_name.c_str ());
415401
416402 // errors/warnings
417- std::ifstream stderr_stream (stderr_file);
403+ std::ifstream stderr_stream (stderr_file () );
418404 error_parse (stderr_stream, result==0 , message);
419405
420- unlink (stderr_file.c_str ());
421-
422406 if (result!=0 )
423407 {
424408 message.error () << " CL Preprocessing failed" << messaget::eom;
@@ -477,7 +461,7 @@ bool c_preprocess_codewarrior(
477461 // preprocessing
478462 messaget message (message_handler);
479463
480- std::string stderr_file= get_temporary_file (" tmp.stderr" , " " );
464+ temporary_filet stderr_file (" tmp.stderr" , " " );
481465
482466 std::string command;
483467
@@ -497,37 +481,32 @@ bool c_preprocess_codewarrior(
497481
498482 int result;
499483
500- std::string tmpi= get_temporary_file (" tmp.cl" , " " );
484+ temporary_filet tmpi (" tmp.cl" , " " );
501485 command+=" \" " +file+" \" " ;
502- command+= " -o \" " + tmpi+ " \" " ;
503- command+= " 2> \" " + stderr_file+ " \" " ;
486+ command += " -o \" " + tmpi () + " \" " ;
487+ command += " 2> \" " + stderr_file () + " \" " ;
504488
505489 result=system (command.c_str ());
506490
507- std::ifstream stream_i (tmpi);
491+ std::ifstream stream_i (tmpi () );
508492
509493 if (stream_i)
510494 {
511495 postprocess_codewarrior (stream_i, outstream);
512496
513497 stream_i.close ();
514- unlink (tmpi.c_str ());
515498 }
516499 else
517500 {
518- unlink (tmpi.c_str ());
519- unlink (stderr_file.c_str ());
520501 message.error () << " Preprocessing failed (fopen failed)"
521502 << messaget::eom;
522503 return true ;
523504 }
524505
525506 // errors/warnings
526- std::ifstream stderr_stream (stderr_file);
507+ std::ifstream stderr_stream (stderr_file () );
527508 error_parse (stderr_stream, result==0 , message);
528509
529- unlink (stderr_file.c_str ());
530-
531510 if (result!=0 )
532511 {
533512 message.error () << " Preprocessing failed" << messaget::eom;
@@ -551,7 +530,7 @@ bool c_preprocess_gcc_clang(
551530 // preprocessing
552531 messaget message (message_handler);
553532
554- std::string stderr_file= get_temporary_file (" tmp.stderr" , " " );
533+ temporary_filet stderr_file (" tmp.stderr" , " " );
555534
556535 std::string command;
557536
@@ -651,39 +630,35 @@ bool c_preprocess_gcc_clang(
651630 #endif
652631
653632 #ifdef _WIN32
654- std::string tmpi= get_temporary_file (" tmp.gcc" , " " );
633+ temporary_filet tmpi (" tmp.gcc" , " " );
655634 command+=" \" " +file+" \" " ;
656- command+= " -o \" " + tmpi+ " \" " ;
657- command+= " 2> \" " + stderr_file+ " \" " ;
635+ command += " -o \" " + tmpi () + " \" " ;
636+ command += " 2> \" " + stderr_file () + " \" " ;
658637
659638 // _popen isn't very reliable on WIN32
660639 // that's why we use system() and a temporary file
661640 result=system (command.c_str ());
662641
663- std::ifstream instream (tmpi);
642+ std::ifstream instream (tmpi () );
664643
665644 // errors/warnings
666- std::ifstream stderr_stream (stderr_file);
645+ std::ifstream stderr_stream (stderr_file () );
667646 error_parse (stderr_stream, result==0 , message);
668647
669- unlink (stderr_file.c_str ());
670-
671648 if (instream)
672649 {
673650 outstream << instream.rdbuf ();
674651 instream.close ();
675- unlink (tmpi.c_str ());
676652 }
677653 else
678654 {
679- unlink (tmpi.c_str ());
680655 message.error () << " GCC preprocessing failed (open failed)"
681656 << messaget::eom;
682657 result=1 ;
683658 }
684659 #else
685660 command+=" \" " +file+" \" " ;
686- command+= " 2> \" " + stderr_file+ " \" " ;
661+ command += " 2> \" " + stderr_file () + " \" " ;
687662
688663 FILE *stream=popen (command.c_str (), " r" );
689664
@@ -703,11 +678,9 @@ bool c_preprocess_gcc_clang(
703678 }
704679
705680 // errors/warnings
706- std::ifstream stderr_stream (stderr_file);
681+ std::ifstream stderr_stream (stderr_file () );
707682 error_parse (stderr_stream, result==0 , message);
708683
709- unlink (stderr_file.c_str ());
710-
711684 #endif
712685
713686 if (result!=0 )
@@ -732,7 +705,7 @@ bool c_preprocess_arm(
732705 // preprocessing using armcc
733706 messaget message (message_handler);
734707
735- std::string stderr_file= get_temporary_file (" tmp.stderr" , " " );
708+ temporary_filet stderr_file (" tmp.stderr" , " " );
736709
737710 std::string command;
738711
@@ -770,34 +743,31 @@ bool c_preprocess_arm(
770743 int result;
771744
772745 #ifdef _WIN32
773- std::string tmpi= get_temporary_file (" tmp.cl" , " " );
746+ temporary_filet tmpi (" tmp.cl" , " " );
774747 command+=" \" " +file+" \" " ;
775- command+= " > \" " + tmpi+ " \" " ;
776- command+= " 2> \" " + stderr_file+ " \" " ;
748+ command += " > \" " + tmpi () + " \" " ;
749+ command += " 2> \" " + stderr_file () + " \" " ;
777750
778751 // _popen isn't very reliable on WIN32
779752 // that's why we use system() and a temporary file
780753 result=system (command.c_str ());
781754
782- std::ifstream instream (tmpi);
755+ std::ifstream instream (tmpi () );
783756
784757 if (!instream)
785758 {
786759 outstream << instream.rdbuf (); // copy
787760 instream.close ();
788- unlink (tmpi.c_str ());
789761 }
790762 else
791763 {
792- unlink (tmpi.c_str ());
793- unlink (stderr_file.c_str ());
794764 message.error () << " ARMCC preprocessing failed (fopen failed)"
795765 << messaget::eom;
796766 return true ;
797767 }
798768 #else
799769 command+=" \" " +file+" \" " ;
800- command+= " 2> \" " + stderr_file+ " \" " ;
770+ command += " 2> \" " + stderr_file () + " \" " ;
801771
802772 FILE *stream=popen (command.c_str (), " r" );
803773
@@ -811,19 +781,16 @@ bool c_preprocess_arm(
811781 }
812782 else
813783 {
814- unlink (stderr_file.c_str ());
815784 message.error () << " ARMCC preprocessing failed (popen failed)"
816785 << messaget::eom;
817786 return true ;
818787 }
819788 #endif
820789
821790 // errors/warnings
822- std::ifstream stderr_stream (stderr_file);
791+ std::ifstream stderr_stream (stderr_file () );
823792 error_parse (stderr_stream, result==0 , message);
824793
825- unlink (stderr_file.c_str ());
826-
827794 if (result!=0 )
828795 {
829796 message.error () << " ARMCC preprocessing failed" << messaget::eom;
0 commit comments