Make BufferedFile::close public.

This commit is contained in:
Victor Zverovich 2014-05-05 17:38:39 -07:00
parent cada26d226
commit e44a2be8b9
2 changed files with 24 additions and 6 deletions

View File

@ -350,6 +350,21 @@ TEST(BufferedFileTest, CloseErrorInDtor) {
}, FormatSystemErrorMessage(EBADF, "cannot close file") + "\n");
}
TEST(BufferedFileTest, Close) {
BufferedFile f = OpenFile(".travis.yml");
int fd = fileno(f.get());
f.close();
EXPECT_TRUE(f.get() == 0);
EXPECT_TRUE(IsClosed(fd));
}
TEST(BufferedFileTest, CloseError) {
BufferedFile f = OpenFile(".travis.yml");
close(fileno(f.get()));
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
EXPECT_TRUE(f.get() == 0);
}
TEST(FileTest, DefaultCtor) {
File f;
EXPECT_EQ(-1, f.descriptor());
@ -656,7 +671,8 @@ TEST(OutputRedirectTest, ErrorInDtor) {
write_dup.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
}
// TODO: compile both in C++11 & C++98 mode
#endif
// TODO: test retry on EINTR
#endif // FMT_USE_FILE_DESCRIPTORS
} // namespace

View File

@ -114,8 +114,6 @@ class BufferedFile {
explicit BufferedFile(std::FILE *f) : file_(f) {}
void close();
public:
// Constructs a BufferedFile object which doesn't represent any file.
BufferedFile() FMT_NOEXCEPT(true) : file_(0) {}
@ -182,10 +180,14 @@ class BufferedFile {
}
#endif
// Closes the file.
void close();
// Returns the pointer to a FILE object representing this file.
std::FILE *get() const { return file_; }
};
// A file.
// A file. Closed file is represented by a File object with descriptor -1.
// Methods that are not declared with FMT_NOEXCEPT(true) may throw
// fmt::SystemError in case of failure. Note that some errors such as
// closing the file multiple times will cause a crash on Windows rather
@ -277,7 +279,7 @@ class File {
// Returns the file descriptor.
int descriptor() const FMT_NOEXCEPT(true) { return fd_; }
// Closes the file if its descriptor is not -1.
// Closes the file.
void close();
// Attempts to read count bytes from the file into the specified buffer.