OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkOSFile.h" | 8 #include "SkOSFile.h" |
9 | 9 |
10 #include "SkTFitsIn.h" | 10 #include "SkTFitsIn.h" |
| 11 #include "SkTypes.h" |
11 | 12 |
| 13 #include <dirent.h> |
12 #include <stdio.h> | 14 #include <stdio.h> |
13 #include <sys/mman.h> | 15 #include <sys/mman.h> |
14 #include <sys/stat.h> | 16 #include <sys/stat.h> |
15 #include <sys/types.h> | 17 #include <sys/types.h> |
16 #include <unistd.h> | 18 #include <unistd.h> |
17 | 19 |
18 bool sk_exists(const char *path, SkFILE_Flags flags) { | 20 bool sk_exists(const char *path, SkFILE_Flags flags) { |
19 int mode = F_OK; | 21 int mode = F_OK; |
20 if (flags & kRead_SkFILE_Flag) { | 22 if (flags & kRead_SkFILE_Flag) { |
21 mode |= R_OK; | 23 mode |= R_OK; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } | 85 } |
84 | 86 |
85 void* sk_fmmap(SkFILE* f, size_t* size) { | 87 void* sk_fmmap(SkFILE* f, size_t* size) { |
86 int fd = sk_fileno(f); | 88 int fd = sk_fileno(f); |
87 if (fd < 0) { | 89 if (fd < 0) { |
88 return NULL; | 90 return NULL; |
89 } | 91 } |
90 | 92 |
91 return sk_fdmmap(fd, size); | 93 return sk_fdmmap(fd, size); |
92 } | 94 } |
| 95 |
| 96 //////////////////////////////////////////////////////////////////////////// |
| 97 |
| 98 struct SkOSFileIterData { |
| 99 SkOSFileIterData() : fDIR(0) { } |
| 100 DIR* fDIR; |
| 101 SkString fPath, fSuffix; |
| 102 }; |
| 103 SK_COMPILE_ASSERT(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, not_
enough_space); |
| 104 |
| 105 SkOSFile::Iter::Iter() { |
| 106 SkNEW_PLACEMENT(fSelf.get(), SkOSFileIterData); |
| 107 } |
| 108 |
| 109 SkOSFile::Iter::Iter(const char path[], const char suffix[]) { |
| 110 SkNEW_PLACEMENT(fSelf.get(), SkOSFileIterData); |
| 111 this->reset(path, suffix); |
| 112 } |
| 113 |
| 114 SkOSFile::Iter::~Iter() { |
| 115 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 116 if (self.fDIR) { |
| 117 ::closedir(self.fDIR); |
| 118 } |
| 119 self.~SkOSFileIterData(); |
| 120 } |
| 121 |
| 122 void SkOSFile::Iter::reset(const char path[], const char suffix[]) { |
| 123 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 124 if (self.fDIR) { |
| 125 ::closedir(self.fDIR); |
| 126 self.fDIR = 0; |
| 127 } |
| 128 |
| 129 self.fPath.set(path); |
| 130 if (path) { |
| 131 self.fDIR = ::opendir(path); |
| 132 self.fSuffix.set(suffix); |
| 133 } else { |
| 134 self.fSuffix.reset(); |
| 135 } |
| 136 } |
| 137 |
| 138 // returns true if suffix is empty, or if str ends with suffix |
| 139 static bool issuffixfor(const SkString& suffix, const char str[]) { |
| 140 size_t suffixLen = suffix.size(); |
| 141 size_t strLen = strlen(str); |
| 142 |
| 143 return strLen >= suffixLen && |
| 144 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0; |
| 145 } |
| 146 |
| 147 bool SkOSFile::Iter::next(SkString* name, bool getDir) { |
| 148 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 149 if (self.fDIR) { |
| 150 dirent* entry; |
| 151 |
| 152 while ((entry = ::readdir(self.fDIR)) != NULL) { |
| 153 struct stat s; |
| 154 SkString str(self.fPath); |
| 155 |
| 156 if (!str.endsWith("/") && !str.endsWith("\\")) { |
| 157 str.append("/"); |
| 158 } |
| 159 str.append(entry->d_name); |
| 160 |
| 161 if (0 == stat(str.c_str(), &s)) { |
| 162 if (getDir) { |
| 163 if (s.st_mode & S_IFDIR) { |
| 164 break; |
| 165 } |
| 166 } else { |
| 167 if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entr
y->d_name)) { |
| 168 break; |
| 169 } |
| 170 } |
| 171 } |
| 172 } |
| 173 if (entry) { // we broke out with a file |
| 174 if (name) { |
| 175 name->set(entry->d_name); |
| 176 } |
| 177 return true; |
| 178 } |
| 179 } |
| 180 return false; |
| 181 } |
OLD | NEW |