epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SkOSFile.h" |
| 8 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 9 | SkString SkOSPath::Join(const char *rootPath, const char *relativePath) { |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 10 | SkString result(rootPath); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame^] | 11 | if (!result.endsWith(SkPATH_SEPARATOR) && !result.isEmpty()) { |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 12 | result.appendUnichar(SkPATH_SEPARATOR); |
| 13 | } |
| 14 | result.append(relativePath); |
| 15 | return result; |
| 16 | } |
| 17 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 18 | SkString SkOSPath::Basename(const char* fullPath) { |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 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 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame^] | 31 | SkString 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | #ifdef SK_BUILD_FOR_WIN |
| 47 | |
reed@android.com | b50a60c | 2009-01-14 17:51:08 +0000 | [diff] [blame] | 48 | static uint16_t* concat_to_16(const char src[], const char suffix[]) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 50 | size_t i, len = strlen(src); |
| 51 | size_t len2 = 3 + (suffix ? strlen(suffix) : 0); |
reed@android.com | b50a60c | 2009-01-14 17:51:08 +0000 | [diff] [blame] | 52 | uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 54 | for (i = 0; i < len; i++) |
| 55 | dst[i] = src[i]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 57 | if (i > 0 && dst[i-1] != '/') |
| 58 | dst[i++] = '/'; |
| 59 | dst[i++] = '*'; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 61 | if (suffix) |
| 62 | { |
| 63 | while (*suffix) |
| 64 | dst[i++] = *suffix++; |
| 65 | } |
| 66 | dst[i] = 0; |
| 67 | SkASSERT(i + 1 <= len + len2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 69 | return dst; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | //////////////////////////////////////////////////////////////////////////// |
| 73 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 74 | SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | { |
| 76 | } |
| 77 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 78 | SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 80 | this->reset(path, suffix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | SkOSFile::Iter::~Iter() |
| 84 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 85 | sk_free(fPath16); |
| 86 | if (fHandle) |
| 87 | ::FindClose(fHandle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void SkOSFile::Iter::reset(const char path[], const char suffix[]) |
| 91 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 92 | if (fHandle) |
| 93 | { |
| 94 | ::FindClose(fHandle); |
| 95 | fHandle = 0; |
| 96 | } |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 97 | if (NULL == path) |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 98 | path = ""; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 100 | sk_free(fPath16); |
| 101 | fPath16 = concat_to_16(path, suffix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
reed@android.com | b50a60c | 2009-01-14 17:51:08 +0000 | [diff] [blame] | 104 | static bool is_magic_dir(const uint16_t dir[]) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 106 | // return true for "." and ".." |
| 107 | return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir) |
| 111 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 112 | WIN32_FIND_DATAW data; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 114 | if (NULL == dataPtr) |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 115 | { |
| 116 | if (::FindNextFileW(handle, &data)) |
| 117 | dataPtr = &data; |
| 118 | else |
| 119 | return false; |
| 120 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 122 | for (;;) |
| 123 | { |
| 124 | if (getDir) |
| 125 | { |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 126 | if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName)) |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 127 | 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.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 139 | name->setUTF16((uint16_t*)dataPtr->cFileName); |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 140 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool SkOSFile::Iter::next(SkString* name, bool getDir) |
| 144 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 145 | WIN32_FIND_DATAW data; |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 146 | WIN32_FIND_DATAW* dataPtr = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 148 | if (fHandle == 0) // our first time |
| 149 | { |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 150 | if (fPath16 == NULL || *fPath16 == 0) // check for no path |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 151 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | c8ad63e | 2011-03-18 14:29:44 +0000 | [diff] [blame] | 153 | fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data); |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 154 | if (fHandle != 0 && fHandle != (HANDLE)~0) |
| 155 | dataPtr = &data; |
| 156 | } |
| 157 | return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
caryclark@google.com | 867cbd8 | 2012-09-20 15:45:41 +0000 | [diff] [blame] | 160 | #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | |
| 162 | #if 0 |
| 163 | OSStatus FSPathMakeRef ( |
| 164 | const UInt8 * path, |
| 165 | FSRef * ref, |
| 166 | Boolean * isDirectory |
| 167 | ); |
| 168 | #endif |
| 169 | |
| 170 | SkOSFile::Iter::Iter() : fDIR(0) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0) |
| 175 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 176 | this->reset(path, suffix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | SkOSFile::Iter::~Iter() |
| 180 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 181 | if (fDIR) |
| 182 | ::closedir(fDIR); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void SkOSFile::Iter::reset(const char path[], const char suffix[]) |
| 186 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 187 | if (fDIR) |
| 188 | { |
| 189 | ::closedir(fDIR); |
| 190 | fDIR = 0; |
| 191 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 193 | fPath.set(path); |
| 194 | if (path) |
| 195 | { |
| 196 | fDIR = ::opendir(path); |
| 197 | fSuffix.set(suffix); |
| 198 | } |
| 199 | else |
| 200 | fSuffix.reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // returns true if suffix is empty, or if str ends with suffix |
| 204 | static bool issuffixfor(const SkString& suffix, const char str[]) |
| 205 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 206 | size_t suffixLen = suffix.size(); |
| 207 | size_t strLen = strlen(str); |
agl@chromium.org | 0afaf9b | 2010-07-28 17:10:28 +0000 | [diff] [blame] | 208 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 209 | return strLen >= suffixLen && |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 210 | memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | #include <sys/stat.h> |
| 214 | |
| 215 | bool SkOSFile::Iter::next(SkString* name, bool getDir) |
| 216 | { |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 217 | if (fDIR) |
| 218 | { |
| 219 | dirent* entry; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 221 | while ((entry = ::readdir(fDIR)) != NULL) |
| 222 | { |
| 223 | struct stat s; |
| 224 | SkString str(fPath); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 226 | if (!str.endsWith("/") && !str.endsWith("\\")) |
| 227 | str.append("/"); |
| 228 | str.append(entry->d_name); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
reed@android.com | 6f25297 | 2009-01-14 16:46:16 +0000 | [diff] [blame] | 230 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | } |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 253 | #endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS |