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)) {
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 #ifdef SK_BUILD_FOR_WIN
33 static uint16_t* concat_to_16(const char src[], const char suffix[])
35 size_t i, len = strlen(src);
36 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
37 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
39 for (i = 0; i < len; i++)
42 if (i > 0 && dst[i-1] != '/')
52 SkASSERT(i + 1 <= len + len2);
57 ////////////////////////////////////////////////////////////////////////////
59 SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
63 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
65 this->reset(path, suffix);
68 SkOSFile::Iter::~Iter()
75 void SkOSFile::Iter::reset(const char path[], const char suffix[])
86 fPath16 = concat_to_16(path, suffix);
89 static bool is_magic_dir(const uint16_t dir[])
91 // return true for "." and ".."
92 return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
95 static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
97 WIN32_FIND_DATAW data;
101 if (::FindNextFileW(handle, &data))
111 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
116 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
119 if (!::FindNextFileW(handle, dataPtr))
122 // if we get here, we've found a file/dir
124 name->setUTF16((uint16_t*)dataPtr->cFileName);
128 bool SkOSFile::Iter::next(SkString* name, bool getDir)
130 WIN32_FIND_DATAW data;
131 WIN32_FIND_DATAW* dataPtr = NULL;
133 if (fHandle == 0) // our first time
135 if (fPath16 == NULL || *fPath16 == 0) // check for no path
138 fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
139 if (fHandle != 0 && fHandle != (HANDLE)~0)
142 return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
145 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
148 OSStatus FSPathMakeRef (
151 Boolean * isDirectory
155 SkOSFile::Iter::Iter() : fDIR(0)
159 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
161 this->reset(path, suffix);
164 SkOSFile::Iter::~Iter()
170 void SkOSFile::Iter::reset(const char path[], const char suffix[])
181 fDIR = ::opendir(path);
188 // returns true if suffix is empty, or if str ends with suffix
189 static bool issuffixfor(const SkString& suffix, const char str[])
191 size_t suffixLen = suffix.size();
192 size_t strLen = strlen(str);
194 return strLen >= suffixLen &&
195 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
198 #include <sys/stat.h>
200 bool SkOSFile::Iter::next(SkString* name, bool getDir)
206 while ((entry = ::readdir(fDIR)) != NULL)
211 if (!str.endsWith("/") && !str.endsWith("\\"))
213 str.append(entry->d_name);
215 if (0 == stat(str.c_str(), &s))
219 if (s.st_mode & S_IFDIR)
224 if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
229 if (entry) // we broke out with a file
232 name->set(entry->d_name);
238 #endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS