33
44#include " opentimelineio/fileUtils.h"
55
6- #include < cstring>
7- #include < vector>
8-
9- #if defined(_WINDOWS)
10- #if !defined(WIN32_LEAN_AND_MEAN)
11- #define WIN32_LEAN_AND_MEAN
12- #endif // WIN32_LEAN_AND_MEAN
13- #include < windows.h>
14- #include < combaseapi.h>
15- #if defined(min)
16- #undef min
17- #endif // min
18- #else // _WINDOWS
19- #include < sys/stat.h>
20- #include < stdlib.h>
21- #include < unistd.h>
22- #endif // _WINDOWS
6+ #include < cstdio>
7+ #include < filesystem>
238
249namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {
2510
@@ -31,78 +16,11 @@ to_unix_separators(std::string const& path)
3116 return result;
3217}
3318
34- #if defined(_WIN32) || defined(_WIN64)
35-
3619std::string create_temp_dir ()
3720{
38- std::string out;
39-
40- // Get the temporary directory.
41- char path[MAX_PATH ];
42- DWORD r = GetTempPath (MAX_PATH , path);
43- if (r)
44- {
45- out = std::string (path);
46-
47- // Replace path separators.
48- for (size_t i = 0 ; i < out.size (); ++i)
49- {
50- if (' \\ ' == out[i])
51- {
52- out[i] = ' /' ;
53- }
54- }
55-
56- // Create a unique name from a GUID.
57- GUID guid;
58- CoCreateGuid (&guid);
59- const uint8_t * guidP = reinterpret_cast <const uint8_t *>(&guid);
60- for (int i = 0 ; i < 16 ; ++i)
61- {
62- char buf[3 ] = " " ;
63- sprintf_s (buf, 3 , " %02x" , guidP[i]);
64- out += buf;
65- }
66-
67- // Create a unique directory.
68- CreateDirectory (out.c_str (), NULL );
69- }
70-
21+ std::string const out (std::tmpnam (nullptr ));
22+ std::filesystem::create_directory (out);
7123 return out;
7224}
7325
74- #else // _WIN32 || _WIN64
75-
76- std::string create_temp_dir ()
77- {
78- // Find the temporary directory.
79- std::string path;
80- char * env = nullptr ;
81- if ((env = getenv (" TEMP" ))) path = env;
82- else if ((env = getenv (" TMP" ))) path = env;
83- else if ((env = getenv (" TMPDIR" ))) path = env;
84- else
85- {
86- for (const auto & i : { " /tmp" , " /var/tmp" , " /usr/tmp" })
87- {
88- struct stat buffer;
89- if (0 == stat (i, &buffer))
90- {
91- path = i;
92- break ;
93- }
94- }
95- }
96-
97- // Create a unique directory.
98- path = path + " /XXXXXX" ;
99- const size_t size = path.size ();
100- std::vector<char > buf (size + 1 );
101- memcpy (buf.data (), path.c_str (), size);
102- buf[size] = 0 ;
103- return mkdtemp (buf.data ());
104- }
105-
106- #endif // _WIN32 || _WIN64
107-
10826}} // namespace opentimelineio::OPENTIMELINEIO_VERSION
0 commit comments