Main MRPT website > C++ reference for MRPT 1.5.6
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-2017, 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 "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/config.h>
13 
14 #ifdef MRPT_OS_WINDOWS
15  #ifdef _MSC_VER
16  #include <sys/utime.h>
17  #endif
18  #include <io.h>
19  #include <windows.h>
20  #include <direct.h>
21 #else
22  #include <sys/types.h>
23  #include <dirent.h>
24  #include <unistd.h>
25  #include <time.h>
26  #include <utime.h>
27  #include <unistd.h>
28  #include <errno.h>
29  #include <cstring>
30 #endif
31 
32 #include <queue>
33 #include <algorithm>
34 #include <iostream>
35 #include <cstdio>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 
39 
41 #include <mrpt/system/filesystem.h>
42 
43 using namespace mrpt::system;
44 using namespace std;
45 
46 /*---------------------------------------------------------------
47  explore
48  ---------------------------------------------------------------*/
50  const string &path,
51  const unsigned long in_mask,
52  TFileInfoList &outList )
53 {
55 
56  unsigned long mask = in_mask;
57 
58  outList.clear();
59 
60  // The path terminated in "/" or "\\"
61  string searchPath( path );
62  if (searchPath.size())
63  if ( searchPath[searchPath.size()-1]!='\\' && searchPath[searchPath.size()-1]!='/')
64  {
65 #ifdef MRPT_OS_WINDOWS
66  searchPath += '\\';
67 #else
68  searchPath.push_back('/');
69 #endif
70  }
71 
72  //cout << "searchPath:"<<searchPath<<endl;
73 
74 #ifdef MRPT_OS_WINDOWS
75  // ====================
76  // WINDOWS VERSION
77  // ====================
78  WIN32_FIND_DATAA f;
79  TFileInfo newEntry;
80 
81  string searchPath_mask=searchPath + string("*.*");
82 
83  HANDLE h = FindFirstFileA( searchPath_mask.c_str(), &f);
84  if (h==INVALID_HANDLE_VALUE)
85  THROW_EXCEPTION("Error starting exploration! (does path exist?)");
86 
87  // Include the FILE_ATTRIB_ARCHIVE flag for files:
88  if (mask & FILE_ATTRIB_ARCHIVE) mask |= FILE_ATTRIBUTE_NORMAL;
89  do
90  {
91  if ( (mask & f.dwFileAttributes)!=0 ) // Passes the user masks:
92  {
93  // File name:
94  newEntry.name = string(f.cFileName);
95 
96  // Complete file path:
97  newEntry.wholePath = searchPath;
98  newEntry.wholePath += newEntry.name;
99 
100  // File size:
101  newEntry.fileSize = ((uint64_t)f.nFileSizeLow) + (((uint64_t)f.nFileSizeHigh) << 32);
102 
103  // File times:
104  struct stat statDat;
105  if (stat( newEntry.wholePath.c_str(),&statDat ))
106  {
107  FindClose(h);
108  THROW_EXCEPTION_FMT("Cannot get stat for file: '%s'",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 = false; // (We donnot look for this in Windows, by now...)
117 
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)
136  THROW_EXCEPTION("Error starting exploration! (does path exist?)");
137 
138 
139  while((ent = readdir(dir)) != NULL)
140  {
141  if ( strcmp(ent->d_name,".") && strcmp(ent->d_name,"..") )
142  {
143  // File name:
144  newEntry.name = string(ent->d_name);
145 
146  // Complete file path:
147  newEntry.wholePath = searchPath;
148  newEntry.wholePath += newEntry.name;
149 
150  // File times:
151  struct stat statDat, lstatDat;
152  if (stat( newEntry.wholePath.c_str(),&statDat ))
153  {
154  closedir(dir);
155  THROW_EXCEPTION_FMT("Cannot get stat for file: '%s'",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 newEntry.isSymLink = false;
176 
177  // Save:
178  outList.push_back( newEntry );
179  }
180  }
181  }
182 
183  closedir(dir);
184 
185  // Done
186 #endif
187 
188  MRPT_END
189 }
190 
191 
192 // Auxiliary function to order by name, ascending
194 {
195  return a.wholePath < b.wholePath;
196 }
198 {
199  return a.wholePath > b.wholePath;
200 }
201 
202 /*---------------------------------------------------------------
203  sortByName
204  ---------------------------------------------------------------*/
205 void CDirectoryExplorer::sortByName( TFileInfoList &lstFiles, bool ascendingOrder )
206 {
207  std::sort(lstFiles.begin(),lstFiles.end(), ascendingOrder ? cmpFileEntriesName_Asc : cmpFileEntriesName_Desc );
208 }
209 
210 
211 /*---------------------------------------------------------------
212  filterByExtension
213  ---------------------------------------------------------------*/
215 {
216  int i,n=(int)lstFiles.size();
217  for (i=n-1;i>=0;i--)
218  {
219  if ( 0!=os::_strcmpi(mrpt::system::extractFileExtension(lstFiles[i].name).c_str(),extension.c_str() ) )
220  {
221  // Does not match:
222  lstFiles.erase( lstFiles.begin()+i );
223  }
224  }
225 }
226 
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...
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...
GLenum GLint GLuint mask
Definition: glext.h:3888
std::string wholePath
The whole file path.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLenum GLsizei n
Definition: glext.h:4618
bool cmpFileEntriesName_Desc(const CDirectoryExplorer::TFileInfo &a, const CDirectoryExplorer::TFileInfo &b)
STL namespace.
This represents the information about each file.
#define FILE_ATTRIB_ARCHIVE
time_t accessTime
Access and modification times.
uint64_t fileSize
The size of the file in bytes.
std::deque< TFileInfo > TFileInfoList
The list type used in "explore".
#define MRPT_END
GLubyte GLubyte b
Definition: glext.h:5575
std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
Definition: filesystem.cpp:96
GLsizei const GLchar ** string
Definition: glext.h:3919
static void sortByName(TFileInfoList &lstFiles, bool ascendingOrder=true)
Sort the file entries by name, in ascending or descending order.
std::string name
The file name (without the whole path).
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:52
#define FILE_ATTRIB_DIRECTORY
int BASE_IMPEXP _strcmpi(const char *str1, const char *str2) MRPT_NO_THROWS
An OS-independent version of strcmpi.
Definition: os.cpp:320
GLuint const GLchar * name
Definition: glext.h:3891
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
bool cmpFileEntriesName_Asc(const CDirectoryExplorer::TFileInfo &a, const CDirectoryExplorer::TFileInfo &b)



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019