@@ -116,90 +116,103 @@ const wchar_t* make_mdstring(std::ios::openmode mode) noexcept {
116116#endif
117117} // namespace
118118
119- open_handle_type create_file (std::ios::openmode mode) {
119+ fs::path create_directory (std::string_view prefix) {
120+ validate_prefix (prefix); // throws std::invalid_argument with a proper text
121+
120122 std::error_code ec;
121- open_handle_type handle = create_file (mode , ec);
123+ fs::path directory = create_directory (prefix , ec);
122124
123125 if (ec) {
124- throw fs::filesystem_error (" Cannot create a temporary file " , ec);
126+ throw fs::filesystem_error (" Cannot create a temporary directory " , ec);
125127 }
126128
127- return handle ;
129+ return directory ;
128130}
129131
130- open_handle_type create_file (std::ios::openmode mode, std::error_code& ec) {
132+ fs::path create_directory (std::string_view prefix, std::error_code& ec) {
133+ if (!is_prefix_valid (prefix)) {
134+ ec = std::make_error_code (std::errc::invalid_argument);
135+ return fs::path ();
136+ }
137+
131138#ifdef _WIN32
132- fs::path::string_type path = make_path (" " );
139+ fs::path::string_type path = make_path (prefix );
133140#else
134- fs::path::string_type path = make_pattern (" " );
141+ fs::path::string_type path = make_pattern (prefix );
135142#endif
136143
144+ #ifdef _WIN32
145+ if (!CreateDirectory (path.c_str (), nullptr )) {
146+ ec = std::error_code (GetLastError (), std::system_category ());
147+ }
148+ #else
149+ if (mkdtemp (path.data ()) == nullptr ) {
150+ ec = std::error_code (errno, std::system_category ());
151+ }
152+ #endif
153+
154+ return path;
155+ }
156+
157+ #if defined(_WIN32)
158+ std::FILE* create_file (std::ios::openmode mode) {
159+ std::error_code ec;
160+ std::FILE* handle = create_file (mode, ec);
161+
162+ if (ec) {
163+ throw fs::filesystem_error (" Cannot create a temporary file" , ec);
164+ }
165+
166+ return handle;
167+ }
168+
169+ std::FILE* create_file (std::ios::openmode mode, std::error_code& ec) {
170+ fs::path::string_type path = make_path (" " );
171+
137172 mode |= std::ios::in | std::ios::out;
138173
139- #ifdef _WIN32
140174 std::FILE* handle;
141175
142176 // FIXME: use _wfopen_s
143177 if (const wchar_t * mdstr = make_mdstring (mode)) {
144178 handle = _wfopen (path.c_str (), mdstr);
145179 if (handle == nullptr ) {
146180 ec = std::error_code (errno, std::system_category ());
147- return {} ;
181+ return nullptr ;
148182 }
149183 } else {
150184 ec = std::make_error_code (std::errc::invalid_argument);
151- return {};
152- }
153- #else
154- int handle = mkstemp (path.data ());
155- if (handle == -1 ) {
156- ec = std::error_code (errno, std::system_category ());
157- return {};
185+ return nullptr ;
158186 }
159187
160- unlink (path.c_str ());
161- #endif
162-
163- (void )mode;
164188 ec.clear ();
165189 return handle;
166190}
167-
168- fs::path create_directory (std::string_view prefix) {
169- validate_prefix (prefix); // throws std::invalid_argument with a proper text
170-
191+ #else
192+ int create_file () {
171193 std::error_code ec;
172- fs::path directory = create_directory (prefix, ec);
194+ int handle = create_file ( ec);
173195
174196 if (ec) {
175- throw fs::filesystem_error (" Cannot create a temporary directory " , ec);
197+ throw fs::filesystem_error (" Cannot create a temporary file " , ec);
176198 }
177199
178- return directory ;
200+ return handle ;
179201}
180202
181- fs::path create_directory (std::string_view prefix, std::error_code& ec) {
182- if (!is_prefix_valid (prefix)) {
183- ec = std::make_error_code (std::errc::invalid_argument);
184- return fs::path ();
185- }
186-
187- #ifdef _WIN32
188- fs::path::string_type path = make_path (prefix);
189- #else
190- fs::path::string_type path = make_pattern (prefix);
191- #endif
203+ int create_file (std::error_code& ec) {
204+ fs::path::string_type path = make_pattern (" " );
192205
193- #ifdef _WIN32
194- if (!CreateDirectory (path.c_str (), nullptr )) {
195- ec = std::error_code (GetLastError (), std::system_category ());
196- }
197- #else
198- if (mkdtemp (path.data ()) == nullptr ) {
206+ int handle = mkstemp (path.data ());
207+ if (handle == -1 ) {
199208 ec = std::error_code (errno, std::system_category ());
209+ return -1 ;
200210 }
201- #endif
202211
203- return path;
212+ unlink (path.c_str ());
213+
214+ ec.clear ();
215+ return handle;
204216}
217+ #endif
205218} // namespace tmp
0 commit comments