UDirectory.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 
22 #ifdef _WIN32
23  #include <Windows.h>
24  #include <direct.h>
25  #include <algorithm>
26  #include <conio.h>
27 #else
28  #include <dirent.h>
29  #include <sys/stat.h>
30  #include <sys/types.h>
31  #include <sys/param.h>
32  #include <unistd.h>
33  #include <stdlib.h>
34  #include <string.h>
35  #include <pwd.h>
36 #endif
37 
38 #include "rtabmap/utilite/UStl.h"
39 #include "rtabmap/utilite/UFile.h"
42 
44 
45 #ifdef _WIN32
46 
47 bool sortCallback(const std::string & a, const std::string & b)
48 {
49  return uStrNumCmp(a,b) < 0;
50 }
51 #elif __APPLE__
52 int sortCallback(const struct dirent ** a, const struct dirent ** b)
53 {
54  return uStrNumCmp((*a)->d_name, (*b)->d_name);
55 }
56 #else
57 int sortCallback( const dirent ** a, const dirent ** b)
58 {
59  return uStrNumCmp((*a)->d_name, (*b)->d_name);
60 }
61 #endif
62 
63 UDirectory::UDirectory(const std::string & path, const std::string & extensions)
64 {
65  extensions_ = uListToVector(uSplit(extensions, ' '));
66  path_ = path;
67  iFileName_ = fileNames_.begin();
68  this->update();
69 }
70 
72 {
73  *this = dir;
74 }
75 
77 {
79  path_ = dir.path_;
80  fileNames_ = dir.fileNames_;
81  for(iFileName_=fileNames_.begin(); iFileName_!=fileNames_.end(); ++iFileName_)
82  {
83  if(iFileName_->compare(*dir.iFileName_) == 0)
84  {
85  break;
86  }
87  }
88  return *this;
89 }
90 
92 {
93 }
94 
95 void UDirectory::setPath(const std::string & path, const std::string & extensions)
96 {
97  extensions_ = uListToVector(uSplit(extensions, ' '));
98  path_ = path;
99  fileNames_.clear();
100  iFileName_ = fileNames_.begin();
101  this->update();
102 }
103 
105 {
106  if(exists(path_))
107  {
108  std::string lastName;
109  bool endOfDir = false;
110  if(iFileName_ != fileNames_.end())
111  {
112  //Record the last file name
113  lastName = *iFileName_;
114  }
115  else if(fileNames_.size())
116  {
117  lastName = *fileNames_.rbegin();
118  endOfDir = true;
119  }
120  fileNames_.clear();
121 #ifdef _WIN32
122  WIN32_FIND_DATA fileInformation;
123  #ifdef UNICODE
124  wchar_t * pathAll = createWCharFromChar((path_+"\\*").c_str());
125  HANDLE hFile = ::FindFirstFile(pathAll, &fileInformation);
126  delete [] pathAll;
127  #else
128  HANDLE hFile = ::FindFirstFile((path_+"\\*").c_str(), &fileInformation);
129  #endif
130  if(hFile != INVALID_HANDLE_VALUE)
131  {
132  do
133  {
134  #ifdef UNICODE
135  char * fileName = createCharFromWChar(fileInformation.cFileName);
136  fileNames_.push_back(fileName);
137  delete [] fileName;
138  #else
139  fileNames_.push_back(fileInformation.cFileName);
140  #endif
141  } while(::FindNextFile(hFile, &fileInformation) == TRUE);
142  ::FindClose(hFile);
143  std::vector<std::string> vFileNames = uListToVector(fileNames_);
144  std::sort(vFileNames.begin(), vFileNames.end(), sortCallback);
145  fileNames_ = uVectorToList(vFileNames);
146  }
147 #else
148  int nameListSize;
149  struct dirent ** nameList = 0;
150  nameListSize = scandir(path_.c_str(), &nameList, 0, sortCallback);
151  if(nameList && nameListSize>0)
152  {
153  for (int i=0;i<nameListSize;++i)
154  {
155  fileNames_.push_back(nameList[i]->d_name);
156  free(nameList[i]);
157  }
158  free(nameList);
159  }
160 #endif
161 
162  //filter extensions...
163  std::list<std::string>::iterator iter = fileNames_.begin();
164  bool valid;
165  while(iter!=fileNames_.end())
166  {
167  valid = false;
168  if(extensions_.size() == 0 &&
169  iter->compare(".") != 0 &&
170  iter->compare("..") != 0)
171  {
172  valid = true;
173  }
174  for(unsigned int i=0; i<extensions_.size(); ++i)
175  {
176  if(UFile::getExtension(*iter).compare(extensions_[i]) == 0)
177  {
178  valid = true;
179  break;
180  }
181  }
182  if(!valid)
183  {
184  iter = fileNames_.erase(iter);
185  }
186  else
187  {
188  ++iter;
189  }
190  }
191  iFileName_ = fileNames_.begin();
192  if(!lastName.empty())
193  {
194  bool found = false;
195  for(std::list<std::string>::iterator iter=fileNames_.begin(); iter!=fileNames_.end(); ++iter)
196  {
197  if(lastName.compare(*iter) == 0)
198  {
199  found = true;
200  iFileName_ = iter;
201  break;
202  }
203  }
204  if(endOfDir && found)
205  {
206  ++iFileName_;
207  }
208  else if(endOfDir && fileNames_.size())
209  {
210  iFileName_ = --fileNames_.end();
211  }
212  }
213  }
214 }
215 
217 {
218  return exists(path_);
219 }
220 
222 {
223  std::string fileName;
224  if(iFileName_ != fileNames_.end())
225  {
226  fileName = *iFileName_;
227  ++iFileName_;
228  }
229  return fileName;
230 }
231 
233 {
234  std::string filePath;
235  if(iFileName_ != fileNames_.end())
236  {
237  filePath = path_+separator()+*iFileName_;
238  ++iFileName_;
239  }
240  return filePath;
241 }
242 
244 {
245  iFileName_ = fileNames_.begin();
246 }
247 
248 
249 bool UDirectory::exists(const std::string & dirPath)
250 {
251  bool r = false;
252 #ifdef _WIN32
253  #ifdef UNICODE
254  wchar_t * wDirPath = createWCharFromChar(dirPath.c_str());
255  DWORD dwAttrib = GetFileAttributes(wDirPath);
256  delete [] wDirPath;
257  #else
258  DWORD dwAttrib = GetFileAttributes(dirPath.c_str());
259  #endif
260  r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
261 #else
262  DIR *dp;
263  if((dp = opendir(dirPath.c_str())) != NULL)
264  {
265  r = true;
266  closedir(dp);
267  }
268 #endif
269  return r;
270 }
271 
272 // return the directory path of the file
273 std::string UDirectory::getDir(const std::string & filePath)
274 {
275  std::string dir = filePath;
276  int i=(int)dir.size()-1;
277  for(; i>=0; --i)
278  {
279  if(dir[i] == '/' || dir[i] == '\\')
280  {
281  //remove separators...
282  dir[i] = 0;
283  --i;
284  while(i>=0 && (dir[i] == '/' || dir[i] == '\\'))
285  {
286  dir[i] = 0;
287  --i;
288  }
289  break;
290  }
291  else
292  {
293  dir[i] = 0;
294  }
295  }
296 
297  if(i<0)
298  {
299  dir = ".";
300  }
301  else
302  {
303  dir.resize(i+1);
304  }
305 
306  return dir;
307 }
308 
309 std::string UDirectory::currentDir(bool trailingSeparator)
310 {
311  std::string dir;
312  char * buffer;
313 
314 #ifdef _WIN32
315  buffer = _getcwd(NULL, 0);
316 #else
317  buffer = getcwd(NULL, MAXPATHLEN);
318 #endif
319 
320  if( buffer != NULL )
321  {
322  dir = buffer;
323  free(buffer);
324  if(trailingSeparator)
325  {
326  dir += separator();
327  }
328  }
329 
330  return dir;
331 }
332 
333 bool UDirectory::makeDir(const std::string & dirPath)
334 {
335  int status;
336 #ifdef _WIN32
337  status = _mkdir(dirPath.c_str());
338 #else
339  status = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
340 #endif
341  return status==0;
342 }
343 
344 bool UDirectory::removeDir(const std::string & dirPath)
345 {
346  int status;
347 #ifdef _WIN32
348  status = _rmdir(dirPath.c_str());
349 #else
350  status = rmdir(dirPath.c_str());
351 #endif
352  return status==0;
353 }
354 
355 std::string UDirectory::homeDir()
356 {
357  std::string path;
358 #ifdef _WIN32
359  #ifdef UNICODE
360  wchar_t wProfilePath[250];
361  ExpandEnvironmentStrings(L"%userprofile%",wProfilePath,250);
362  char * profilePath = createCharFromWChar(wProfilePath);
363  path = profilePath;
364  delete [] profilePath;
365  #else
366  char profilePath[250];
367  ExpandEnvironmentStrings("%userprofile%",profilePath,250);
368  path = profilePath;
369  #endif
370 #else
371  char * pathstr = getenv("HOME");
372  if(pathstr)
373  {
374  path = pathstr;
375  }
376  if(path.empty())
377  {
378  struct passwd *pw = getpwuid(getuid());
379  if(pw) {
380  path = pw->pw_dir;
381  }
382  if(path.empty())
383  {
384  UFATAL("Environment variable HOME is not set, cannot get home directory! Please set HOME environment variable to a valid directory.");
385  }
386  }
387 #endif
388  return path;
389 }
390 
392 {
393 #ifdef _WIN32
394  return "\\";
395 #else
396  return "/";
397 #endif
398 }
static std::string homeDir()
Definition: UDirectory.cpp:355
int sortCallback(const dirent **a, const dirent **b)
Definition: UDirectory.cpp:57
#define NULL
void rewind()
Definition: UDirectory.cpp:243
static bool makeDir(const std::string &dirPath)
Definition: UDirectory.cpp:333
bool isValid()
Definition: UDirectory.cpp:216
int uStrNumCmp(const std::string &a, const std::string &b)
Definition: UStl.h:719
std::list< std::string > fileNames_
Definition: UDirectory.h:139
static std::string getDir(const std::string &filePath)
Definition: UDirectory.cpp:273
UDirectory(const std::string &path="", const std::string &extensions="")
Definition: UDirectory.cpp:63
static std::string separator()
Definition: UDirectory.cpp:391
UDirectory & operator=(const UDirectory &dir)
Definition: UDirectory.cpp:76
Some conversion functions.
std::string getExtension()
Definition: UFile.h:140
#define UFATAL(...)
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
Definition: UStl.h:566
Wrappers of STL for convenient functions.
std::vector< std::string > extensions_
Definition: UDirectory.h:138
static std::string currentDir(bool trailingSeparator=false)
Definition: UDirectory.cpp:309
void setPath(const std::string &path, const std::string &extensions="")
Definition: UDirectory.cpp:95
std::list< std::string >::iterator iFileName_
Definition: UDirectory.h:140
std::list< V > uVectorToList(const std::vector< V > &v)
Definition: UStl.h:486
void update()
Definition: UDirectory.cpp:104
std::vector< V > uListToVector(const std::list< V > &list)
Definition: UStl.h:475
std::string getNextFilePath()
Definition: UDirectory.cpp:232
std::string path_
Definition: UDirectory.h:137
std::string getNextFileName()
Definition: UDirectory.cpp:221
static bool removeDir(const std::string &dirPath)
Definition: UDirectory.cpp:344
ULogger class and convenient macros.
static bool exists(const std::string &dirPath)
Definition: UDirectory.cpp:249


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06