Skip to content

Commit 1b0c115

Browse files
author
Daniel Kroening
committed
modernize file copying in c_preprocess
1 parent 2770756 commit 1b0c115

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

src/ansi-c/c_preprocess.cpp

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,22 @@ bool c_preprocess(
374374
std::ostream &outstream,
375375
message_handlert &message_handler)
376376
{
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");
379378

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+
}
383387

384-
fclose(tmp);
388+
tmp << instream.rdbuf(); // copy
385389

386-
bool result=c_preprocess(file, outstream, message_handler);
390+
tmp.close(); // flush
387391

388-
unlink(file.c_str());
392+
bool result=c_preprocess(tmp_file(), outstream, message_handler);
389393

390394
return result;
391395
}
@@ -553,25 +557,21 @@ bool c_preprocess_visual_studio(
553557
// that's why we use system()
554558
int result=system(command.c_str());
555559

556-
FILE *stream=fopen(tmpi.c_str(), "r");
560+
std::ifstream instream(tmpi);
557561

558-
if(stream==NULL)
562+
if(!instream)
559563
{
560564
unlink(tmpi.c_str());
561565
unlink(stderr_file.c_str());
562566
unlink(command_file_name.c_str());
563-
message.error() << "CL Preprocessing failed (fopen failed)"
567+
message.error() << "CL Preprocessing failed (open failed)"
564568
<< messaget::eom;
565569
return true;
566570
}
567571

568-
{
569-
int ch;
570-
while((ch=fgetc(stream))!=EOF)
571-
outstream << (unsigned char)ch;
572-
}
572+
outstream << instream.rdbuf(); // copy
573573

574-
fclose(stream);
574+
instream.close();
575575
unlink(tmpi.c_str());
576576
unlink(command_file_name.c_str());
577577

@@ -998,27 +998,24 @@ bool c_preprocess_gcc_clang(
998998
// that's why we use system() and a temporary file
999999
result=system(command.c_str());
10001000

1001-
FILE *stream=fopen(tmpi.c_str(), "r");
1001+
std::ifstream instream(tmpi);
10021002

10031003
// errors/warnings
10041004
std::ifstream stderr_stream(stderr_file);
10051005
error_parse(stderr_stream, result==0, message);
10061006

10071007
unlink(stderr_file.c_str());
10081008

1009-
if(stream!=NULL)
1009+
if(instream)
10101010
{
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();
10161013
unlink(tmpi.c_str());
10171014
}
10181015
else
10191016
{
10201017
unlink(tmpi.c_str());
1021-
message.error() << "GCC preprocessing failed (fopen failed)"
1018+
message.error() << "GCC preprocessing failed (open failed)"
10221019
<< messaget::eom;
10231020
result=1;
10241021
}
@@ -1145,15 +1142,12 @@ bool c_preprocess_arm(
11451142
// that's why we use system() and a temporary file
11461143
result=system(command.c_str());
11471144

1148-
FILE *stream=fopen(tmpi.c_str(), "r");
1145+
std::ifstream instream(tmpi);
11491146

1150-
if(stream!=NULL)
1147+
if(!instream)
11511148
{
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();
11571151
unlink(tmpi.c_str());
11581152
}
11591153
else

0 commit comments

Comments
 (0)