Main MRPT website > C++ reference for MRPT 1.9.9
CDirectoryExplorer.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #ifdef _WIN32
13 #ifdef _MSC_VER
14 #include <sys/utime.h>
15 #endif
16 #include <io.h>
17 #include <windows.h>
18 #include <direct.h>
19 #else
20 #include <sys/types.h>
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <time.h>
24 #include <utime.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <cstring>
28 #endif
29 
30 #include <queue>
31 #include <algorithm>
32 #include <iostream>
33 #include <cstdio>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 
38 #include <mrpt/system/filesystem.h>
39 #include <mrpt/core/exceptions.h>
40 
41 using namespace mrpt::system;
42 using namespace std;
43 
44 /*---------------------------------------------------------------
45  explore
46  ---------------------------------------------------------------*/
48  const string& path, const unsigned long in_mask, TFileInfoList& outList)
49 {
51 
52  unsigned long mask = in_mask;
53 
54  outList.clear();
55 
56  // The path terminated in "/" or "\\"
57  string searchPath(path);
58  if (searchPath.size())
59  if (searchPath[searchPath.size() - 1] != '\\' &&
60  searchPath[searchPath.size() - 1] != '/')
61  {
62 #ifdef _WIN32
63  searchPath += '\\';
64 #else
65  searchPath.push_back('/');
66 #endif
67  }
68 
69 // cout << "searchPath:"<<searchPath<<endl;
70 
71 #ifdef _WIN32
72  // ====================
73  // WINDOWS VERSION
74  // ====================
75  WIN32_FIND_DATAA f;
76  TFileInfo newEntry;
77 
78  string searchPath_mask = searchPath + string("*.*");
79 
80  HANDLE h = FindFirstFileA(searchPath_mask.c_str(), &f);
81  if (h == INVALID_HANDLE_VALUE)
82  THROW_EXCEPTION("Error starting exploration! (does path exist?)");
83 
84  // Include the FILE_ATTRIB_ARCHIVE flag for files:
85  if (mask & FILE_ATTRIB_ARCHIVE) mask |= FILE_ATTRIBUTE_NORMAL;
86  do
87  {
88  if ((mask & f.dwFileAttributes) != 0) // Passes the user masks:
89  {
90  // File name:
91  newEntry.name = string(f.cFileName);
92 
93  // Complete file path:
94  newEntry.wholePath = searchPath;
95  newEntry.wholePath += newEntry.name;
96 
97  // File size:
98  newEntry.fileSize = ((uint64_t)f.nFileSizeLow) +
99  (((uint64_t)f.nFileSizeHigh) << 32);
100 
101  // File times:
102  struct stat statDat;
103  if (stat(newEntry.wholePath.c_str(), &statDat))
104  {
105  FindClose(h);
107  "Cannot get stat for file: '%s'",
108  newEntry.wholePath.c_str())
109  }
110 
111  newEntry.modTime = statDat.st_mtime;
112  newEntry.accessTime = statDat.st_atime;
113 
114  // Flags:
115  newEntry.isDir = 0 != (statDat.st_mode & _S_IFDIR);
116  newEntry.isSymLink =
117  false; // (We donnot look for this in Windows, by now...)
118 
119  // Save:
120  outList.push_back(newEntry);
121  }
122  } while (FindNextFileA(h, &f));
123 
124  FindClose(h); // Ignore possible errors..
125 
126 // Done
127 #else
128  // ====================
129  // LINUX VERSION
130  // ====================
131  TFileInfo newEntry;
132  struct dirent* ent;
133 
134  DIR* dir = opendir(searchPath.c_str());
135  if (!dir) THROW_EXCEPTION("Error starting exploration! (does path exist?)");
136 
137  while ((ent = readdir(dir)) != nullptr)
138  {
139  if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
140  {
141  // File name:
142  newEntry.name = string(ent->d_name);
143 
144  // Complete file path:
145  newEntry.wholePath = searchPath;
146  newEntry.wholePath += newEntry.name;
147 
148  // File times:
149  struct stat statDat, lstatDat;
150  if (stat(newEntry.wholePath.c_str(), &statDat))
151  {
152  closedir(dir);
154  "Cannot get stat for file: '%s'",
155  newEntry.wholePath.c_str())
156  }
157 
158  newEntry.modTime = statDat.st_mtime;
159  newEntry.accessTime = statDat.st_atime;
160 
161  // Flags:
162  newEntry.isDir = S_ISDIR(statDat.st_mode);
163 
164  if (((mask & FILE_ATTRIB_ARCHIVE) != 0 && !newEntry.isDir) ||
165  ((mask & FILE_ATTRIB_DIRECTORY) != 0 && newEntry.isDir))
166  {
167  // File size:
168  newEntry.fileSize = (intmax_t)statDat.st_size;
169 
170  // Is it a symbolic link?? Need to call "lstat":
171  if (!lstat(newEntry.wholePath.c_str(), &lstatDat))
172  {
173  newEntry.isSymLink = S_ISLNK(lstatDat.st_mode);
174  }
175  else
176  newEntry.isSymLink = false;
177 
178  // Save:
179  outList.push_back(newEntry);
180  }
181  }
182  }
183 
184  closedir(dir);
185 
186 // Done
187 #endif
188 
189  MRPT_END
190 }
191 
192 // Auxiliary function to order by name, ascending
196 {
197  return a.wholePath < b.wholePath;
198 }
202 {
203  return a.wholePath > b.wholePath;
204 }
205 
206 /*---------------------------------------------------------------
207  sortByName
208  ---------------------------------------------------------------*/
210  TFileInfoList& lstFiles, bool ascendingOrder)
211 {
212  std::sort(
213  lstFiles.begin(), lstFiles.end(),
215 }
216 
217 /*---------------------------------------------------------------
218  filterByExtension
219  ---------------------------------------------------------------*/
221  TFileInfoList& lstFiles, const std::string& extension)
222 {
223  int i, n = (int)lstFiles.size();
224  for (i = n - 1; i >= 0; i--)
225  {
226  if (0 !=
227  os::_strcmpi(
228  mrpt::system::extractFileExtension(lstFiles[i].name).c_str(),
229  extension.c_str()))
230  {
231  // Does not match:
232  lstFiles.erase(lstFiles.begin() + i);
233  }
234  }
235 }
n
GLenum GLsizei n
Definition: glext.h:5074
filesystem.h
intmax_t
int32_t intmax_t
Definition: xSens_MT3/pstdint.h:397
exceptions.h
mrpt::system::CDirectoryExplorer::TFileInfo::accessTime
time_t accessTime
Access and modification times.
Definition: CDirectoryExplorer.h:48
system-precomp.h
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
cmpFileEntriesName_Asc
bool cmpFileEntriesName_Asc(const CDirectoryExplorer::TFileInfo &a, const CDirectoryExplorer::TFileInfo &b)
Definition: CDirectoryExplorer.cpp:193
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
FILE_ATTRIB_ARCHIVE
#define FILE_ATTRIB_ARCHIVE
Definition: CDirectoryExplorer.h:18
FILE_ATTRIB_DIRECTORY
#define FILE_ATTRIB_DIRECTORY
Definition: CDirectoryExplorer.h:19
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::system::CDirectoryExplorer::TFileInfo::fileSize
uint64_t fileSize
The size of the file in bytes.
Definition: CDirectoryExplorer.h:54
mrpt::system::CDirectoryExplorer::TFileInfoList
std::deque< TFileInfo > TFileInfoList
The list type used in "explore".
Definition: CDirectoryExplorer.h:60
cmpFileEntriesName_Desc
bool cmpFileEntriesName_Desc(const CDirectoryExplorer::TFileInfo &a, const CDirectoryExplorer::TFileInfo &b)
Definition: CDirectoryExplorer.cpp:199
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
b
GLubyte GLubyte b
Definition: glext.h:6279
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::system::CDirectoryExplorer::TFileInfo::name
std::string name
The file name (without the whole path).
Definition: CDirectoryExplorer.h:40
mask
GLenum GLint GLuint mask
Definition: glext.h:4050
mrpt::system::extractFileExtension
std::string extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
Definition: filesystem.cpp:97
mrpt::system::CDirectoryExplorer::TFileInfo::modTime
time_t modTime
Definition: CDirectoryExplorer.h:48
mrpt::system::CDirectoryExplorer::TFileInfo::isSymLink
bool isSymLink
Definition: CDirectoryExplorer.h:50
mrpt::system::CDirectoryExplorer::TFileInfo::isDir
bool isDir
Definition: CDirectoryExplorer.h:50
CDirectoryExplorer.h
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::CDirectoryExplorer::TFileInfo
This represents the information about each file.
Definition: CDirectoryExplorer.h:36
mrpt::system::CDirectoryExplorer::filterByExtension
static void filterByExtension(TFileInfoList &lstFiles, const std::string &extension)
Remove from the list of files those whose extension does not coincide (without case) with the given o...
Definition: CDirectoryExplorer.cpp:220
mrpt::system::CDirectoryExplorer::sortByName
static void sortByName(TFileInfoList &lstFiles, bool ascendingOrder=true)
Sort the file entries by name, in ascending or descending order.
Definition: CDirectoryExplorer.cpp:209
mrpt::system::os::_strcmpi
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:320
mrpt::system::CDirectoryExplorer::explore
static void explore(const std::string &path, const unsigned long mask, TFileInfoList &outList)
The path of the directory to examine must be passed to this constructor, among the According to the f...
Definition: CDirectoryExplorer.cpp:47
mrpt::system::CDirectoryExplorer::TFileInfo::wholePath
std::string wholePath
The whole file path.
Definition: CDirectoryExplorer.h:44
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST