2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 SkString SkOSPath::Join(const char *rootPath, const char *relativePath) {
10 SkString result(rootPath);
11 if (!result.endsWith(SkPATH_SEPARATOR) && !result.isEmpty()) {
12 result.appendUnichar(SkPATH_SEPARATOR);
14 result.append(relativePath);
18 SkString SkOSPath::Basename(const char* fullPath) {
22 const char* filename = strrchr(fullPath, SkPATH_SEPARATOR);
23 if (NULL == filename) {
28 return SkString(filename);
31 SkString SkOSPath::Dirname(const char* fullPath) {
35 const char* end = strrchr(fullPath, SkPATH_SEPARATOR);
39 if (end == fullPath) {
40 SkASSERT(fullPath[0] == SkPATH_SEPARATOR);
43 return SkString(fullPath, end - fullPath);
46 #ifdef SK_BUILD_FOR_WIN
48 static uint16_t* concat_to_16(const char src[], const char suffix[])
50 size_t i, len = strlen(src);
51 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
52 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
54 for (i = 0; i < len; i++)
57 if (i > 0 && dst[i-1] != '/')
67 SkASSERT(i + 1 <= len + len2);
72 ////////////////////////////////////////////////////////////////////////////
74 SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
78 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
80 this->reset(path, suffix);
83 SkOSFile::Iter::~Iter()
90 void SkOSFile::Iter::reset(const char path[], const char suffix[])
101 fPath16 = concat_to_16(path, suffix);
104 static bool is_magic_dir(const uint16_t dir[])
106 // return true for "." and ".."
107 return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
110 static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
112 WIN32_FIND_DATAW data;
116 if (::FindNextFileW(handle, &data))
126 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
131 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
134 if (!::FindNextFileW(handle, dataPtr))
137 // if we get here, we've found a file/dir
139 name->setUTF16((uint16_t*)dataPtr->cFileName);
143 bool SkOSFile::Iter::next(SkString* name, bool getDir)
145 WIN32_FIND_DATAW data;
146 WIN32_FIND_DATAW* dataPtr = NULL;
148 if (fHandle == 0) // our first time
150 if (fPath16 == NULL || *fPath16 == 0) // check for no path
153 fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
154 if (fHandle != 0 && fHandle != (HANDLE)~0)
157 return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
160 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
163 OSStatus FSPathMakeRef (
166 Boolean * isDirectory
170 SkOSFile::Iter::Iter() : fDIR(0)
174 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
176 this->reset(path, suffix);
179 SkOSFile::Iter::~Iter()
185 void SkOSFile::Iter::reset(const char path[], const char suffix[])
196 fDIR = ::opendir(path);
203 // returns true if suffix is empty, or if str ends with suffix
204 static bool issuffixfor(const SkString& suffix, const char str[])
206 size_t suffixLen = suffix.size();
207 size_t strLen = strlen(str);
209 return strLen >= suffixLen &&
210 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
213 #include <sys/stat.h>
215 bool SkOSFile::Iter::next(SkString* name, bool getDir)
221 while ((entry = ::readdir(fDIR)) != NULL)
226 if (!str.endsWith("/") && !str.endsWith("\\"))
228 str.append(entry->d_name);
230 if (0 == stat(str.c_str(), &s))
234 if (s.st_mode & S_IFDIR)
239 if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
244 if (entry) // we broke out with a file
247 name->set(entry->d_name);
253 #endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS