@@ -374,18 +374,22 @@ bool c_preprocess(
374
374
std::ostream &outstream,
375
375
message_handlert &message_handler)
376
376
{
377
- std::string file=get_temporary_file (" tmp.stdin" , " .c" );
378
- FILE *tmp=fopen (file.c_str (), " wt" );
377
+ temporary_filet tmp_file (" tmp.stdin" , " .c" );
379
378
380
- char ch;
381
- while (instream.read (&ch, 1 ))
382
- fputc (ch, tmp);
379
+ std::ofstream tmp (tmp_file ());
380
+
381
+ if (!tmp)
382
+ {
383
+ messaget message (message_handler);
384
+ message.error () << " failed to open temporary file" << messaget::eom;
385
+ return true ; // error
386
+ }
383
387
384
- fclose ( tmp);
388
+ tmp << instream. rdbuf (); // copy
385
389
386
- bool result= c_preprocess (file, outstream, message_handler);
390
+ tmp. close (); // flush
387
391
388
- unlink (file. c_str () );
392
+ bool result= c_preprocess ( tmp_file (), outstream, message_handler );
389
393
390
394
return result;
391
395
}
@@ -553,25 +557,21 @@ bool c_preprocess_visual_studio(
553
557
// that's why we use system()
554
558
int result=system (command.c_str ());
555
559
556
- FILE *stream= fopen (tmpi. c_str (), " r " );
560
+ std::ifstream instream (tmpi);
557
561
558
- if (stream== NULL )
562
+ if (!instream )
559
563
{
560
564
unlink (tmpi.c_str ());
561
565
unlink (stderr_file.c_str ());
562
566
unlink (command_file_name.c_str ());
563
- message.error () << " CL Preprocessing failed (fopen failed)"
567
+ message.error () << " CL Preprocessing failed (open failed)"
564
568
<< messaget::eom;
565
569
return true ;
566
570
}
567
571
568
- {
569
- int ch;
570
- while ((ch=fgetc (stream))!=EOF)
571
- outstream << (unsigned char )ch;
572
- }
572
+ outstream << instream.rdbuf (); // copy
573
573
574
- fclose (stream );
574
+ instream. close ( );
575
575
unlink (tmpi.c_str ());
576
576
unlink (command_file_name.c_str ());
577
577
@@ -998,27 +998,24 @@ bool c_preprocess_gcc_clang(
998
998
// that's why we use system() and a temporary file
999
999
result=system (command.c_str ());
1000
1000
1001
- FILE *stream= fopen (tmpi. c_str (), " r " );
1001
+ std::ifstream instream (tmpi);
1002
1002
1003
1003
// errors/warnings
1004
1004
std::ifstream stderr_stream (stderr_file);
1005
1005
error_parse (stderr_stream, result==0 , message);
1006
1006
1007
1007
unlink (stderr_file.c_str ());
1008
1008
1009
- if (stream!= NULL )
1009
+ if (instream )
1010
1010
{
1011
- int ch;
1012
- while ((ch=fgetc (stream))!=EOF)
1013
- outstream << (unsigned char )ch;
1014
-
1015
- fclose (stream);
1011
+ outstream << instream.rdbuf ();
1012
+ instream.close ();
1016
1013
unlink (tmpi.c_str ());
1017
1014
}
1018
1015
else
1019
1016
{
1020
1017
unlink (tmpi.c_str ());
1021
- message.error () << " GCC preprocessing failed (fopen failed)"
1018
+ message.error () << " GCC preprocessing failed (open failed)"
1022
1019
<< messaget::eom;
1023
1020
result=1 ;
1024
1021
}
@@ -1145,15 +1142,12 @@ bool c_preprocess_arm(
1145
1142
// that's why we use system() and a temporary file
1146
1143
result=system (command.c_str ());
1147
1144
1148
- FILE *stream= fopen (tmpi. c_str (), " r " );
1145
+ std::ifstream instream (tmpi);
1149
1146
1150
- if (stream!= NULL )
1147
+ if (!instream )
1151
1148
{
1152
- int ch;
1153
- while ((ch=fgetc (stream))!=EOF)
1154
- outstream << (unsigned char )ch;
1155
-
1156
- fclose (stream);
1149
+ outstream << instream.rdbuf (); // copy
1150
+ instream.close ();
1157
1151
unlink (tmpi.c_str ());
1158
1152
}
1159
1153
else
0 commit comments