@@ -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