Cleanup: Rename SkOSPath functions.
[platform/upstream/libSkiaSharp.git] / src / utils / SkOSFile.cpp
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  */
7 #include "SkOSFile.h"
8
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);
13     }
14     result.append(relativePath);
15     return result;
16 }
17
18 SkString SkOSPath::Basename(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
31 #ifdef SK_BUILD_FOR_WIN
32
33 static uint16_t* concat_to_16(const char src[], const char suffix[])
34 {
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));
38
39     for (i = 0; i < len; i++)
40         dst[i] = src[i];
41
42     if (i > 0 && dst[i-1] != '/')
43         dst[i++] = '/';
44     dst[i++] = '*';
45
46     if (suffix)
47     {
48         while (*suffix)
49             dst[i++] = *suffix++;
50     }
51     dst[i] = 0;
52     SkASSERT(i + 1 <= len + len2);
53
54     return dst;
55 }
56
57 ////////////////////////////////////////////////////////////////////////////
58
59 SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
60 {
61 }
62
63 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
64 {
65     this->reset(path, suffix);
66 }
67
68 SkOSFile::Iter::~Iter()
69 {
70     sk_free(fPath16);
71     if (fHandle)
72         ::FindClose(fHandle);
73 }
74
75 void SkOSFile::Iter::reset(const char path[], const char suffix[])
76 {
77     if (fHandle)
78     {
79         ::FindClose(fHandle);
80         fHandle = 0;
81     }
82     if (NULL == path)
83         path = "";
84
85     sk_free(fPath16);
86     fPath16 = concat_to_16(path, suffix);
87 }
88
89 static bool is_magic_dir(const uint16_t dir[])
90 {
91     // return true for "." and ".."
92     return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
93 }
94
95 static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
96 {
97     WIN32_FIND_DATAW    data;
98
99     if (NULL == dataPtr)
100     {
101         if (::FindNextFileW(handle, &data))
102             dataPtr = &data;
103         else
104             return false;
105     }
106
107     for (;;)
108     {
109         if (getDir)
110         {
111             if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
112                 break;
113         }
114         else
115         {
116             if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
117                 break;
118         }
119         if (!::FindNextFileW(handle, dataPtr))
120             return false;
121     }
122     // if we get here, we've found a file/dir
123     if (name)
124         name->setUTF16((uint16_t*)dataPtr->cFileName);
125     return true;
126 }
127
128 bool SkOSFile::Iter::next(SkString* name, bool getDir)
129 {
130     WIN32_FIND_DATAW    data;
131     WIN32_FIND_DATAW*   dataPtr = NULL;
132
133     if (fHandle == 0)   // our first time
134     {
135         if (fPath16 == NULL || *fPath16 == 0)    // check for no path
136             return false;
137
138         fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
139         if (fHandle != 0 && fHandle != (HANDLE)~0)
140             dataPtr = &data;
141     }
142     return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
143 }
144
145 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
146
147 #if 0
148 OSStatus FSPathMakeRef (
149    const UInt8 * path,
150    FSRef * ref,
151    Boolean * isDirectory
152 );
153 #endif
154
155 SkOSFile::Iter::Iter() : fDIR(0)
156 {
157 }
158
159 SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
160 {
161     this->reset(path, suffix);
162 }
163
164 SkOSFile::Iter::~Iter()
165 {
166     if (fDIR)
167         ::closedir(fDIR);
168 }
169
170 void SkOSFile::Iter::reset(const char path[], const char suffix[])
171 {
172     if (fDIR)
173     {
174         ::closedir(fDIR);
175         fDIR = 0;
176     }
177
178     fPath.set(path);
179     if (path)
180     {
181         fDIR = ::opendir(path);
182         fSuffix.set(suffix);
183     }
184     else
185         fSuffix.reset();
186 }
187
188 // returns true if suffix is empty, or if str ends with suffix
189 static bool issuffixfor(const SkString& suffix, const char str[])
190 {
191     size_t  suffixLen = suffix.size();
192     size_t  strLen = strlen(str);
193
194     return  strLen >= suffixLen &&
195             memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
196 }
197
198 #include <sys/stat.h>
199
200 bool SkOSFile::Iter::next(SkString* name, bool getDir)
201 {
202     if (fDIR)
203     {
204         dirent* entry;
205
206         while ((entry = ::readdir(fDIR)) != NULL)
207         {
208             struct stat s;
209             SkString    str(fPath);
210
211             if (!str.endsWith("/") && !str.endsWith("\\"))
212                 str.append("/");
213             str.append(entry->d_name);
214
215             if (0 == stat(str.c_str(), &s))
216             {
217                 if (getDir)
218                 {
219                     if (s.st_mode & S_IFDIR)
220                         break;
221                 }
222                 else
223                 {
224                     if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
225                         break;
226                 }
227             }
228         }
229         if (entry)  // we broke out with a file
230         {
231             if (name)
232                 name->set(entry->d_name);
233             return true;
234         }
235     }
236     return false;
237 }
238 #endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS