2018-03-04 17:16:51 +00:00
|
|
|
// Formatting library for C++ - custom Google Test assertions
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
2014-05-03 16:48:54 +00:00
|
|
|
|
|
|
|
#include "gtest-extra.h"
|
|
|
|
|
2019-11-15 13:41:14 +00:00
|
|
|
#if FMT_USE_FCNTL
|
2014-05-03 16:48:54 +00:00
|
|
|
|
2018-05-19 17:32:53 +00:00
|
|
|
using fmt::file;
|
2014-05-04 13:44:50 +00:00
|
|
|
|
2023-11-24 18:09:21 +00:00
|
|
|
output_redirect::output_redirect(FILE* f, bool flush) : file_(f) {
|
|
|
|
if (flush) this->flush();
|
2018-05-19 17:32:53 +00:00
|
|
|
int fd = FMT_POSIX(fileno(f));
|
|
|
|
// Create a file object referring to the original file.
|
|
|
|
original_ = file::dup(fd);
|
2014-05-03 23:47:00 +00:00
|
|
|
// Create a pipe.
|
2024-01-01 21:25:07 +00:00
|
|
|
auto pipe = fmt::pipe();
|
|
|
|
read_end_ = std::move(pipe.read_end);
|
2014-05-04 16:01:18 +00:00
|
|
|
// Connect the passed FILE object to the write end of the pipe.
|
2024-01-01 21:25:07 +00:00
|
|
|
pipe.write_end.dup2(fd);
|
2014-05-03 16:48:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:55:47 +00:00
|
|
|
output_redirect::~output_redirect() noexcept {
|
2014-05-03 23:47:00 +00:00
|
|
|
try {
|
2014-07-29 14:50:05 +00:00
|
|
|
restore();
|
2019-01-13 02:27:38 +00:00
|
|
|
} catch (const std::exception& e) {
|
2014-05-04 17:08:29 +00:00
|
|
|
std::fputs(e.what(), stderr);
|
2014-05-03 23:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:55:55 +00:00
|
|
|
void output_redirect::flush() {
|
|
|
|
int result = 0;
|
2021-07-03 14:21:54 +00:00
|
|
|
do {
|
|
|
|
result = fflush(file_);
|
|
|
|
} while (result == EOF && errno == EINTR);
|
2021-05-02 12:55:55 +00:00
|
|
|
if (result != 0) throw fmt::system_error(errno, "cannot flush stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_redirect::restore() {
|
|
|
|
if (original_.descriptor() == -1) return; // Already restored.
|
|
|
|
flush();
|
|
|
|
// Restore the original file.
|
|
|
|
original_.dup2(FMT_POSIX(fileno(file_)));
|
|
|
|
original_.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string output_redirect::restore_and_read() {
|
2014-05-03 16:48:54 +00:00
|
|
|
// Restore output.
|
2014-07-29 14:50:05 +00:00
|
|
|
restore();
|
2014-05-03 16:48:54 +00:00
|
|
|
|
2014-05-03 18:26:46 +00:00
|
|
|
// Read everything from the pipe.
|
|
|
|
std::string content;
|
2019-01-13 02:27:38 +00:00
|
|
|
if (read_end_.descriptor() == -1) return content; // Already read.
|
2014-05-03 18:26:46 +00:00
|
|
|
enum { BUFFER_SIZE = 4096 };
|
2014-05-03 16:48:54 +00:00
|
|
|
char buffer[BUFFER_SIZE];
|
2020-05-07 22:59:46 +00:00
|
|
|
size_t count = 0;
|
2014-05-03 18:26:46 +00:00
|
|
|
do {
|
2014-05-03 19:28:02 +00:00
|
|
|
count = read_end_.read(buffer, BUFFER_SIZE);
|
2016-03-02 15:53:14 +00:00
|
|
|
content.append(buffer, count);
|
2014-05-03 18:26:46 +00:00
|
|
|
} while (count != 0);
|
2014-05-04 17:08:29 +00:00
|
|
|
read_end_.close();
|
2014-05-03 18:26:46 +00:00
|
|
|
return content;
|
2014-05-03 16:48:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:59:46 +00:00
|
|
|
std::string read(file& f, size_t count) {
|
2015-06-24 16:16:03 +00:00
|
|
|
std::string buffer(count, '\0');
|
2020-05-07 22:59:46 +00:00
|
|
|
size_t n = 0, offset = 0;
|
2015-06-24 16:16:03 +00:00
|
|
|
do {
|
|
|
|
n = f.read(&buffer[offset], count - offset);
|
|
|
|
// We can't read more than size_t bytes since count has type size_t.
|
2018-06-06 13:57:59 +00:00
|
|
|
offset += n;
|
2015-06-24 16:16:03 +00:00
|
|
|
} while (offset < count && n != 0);
|
|
|
|
buffer.resize(offset);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-11-15 15:32:50 +00:00
|
|
|
#endif // FMT_USE_FCNTL
|