| 208 | } |
| 209 | |
| 210 | unsigned long Directory::GetNumberOfFilesInDirectory(std::string const& name, |
| 211 | std::string* errorMessage) |
| 212 | { |
| 213 | HANDLE srchHandle; |
| 214 | char* buf; |
| 215 | size_t bufLength; |
| 216 | size_t n = name.size(); |
| 217 | if (name.back() == '/') { |
| 218 | bufLength = n + 1 + 1; |
| 219 | buf = new char[n + 1 + 1]; |
| 220 | snprintf(buf, bufLength, "%s*", name.c_str()); |
| 221 | } else { |
| 222 | bufLength = n + 2 + 1; |
| 223 | buf = new char[n + 2 + 1]; |
| 224 | snprintf(buf, bufLength, "%s/*", name.c_str()); |
| 225 | } |
| 226 | WIN32_FIND_DATAW data; // data of current file |
| 227 | |
| 228 | // Now put them into the file array |
| 229 | srchHandle = FindFirstFileW(Encoding::ToWide(buf).c_str(), &data); |
| 230 | delete[] buf; |
| 231 | |
| 232 | if (srchHandle == INVALID_HANDLE_VALUE) { |
| 233 | if (errorMessage) { |
| 234 | if (unsigned int errorId = GetLastError()) { |
| 235 | LPSTR message = nullptr; |
| 236 | DWORD size = FormatMessageA( |
| 237 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 238 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 239 | nullptr, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 240 | (LPSTR)&message, 0, nullptr); |
| 241 | *errorMessage = std::string(message, size); |
| 242 | LocalFree(message); |
| 243 | } else { |
| 244 | *errorMessage = "Unknown error."; |
| 245 | } |
| 246 | } |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | // Loop through names |
| 251 | unsigned long count = 0; |
| 252 | do { |
| 253 | count++; |
| 254 | } while (FindNextFileW(srchHandle, &data)); |
| 255 | FindClose(srchHandle); |
| 256 | return count; |
| 257 | } |
| 258 | |
| 259 | } // namespace KWSYS_NAMESPACE |
| 260 | |