1
1
#include < cstdio> // fopen
2
+ #include < dirent.h> // opendir
2
3
#include < boost/filesystem.hpp>
3
- #include < boost/range/iterator_range_core.hpp>
4
4
#include < openpose/utilities/string.hpp>
5
5
#include < openpose/utilities/fileSystem.hpp>
6
6
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
+
7
23
namespace op
8
24
{
9
25
void mkdir (const std::string& directoryPath)
@@ -12,10 +28,17 @@ namespace op
12
28
{
13
29
if (!directoryPath.empty ())
14
30
{
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
+
15
37
// Create folder if it does not exist
16
38
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__);
19
42
};
20
43
}
21
44
catch (const std::exception & e)
@@ -24,12 +47,17 @@ namespace op
24
47
}
25
48
}
26
49
27
- bool existDir (const std::string& directoryPath)
50
+ bool existDirectory (const std::string& directoryPath)
28
51
{
29
52
try
30
53
{
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 ;
33
61
}
34
62
catch (const std::exception & e)
35
63
{
@@ -57,19 +85,6 @@ namespace op
57
85
}
58
86
}
59
87
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
-
73
88
std::string formatAsDirectory (const std::string& directoryPathString)
74
89
{
75
90
try
@@ -94,7 +109,11 @@ namespace op
94
109
{
95
110
try
96
111
{
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;
98
117
}
99
118
catch (const std::exception & e)
100
119
{
@@ -107,7 +126,14 @@ namespace op
107
126
{
108
127
try
109
128
{
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;
111
137
}
112
138
catch (const std::exception & e)
113
139
{
@@ -120,7 +146,14 @@ namespace op
120
146
{
121
147
try
122
148
{
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 " " ;
124
157
}
125
158
catch (const std::exception & e)
126
159
{
@@ -174,18 +207,28 @@ namespace op
174
207
{
175
208
try
176
209
{
210
+ // Format the path first
211
+ std::string formatedPath = formatAsDirectory (directoryPath);
177
212
// Check folder exits
178
- if (!existDir (directoryPath))
213
+ if (!existDirectory (directoryPath))
179
214
error (" Folder " + directoryPath + " does not exist." , __LINE__, __FUNCTION__, __FILE__);
180
215
// Read images
181
216
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
+ }
185
229
// Check #files > 0
186
230
if (filePaths.empty ())
187
231
error (" No files were found on " + directoryPath, __LINE__, __FUNCTION__, __FILE__);
188
-
189
232
// If specific extensions specified
190
233
if (!extensions.empty ())
191
234
{
@@ -197,10 +240,9 @@ namespace op
197
240
specificExtensionPaths.emplace_back (filePath);
198
241
std::swap (filePaths, specificExtensionPaths);
199
242
}
200
-
201
243
// Sort alphabetically
202
244
std::sort (filePaths.begin (), filePaths.end ());
203
-
245
+ // Return result
204
246
return filePaths;
205
247
}
206
248
catch (const std::exception & e)
0 commit comments