未验证 提交 0bd839be 编写于 作者: G Gines 提交者: GitHub

Removed most boost dependencies (#368)

* Remove the boost dependencies in fileSystem.cpp

* build ok

* Update fileSystem.cpp
上级 07870d76
......@@ -7,12 +7,10 @@ namespace op
{
OP_API void mkdir(const std::string& directoryPath);
OP_API bool existDir(const std::string& directoryPath);
OP_API bool existDirectory(const std::string& directoryPath);
OP_API bool existFile(const std::string& filePath);
OP_API bool isDirectory(const std::string& directoryPath);
/**
* This function makes sure that the directoryPathString is properly formatted. I.e., it
* changes all '\' by '/', and it makes sure that the string finishes with '/'.
......
#include <cstdio> // fopen
#include <dirent.h> // opendir
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <openpose/utilities/string.hpp>
#include <openpose/utilities/fileSystem.hpp>
// The only remaining boost dependency part
// We could use the mkdir method to create dir
// in unix or windows
// #include <algorithm>
// #include <cstring>
// #include <fstream>
// #include <memory>
// #include <unistd.h>
//#if defined _MSC_VER
//#include <direct.h>
//#elif defined __GNUC__
//#include <sys/types.h>
//#include <sys/stat.h>
//#endif
namespace op
{
void mkdir(const std::string& directoryPath)
......@@ -12,10 +28,17 @@ namespace op
{
if (!directoryPath.empty())
{
//#if defined _MSC_VER
//_mkdir(directoryPath.c_str());
//#elif defined __GNUC__
//mkdir(directoryPath.data(), S_IRUSR | S_IWUSR | S_IXUSR);
//#endif
// Create folder if it does not exist
const boost::filesystem::path directory{directoryPath};
if (!isDirectory(directoryPath) && !boost::filesystem::create_directory(directory))
error("Could not write to or create directory to save processed frames.", __LINE__, __FUNCTION__, __FILE__);
if (!existDirectory(directoryPath) && !boost::filesystem::create_directory(directory))
error("Could not write to or create directory to save processed frames.",
__LINE__, __FUNCTION__, __FILE__);
};
}
catch (const std::exception& e)
......@@ -24,12 +47,17 @@ namespace op
}
}
bool existDir(const std::string& directoryPath)
bool existDirectory(const std::string& directoryPath)
{
try
{
// Maybe existFile also works for directories in Ubuntu/Windows/Mac?
return boost::filesystem::exists(directoryPath);
if (auto* directory = opendir(directoryPath.c_str()))
{
closedir(directory);
return true;
}
else
return false;
}
catch (const std::exception& e)
{
......@@ -57,19 +85,6 @@ namespace op
}
}
bool isDirectory(const std::string& directoryPath)
{
try
{
return (!directoryPath.empty() && boost::filesystem::is_directory(directoryPath));
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
std::string formatAsDirectory(const std::string& directoryPathString)
{
try
......@@ -94,7 +109,11 @@ namespace op
{
try
{
return boost::filesystem::path{fullPath}.filename().string();
size_t lastSlashPos = fullPath.find_last_of("\\/");
if (lastSlashPos != std::string::npos)
return fullPath.substr(lastSlashPos+1, fullPath.size() - lastSlashPos - 1);
else
return fullPath;
}
catch (const std::exception& e)
{
......@@ -107,7 +126,14 @@ namespace op
{
try
{
return boost::filesystem::path{fullPath}.stem().string();
// Name + extension
std::string nameExt = getFileNameAndExtension(fullPath);
// Name
size_t dotPos = nameExt.find_last_of(".");
if (dotPos != std::string::npos)
return nameExt.substr(0, dotPos);
else
return nameExt;
}
catch (const std::exception& e)
{
......@@ -120,7 +146,14 @@ namespace op
{
try
{
return boost::filesystem::path{fullPath}.extension().string();
// Name + extension
std::string nameExt = getFileNameAndExtension(fullPath);
// Extension
size_t dotPos = nameExt.find_last_of(".");
if (dotPos != std::string::npos)
return nameExt.substr(dotPos + 1, nameExt.size() - dotPos - 1);
else
return "";
}
catch (const std::exception& e)
{
......@@ -174,18 +207,28 @@ namespace op
{
try
{
// Format the path first
std::string formatedPath = formatAsDirectory(directoryPath);
// Check folder exits
if (!existDir(directoryPath))
if (!existDirectory(directoryPath))
error("Folder " + directoryPath + " does not exist.", __LINE__, __FUNCTION__, __FILE__);
// Read images
std::vector<std::string> filePaths;
for (const auto& file : boost::make_iterator_range(boost::filesystem::directory_iterator{directoryPath}, {}))
if (!boost::filesystem::is_directory(file.status())) // Skip directories
filePaths.emplace_back(file.path().string());
std::shared_ptr<DIR> directoryPtr(
opendir(formatedPath.c_str()),
[](DIR* formatedPath){ formatedPath && closedir(formatedPath); }
);
struct dirent* direntPtr;
while ((direntPtr = readdir(directoryPtr.get())) != nullptr)
{
std::string currentPath = formatedPath + direntPtr->d_name;
if ((strncmp(direntPtr->d_name, ".", 1) == 0) || existDirectory(currentPath))
continue;
filePaths.push_back(currentPath);
}
// Check #files > 0
if (filePaths.empty())
error("No files were found on " + directoryPath, __LINE__, __FUNCTION__, __FILE__);
// If specific extensions specified
if (!extensions.empty())
{
......@@ -197,10 +240,9 @@ namespace op
specificExtensionPaths.emplace_back(filePath);
std::swap(filePaths, specificExtensionPaths);
}
// Sort alphabetically
std::sort(filePaths.begin(), filePaths.end());
// Return result
return filePaths;
}
catch (const std::exception& e)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册