| 155 | namespace KWSYS_NAMESPACE { |
| 156 | |
| 157 | Status Directory::Load(std::string const& name, std::string* errorMessage) |
| 158 | { |
| 159 | this->Clear(); |
| 160 | HANDLE srchHandle; |
| 161 | char* buf; |
| 162 | size_t bufLength; |
| 163 | size_t n = name.size(); |
| 164 | if (name.back() == '/' || name.back() == '\\') { |
| 165 | bufLength = n + 1 + 1; |
| 166 | buf = new char[bufLength]; |
| 167 | snprintf(buf, bufLength, "%s*", name.c_str()); |
| 168 | } else { |
| 169 | // Make sure the slashes in the wildcard suffix are consistent with the |
| 170 | // rest of the path |
| 171 | bufLength = n + 2 + 1; |
| 172 | buf = new char[bufLength]; |
| 173 | if (name.find('\\') != std::string::npos) { |
| 174 | snprintf(buf, bufLength, "%s\\*", name.c_str()); |
| 175 | } else { |
| 176 | snprintf(buf, bufLength, "%s/*", name.c_str()); |
| 177 | } |
| 178 | } |
| 179 | WIN32_FIND_DATAW data; // data of current file |
| 180 | |
| 181 | // Now put them into the file array |
| 182 | srchHandle = |
| 183 | FindFirstFileW(Encoding::ToWindowsExtendedPath(buf).c_str(), &data); |
| 184 | delete[] buf; |
| 185 | |
| 186 | if (srchHandle == INVALID_HANDLE_VALUE) { |
| 187 | Status status = Status::Windows_GetLastError(); |
| 188 | if (errorMessage) { |
| 189 | *errorMessage = status.GetString(); |
| 190 | } |
| 191 | return status; |
| 192 | } |
| 193 | |
| 194 | // Loop through names |
| 195 | do { |
| 196 | this->Internal->Files.emplace_back(Encoding::ToNarrow(data.cFileName), |
| 197 | data); |
| 198 | } while (FindNextFileW(srchHandle, &data)); |
| 199 | this->Internal->Path = name; |
| 200 | if (!FindClose(srchHandle)) { |
| 201 | Status status = Status::Windows_GetLastError(); |
| 202 | if (errorMessage) { |
| 203 | *errorMessage = status.GetString(); |
| 204 | } |
| 205 | return status; |
| 206 | } |
| 207 | return Status::Success(); |
| 208 | } |
| 209 | |
| 210 | unsigned long Directory::GetNumberOfFilesInDirectory(std::string const& name, |
| 211 | std::string* errorMessage) |