mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Improve naming consistency
This commit is contained in:
@ -113,14 +113,14 @@ int safe_strerror(
|
|||||||
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT {
|
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT {
|
||||||
FMT_ASSERT(buffer != FMT_NULL && buffer_size != 0, "invalid buffer");
|
FMT_ASSERT(buffer != FMT_NULL && buffer_size != 0, "invalid buffer");
|
||||||
|
|
||||||
class StrError {
|
class dispatcher {
|
||||||
private:
|
private:
|
||||||
int error_code_;
|
int error_code_;
|
||||||
char *&buffer_;
|
char *&buffer_;
|
||||||
std::size_t buffer_size_;
|
std::size_t buffer_size_;
|
||||||
|
|
||||||
// A noop assignment operator to avoid bogus warnings.
|
// A noop assignment operator to avoid bogus warnings.
|
||||||
void operator=(const StrError &) {}
|
void operator=(const dispatcher &) {}
|
||||||
|
|
||||||
// Handle the result of XSI-compliant version of strerror_r.
|
// Handle the result of XSI-compliant version of strerror_r.
|
||||||
int handle(int result) {
|
int handle(int result) {
|
||||||
@ -157,14 +157,14 @@ int safe_strerror(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StrError(int err_code, char *&buf, std::size_t buf_size)
|
dispatcher(int err_code, char *&buf, std::size_t buf_size)
|
||||||
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
|
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
|
||||||
|
|
||||||
int run() {
|
int run() {
|
||||||
return handle(strerror_r(error_code_, buffer_, buffer_size_));
|
return handle(strerror_r(error_code_, buffer_, buffer_size_));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return StrError(error_code, buffer, buffer_size).run();
|
return dispatcher(error_code, buffer, buffer_size).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
void format_error_code(internal::buffer &out, int error_code,
|
void format_error_code(internal::buffer &out, int error_code,
|
||||||
|
@ -2974,7 +2974,7 @@ FMT_API void report_windows_error(int error_code,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Fast integer formatter. */
|
/** Fast integer formatter. */
|
||||||
class FormatInt {
|
class format_int {
|
||||||
private:
|
private:
|
||||||
// Buffer should be large enough to hold all digits (digits10 + 1),
|
// Buffer should be large enough to hold all digits (digits10 + 1),
|
||||||
// a sign and a null character.
|
// a sign and a null character.
|
||||||
@ -3015,12 +3015,12 @@ class FormatInt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit FormatInt(int value) { format_signed(value); }
|
explicit format_int(int value) { format_signed(value); }
|
||||||
explicit FormatInt(long value) { format_signed(value); }
|
explicit format_int(long value) { format_signed(value); }
|
||||||
explicit FormatInt(long long value) { format_signed(value); }
|
explicit format_int(long long value) { format_signed(value); }
|
||||||
explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
|
explicit format_int(unsigned value) : str_(format_decimal(value)) {}
|
||||||
explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
|
explicit format_int(unsigned long value) : str_(format_decimal(value)) {}
|
||||||
explicit FormatInt(unsigned long long value) : str_(format_decimal(value)) {}
|
explicit format_int(unsigned long long value) : str_(format_decimal(value)) {}
|
||||||
|
|
||||||
/** Returns the number of characters written to the output buffer. */
|
/** Returns the number of characters written to the output buffer. */
|
||||||
std::size_t size() const {
|
std::size_t size() const {
|
||||||
@ -3647,7 +3647,7 @@ FMT_END_NAMESPACE
|
|||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
// A compile-time error because 'd' is an invalid specifier for strings.
|
// A compile-time error because 'd' is an invalid specifier for strings.
|
||||||
std::string s = fmt::format(fmt("{:d}"), "foo");
|
std::string s = format(fmt("{:d}"), "foo");
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
# define fmt(s) FMT_STRING(s)
|
# define fmt(s) FMT_STRING(s)
|
||||||
|
@ -15,7 +15,7 @@ FMT_BEGIN_NAMESPACE
|
|||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
template <class Char>
|
template <class Char>
|
||||||
class FormatBuf : public std::basic_streambuf<Char> {
|
class formatbuf : public std::basic_streambuf<Char> {
|
||||||
private:
|
private:
|
||||||
typedef typename std::basic_streambuf<Char>::int_type int_type;
|
typedef typename std::basic_streambuf<Char>::int_type int_type;
|
||||||
typedef typename std::basic_streambuf<Char>::traits_type traits_type;
|
typedef typename std::basic_streambuf<Char>::traits_type traits_type;
|
||||||
@ -23,7 +23,7 @@ class FormatBuf : public std::basic_streambuf<Char> {
|
|||||||
basic_buffer<Char> &buffer_;
|
basic_buffer<Char> &buffer_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FormatBuf(basic_buffer<Char> &buffer) : buffer_(buffer) {}
|
formatbuf(basic_buffer<Char> &buffer) : buffer_(buffer) {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// The put-area is actually always empty. This makes the implementation
|
// The put-area is actually always empty. This makes the implementation
|
||||||
@ -90,7 +90,7 @@ void write(std::basic_ostream<Char> &os, basic_buffer<Char> &buf) {
|
|||||||
|
|
||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
void format_value(basic_buffer<Char> &buffer, const T &value) {
|
void format_value(basic_buffer<Char> &buffer, const T &value) {
|
||||||
internal::FormatBuf<Char> format_buf(buffer);
|
internal::formatbuf<Char> format_buf(buffer);
|
||||||
std::basic_ostream<Char> output(&format_buf);
|
std::basic_ostream<Char> output(&format_buf);
|
||||||
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
||||||
output << value;
|
output << value;
|
||||||
|
@ -124,20 +124,20 @@ class ErrorCode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// A buffered file.
|
// A buffered file.
|
||||||
class BufferedFile {
|
class buffered_file {
|
||||||
private:
|
private:
|
||||||
FILE *file_;
|
FILE *file_;
|
||||||
|
|
||||||
friend class File;
|
friend class File;
|
||||||
|
|
||||||
explicit BufferedFile(FILE *f) : file_(f) {}
|
explicit buffered_file(FILE *f) : file_(f) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructs a BufferedFile object which doesn't represent any file.
|
// Constructs a buffered_file object which doesn't represent any file.
|
||||||
BufferedFile() FMT_NOEXCEPT : file_(FMT_NULL) {}
|
buffered_file() FMT_NOEXCEPT : file_(FMT_NULL) {}
|
||||||
|
|
||||||
// Destroys the object closing the file it represents if any.
|
// Destroys the object closing the file it represents if any.
|
||||||
FMT_API ~BufferedFile() FMT_DTOR_NOEXCEPT;
|
FMT_API ~buffered_file() FMT_DTOR_NOEXCEPT;
|
||||||
|
|
||||||
#if !FMT_USE_RVALUE_REFERENCES
|
#if !FMT_USE_RVALUE_REFERENCES
|
||||||
// Emulate a move constructor and a move assignment operator if rvalue
|
// Emulate a move constructor and a move assignment operator if rvalue
|
||||||
@ -152,22 +152,22 @@ class BufferedFile {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// A "move constructor" for moving from a temporary.
|
// A "move constructor" for moving from a temporary.
|
||||||
BufferedFile(Proxy p) FMT_NOEXCEPT : file_(p.file) {}
|
buffered_file(Proxy p) FMT_NOEXCEPT : file_(p.file) {}
|
||||||
|
|
||||||
// A "move constructor" for moving from an lvalue.
|
// A "move constructor" for moving from an lvalue.
|
||||||
BufferedFile(BufferedFile &f) FMT_NOEXCEPT : file_(f.file_) {
|
buffered_file(buffered_file &f) FMT_NOEXCEPT : file_(f.file_) {
|
||||||
f.file_ = FMT_NULL;
|
f.file_ = FMT_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A "move assignment operator" for moving from a temporary.
|
// A "move assignment operator" for moving from a temporary.
|
||||||
BufferedFile &operator=(Proxy p) {
|
buffered_file &operator=(Proxy p) {
|
||||||
close();
|
close();
|
||||||
file_ = p.file;
|
file_ = p.file;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A "move assignment operator" for moving from an lvalue.
|
// A "move assignment operator" for moving from an lvalue.
|
||||||
BufferedFile &operator=(BufferedFile &other) {
|
buffered_file &operator=(buffered_file &other) {
|
||||||
close();
|
close();
|
||||||
file_ = other.file_;
|
file_ = other.file_;
|
||||||
other.file_ = FMT_NULL;
|
other.file_ = FMT_NULL;
|
||||||
@ -175,7 +175,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns a proxy object for moving from a temporary:
|
// Returns a proxy object for moving from a temporary:
|
||||||
// BufferedFile file = BufferedFile(...);
|
// buffered_file file = buffered_file(...);
|
||||||
operator Proxy() FMT_NOEXCEPT {
|
operator Proxy() FMT_NOEXCEPT {
|
||||||
Proxy p = {file_};
|
Proxy p = {file_};
|
||||||
file_ = FMT_NULL;
|
file_ = FMT_NULL;
|
||||||
@ -184,14 +184,14 @@ public:
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
private:
|
private:
|
||||||
FMT_DISALLOW_COPY_AND_ASSIGN(BufferedFile);
|
FMT_DISALLOW_COPY_AND_ASSIGN(buffered_file);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_) {
|
buffered_file(buffered_file &&other) FMT_NOEXCEPT : file_(other.file_) {
|
||||||
other.file_ = FMT_NULL;
|
other.file_ = FMT_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedFile& operator=(BufferedFile &&other) {
|
buffered_file& operator=(buffered_file &&other) {
|
||||||
close();
|
close();
|
||||||
file_ = other.file_;
|
file_ = other.file_;
|
||||||
other.file_ = FMT_NULL;
|
other.file_ = FMT_NULL;
|
||||||
@ -200,7 +200,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Opens a file.
|
// Opens a file.
|
||||||
FMT_API BufferedFile(cstring_view filename, cstring_view mode);
|
FMT_API buffered_file(cstring_view filename, cstring_view mode);
|
||||||
|
|
||||||
// Closes the file.
|
// Closes the file.
|
||||||
FMT_API void close();
|
FMT_API void close();
|
||||||
@ -344,9 +344,9 @@ class File {
|
|||||||
// and writing respectively.
|
// and writing respectively.
|
||||||
FMT_API static void pipe(File &read_end, File &write_end);
|
FMT_API static void pipe(File &read_end, File &write_end);
|
||||||
|
|
||||||
// Creates a BufferedFile object associated with this file and detaches
|
// Creates a buffered_file object associated with this file and detaches
|
||||||
// this File object from the file.
|
// this File object from the file.
|
||||||
FMT_API BufferedFile fdopen(const char *mode);
|
FMT_API buffered_file fdopen(const char *mode);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns the memory page size.
|
// Returns the memory page size.
|
||||||
@ -409,7 +409,7 @@ FMT_END_NAMESPACE
|
|||||||
#if !FMT_USE_RVALUE_REFERENCES
|
#if !FMT_USE_RVALUE_REFERENCES
|
||||||
namespace std {
|
namespace std {
|
||||||
// For compatibility with C++98.
|
// For compatibility with C++98.
|
||||||
inline fmt::BufferedFile &move(fmt::BufferedFile &f) { return f; }
|
inline fmt::buffered_file &move(fmt::buffered_file &f) { return f; }
|
||||||
inline fmt::File &move(fmt::File &f) { return f; }
|
inline fmt::File &move(fmt::File &f) { return f; }
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,11 +22,11 @@ inline null<> gmtime_s(...) { return null<>(); }
|
|||||||
|
|
||||||
// Thread-safe replacement for std::localtime
|
// Thread-safe replacement for std::localtime
|
||||||
inline std::tm localtime(std::time_t time) {
|
inline std::tm localtime(std::time_t time) {
|
||||||
struct LocalTime {
|
struct dispatcher {
|
||||||
std::time_t time_;
|
std::time_t time_;
|
||||||
std::tm tm_;
|
std::tm tm_;
|
||||||
|
|
||||||
LocalTime(std::time_t t): time_(t) {}
|
dispatcher(std::time_t t): time_(t) {}
|
||||||
|
|
||||||
bool run() {
|
bool run() {
|
||||||
using namespace fmt::internal;
|
using namespace fmt::internal;
|
||||||
@ -49,7 +49,7 @@ inline std::tm localtime(std::time_t time) {
|
|||||||
return tm != FMT_NULL;
|
return tm != FMT_NULL;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
LocalTime lt(time);
|
dispatcher lt(time);
|
||||||
if (lt.run())
|
if (lt.run())
|
||||||
return lt.tm_;
|
return lt.tm_;
|
||||||
// Too big time values may be unsupported.
|
// Too big time values may be unsupported.
|
||||||
@ -59,11 +59,11 @@ inline std::tm localtime(std::time_t time) {
|
|||||||
|
|
||||||
// Thread-safe replacement for std::gmtime
|
// Thread-safe replacement for std::gmtime
|
||||||
inline std::tm gmtime(std::time_t time) {
|
inline std::tm gmtime(std::time_t time) {
|
||||||
struct GMTime {
|
struct dispatcher {
|
||||||
std::time_t time_;
|
std::time_t time_;
|
||||||
std::tm tm_;
|
std::tm tm_;
|
||||||
|
|
||||||
GMTime(std::time_t t): time_(t) {}
|
dispatcher(std::time_t t): time_(t) {}
|
||||||
|
|
||||||
bool run() {
|
bool run() {
|
||||||
using namespace fmt::internal;
|
using namespace fmt::internal;
|
||||||
@ -85,7 +85,7 @@ inline std::tm gmtime(std::time_t time) {
|
|||||||
return tm != FMT_NULL;
|
return tm != FMT_NULL;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
GMTime gt(time);
|
dispatcher gt(time);
|
||||||
if (gt.run())
|
if (gt.run())
|
||||||
return gt.tm_;
|
return gt.tm_;
|
||||||
// Too big time values may be unsupported.
|
// Too big time values may be unsupported.
|
||||||
@ -94,11 +94,13 @@ inline std::tm gmtime(std::time_t time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
inline std::size_t strftime(char *str, std::size_t count, const char *format, const std::tm *time) {
|
inline std::size_t strftime(char *str, std::size_t count, const char *format,
|
||||||
|
const std::tm *time) {
|
||||||
return std::strftime(str, count, format, time);
|
return std::strftime(str, count, format, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::size_t strftime(wchar_t *str, std::size_t count, const wchar_t *format, const std::tm *time) {
|
inline std::size_t strftime(wchar_t *str, std::size_t count,
|
||||||
|
const wchar_t *format, const std::tm *time) {
|
||||||
return std::wcsftime(str, count, format, time);
|
return std::wcsftime(str, count, format, time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,7 +128,8 @@ struct formatter<std::tm, Char> {
|
|||||||
std::size_t start = buf.size();
|
std::size_t start = buf.size();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
std::size_t size = buf.capacity() - start;
|
std::size_t size = buf.capacity() - start;
|
||||||
std::size_t count = internal::strftime(&buf[start], size, &tm_format[0], &tm);
|
std::size_t count =
|
||||||
|
internal::strftime(&buf[start], size, &tm_format[0], &tm);
|
||||||
if (count != 0) {
|
if (count != 0) {
|
||||||
buf.resize(start + count);
|
buf.resize(start + count);
|
||||||
break;
|
break;
|
||||||
|
12
src/posix.cc
12
src/posix.cc
@ -66,19 +66,19 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; }
|
|||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
BufferedFile::~BufferedFile() FMT_NOEXCEPT {
|
buffered_file::~buffered_file() FMT_NOEXCEPT {
|
||||||
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
|
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
|
||||||
report_system_error(errno, "cannot close file");
|
report_system_error(errno, "cannot close file");
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedFile::BufferedFile(cstring_view filename, cstring_view mode) {
|
buffered_file::buffered_file(cstring_view filename, cstring_view mode) {
|
||||||
FMT_RETRY_VAL(file_,
|
FMT_RETRY_VAL(file_,
|
||||||
FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), FMT_NULL);
|
FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), FMT_NULL);
|
||||||
if (!file_)
|
if (!file_)
|
||||||
FMT_THROW(system_error(errno, "cannot open file {}", filename.c_str()));
|
FMT_THROW(system_error(errno, "cannot open file {}", filename.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BufferedFile::close() {
|
void buffered_file::close() {
|
||||||
if (!file_)
|
if (!file_)
|
||||||
return;
|
return;
|
||||||
int result = FMT_SYSTEM(fclose(file_));
|
int result = FMT_SYSTEM(fclose(file_));
|
||||||
@ -90,7 +90,7 @@ void BufferedFile::close() {
|
|||||||
// A macro used to prevent expansion of fileno on broken versions of MinGW.
|
// A macro used to prevent expansion of fileno on broken versions of MinGW.
|
||||||
#define FMT_ARGS
|
#define FMT_ARGS
|
||||||
|
|
||||||
int BufferedFile::fileno() const {
|
int buffered_file::fileno() const {
|
||||||
int fd = FMT_POSIX_CALL(fileno FMT_ARGS(file_));
|
int fd = FMT_POSIX_CALL(fileno FMT_ARGS(file_));
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
FMT_THROW(system_error(errno, "cannot get file descriptor"));
|
FMT_THROW(system_error(errno, "cannot get file descriptor"));
|
||||||
@ -217,13 +217,13 @@ void File::pipe(File &read_end, File &write_end) {
|
|||||||
write_end = File(fds[1]);
|
write_end = File(fds[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedFile File::fdopen(const char *mode) {
|
buffered_file File::fdopen(const char *mode) {
|
||||||
// Don't retry as fdopen doesn't return EINTR.
|
// Don't retry as fdopen doesn't return EINTR.
|
||||||
FILE *f = FMT_POSIX_CALL(fdopen(fd_, mode));
|
FILE *f = FMT_POSIX_CALL(fdopen(fd_, mode));
|
||||||
if (!f)
|
if (!f)
|
||||||
FMT_THROW(system_error(errno,
|
FMT_THROW(system_error(errno,
|
||||||
"cannot associate stream with file descriptor"));
|
"cannot associate stream with file descriptor"));
|
||||||
BufferedFile file(f);
|
buffered_file file(f);
|
||||||
fd_ = -1;
|
fd_ = -1;
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
@ -1207,23 +1207,23 @@ TEST(FormatterTest, Examples) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatIntTest, Data) {
|
TEST(FormatIntTest, Data) {
|
||||||
fmt::FormatInt format_int(42);
|
fmt::format_int format_int(42);
|
||||||
EXPECT_EQ("42", std::string(format_int.data(), format_int.size()));
|
EXPECT_EQ("42", std::string(format_int.data(), format_int.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatIntTest, FormatInt) {
|
TEST(FormatIntTest, FormatInt) {
|
||||||
EXPECT_EQ("42", fmt::FormatInt(42).str());
|
EXPECT_EQ("42", fmt::format_int(42).str());
|
||||||
EXPECT_EQ(2u, fmt::FormatInt(42).size());
|
EXPECT_EQ(2u, fmt::format_int(42).size());
|
||||||
EXPECT_EQ("-42", fmt::FormatInt(-42).str());
|
EXPECT_EQ("-42", fmt::format_int(-42).str());
|
||||||
EXPECT_EQ(3u, fmt::FormatInt(-42).size());
|
EXPECT_EQ(3u, fmt::format_int(-42).size());
|
||||||
EXPECT_EQ("42", fmt::FormatInt(42ul).str());
|
EXPECT_EQ("42", fmt::format_int(42ul).str());
|
||||||
EXPECT_EQ("-42", fmt::FormatInt(-42l).str());
|
EXPECT_EQ("-42", fmt::format_int(-42l).str());
|
||||||
EXPECT_EQ("42", fmt::FormatInt(42ull).str());
|
EXPECT_EQ("42", fmt::format_int(42ull).str());
|
||||||
EXPECT_EQ("-42", fmt::FormatInt(-42ll).str());
|
EXPECT_EQ("-42", fmt::format_int(-42ll).str());
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << std::numeric_limits<int64_t>::max();
|
os << std::numeric_limits<int64_t>::max();
|
||||||
EXPECT_EQ(os.str(),
|
EXPECT_EQ(os.str(),
|
||||||
fmt::FormatInt(std::numeric_limits<int64_t>::max()).str());
|
fmt::format_int(std::numeric_limits<int64_t>::max()).str());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -306,7 +306,7 @@ TEST(UtilTest, FormatSystemError) {
|
|||||||
|
|
||||||
#if FMT_USE_FILE_DESCRIPTORS
|
#if FMT_USE_FILE_DESCRIPTORS
|
||||||
|
|
||||||
using fmt::BufferedFile;
|
using fmt::buffered_file;
|
||||||
using fmt::ErrorCode;
|
using fmt::ErrorCode;
|
||||||
using fmt::File;
|
using fmt::File;
|
||||||
|
|
||||||
@ -319,7 +319,7 @@ TEST(OutputRedirectTest, ScopedRedirect) {
|
|||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
{
|
{
|
||||||
BufferedFile file(write_end.fdopen("w"));
|
buffered_file file(write_end.fdopen("w"));
|
||||||
std::fprintf(file.get(), "[[[");
|
std::fprintf(file.get(), "[[[");
|
||||||
{
|
{
|
||||||
OutputRedirect redir(file.get());
|
OutputRedirect redir(file.get());
|
||||||
@ -336,7 +336,7 @@ TEST(OutputRedirectTest, FlushErrorInCtor) {
|
|||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
int write_fd = write_end.descriptor();
|
int write_fd = write_end.descriptor();
|
||||||
File write_copy = write_end.dup(write_fd);
|
File write_copy = write_end.dup(write_fd);
|
||||||
BufferedFile f = write_end.fdopen("w");
|
buffered_file f = write_end.fdopen("w");
|
||||||
// Put a character in a file buffer.
|
// Put a character in a file buffer.
|
||||||
EXPECT_EQ('x', fputc('x', f.get()));
|
EXPECT_EQ('x', fputc('x', f.get()));
|
||||||
FMT_POSIX(close(write_fd));
|
FMT_POSIX(close(write_fd));
|
||||||
@ -348,7 +348,7 @@ TEST(OutputRedirectTest, FlushErrorInCtor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(OutputRedirectTest, DupErrorInCtor) {
|
TEST(OutputRedirectTest, DupErrorInCtor) {
|
||||||
BufferedFile f = open_buffered_file();
|
buffered_file f = open_buffered_file();
|
||||||
int fd = (f.fileno)();
|
int fd = (f.fileno)();
|
||||||
File copy = File::dup(fd);
|
File copy = File::dup(fd);
|
||||||
FMT_POSIX(close(fd));
|
FMT_POSIX(close(fd));
|
||||||
@ -361,14 +361,14 @@ TEST(OutputRedirectTest, DupErrorInCtor) {
|
|||||||
TEST(OutputRedirectTest, RestoreAndRead) {
|
TEST(OutputRedirectTest, RestoreAndRead) {
|
||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
BufferedFile file(write_end.fdopen("w"));
|
buffered_file file(write_end.fdopen("w"));
|
||||||
std::fprintf(file.get(), "[[[");
|
std::fprintf(file.get(), "[[[");
|
||||||
OutputRedirect redir(file.get());
|
OutputRedirect redir(file.get());
|
||||||
std::fprintf(file.get(), "censored");
|
std::fprintf(file.get(), "censored");
|
||||||
EXPECT_EQ("censored", sanitize(redir.restore_and_read()));
|
EXPECT_EQ("censored", sanitize(redir.restore_and_read()));
|
||||||
EXPECT_EQ("", sanitize(redir.restore_and_read()));
|
EXPECT_EQ("", sanitize(redir.restore_and_read()));
|
||||||
std::fprintf(file.get(), "]]]");
|
std::fprintf(file.get(), "]]]");
|
||||||
file = BufferedFile();
|
file = buffered_file();
|
||||||
EXPECT_READ(read_end, "[[[]]]");
|
EXPECT_READ(read_end, "[[[]]]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,7 +378,7 @@ TEST(OutputRedirectTest, FlushErrorInRestoreAndRead) {
|
|||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
int write_fd = write_end.descriptor();
|
int write_fd = write_end.descriptor();
|
||||||
File write_copy = write_end.dup(write_fd);
|
File write_copy = write_end.dup(write_fd);
|
||||||
BufferedFile f = write_end.fdopen("w");
|
buffered_file f = write_end.fdopen("w");
|
||||||
OutputRedirect redir(f.get());
|
OutputRedirect redir(f.get());
|
||||||
// Put a character in a file buffer.
|
// Put a character in a file buffer.
|
||||||
EXPECT_EQ('x', fputc('x', f.get()));
|
EXPECT_EQ('x', fputc('x', f.get()));
|
||||||
@ -393,7 +393,7 @@ TEST(OutputRedirectTest, ErrorInDtor) {
|
|||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
int write_fd = write_end.descriptor();
|
int write_fd = write_end.descriptor();
|
||||||
File write_copy = write_end.dup(write_fd);
|
File write_copy = write_end.dup(write_fd);
|
||||||
BufferedFile f = write_end.fdopen("w");
|
buffered_file f = write_end.fdopen("w");
|
||||||
scoped_ptr<OutputRedirect> redir(new OutputRedirect(f.get()));
|
scoped_ptr<OutputRedirect> redir(new OutputRedirect(f.get()));
|
||||||
// Put a character in a file buffer.
|
// Put a character in a file buffer.
|
||||||
EXPECT_EQ('x', fputc('x', f.get()));
|
EXPECT_EQ('x', fputc('x', f.get()));
|
||||||
@ -405,7 +405,7 @@ TEST(OutputRedirectTest, ErrorInDtor) {
|
|||||||
FMT_POSIX(close(write_fd));
|
FMT_POSIX(close(write_fd));
|
||||||
SUPPRESS_ASSERT(redir.reset());
|
SUPPRESS_ASSERT(redir.reset());
|
||||||
}, format_system_error(EBADF, "cannot flush stream"));
|
}, format_system_error(EBADF, "cannot flush stream"));
|
||||||
write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
|
write_copy.dup2(write_fd); // "undo" close or dtor of buffered_file will fail
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
#include "gtest-extra.h"
|
#include "gtest-extra.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
using fmt::BufferedFile;
|
using fmt::buffered_file;
|
||||||
using fmt::ErrorCode;
|
using fmt::ErrorCode;
|
||||||
using fmt::File;
|
using fmt::File;
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ int (test::fileno)(FILE *stream) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void write_file(fmt::cstring_view filename, fmt::string_view content) {
|
void write_file(fmt::cstring_view filename, fmt::string_view content) {
|
||||||
fmt::BufferedFile f(filename, "w");
|
fmt::buffered_file f(filename, "w");
|
||||||
f.print("{}", content);
|
f.print("{}", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,8 +383,8 @@ TEST(FileTest, FdopenNoRetry) {
|
|||||||
|
|
||||||
TEST(BufferedFileTest, OpenRetry) {
|
TEST(BufferedFileTest, OpenRetry) {
|
||||||
write_file("test", "there must be something here");
|
write_file("test", "there must be something here");
|
||||||
scoped_ptr<BufferedFile> f;
|
scoped_ptr<buffered_file> f;
|
||||||
EXPECT_RETRY(f.reset(new BufferedFile("test", "r")),
|
EXPECT_RETRY(f.reset(new buffered_file("test", "r")),
|
||||||
fopen, "cannot open file test");
|
fopen, "cannot open file test");
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
char c = 0;
|
char c = 0;
|
||||||
@ -396,7 +396,7 @@ TEST(BufferedFileTest, OpenRetry) {
|
|||||||
TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
scoped_ptr<BufferedFile> f(new BufferedFile(read_end.fdopen("r")));
|
scoped_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
|
||||||
int saved_fclose_count = 0;
|
int saved_fclose_count = 0;
|
||||||
EXPECT_WRITE(stderr, {
|
EXPECT_WRITE(stderr, {
|
||||||
fclose_count = 1;
|
fclose_count = 1;
|
||||||
@ -410,7 +410,7 @@ TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
|||||||
TEST(BufferedFileTest, CloseNoRetry) {
|
TEST(BufferedFileTest, CloseNoRetry) {
|
||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
BufferedFile f = read_end.fdopen("r");
|
buffered_file f = read_end.fdopen("r");
|
||||||
fclose_count = 1;
|
fclose_count = 1;
|
||||||
EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
|
EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
|
||||||
EXPECT_EQ(2, fclose_count);
|
EXPECT_EQ(2, fclose_count);
|
||||||
@ -420,7 +420,7 @@ TEST(BufferedFileTest, CloseNoRetry) {
|
|||||||
TEST(BufferedFileTest, FilenoNoRetry) {
|
TEST(BufferedFileTest, FilenoNoRetry) {
|
||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
BufferedFile f = read_end.fdopen("r");
|
buffered_file f = read_end.fdopen("r");
|
||||||
fileno_count = 1;
|
fileno_count = 1;
|
||||||
EXPECT_SYSTEM_ERROR((f.fileno)(), EINTR, "cannot get file descriptor");
|
EXPECT_SYSTEM_ERROR((f.fileno)(), EINTR, "cannot get file descriptor");
|
||||||
EXPECT_EQ(2, fileno_count);
|
EXPECT_EQ(2, fileno_count);
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
# undef fileno
|
# undef fileno
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using fmt::BufferedFile;
|
using fmt::buffered_file;
|
||||||
using fmt::ErrorCode;
|
using fmt::ErrorCode;
|
||||||
using fmt::File;
|
using fmt::File;
|
||||||
|
|
||||||
@ -58,32 +58,32 @@ void write(File &f, fmt::string_view s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, DefaultCtor) {
|
TEST(BufferedFileTest, DefaultCtor) {
|
||||||
BufferedFile f;
|
buffered_file f;
|
||||||
EXPECT_TRUE(f.get() == 0);
|
EXPECT_TRUE(f.get() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, MoveCtor) {
|
TEST(BufferedFileTest, MoveCtor) {
|
||||||
BufferedFile bf = open_buffered_file();
|
buffered_file bf = open_buffered_file();
|
||||||
FILE *fp = bf.get();
|
FILE *fp = bf.get();
|
||||||
EXPECT_TRUE(fp != 0);
|
EXPECT_TRUE(fp != 0);
|
||||||
BufferedFile bf2(std::move(bf));
|
buffered_file bf2(std::move(bf));
|
||||||
EXPECT_EQ(fp, bf2.get());
|
EXPECT_EQ(fp, bf2.get());
|
||||||
EXPECT_TRUE(bf.get() == 0);
|
EXPECT_TRUE(bf.get() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, MoveAssignment) {
|
TEST(BufferedFileTest, MoveAssignment) {
|
||||||
BufferedFile bf = open_buffered_file();
|
buffered_file bf = open_buffered_file();
|
||||||
FILE *fp = bf.get();
|
FILE *fp = bf.get();
|
||||||
EXPECT_TRUE(fp != 0);
|
EXPECT_TRUE(fp != 0);
|
||||||
BufferedFile bf2;
|
buffered_file bf2;
|
||||||
bf2 = std::move(bf);
|
bf2 = std::move(bf);
|
||||||
EXPECT_EQ(fp, bf2.get());
|
EXPECT_EQ(fp, bf2.get());
|
||||||
EXPECT_TRUE(bf.get() == 0);
|
EXPECT_TRUE(bf.get() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, MoveAssignmentClosesFile) {
|
TEST(BufferedFileTest, MoveAssignmentClosesFile) {
|
||||||
BufferedFile bf = open_buffered_file();
|
buffered_file bf = open_buffered_file();
|
||||||
BufferedFile bf2 = open_buffered_file();
|
buffered_file bf2 = open_buffered_file();
|
||||||
int old_fd = bf2.fileno();
|
int old_fd = bf2.fileno();
|
||||||
bf2 = std::move(bf);
|
bf2 = std::move(bf);
|
||||||
EXPECT_TRUE(isclosed(old_fd));
|
EXPECT_TRUE(isclosed(old_fd));
|
||||||
@ -91,19 +91,19 @@ TEST(BufferedFileTest, MoveAssignmentClosesFile) {
|
|||||||
|
|
||||||
TEST(BufferedFileTest, MoveFromTemporaryInCtor) {
|
TEST(BufferedFileTest, MoveFromTemporaryInCtor) {
|
||||||
FILE *fp = 0;
|
FILE *fp = 0;
|
||||||
BufferedFile f(open_buffered_file(&fp));
|
buffered_file f(open_buffered_file(&fp));
|
||||||
EXPECT_EQ(fp, f.get());
|
EXPECT_EQ(fp, f.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, MoveFromTemporaryInAssignment) {
|
TEST(BufferedFileTest, MoveFromTemporaryInAssignment) {
|
||||||
FILE *fp = 0;
|
FILE *fp = 0;
|
||||||
BufferedFile f;
|
buffered_file f;
|
||||||
f = open_buffered_file(&fp);
|
f = open_buffered_file(&fp);
|
||||||
EXPECT_EQ(fp, f.get());
|
EXPECT_EQ(fp, f.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
TEST(BufferedFileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
||||||
BufferedFile f = open_buffered_file();
|
buffered_file f = open_buffered_file();
|
||||||
int old_fd = f.fileno();
|
int old_fd = f.fileno();
|
||||||
f = open_buffered_file();
|
f = open_buffered_file();
|
||||||
EXPECT_TRUE(isclosed(old_fd));
|
EXPECT_TRUE(isclosed(old_fd));
|
||||||
@ -112,14 +112,14 @@ TEST(BufferedFileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
|||||||
TEST(BufferedFileTest, CloseFileInDtor) {
|
TEST(BufferedFileTest, CloseFileInDtor) {
|
||||||
int fd = 0;
|
int fd = 0;
|
||||||
{
|
{
|
||||||
BufferedFile f = open_buffered_file();
|
buffered_file f = open_buffered_file();
|
||||||
fd = f.fileno();
|
fd = f.fileno();
|
||||||
}
|
}
|
||||||
EXPECT_TRUE(isclosed(fd));
|
EXPECT_TRUE(isclosed(fd));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, CloseErrorInDtor) {
|
TEST(BufferedFileTest, CloseErrorInDtor) {
|
||||||
scoped_ptr<BufferedFile> f(new BufferedFile(open_buffered_file()));
|
scoped_ptr<buffered_file> f(new buffered_file(open_buffered_file()));
|
||||||
EXPECT_WRITE(stderr, {
|
EXPECT_WRITE(stderr, {
|
||||||
// The close function must be called inside EXPECT_WRITE, otherwise
|
// The close function must be called inside EXPECT_WRITE, otherwise
|
||||||
// the system may recycle closed file descriptor when redirecting the
|
// the system may recycle closed file descriptor when redirecting the
|
||||||
@ -131,7 +131,7 @@ TEST(BufferedFileTest, CloseErrorInDtor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, Close) {
|
TEST(BufferedFileTest, Close) {
|
||||||
BufferedFile f = open_buffered_file();
|
buffered_file f = open_buffered_file();
|
||||||
int fd = f.fileno();
|
int fd = f.fileno();
|
||||||
f.close();
|
f.close();
|
||||||
EXPECT_TRUE(f.get() == 0);
|
EXPECT_TRUE(f.get() == 0);
|
||||||
@ -139,14 +139,14 @@ TEST(BufferedFileTest, Close) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, CloseError) {
|
TEST(BufferedFileTest, CloseError) {
|
||||||
BufferedFile f = open_buffered_file();
|
buffered_file f = open_buffered_file();
|
||||||
FMT_POSIX(close(f.fileno()));
|
FMT_POSIX(close(f.fileno()));
|
||||||
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
|
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
|
||||||
EXPECT_TRUE(f.get() == 0);
|
EXPECT_TRUE(f.get() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BufferedFileTest, Fileno) {
|
TEST(BufferedFileTest, Fileno) {
|
||||||
BufferedFile f;
|
buffered_file f;
|
||||||
#ifndef __COVERITY__
|
#ifndef __COVERITY__
|
||||||
// fileno on a null FILE pointer either crashes or returns an error.
|
// fileno on a null FILE pointer either crashes or returns an error.
|
||||||
// Disable Coverity because this is intentional.
|
// Disable Coverity because this is intentional.
|
||||||
|
@ -32,12 +32,12 @@ std::string get_system_error(int error_code) {
|
|||||||
|
|
||||||
const char *const FILE_CONTENT = "Don't panic!";
|
const char *const FILE_CONTENT = "Don't panic!";
|
||||||
|
|
||||||
fmt::BufferedFile open_buffered_file(FILE **fp) {
|
fmt::buffered_file open_buffered_file(FILE **fp) {
|
||||||
fmt::File read_end, write_end;
|
fmt::File read_end, write_end;
|
||||||
fmt::File::pipe(read_end, write_end);
|
fmt::File::pipe(read_end, write_end);
|
||||||
write_end.write(FILE_CONTENT, std::strlen(FILE_CONTENT));
|
write_end.write(FILE_CONTENT, std::strlen(FILE_CONTENT));
|
||||||
write_end.close();
|
write_end.close();
|
||||||
fmt::BufferedFile f = read_end.fdopen("r");
|
fmt::buffered_file f = read_end.fdopen("r");
|
||||||
if (fp)
|
if (fp)
|
||||||
*fp = f.get();
|
*fp = f.get();
|
||||||
return f;
|
return f;
|
||||||
|
@ -35,7 +35,7 @@ std::string get_system_error(int error_code);
|
|||||||
extern const char *const FILE_CONTENT;
|
extern const char *const FILE_CONTENT;
|
||||||
|
|
||||||
// Opens a buffered file for reading.
|
// Opens a buffered file for reading.
|
||||||
fmt::BufferedFile open_buffered_file(FILE **fp = 0);
|
fmt::buffered_file open_buffered_file(FILE **fp = 0);
|
||||||
|
|
||||||
inline FILE *safe_fopen(const char *filename, const char *mode) {
|
inline FILE *safe_fopen(const char *filename, const char *mode) {
|
||||||
#if defined(_WIN32) && !defined(__MINGW32__)
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
||||||
|
Reference in New Issue
Block a user