UDirectory.cpp
Go to the documentation of this file.
1 // Taken from UtiLite library r266 [www.utilite.googlecode.com]
2 
3 /*
4 * utilite is a cross-platform library with
5 * useful utilities for fast and small developing.
6 * Copyright (C) 2010 Mathieu Labbe
7 *
8 * utilite is free library: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * utilite is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "utilite/UDirectory.h"
23 
24 #ifdef WIN32
25  #include <Windows.h>
26  #include <direct.h>
27  #include <algorithm>
28  #include <conio.h>
29 #else
30  #include <dirent.h>
31  #include <sys/stat.h>
32  #include <sys/types.h>
33  #include <sys/param.h>
34  #include <sys/dir.h>
35  #include <unistd.h>
36  #include <stdlib.h>
37  #include <string.h>
38 #endif
39 
40 #include "utilite/UStl.h"
41 #include "utilite/UFile.h"
42 #include "utilite/UDirectory.h"
43 #include "utilite/UConversion.h"
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  iFileName_ = fileNames_.begin();
235 }
236 
237 
238 bool UDirectory::exists(const std::string & dirPath)
239 {
240  bool r = false;
241 #if WIN32
242  #ifdef UNICODE
243  wchar_t * wDirPath = createWCharFromChar(dirPath.c_str());
244  DWORD dwAttrib = GetFileAttributes(wDirPath);
245  delete [] wDirPath;
246  #else
247  DWORD dwAttrib = GetFileAttributes(dirPath.c_str());
248  #endif
249  r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
250 #else
251  DIR *dp;
252  if((dp = opendir(dirPath.c_str())) != NULL)
253  {
254  r = true;
255  closedir(dp);
256  }
257 #endif
258  return r;
259 }
260 
261 // return the directory path of the file
262 std::string UDirectory::getDir(const std::string & filePath)
263 {
264  std::string dir = filePath;
265  int i=(int)dir.size()-1;
266  for(; i>=0; --i)
267  {
268  if(dir[i] == '/' || dir[i] == '\\')
269  {
270  //remove separators...
271  dir[i] = 0;
272  --i;
273  while(i>=0 && (dir[i] == '/' || dir[i] == '\\'))
274  {
275  dir[i] = 0;
276  --i;
277  }
278  break;
279  }
280  else
281  {
282  dir[i] = 0;
283  }
284  }
285 
286  if(i<0)
287  {
288  dir = ".";
289  }
290  else
291  {
292  dir.resize(i+1);
293  }
294 
295  return dir;
296 }
297 
298 std::string UDirectory::currentDir(bool trailingSeparator)
299 {
300  std::string dir;
301  char * buffer;
302 
303 #ifdef WIN32
304  buffer = _getcwd(NULL, 0);
305 #else
306  buffer = getcwd(NULL, MAXPATHLEN);
307 #endif
308 
309  if( buffer != NULL )
310  {
311  dir = buffer;
312  free(buffer);
313  if(trailingSeparator)
314  {
315  dir += separator();
316  }
317  }
318 
319  return dir;
320 }
321 
322 bool UDirectory::makeDir(const std::string & dirPath)
323 {
324  int status;
325 #if WIN32
326  status = _mkdir(dirPath.c_str());
327 #else
328  status = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
329 #endif
330  return status==0;
331 }
332 
333 bool UDirectory::removeDir(const std::string & dirPath)
334 {
335  int status;
336 #if WIN32
337  status = _rmdir(dirPath.c_str());
338 #else
339  status = rmdir(dirPath.c_str());
340 #endif
341  return status==0;
342 }
343 
344 std::string UDirectory::homeDir()
345 {
346  std::string path;
347 #if WIN32
348  #ifdef UNICODE
349  wchar_t wProfilePath[250];
350  ExpandEnvironmentStrings(L"%userprofile%",wProfilePath,250);
351  char * profilePath = createCharFromWChar(wProfilePath);
352  path = profilePath;
353  delete [] profilePath;
354  #else
355  char profilePath[250];
356  ExpandEnvironmentStrings("%userprofile%",profilePath,250);
357  path = profilePath;
358  #endif
359 #else
360  path = getenv("HOME");
361 #endif
362  return path;
363 }
364 
366 {
367 #ifdef WIN32
368  return "\\";
369 #else
370  return "/";
371 #endif
372 }
static std::string homeDir()
Definition: UDirectory.cpp:344
int sortCallback(const dirent **a, const dirent **b)
Definition: UDirectory.cpp:57
void rewind()
Definition: UDirectory.cpp:232
static bool makeDir(const std::string &dirPath)
Definition: UDirectory.cpp:322
bool isValid()
Definition: UDirectory.cpp:216
int uStrNumCmp(const std::string &a, const std::string &b)
Definition: UStl.h:528
std::list< std::string > fileNames_
Definition: UDirectory.h:135
static std::string getDir(const std::string &filePath)
Definition: UDirectory.cpp:262
UDirectory(const std::string &path="", const std::string &extensions="")
Definition: UDirectory.cpp:63
static std::string separator()
Definition: UDirectory.cpp:365
UDirectory & operator=(const UDirectory &dir)
Definition: UDirectory.cpp:76
Some conversion functions.
std::string getExtension()
Definition: UFile.h:131
Wrappers of STL for convenient functions.
std::list< std::string > uSplit(const std::string &str, char separator=' ')
Definition: UStl.h:438
std::vector< std::string > extensions_
Definition: UDirectory.h:134
static std::string currentDir(bool trailingSeparator=false)
Definition: UDirectory.cpp:298
void setPath(const std::string &path, const std::string &extensions="")
Definition: UDirectory.cpp:95
std::list< std::string >::iterator iFileName_
Definition: UDirectory.h:136
std::list< V > uVectorToList(const std::vector< V > &v)
Definition: UStl.h:387
void update()
Definition: UDirectory.cpp:104
std::vector< V > uListToVector(const std::list< V > &list)
Definition: UStl.h:376
std::string path_
Definition: UDirectory.h:133
std::string getNextFileName()
Definition: UDirectory.cpp:221
static bool removeDir(const std::string &dirPath)
Definition: UDirectory.cpp:333
static bool exists(const std::string &dirPath)
Definition: UDirectory.cpp:238


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:10