Add extra BufferedFile methods
This commit is contained in:
parent
bfa4731b35
commit
284687d8f7
25
posix.cc
25
posix.cc
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
#include "posix.h"
|
#include "posix.h"
|
||||||
|
|
||||||
#include <limits.h>
|
#include <climits>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
@ -96,12 +96,35 @@ void fmt::BufferedFile::close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int fmt::BufferedFile::fileno() const {
|
int fmt::BufferedFile::fileno() const {
|
||||||
|
assert(file_ && "File has been closed");
|
||||||
int fd = FMT_POSIX_CALL(fileno(file_));
|
int fd = FMT_POSIX_CALL(fileno(file_));
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
throw SystemError(errno, "cannot get file descriptor");
|
throw SystemError(errno, "cannot get file descriptor");
|
||||||
return fd;
|
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) {
|
fmt::File::File(fmt::StringRef path, int oflag) {
|
||||||
int mode = S_IRUSR | S_IWUSR;
|
int mode = S_IRUSR | S_IWUSR;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|||||||
27
posix.h
27
posix.h
@ -33,6 +33,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
#include "format.h"
|
#include "format.h"
|
||||||
|
|
||||||
@ -45,7 +46,7 @@
|
|||||||
// Fix warnings about deprecated symbols.
|
// Fix warnings about deprecated symbols.
|
||||||
# define FMT_POSIX(call) _##call
|
# define FMT_POSIX(call) _##call
|
||||||
# else
|
# else
|
||||||
# define FMT_POSIX(call) call
|
# define FMT_POSIX(call) ::call
|
||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -53,7 +54,7 @@
|
|||||||
#ifdef FMT_SYSTEM
|
#ifdef FMT_SYSTEM
|
||||||
# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
|
# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
|
||||||
#else
|
#else
|
||||||
# define FMT_SYSTEM(call) call
|
# define FMT_SYSTEM(call) ::call
|
||||||
# ifdef _WIN32
|
# ifdef _WIN32
|
||||||
// Fix warnings about deprecated symbols.
|
// Fix warnings about deprecated symbols.
|
||||||
# define FMT_POSIX_CALL(call) ::_##call
|
# define FMT_POSIX_CALL(call) ::_##call
|
||||||
@ -188,12 +189,34 @@ public:
|
|||||||
// Returns the pointer to a FILE object representing this file.
|
// Returns the pointer to a FILE object representing this file.
|
||||||
FILE *get() const FMT_NOEXCEPT { return file_; }
|
FILE *get() const FMT_NOEXCEPT { return file_; }
|
||||||
|
|
||||||
|
// Returns the system file number of this file
|
||||||
int fileno() const;
|
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) {
|
void print(fmt::StringRef format_str, const ArgList &args) {
|
||||||
|
assert(file_ && "File has been closed");
|
||||||
fmt::print(file_, format_str, args);
|
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, print, fmt::StringRef)
|
||||||
|
FMT_VARIADIC(void, printf, fmt::StringRef)
|
||||||
};
|
};
|
||||||
|
|
||||||
// A file. Closed file is represented by a File object with descriptor -1.
|
// A file. Closed file is represented by a File object with descriptor -1.
|
||||||
|
|||||||
@ -178,6 +178,18 @@ int test::fileno(FILE *stream) {
|
|||||||
return ::FMT_POSIX(fileno(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
|
#ifndef _WIN32
|
||||||
# define EXPECT_RETRY(statement, func, message) \
|
# define EXPECT_RETRY(statement, func, message) \
|
||||||
func##_count = 1; \
|
func##_count = 1; \
|
||||||
|
|||||||
@ -73,7 +73,10 @@ int pipe(int *pfds, unsigned psize, int textmode);
|
|||||||
FILE *fopen(const char *filename, const char *mode);
|
FILE *fopen(const char *filename, const char *mode);
|
||||||
int fclose(FILE *stream);
|
int fclose(FILE *stream);
|
||||||
int fileno(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
|
} // namespace test
|
||||||
|
|
||||||
#define FMT_SYSTEM(call) test::call
|
#define FMT_SYSTEM(call) test::call
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user