forked from AcademySoftwareFoundation/OpenTimelineIO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
199 lines (169 loc) · 4.5 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
#include "util.h"
#include <opentimelineio/timeline.h>
#include <Python.h>
#include <codecvt>
#include <iostream>
#include <locale>
#if defined(_WINDOWS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <combaseapi.h>
#if defined(min)
#undef min
#endif // min
#else // _WINDOWS
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <unistd.h>
#endif // _WINDOWS
namespace otio = opentimelineio::OPENTIMELINEIO_VERSION;
namespace examples {
#if defined(_WINDOWS)
std::string normalize_path(std::string const& in)
{
std::string out;
for (auto i : in)
{
out.push_back('\\' == i ? '/' : i);
}
return out;
}
std::string absolute(std::string const& in)
{
wchar_t buf[MAX_PATH];
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16;
if (!::_wfullpath(buf, utf16.from_bytes(in).c_str(), MAX_PATH))
{
buf[0] = 0;
}
return normalize_path(utf16.to_bytes(buf));
}
std::string create_temp_dir()
{
std::string out;
// Get the temporary directory.
char path[MAX_PATH];
DWORD r = GetTempPath(MAX_PATH, path);
if (r)
{
out = std::string(path);
// Replace path separators.
for (size_t i = 0; i < out.size(); ++i)
{
if ('\\' == out[i])
{
out[i] = '/';
}
}
// Create a unique name from a GUID.
GUID guid;
CoCreateGuid(&guid);
const uint8_t* guidP = reinterpret_cast<const uint8_t*>(&guid);
for (int i = 0; i < 16; ++i)
{
char buf[3] = "";
sprintf_s(buf, 3, "%02x", guidP[i]);
out += buf;
}
// Create a unique directory.
CreateDirectory(out.c_str(), NULL);
}
return out;
}
std::vector<std::string> glob(std::string const& path, std::string const& pattern)
{
std::vector<std::string> out;
// Prepare the path.
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16;
std::wstring wpath = utf16.from_bytes(path + '/' + pattern);
WCHAR wbuf[MAX_PATH];
size_t size = std::min(wpath.size(), static_cast<size_t>(MAX_PATH - 1));
memcpy(wbuf, wpath.c_str(), size * sizeof(WCHAR));
wbuf[size++] = 0;
// List the directory contents.
std::string const absolutePath = absolute(path);
WIN32_FIND_DATAW ffd;
HANDLE hFind = FindFirstFileW(wbuf, &ffd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
out.push_back(absolutePath + '/' + utf16.to_bytes(ffd.cFileName));
} while (FindNextFileW(hFind, &ffd) != 0);
FindClose(hFind);
}
return out;
}
#else // _WINDOWS
std::string normalize_path(std::string const& in)
{
return in;
}
std::string absolute(std::string const& in)
{
char buf[PATH_MAX];
realpath(in.c_str(), buf);
return buf;
}
std::string create_temp_dir()
{
// Find the temporary directory.
std::string path;
char* env = nullptr;
if ((env = getenv("TEMP"))) path = env;
else if ((env = getenv("TMP"))) path = env;
else if ((env = getenv("TMPDIR"))) path = env;
else
{
for (const auto& i : { "/tmp", "/var/tmp", "/usr/tmp" })
{
struct stat buffer;
if (0 == stat(i, &buffer))
{
path = i;
break;
}
}
}
// Create a unique directory.
path = path + "/XXXXXX";
const size_t size = path.size();
std::vector<char> buf(size + 1);
memcpy(buf.data(), path.c_str(), size);
buf[size] = 0;
return mkdtemp(buf.data());
}
std::vector<std::string> glob(std::string const& path, std::string const& pattern)
{
std::vector<std::string> out;
std::string const absolutePath = absolute(path);
DIR* dir = opendir(path.c_str());
if (dir)
{
struct dirent* e = nullptr;
while ((e = readdir(dir)) != nullptr)
{
if (0 == fnmatch(pattern.c_str(), e->d_name, 0))
{
out.push_back(absolutePath + '/' + e->d_name);
}
}
closedir(dir);
}
return out;
}
#endif // _WINDOWS
void print_error(otio::ErrorStatus const& error_status)
{
std::cout << "ERROR: " <<
otio::ErrorStatus::outcome_to_string(error_status.outcome) << ": " <<
error_status.details << std::endl;
}
}