Commit eec0add2 authored by Brad King's avatar Brad King
Browse files

Directory: Return Status from Load

parent 85e76a10
......@@ -178,6 +178,7 @@ if(KWSYS_USE_System)
endif()
if(KWSYS_USE_Directory)
set(KWSYS_USE_Encoding 1)
set(KWSYS_USE_Status 1)
endif()
if(KWSYS_USE_DynamicLoader)
set(KWSYS_USE_Encoding 1)
......
......@@ -94,7 +94,7 @@ void Directory::Clear()
namespace KWSYS_NAMESPACE {
bool Directory::Load(const std::string& name, std::string* errorMessage)
Status Directory::Load(std::string const& name, std::string* errorMessage)
{
this->Clear();
intptr_t srchHandle;
......@@ -121,21 +121,11 @@ bool Directory::Load(const std::string& name, std::string* errorMessage)
delete[] buf;
if (srchHandle == -1) {
Status status = Status::POSIX_errno();
if (errorMessage) {
if (unsigned int errorId = GetLastError()) {
LPSTR message = nullptr;
DWORD size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&message, 0, nullptr);
*errorMessage = std::string(message, size);
LocalFree(message);
} else {
*errorMessage = "Unknown error.";
}
*errorMessage = status.GetString();
}
return false;
return status;
}
// Loop through names
......@@ -143,7 +133,14 @@ bool Directory::Load(const std::string& name, std::string* errorMessage)
this->Internal->Files.push_back(Encoding::ToNarrow(data.name));
} while (_wfindnext(srchHandle, &data) != -1);
this->Internal->Path = name;
return _findclose(srchHandle) != -1;
if (_findclose(srchHandle) == -1) {
Status status = Status::POSIX_errno();
if (errorMessage) {
*errorMessage = status.GetString();
}
return status;
}
return Status::Success();
}
unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
......@@ -220,7 +217,7 @@ unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
namespace KWSYS_NAMESPACE {
bool Directory::Load(const std::string& name, std::string* errorMessage)
Status Directory::Load(std::string const& name, std::string* errorMessage)
{
this->Clear();
......@@ -231,7 +228,7 @@ bool Directory::Load(const std::string& name, std::string* errorMessage)
if (errorMessage != nullptr) {
*errorMessage = std::string(strerror(errno));
}
return false;
return Status::POSIX_errno();
}
errno = 0;
......@@ -242,12 +239,12 @@ bool Directory::Load(const std::string& name, std::string* errorMessage)
if (errorMessage != nullptr) {
*errorMessage = std::string(strerror(errno));
}
return false;
return Status::POSIX_errno();
}
this->Internal->Path = name;
closedir(dir);
return true;
return Status::Success();
}
unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
......
......@@ -4,6 +4,7 @@
#define @KWSYS_NAMESPACE@_Directory_hxx
#include <@KWSYS_NAMESPACE@/Configure.h>
#include <@KWSYS_NAMESPACE@/Status.hxx>
#include <string>
......@@ -32,10 +33,9 @@ public:
/**
* Load the specified directory and load the names of the files
* in that directory. 0 is returned if the directory can not be
* opened, 1 if it is opened.
* in that directory.
*/
bool Load(const std::string&, std::string* errorMessage = nullptr);
Status Load(std::string const&, std::string* errorMessage = nullptr);
/**
* Return the number of files in the current directory.
......
......@@ -2440,7 +2440,7 @@ bool SystemTools::CopyADirectory(const std::string& source,
const std::string& destination, bool always)
{
Directory dir;
if (dir.Load(source) == 0) {
if (!dir.Load(source)) {
return false;
}
size_t fileNum;
......
......@@ -88,7 +88,7 @@ int _nonExistentDirectoryTest()
errorMessage = "foo";
// Increment res failure if directory lists
res += testdir.Load(testdirpath, &errorMessage);
res += testdir.Load(testdirpath, &errorMessage) ? 1 : 0;
#if !defined(_WIN32) || defined(__CYGWIN__)
// Increment res failure if errorMessage is unmodified
res += (errorMessage == "foo");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment