blob: 04a4fe910e9ae2da0f9ed1fd604a8cb638891ed7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SkOSFile.h"
8
tfarinaa8e2e152014-07-28 19:26:58 -07009SkString SkOSPath::Join(const char *rootPath, const char *relativePath) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000010 SkString result(rootPath);
bsalomon6eb03cc2014-08-07 14:28:50 -070011 if (!result.endsWith(SkPATH_SEPARATOR) && !result.isEmpty()) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000012 result.appendUnichar(SkPATH_SEPARATOR);
13 }
14 result.append(relativePath);
15 return result;
16}
17
tfarinaa8e2e152014-07-28 19:26:58 -070018SkString SkOSPath::Basename(const char* fullPath) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000019 if (!fullPath) {
20 return SkString();
21 }
22 const char* filename = strrchr(fullPath, SkPATH_SEPARATOR);
23 if (NULL == filename) {
24 filename = fullPath;
25 } else {
26 ++filename;
27 }
28 return SkString(filename);
29}
30
bsalomon6eb03cc2014-08-07 14:28:50 -070031SkString SkOSPath::Dirname(const char* fullPath) {
32 if (!fullPath) {
33 return SkString();
34 }
35 const char* end = strrchr(fullPath, SkPATH_SEPARATOR);
36 if (NULL == end) {
37 return SkString();
38 }
39 if (end == fullPath) {
40 SkASSERT(fullPath[0] == SkPATH_SEPARATOR);
41 ++end;
42 }
43 return SkString(fullPath, end - fullPath);
44}
45
reed@android.com8a1c16f2008-12-17 15:59:43 +000046#ifdef SK_BUILD_FOR_WIN
47
reed@android.comb50a60c2009-01-14 17:51:08 +000048static uint16_t* concat_to_16(const char src[], const char suffix[])
reed@android.com8a1c16f2008-12-17 15:59:43 +000049{
reed@android.com6f252972009-01-14 16:46:16 +000050 size_t i, len = strlen(src);
51 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
reed@android.comb50a60c2009-01-14 17:51:08 +000052 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
reed@android.com6f252972009-01-14 16:46:16 +000054 for (i = 0; i < len; i++)
55 dst[i] = src[i];
reed@android.com8a1c16f2008-12-17 15:59:43 +000056
reed@android.com6f252972009-01-14 16:46:16 +000057 if (i > 0 && dst[i-1] != '/')
58 dst[i++] = '/';
59 dst[i++] = '*';
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
reed@android.com6f252972009-01-14 16:46:16 +000061 if (suffix)
62 {
63 while (*suffix)
64 dst[i++] = *suffix++;
65 }
66 dst[i] = 0;
67 SkASSERT(i + 1 <= len + len2);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068
reed@android.com6f252972009-01-14 16:46:16 +000069 return dst;
reed@android.com8a1c16f2008-12-17 15:59:43 +000070}
71
reed@android.com8a1c16f2008-12-17 15:59:43 +000072////////////////////////////////////////////////////////////////////////////
73
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000074SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +000075{
76}
77
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000078SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +000079{
reed@android.com6f252972009-01-14 16:46:16 +000080 this->reset(path, suffix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081}
82
83SkOSFile::Iter::~Iter()
84{
reed@android.com6f252972009-01-14 16:46:16 +000085 sk_free(fPath16);
86 if (fHandle)
87 ::FindClose(fHandle);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088}
89
90void SkOSFile::Iter::reset(const char path[], const char suffix[])
91{
reed@android.com6f252972009-01-14 16:46:16 +000092 if (fHandle)
93 {
94 ::FindClose(fHandle);
95 fHandle = 0;
96 }
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000097 if (NULL == path)
reed@android.com6f252972009-01-14 16:46:16 +000098 path = "";
reed@android.com8a1c16f2008-12-17 15:59:43 +000099
reed@android.com6f252972009-01-14 16:46:16 +0000100 sk_free(fPath16);
101 fPath16 = concat_to_16(path, suffix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102}
103
reed@android.comb50a60c2009-01-14 17:51:08 +0000104static bool is_magic_dir(const uint16_t dir[])
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105{
reed@android.com6f252972009-01-14 16:46:16 +0000106 // return true for "." and ".."
107 return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108}
109
110static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
111{
reed@android.com6f252972009-01-14 16:46:16 +0000112 WIN32_FIND_DATAW data;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000114 if (NULL == dataPtr)
reed@android.com6f252972009-01-14 16:46:16 +0000115 {
116 if (::FindNextFileW(handle, &data))
117 dataPtr = &data;
118 else
119 return false;
120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121
reed@android.com6f252972009-01-14 16:46:16 +0000122 for (;;)
123 {
124 if (getDir)
125 {
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000126 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
reed@android.com6f252972009-01-14 16:46:16 +0000127 break;
128 }
129 else
130 {
131 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
132 break;
133 }
134 if (!::FindNextFileW(handle, dataPtr))
135 return false;
136 }
137 // if we get here, we've found a file/dir
138 if (name)
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000139 name->setUTF16((uint16_t*)dataPtr->cFileName);
reed@android.com6f252972009-01-14 16:46:16 +0000140 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141}
142
143bool SkOSFile::Iter::next(SkString* name, bool getDir)
144{
reed@android.com6f252972009-01-14 16:46:16 +0000145 WIN32_FIND_DATAW data;
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000146 WIN32_FIND_DATAW* dataPtr = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
reed@android.com6f252972009-01-14 16:46:16 +0000148 if (fHandle == 0) // our first time
149 {
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000150 if (fPath16 == NULL || *fPath16 == 0) // check for no path
reed@android.com6f252972009-01-14 16:46:16 +0000151 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000153 fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
reed@android.com6f252972009-01-14 16:46:16 +0000154 if (fHandle != 0 && fHandle != (HANDLE)~0)
155 dataPtr = &data;
156 }
157 return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158}
159
caryclark@google.com867cbd82012-09-20 15:45:41 +0000160#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
162#if 0
163OSStatus FSPathMakeRef (
164 const UInt8 * path,
165 FSRef * ref,
166 Boolean * isDirectory
167);
168#endif
169
170SkOSFile::Iter::Iter() : fDIR(0)
171{
172}
173
174SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
175{
reed@android.com6f252972009-01-14 16:46:16 +0000176 this->reset(path, suffix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177}
178
179SkOSFile::Iter::~Iter()
180{
reed@android.com6f252972009-01-14 16:46:16 +0000181 if (fDIR)
182 ::closedir(fDIR);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183}
184
185void SkOSFile::Iter::reset(const char path[], const char suffix[])
186{
reed@android.com6f252972009-01-14 16:46:16 +0000187 if (fDIR)
188 {
189 ::closedir(fDIR);
190 fDIR = 0;
191 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192
reed@android.com6f252972009-01-14 16:46:16 +0000193 fPath.set(path);
194 if (path)
195 {
196 fDIR = ::opendir(path);
197 fSuffix.set(suffix);
198 }
199 else
200 fSuffix.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201}
202
203// returns true if suffix is empty, or if str ends with suffix
204static bool issuffixfor(const SkString& suffix, const char str[])
205{
reed@android.com6f252972009-01-14 16:46:16 +0000206 size_t suffixLen = suffix.size();
207 size_t strLen = strlen(str);
agl@chromium.org0afaf9b2010-07-28 17:10:28 +0000208
reed@android.com6f252972009-01-14 16:46:16 +0000209 return strLen >= suffixLen &&
reed@android.com6f252972009-01-14 16:46:16 +0000210 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211}
212
213#include <sys/stat.h>
214
215bool SkOSFile::Iter::next(SkString* name, bool getDir)
216{
reed@android.com6f252972009-01-14 16:46:16 +0000217 if (fDIR)
218 {
219 dirent* entry;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220
reed@android.com6f252972009-01-14 16:46:16 +0000221 while ((entry = ::readdir(fDIR)) != NULL)
222 {
223 struct stat s;
224 SkString str(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
reed@android.com6f252972009-01-14 16:46:16 +0000226 if (!str.endsWith("/") && !str.endsWith("\\"))
227 str.append("/");
228 str.append(entry->d_name);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
reed@android.com6f252972009-01-14 16:46:16 +0000230 if (0 == stat(str.c_str(), &s))
231 {
232 if (getDir)
233 {
234 if (s.st_mode & S_IFDIR)
235 break;
236 }
237 else
238 {
239 if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
240 break;
241 }
242 }
243 }
244 if (entry) // we broke out with a file
245 {
246 if (name)
247 name->set(entry->d_name);
248 return true;
249 }
250 }
251 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252}
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000253#endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS