From 9b80e75b050c14e8fb69c554e11c30eac321a053 Mon Sep 17 00:00:00 2001 From: carterl Date: Sun, 15 Feb 2015 08:53:11 +0100 Subject: [PATCH 1/7] Add #pragma once --- format.h | 2 ++ posix.h | 2 ++ test/gtest-extra.h | 2 ++ test/mock-allocator.h | 2 ++ test/posix-test.h | 2 ++ 5 files changed, 10 insertions(+) diff --git a/format.h b/format.h index 52e8d5b3..1f217b5e 100644 --- a/format.h +++ b/format.h @@ -25,6 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#pragma once + #ifndef FMT_FORMAT_H_ #define FMT_FORMAT_H_ diff --git a/posix.h b/posix.h index a2cf2b13..8ae38bec 100644 --- a/posix.h +++ b/posix.h @@ -25,6 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#pragma once + #ifndef FMT_POSIX_H_ #define FMT_POSIX_H_ diff --git a/test/gtest-extra.h b/test/gtest-extra.h index df9cb7f0..d34e8188 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -25,6 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#pragma once + #ifndef FMT_GTEST_EXTRA_H_ #define FMT_GTEST_EXTRA_H_ diff --git a/test/mock-allocator.h b/test/mock-allocator.h index 7de1e1a0..8b23aaf6 100644 --- a/test/mock-allocator.h +++ b/test/mock-allocator.h @@ -25,6 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#pragma once + #ifndef FMT_MOCK_ALLOCATOR_H_ #define FMT_MOCK_ALLOCATOR_H_ diff --git a/test/posix-test.h b/test/posix-test.h index 4768cb94..60242439 100644 --- a/test/posix-test.h +++ b/test/posix-test.h @@ -25,6 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#pragma once + #ifndef FMT_POSIX_TEST_H #define FMT_POSIX_TEST_H From a243548cecda20f4f784960868673246d67448bc Mon Sep 17 00:00:00 2001 From: carterl Date: Sun, 15 Feb 2015 09:03:49 +0100 Subject: [PATCH 2/7] Silence "warning C4996: 'strcpy': This function or variable may be unsafe." on MSVC --- test/util-test.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/util-test.cc b/test/util-test.cc index fd295525..8b8a20a1 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -699,9 +699,8 @@ void test_count_digits() { } TEST(UtilTest, StringRef) { - char space[100]; - std::strcpy(space, "some string"); - EXPECT_EQ(sizeof("some string") - 1, StringRef(space).size()); + char space[100] = "some string"; + EXPECT_EQ(std::strlen(space), StringRef(space).size()); } TEST(UtilTest, CountDigits) { From 02aeb79cdcecff46e4f7c25b5cc18d61ba8a48b6 Mon Sep 17 00:00:00 2001 From: carterl Date: Sun, 15 Feb 2015 09:36:36 +0100 Subject: [PATCH 3/7] Silence "warning: declaration of '*' shadows a member of 'this' [-Wshadow]" on mingw-gcc --- format.cc | 9 +++++++++ posix.cc | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/format.cc b/format.cc index 1a6e4e22..124d552f 100644 --- a/format.cc +++ b/format.cc @@ -487,6 +487,11 @@ FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) { return 0; } +# ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wshadow" +# endif + FMT_FUNC void fmt::WindowsError::init( int error_code, StringRef format_str, ArgList args) { error_code_ = error_code; @@ -496,6 +501,10 @@ FMT_FUNC void fmt::WindowsError::init( base = std::runtime_error(w.str()); } +# ifdef __GNUC__ +# pragma GCC diagnostic pop +# endif + #endif FMT_FUNC void fmt::internal::format_system_error( diff --git a/posix.cc b/posix.cc index b4893cd6..f0b1bece 100644 --- a/posix.cc +++ b/posix.cc @@ -134,13 +134,13 @@ void fmt::File::close() { fmt::LongLong fmt::File::size() const { #ifdef _WIN32 - LARGE_INTEGER size = {}; + LARGE_INTEGER filesize = {}; HANDLE handle = reinterpret_cast(_get_osfhandle(fd_)); - if (!FMT_SYSTEM(GetFileSizeEx(handle, &size))) + if (!FMT_SYSTEM(GetFileSizeEx(handle, &filesize))) throw WindowsError(GetLastError(), "cannot get file size"); - FMT_STATIC_ASSERT(sizeof(fmt::LongLong) >= sizeof(size.QuadPart), + FMT_STATIC_ASSERT(sizeof(fmt::LongLong) >= sizeof(filesize.QuadPart), "return type of File::size is not large enough"); - return size.QuadPart; + return filesize.QuadPart; #else typedef struct stat Stat; Stat file_stat = Stat(); From d0e57e8639065b471184abbf67c30291e11c5e07 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sun, 15 Feb 2015 16:52:00 +0800 Subject: [PATCH 4/7] Silence "warning: missing initializer for member '_SYSTEM_INFO::' [-Wmissing-field-initializers]" on mingw-gcc --- posix.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posix.cc b/posix.cc index f0b1bece..d8f9bb62 100644 --- a/posix.cc +++ b/posix.cc @@ -228,7 +228,7 @@ fmt::BufferedFile fmt::File::fdopen(const char *mode) { long fmt::getpagesize() { #ifdef _WIN32 - SYSTEM_INFO si = {}; + SYSTEM_INFO si; GetSystemInfo(&si); return si.dwPageSize; #else From f30cd36196b558c263ae3f666fd8b0dee12aef94 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sun, 15 Feb 2015 21:58:48 +0800 Subject: [PATCH 5/7] Revert "Add #pragma once" Not everyone like it This reverts commit 9b80e75b050c14e8fb69c554e11c30eac321a053. --- format.h | 2 -- posix.h | 2 -- test/gtest-extra.h | 2 -- test/mock-allocator.h | 2 -- test/posix-test.h | 2 -- 5 files changed, 10 deletions(-) diff --git a/format.h b/format.h index 1f217b5e..52e8d5b3 100644 --- a/format.h +++ b/format.h @@ -25,8 +25,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once - #ifndef FMT_FORMAT_H_ #define FMT_FORMAT_H_ diff --git a/posix.h b/posix.h index 8ae38bec..a2cf2b13 100644 --- a/posix.h +++ b/posix.h @@ -25,8 +25,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once - #ifndef FMT_POSIX_H_ #define FMT_POSIX_H_ diff --git a/test/gtest-extra.h b/test/gtest-extra.h index d34e8188..df9cb7f0 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -25,8 +25,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once - #ifndef FMT_GTEST_EXTRA_H_ #define FMT_GTEST_EXTRA_H_ diff --git a/test/mock-allocator.h b/test/mock-allocator.h index 8b23aaf6..7de1e1a0 100644 --- a/test/mock-allocator.h +++ b/test/mock-allocator.h @@ -25,8 +25,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once - #ifndef FMT_MOCK_ALLOCATOR_H_ #define FMT_MOCK_ALLOCATOR_H_ diff --git a/test/posix-test.h b/test/posix-test.h index 60242439..4768cb94 100644 --- a/test/posix-test.h +++ b/test/posix-test.h @@ -25,8 +25,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#pragma once - #ifndef FMT_POSIX_TEST_H #define FMT_POSIX_TEST_H From 284687d8f722ad945e89e5f49a406a1e75af5386 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 16 Feb 2015 10:55:07 +0800 Subject: [PATCH 6/7] Add extra BufferedFile methods --- posix.cc | 25 ++++++++++++++++++++++++- posix.h | 27 +++++++++++++++++++++++++-- test/posix-test.cc | 12 ++++++++++++ test/posix-test.h | 3 +++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/posix.cc b/posix.cc index d8f9bb62..be5f8eba 100644 --- a/posix.cc +++ b/posix.cc @@ -30,7 +30,7 @@ #include "posix.h" -#include +#include #include #include @@ -96,12 +96,35 @@ void fmt::BufferedFile::close() { } int fmt::BufferedFile::fileno() const { + assert(file_ && "File has been closed"); int fd = FMT_POSIX_CALL(fileno(file_)); if (fd == -1) throw SystemError(errno, "cannot get file descriptor"); return fd; } +void fmt::BufferedFile::flush() const { + assert(file_ && "File has been closed"); + if (FMT_SYSTEM(fflush(file_)) != 0) { + throw SystemError(errno, "cannot flush file"); + } +} + +void fmt::BufferedFile::write(fmt::StringRef str) { + assert(file_ && "File has been closed"); + size_t result = FMT_SYSTEM(fwrite(str.c_str(), str.size(), 1, file_)); + if (result < str.size()) { + // FIXME: Will fwrite set errno? + throw SystemError(errno, "failed to write raw string, only {} character(s) written", result); + } +} + +void fmt::BufferedFile::write(char c) { + assert(file_ && "File has been closed"); + if (FMT_SYSTEM(fputc(c, file_)) == EOF) + throw SystemError(errno, "failed to write '{}'", c); +} + fmt::File::File(fmt::StringRef path, int oflag) { int mode = S_IRUSR | S_IWUSR; #ifdef _WIN32 diff --git a/posix.h b/posix.h index a2cf2b13..995784fa 100644 --- a/posix.h +++ b/posix.h @@ -33,6 +33,7 @@ #include #include +#include #include "format.h" @@ -45,7 +46,7 @@ // Fix warnings about deprecated symbols. # define FMT_POSIX(call) _##call # else -# define FMT_POSIX(call) call +# define FMT_POSIX(call) ::call # endif #endif @@ -53,7 +54,7 @@ #ifdef FMT_SYSTEM # define FMT_POSIX_CALL(call) FMT_SYSTEM(call) #else -# define FMT_SYSTEM(call) call +# define FMT_SYSTEM(call) ::call # ifdef _WIN32 // Fix warnings about deprecated symbols. # define FMT_POSIX_CALL(call) ::_##call @@ -188,12 +189,34 @@ public: // Returns the pointer to a FILE object representing this file. FILE *get() const FMT_NOEXCEPT { return file_; } + // Returns the system file number of this file int fileno() const; + // Flushes the buffer of this file + void flush() const; + + // Writes raw string without formatting + void write(fmt::StringRef str); + + // Writes a single charactor without formatting + void write(char c); + + // Writes a new line + void writeln() { + write('\n'); + } + void print(fmt::StringRef format_str, const ArgList &args) { + assert(file_ && "File has been closed"); fmt::print(file_, format_str, args); } + void printf(fmt::StringRef format_str, const ArgList &args) { + assert(file_ && "File has been closed"); + fmt::fprintf(file_, format_str, args); + } + FMT_VARIADIC(void, print, fmt::StringRef) + FMT_VARIADIC(void, printf, fmt::StringRef) }; // A file. Closed file is represented by a File object with descriptor -1. diff --git a/test/posix-test.cc b/test/posix-test.cc index c9529688..fe18cd62 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -178,6 +178,18 @@ int test::fileno(FILE *stream) { return ::FMT_POSIX(fileno(stream)); } +int test::fflush(FILE *stream) { + return ::fflush(stream); +} + +int test::fwrite(const void *str, size_t size, size_t count, FILE *stream) { + return ::fwrite(str, size, count, stream); +} + +int test::fputc(int ch, FILE *stream) { + return ::fputc(ch, stream); +} + #ifndef _WIN32 # define EXPECT_RETRY(statement, func, message) \ func##_count = 1; \ diff --git a/test/posix-test.h b/test/posix-test.h index 4768cb94..7ab69bcb 100644 --- a/test/posix-test.h +++ b/test/posix-test.h @@ -73,7 +73,10 @@ int pipe(int *pfds, unsigned psize, int textmode); FILE *fopen(const char *filename, const char *mode); int fclose(FILE *stream); int fileno(FILE *stream); +int fflush(FILE *stream); +int fwrite(const void *str, size_t size, size_t count, FILE *stream); +int fputc(int ch, FILE *stream); } // namespace test #define FMT_SYSTEM(call) test::call From 06eb3c107b67de3edb5f147823f0ed3927f2b095 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 16 Feb 2015 12:35:47 +0800 Subject: [PATCH 7/7] Fix POSIX build --- posix.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posix.h b/posix.h index 995784fa..6d0e4dab 100644 --- a/posix.h +++ b/posix.h @@ -44,9 +44,9 @@ #ifndef FMT_POSIX # ifdef _WIN32 // Fix warnings about deprecated symbols. -# define FMT_POSIX(call) _##call +# define FMT_POSIX(symbol) _##symbol # else -# define FMT_POSIX(call) ::call +# define FMT_POSIX(symbol) symbol # endif #endif