Skip to content

Commit 489afac

Browse files
committed
Move out filebuf opening to a private function
1 parent b7e6ab5 commit 489afac

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

.clang-tidy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ Checks: >
3030
-readability-redundant-declaration
3131
3232
CheckOptions:
33-
readability-identifier-length.IgnoredParameterNames: 'ec'
34-
readability-identifier-length.IgnoredVariableNames: 'ec'
33+
readability-identifier-length.IgnoredParameterNames: 'ec|sb'
34+
readability-identifier-length.IgnoredVariableNames: 'ec|sb'

include/tmp/file

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,8 @@ public:
7373
/// @throws std::filesystem::filesystem_error if cannot create a file
7474
explicit file()
7575
: std::iostream(std::addressof(sb)),
76-
underlying(create_file(), &std::fclose) {
77-
#if defined(_MSC_VER)
78-
sb = std::filebuf(underlying.get());
79-
#elif defined(_LIBCPP_VERSION)
80-
sb.__open(fileno(underlying.get()), binary | in | out);
81-
#else
82-
sb = __gnu_cxx::stdio_filebuf<char>(underlying.get(), binary | in | out);
83-
#endif
84-
85-
if (!sb.is_open()) {
86-
std::error_code ec = std::make_error_code(std::io_errc::stream);
87-
throw std::filesystem::filesystem_error("Cannot create a temporary file",
88-
ec);
89-
}
90-
}
76+
underlying(create_file(), &std::fclose),
77+
sb(open_filebuf(underlying.get())) {}
9178

9279
file(const file&) = delete;
9380
file& operator=(const file&) = delete;
@@ -129,6 +116,29 @@ private:
129116
/// The underlying raw file device object
130117
std::filebuf sb;
131118
#endif
119+
120+
/// Returns a file device for the given file stream
121+
/// @param file The file stream
122+
/// @returns The new file device
123+
/// @throws std::filesystem::filesystem_error if cannot open the file stream
124+
decltype(sb) open_filebuf(std::FILE* file) {
125+
decltype(sb) sb;
126+
#if defined(_MSC_VER)
127+
sb = std::filebuf(file);
128+
#elif defined(_LIBCPP_VERSION)
129+
sb.__open(get_native_handle(file), binary | in | out);
130+
#else
131+
sb = __gnu_cxx::stdio_filebuf<char>(file, binary | in | out);
132+
#endif
133+
134+
if (!sb.is_open()) {
135+
std::error_code ec = std::make_error_code(std::io_errc::stream);
136+
throw std::filesystem::filesystem_error("Cannot create a temporary file",
137+
ec);
138+
}
139+
140+
return sb;
141+
}
132142
};
133143
} // namespace tmp
134144

0 commit comments

Comments
 (0)