Skip to content

Commit

Permalink
io:native-file
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Jun 8, 2024
1 parent d84c543 commit 0be2a93
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 deletions.
9 changes: 9 additions & 0 deletions io/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ cc_library(
deps = [":file"],
)

cc_library(
name = "native-file",
hdrs = ["native-file.h"],
deps = select({
"@platforms//os:windows": ["//io/win32:file"],
"//conditions:default": ["//io/posix:file"],
}),
)

cc_library(
name = "stream",
srcs = ["stream.cc"],
Expand Down
30 changes: 30 additions & 0 deletions io/native-file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _IO_NATIVE_FILE_H
#define _IO_NATIVE_FILE_H

#ifndef _WIN32
#include "io/posix/file.h"
#else
#include "io/win32/file.h"
#endif

namespace io {

#ifndef _WIN32
using NativeSharedFile = posix::SharedFile;
using NativeFile = posix::File;

using posix::std_input;
using posix::std_output;
using posix::std_error;
#else
using NativeSharedFile = win32::SharedFile;
using NativeFile = win32::File;

using win32::std_input;
using win32::std_output;
using win32::std_error;
#endif

} // namespace io

#endif // _IO_NATIVE_FILE_H
17 changes: 14 additions & 3 deletions io/posix/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@
namespace io {
namespace posix {

SharedFile std_input(STDIN_FILENO);
SharedFile std_output(STDOUT_FILENO);
SharedFile std_error(STDERR_FILENO);
SharedFile &std_input() {
static SharedFile result(STDIN_FILENO);
return result;
}

SharedFile &std_output() {
static SharedFile result(STDOUT_FILENO);
return result;
}

SharedFile &std_error() {
static SharedFile result(STDERR_FILENO);
return result;
}

std::error_code SharedFile::read(BufferSpan buffer, size_t &size) {
ssize_t ret = ::read(fd_, buffer.data(), buffer.size());
Expand Down
6 changes: 3 additions & 3 deletions io/posix/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class SharedFile : public io::File {
int fd_;
};

extern SharedFile std_input;
extern SharedFile std_output;
extern SharedFile std_error;
SharedFile &std_input();
SharedFile &std_output();
SharedFile &std_error();

class File : public SharedFile {
public:
Expand Down
17 changes: 14 additions & 3 deletions io/win32/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
namespace io {
namespace win32 {

SharedFile std_input(GetStdHandle(STD_INPUT_HANDLE));
SharedFile std_output(GetStdHandle(STD_OUTPUT_HANDLE));
SharedFile std_error(GetStdHandle(STD_ERROR_HANDLE));
SharedFile &std_input() {
static SharedFile result(GetStdHandle(STD_INPUT_HANDLE));
return result;
}

SharedFile &std_output() {
static SharedFile result(GetStdHandle(STD_OUTPUT_HANDLE));
return result;
}

SharedFile &std_error() {
static SharedFile result(GetStdHandle(STD_ERROR_HANDLE));
return result;
}

std::error_code SharedFile::read(BufferSpan buffer, size_t &size) {
DWORD bytes_read;
Expand Down
6 changes: 3 additions & 3 deletions io/win32/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class SharedFile : public io::File {
HANDLE handle_;
};

extern SharedFile std_input;
extern SharedFile std_output;
extern SharedFile std_error;
SharedFile &std_input();
SharedFile &std_output();
SharedFile &std_error();

class File : public SharedFile {
public:
Expand Down
2 changes: 1 addition & 1 deletion util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ cc_binary(
srcs = ["hash-filter_stats.cc"],
deps = [
":hash-filter",
"//io:native-file",
"//io:stream",
"//io/posix:file",
"@com_google_absl//absl/random",
],
)
Expand Down
4 changes: 2 additions & 2 deletions util/hash-filter_stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include <vector>

#include "absl/random/random.h"
#include "io/posix/file.h"
#include "io/native-file.h"
#include "io/stream.h"
#include "util/hash-filter.h"

int main() {
io::OStream os(io::posix::std_output);
io::OStream os(io::std_output());
absl::InsecureBitGen gen;
util::HashFilter32 filter(262144);
std::vector<uint64_t> fingerprints;
Expand Down

0 comments on commit 0be2a93

Please sign in to comment.