blob: 0a403750b8b9f44165163ef61475b1465ec22144 [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
scroggo@google.comccd7afb2013-05-28 16:45:07 +00009SkString SkOSPath::SkPathJoin(const char *rootPath, const char *relativePath) {
10 SkString result(rootPath);
11 if (!result.endsWith(SkPATH_SEPARATOR)) {
12 result.appendUnichar(SkPATH_SEPARATOR);
13 }
14 result.append(relativePath);
15 return result;
16}
17
18SkString SkOSPath::SkBasename(const char* fullPath) {
19 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
reed@android.com8a1c16f2008-12-17 15:59:43 +000031#ifdef SK_BUILD_FOR_WIN
32
reed@android.comb50a60c2009-01-14 17:51:08 +000033static uint16_t* concat_to_16(const char src[], const char suffix[])
reed@android.com8a1c16f2008-12-17 15:59:43 +000034{
reed@android.com6f252972009-01-14 16:46:16 +000035 size_t i, len = strlen(src);
36 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
reed@android.comb50a60c2009-01-14 17:51:08 +000037 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
reed@android.com6f252972009-01-14 16:46:16 +000039 for (i = 0; i < len; i++)
40 dst[i] = src[i];
reed@android.com8a1c16f2008-12-17 15:59:43 +000041
reed@android.com6f252972009-01-14 16:46:16 +000042 if (i > 0 && dst[i-1] != '/')
43 dst[i++] = '/';
44 dst[i++] = '*';
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
reed@android.com6f252972009-01-14 16:46:16 +000046 if (suffix)
47 {
48 while (*suffix)
49 dst[i++] = *suffix++;
50 }
51 dst[i] = 0;
52 SkASSERT(i + 1 <= len + len2);
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
reed@android.com6f252972009-01-14 16:46:16 +000054 return dst;
reed@android.com8a1c16f2008-12-17 15:59:43 +000055}
56
57SkUTF16_Str::SkUTF16_Str(const char src[])
58{
reed@android.com6f252972009-01-14 16:46:16 +000059 size_t len = strlen(src);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
reed@android.comb50a60c2009-01-14 17:51:08 +000061 fStr = (uint16_t*)sk_malloc_throw((len + 1) * sizeof(uint16_t));
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000062 size_t i;
63 for (i = 0; i < len; i++)
reed@android.com6f252972009-01-14 16:46:16 +000064 fStr[i] = src[i];
65 fStr[i] = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066}
67
68////////////////////////////////////////////////////////////////////////////
69
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000070SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +000071{
72}
73
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000074SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +000075{
reed@android.com6f252972009-01-14 16:46:16 +000076 this->reset(path, suffix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077}
78
79SkOSFile::Iter::~Iter()
80{
reed@android.com6f252972009-01-14 16:46:16 +000081 sk_free(fPath16);
82 if (fHandle)
83 ::FindClose(fHandle);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084}
85
86void SkOSFile::Iter::reset(const char path[], const char suffix[])
87{
reed@android.com6f252972009-01-14 16:46:16 +000088 if (fHandle)
89 {
90 ::FindClose(fHandle);
91 fHandle = 0;
92 }
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +000093 if (NULL == path)
reed@android.com6f252972009-01-14 16:46:16 +000094 path = "";
reed@android.com8a1c16f2008-12-17 15:59:43 +000095
reed@android.com6f252972009-01-14 16:46:16 +000096 sk_free(fPath16);
97 fPath16 = concat_to_16(path, suffix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098}
99
reed@android.comb50a60c2009-01-14 17:51:08 +0000100static bool is_magic_dir(const uint16_t dir[])
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101{
reed@android.com6f252972009-01-14 16:46:16 +0000102 // return true for "." and ".."
103 return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104}
105
106static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
107{
reed@android.com6f252972009-01-14 16:46:16 +0000108 WIN32_FIND_DATAW data;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000110 if (NULL == dataPtr)
reed@android.com6f252972009-01-14 16:46:16 +0000111 {
112 if (::FindNextFileW(handle, &data))
113 dataPtr = &data;
114 else
115 return false;
116 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117
reed@android.com6f252972009-01-14 16:46:16 +0000118 for (;;)
119 {
120 if (getDir)
121 {
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000122 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
reed@android.com6f252972009-01-14 16:46:16 +0000123 break;
124 }
125 else
126 {
127 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
128 break;
129 }
130 if (!::FindNextFileW(handle, dataPtr))
131 return false;
132 }
133 // if we get here, we've found a file/dir
134 if (name)
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000135 name->setUTF16((uint16_t*)dataPtr->cFileName);
reed@android.com6f252972009-01-14 16:46:16 +0000136 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137}
138
139bool SkOSFile::Iter::next(SkString* name, bool getDir)
140{
reed@android.com6f252972009-01-14 16:46:16 +0000141 WIN32_FIND_DATAW data;
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000142 WIN32_FIND_DATAW* dataPtr = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143
reed@android.com6f252972009-01-14 16:46:16 +0000144 if (fHandle == 0) // our first time
145 {
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000146 if (fPath16 == NULL || *fPath16 == 0) // check for no path
reed@android.com6f252972009-01-14 16:46:16 +0000147 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +0000149 fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
reed@android.com6f252972009-01-14 16:46:16 +0000150 if (fHandle != 0 && fHandle != (HANDLE)~0)
151 dataPtr = &data;
152 }
153 return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154}
155
caryclark@google.com867cbd82012-09-20 15:45:41 +0000156#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 +0000157
158#if 0
159OSStatus FSPathMakeRef (
160 const UInt8 * path,
161 FSRef * ref,
162 Boolean * isDirectory
163);
164#endif
165
166SkOSFile::Iter::Iter() : fDIR(0)
167{
168}
169
170SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
171{
reed@android.com6f252972009-01-14 16:46:16 +0000172 this->reset(path, suffix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173}
174
175SkOSFile::Iter::~Iter()
176{
reed@android.com6f252972009-01-14 16:46:16 +0000177 if (fDIR)
178 ::closedir(fDIR);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179}
180
181void SkOSFile::Iter::reset(const char path[], const char suffix[])
182{
reed@android.com6f252972009-01-14 16:46:16 +0000183 if (fDIR)
184 {
185 ::closedir(fDIR);
186 fDIR = 0;
187 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188
reed@android.com6f252972009-01-14 16:46:16 +0000189 fPath.set(path);
190 if (path)
191 {
192 fDIR = ::opendir(path);
193 fSuffix.set(suffix);
194 }
195 else
196 fSuffix.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197}
198
199// returns true if suffix is empty, or if str ends with suffix
200static bool issuffixfor(const SkString& suffix, const char str[])
201{
reed@android.com6f252972009-01-14 16:46:16 +0000202 size_t suffixLen = suffix.size();
203 size_t strLen = strlen(str);
agl@chromium.org0afaf9b2010-07-28 17:10:28 +0000204
reed@android.com6f252972009-01-14 16:46:16 +0000205 return strLen >= suffixLen &&
reed@android.com6f252972009-01-14 16:46:16 +0000206 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207}
208
209#include <sys/stat.h>
210
211bool SkOSFile::Iter::next(SkString* name, bool getDir)
212{
reed@android.com6f252972009-01-14 16:46:16 +0000213 if (fDIR)
214 {
215 dirent* entry;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216
reed@android.com6f252972009-01-14 16:46:16 +0000217 while ((entry = ::readdir(fDIR)) != NULL)
218 {
219 struct stat s;
220 SkString str(fPath);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221
reed@android.com6f252972009-01-14 16:46:16 +0000222 if (!str.endsWith("/") && !str.endsWith("\\"))
223 str.append("/");
224 str.append(entry->d_name);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
reed@android.com6f252972009-01-14 16:46:16 +0000226 if (0 == stat(str.c_str(), &s))
227 {
228 if (getDir)
229 {
230 if (s.st_mode & S_IFDIR)
231 break;
232 }
233 else
234 {
235 if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
236 break;
237 }
238 }
239 }
240 if (entry) // we broke out with a file
241 {
242 if (name)
243 name->set(entry->d_name);
244 return true;
245 }
246 }
247 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248}
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000249#endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS