Skip to content

Commit 0bd839b

Browse files
Removed most boost dependencies (CMU-Perceptual-Computing-Lab#368)
* Remove the boost dependencies in fileSystem.cpp * build ok * Update fileSystem.cpp
1 parent 07870d7 commit 0bd839b

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

include/openpose/utilities/fileSystem.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ namespace op
77
{
88
OP_API void mkdir(const std::string& directoryPath);
99

10-
OP_API bool existDir(const std::string& directoryPath);
10+
OP_API bool existDirectory(const std::string& directoryPath);
1111

1212
OP_API bool existFile(const std::string& filePath);
1313

14-
OP_API bool isDirectory(const std::string& directoryPath);
15-
1614
/**
1715
* This function makes sure that the directoryPathString is properly formatted. I.e., it
1816
* changes all '\' by '/', and it makes sure that the string finishes with '/'.

src/openpose/utilities/fileSystem.cpp

+71-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
#include <cstdio> // fopen
2+
#include <dirent.h> // opendir
23
#include <boost/filesystem.hpp>
3-
#include <boost/range/iterator_range_core.hpp>
44
#include <openpose/utilities/string.hpp>
55
#include <openpose/utilities/fileSystem.hpp>
66

7+
// The only remaining boost dependency part
8+
// We could use the mkdir method to create dir
9+
// in unix or windows
10+
11+
// #include <algorithm>
12+
// #include <cstring>
13+
// #include <fstream>
14+
// #include <memory>
15+
// #include <unistd.h>
16+
//#if defined _MSC_VER
17+
//#include <direct.h>
18+
//#elif defined __GNUC__
19+
//#include <sys/types.h>
20+
//#include <sys/stat.h>
21+
//#endif
22+
723
namespace op
824
{
925
void mkdir(const std::string& directoryPath)
@@ -12,10 +28,17 @@ namespace op
1228
{
1329
if (!directoryPath.empty())
1430
{
31+
//#if defined _MSC_VER
32+
//_mkdir(directoryPath.c_str());
33+
//#elif defined __GNUC__
34+
//mkdir(directoryPath.data(), S_IRUSR | S_IWUSR | S_IXUSR);
35+
//#endif
36+
1537
// Create folder if it does not exist
1638
const boost::filesystem::path directory{directoryPath};
17-
if (!isDirectory(directoryPath) && !boost::filesystem::create_directory(directory))
18-
error("Could not write to or create directory to save processed frames.", __LINE__, __FUNCTION__, __FILE__);
39+
if (!existDirectory(directoryPath) && !boost::filesystem::create_directory(directory))
40+
error("Could not write to or create directory to save processed frames.",
41+
__LINE__, __FUNCTION__, __FILE__);
1942
};
2043
}
2144
catch (const std::exception& e)
@@ -24,12 +47,17 @@ namespace op
2447
}
2548
}
2649

27-
bool existDir(const std::string& directoryPath)
50+
bool existDirectory(const std::string& directoryPath)
2851
{
2952
try
3053
{
31-
// Maybe existFile also works for directories in Ubuntu/Windows/Mac?
32-
return boost::filesystem::exists(directoryPath);
54+
if (auto* directory = opendir(directoryPath.c_str()))
55+
{
56+
closedir(directory);
57+
return true;
58+
}
59+
else
60+
return false;
3361
}
3462
catch (const std::exception& e)
3563
{
@@ -57,19 +85,6 @@ namespace op
5785
}
5886
}
5987

60-
bool isDirectory(const std::string& directoryPath)
61-
{
62-
try
63-
{
64-
return (!directoryPath.empty() && boost::filesystem::is_directory(directoryPath));
65-
}
66-
catch (const std::exception& e)
67-
{
68-
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
69-
return false;
70-
}
71-
}
72-
7388
std::string formatAsDirectory(const std::string& directoryPathString)
7489
{
7590
try
@@ -94,7 +109,11 @@ namespace op
94109
{
95110
try
96111
{
97-
return boost::filesystem::path{fullPath}.filename().string();
112+
size_t lastSlashPos = fullPath.find_last_of("\\/");
113+
if (lastSlashPos != std::string::npos)
114+
return fullPath.substr(lastSlashPos+1, fullPath.size() - lastSlashPos - 1);
115+
else
116+
return fullPath;
98117
}
99118
catch (const std::exception& e)
100119
{
@@ -107,7 +126,14 @@ namespace op
107126
{
108127
try
109128
{
110-
return boost::filesystem::path{fullPath}.stem().string();
129+
// Name + extension
130+
std::string nameExt = getFileNameAndExtension(fullPath);
131+
// Name
132+
size_t dotPos = nameExt.find_last_of(".");
133+
if (dotPos != std::string::npos)
134+
return nameExt.substr(0, dotPos);
135+
else
136+
return nameExt;
111137
}
112138
catch (const std::exception& e)
113139
{
@@ -120,7 +146,14 @@ namespace op
120146
{
121147
try
122148
{
123-
return boost::filesystem::path{fullPath}.extension().string();
149+
// Name + extension
150+
std::string nameExt = getFileNameAndExtension(fullPath);
151+
// Extension
152+
size_t dotPos = nameExt.find_last_of(".");
153+
if (dotPos != std::string::npos)
154+
return nameExt.substr(dotPos + 1, nameExt.size() - dotPos - 1);
155+
else
156+
return "";
124157
}
125158
catch (const std::exception& e)
126159
{
@@ -174,18 +207,28 @@ namespace op
174207
{
175208
try
176209
{
210+
// Format the path first
211+
std::string formatedPath = formatAsDirectory(directoryPath);
177212
// Check folder exits
178-
if (!existDir(directoryPath))
213+
if (!existDirectory(directoryPath))
179214
error("Folder " + directoryPath + " does not exist.", __LINE__, __FUNCTION__, __FILE__);
180215
// Read images
181216
std::vector<std::string> filePaths;
182-
for (const auto& file : boost::make_iterator_range(boost::filesystem::directory_iterator{directoryPath}, {}))
183-
if (!boost::filesystem::is_directory(file.status())) // Skip directories
184-
filePaths.emplace_back(file.path().string());
217+
std::shared_ptr<DIR> directoryPtr(
218+
opendir(formatedPath.c_str()),
219+
[](DIR* formatedPath){ formatedPath && closedir(formatedPath); }
220+
);
221+
struct dirent* direntPtr;
222+
while ((direntPtr = readdir(directoryPtr.get())) != nullptr)
223+
{
224+
std::string currentPath = formatedPath + direntPtr->d_name;
225+
if ((strncmp(direntPtr->d_name, ".", 1) == 0) || existDirectory(currentPath))
226+
continue;
227+
filePaths.push_back(currentPath);
228+
}
185229
// Check #files > 0
186230
if (filePaths.empty())
187231
error("No files were found on " + directoryPath, __LINE__, __FUNCTION__, __FILE__);
188-
189232
// If specific extensions specified
190233
if (!extensions.empty())
191234
{
@@ -197,10 +240,9 @@ namespace op
197240
specificExtensionPaths.emplace_back(filePath);
198241
std::swap(filePaths, specificExtensionPaths);
199242
}
200-
201243
// Sort alphabetically
202244
std::sort(filePaths.begin(), filePaths.end());
203-
245+
// Return result
204246
return filePaths;
205247
}
206248
catch (const std::exception& e)

0 commit comments

Comments
 (0)