OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimDirectory.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 // Description: A brief description of the contents of the file.
11 //
12 //*************************************************************************
13 // $Id: ossimDirectory.cpp 20229 2011-11-08 17:01:17Z oscarkramer $
14 
15 #include <cstring> /* for strncasecmp */
16 #include <iostream>
17 
18 #if defined (_WIN32)
19 #include <io.h>
20 #include <direct.h>
21 #else
23 #endif
24 #ifdef __BORLANDC__
25 # include <dir.h>
26 #include <direct.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <io.h>
30 #define _chdir chdir
31 #endif
32 
34 #include <ossim/base/ossimRegExp.h>
35 
36 //#include "wx/filefn.h" // for wxMatchWild
37 
38 // #include <sys/types.h>
39 
40 // #include <dirent.h>
41 
42 // ----------------------------------------------------------------------------
43 // ossimDirectory construction/destruction
44 // ----------------------------------------------------------------------------
45 
46 #if defined (_WIN32)
48  :
49  theData(0),
50  theDirectoryName(),
51  theFlags(0)
52 {}
53 
55  :
56  theData(0),
57  theDirectoryName(dirname),
58  theFlags(0)
59 {
60  open(dirname);
61 }
62 
64 {
65  if (theData != 0) _findclose( theData );
66 }
67 
68 bool ossimDirectory::open(const ossimFilename &dirname)
69 {
70  // close out currently open directory
71  if (theData != 0)
72  {
73  _findclose( theData );
74  theData = 0;
75  }
76 
77  // set new directory name
78  theDirectoryName = dirname;
79 
80  return (dirname.isDir());
81  // cd to the new directory
82 // if (_chdir( theDirectoryName) == -1)
83 // {
84 // return false;
85 // }
86 
87 // return true;
88 }
89 
90 bool ossimDirectory::getFirst(ossimFilename &filename, int flags)
91 {
92  struct _finddata_t c_file;
93  ossimFilename temp;
94  //long theData1 = _findfirst( "*.*", &c_file );
95  ossimFilename dirName = theDirectoryName.dirCat("*");
96  if( (theData = _findfirst( dirName.c_str(), &c_file )) != 0L )
97  {
98  setFlags(flags);
99 
100  temp = theDirectoryName.dirCat(c_file.name);
101 
102 
103  while (!fileMatched(temp))
104  {
105  // look for next file in the directory
106  if (_findnext( theData, &c_file ) == 0 )
107  {
108  temp = theDirectoryName.dirCat(c_file.name);
109  }
110  else
111  {
112  // no more file in the directory
113  filename.clear();
114  return false;
115  }
116  }
117  }
118 
119  // set the filenane that matches
120  filename = temp.trim();
121 
122  return (filename!="");
123 }
124 
125 bool ossimDirectory::getNext(ossimFilename &filename) const
126 {
127  struct _finddata_t c_file;
128  bool matches = false;
129  ossimFilename temp;
130 
131  while (!matches )
132  {
133  // look for next file in the directory
134  if (_findnext( theData, &c_file ) == 0 )
135  {
136  temp = theDirectoryName.dirCat(c_file.name);
137  matches = fileMatched(temp);
138  }
139  else
140  {
141  // no more file in the directory
142  filename.clear();
143  return false;
144  }
145  }
146 
147  // set the filenane that matches
148  if (matches)
149  {
150  filename = temp.trim();
151  }
152 
153  return (matches&&(filename!=""));
154 }
155 
156 bool ossimDirectory::fileMatched(ossimFilename &filename) const
157 {
158  bool matches = false;
159 
160  // Don't return "." and ".." unless asked for.
161  if ( (filename.file() == "..") || (filename.file() == ".") )
162  {
163  if (theFlags & ossimDirectory::OSSIM_DIR_DOTDOT)
164  {
165  matches = true;
166  }
167  }
168  else if((filename.isDir()) && (theFlags & ossimDirectory::OSSIM_DIR_DIRS))
169  {
170  matches = true;
171  }
172  else if((filename.isFile()) && (theFlags & ossimDirectory::OSSIM_DIR_FILES))
173  {
174  matches = true;
175  }
176 
177  return matches;
178 }
179 
180 bool ossimDirectory::isOpened() const
181 {
182  return theDirectoryName.isDir();
183  //return theData != 0;
184 }
185 
186 #else
187 
189  :
190  theData(NULL)
191 {}
192 
194 {
195  theData = NULL;
196  open(dirname);
197 }
198 
200 {
201  delete theData;
202  theData = new ossimDirectoryData(dirname);
203 
204  if ( theData &&
205  (!theData->isOk()) )
206  {
207  delete theData;
208  theData = NULL;
209 
210  return false;
211  }
212 
213  return true;
214 }
215 
217 {
218  delete theData;
219 }
220 
221 // ----------------------------------------------------------------------------
222 // ossimDirectory enumerating
223 // ----------------------------------------------------------------------------
224 
226  int flags)
227 {
228  if(theData && isOpened())
229  {
230  theData->rewind();
231 
232  theData->setFlags(flags);
233 
234  return getNext(filename);
235  }
236 
237  return false;
238 }
239 
241 {
242  if(theData && isOpened())
243  {
244  return theData->read(filename);
245  }
246 
247  return false;
248 }
249 
251 {
252  return theData != NULL;
253 }
254 
255 #endif
256 
257 void ossimDirectory::findAllFilesThatMatch(std::vector<ossimFilename>& result,
258  const ossimString& regularExpressionPattern,
259  int flags)
260 {
261  ossimFilename filename;
262  ossimRegExp regExpr;
263  regExpr.compile(regularExpressionPattern.c_str());
264  if(getFirst(filename, flags))
265  {
266  do
267  {
268  ossimString fileOnly = filename.file();
269  if(regExpr.find(fileOnly.c_str()))
270  {
271  result.push_back(filename);
272  }
273  }while(getNext(filename));
274  }
275 }
276 
277 // ESH 07/2008, Trac #234: OSSIM is case sensitive
278 // when using worldfile templates during ingest
280  const ossimFilename &filename,
281  std::vector<ossimFilename>& result,
282  bool bExcludeExactMatch )
283 {
284  bool bSuccess = false;
285  ossimFilename candidate;
286  bool bFoundCandidate = getFirst( candidate );
287  int compareSize = static_cast<int>( filename.length() );
288 
289  while( bFoundCandidate == true )
290  {
291  // Do a case insensitive string compare
292 #if defined (_WIN32)
293  bool bFoundEquivalent = _strnicmp( filename.c_str(), candidate.c_str(),
294  compareSize ) == 0 ? true : false;
295 #else
296  //bool bFoundEquivalent = strnicmp( filename.c_str(), candidate.c_str(), //
297  // compareSize ) == 0 ? true : false;
298  bool bFoundEquivalent = strncasecmp( filename.c_str(), candidate.c_str(),
299  compareSize ) == 0 ? true : false;
300 #endif
301 
302  if ( bFoundEquivalent == true )
303  {
304  bool bFoundExact = ( filename == candidate.c_str() ) ? true : false;
305  bool bShouldExclude = ( bFoundExact == true &&
306  bExcludeExactMatch == true ) ? true : false;
307 
308  if ( bShouldExclude == false )
309  {
310  bSuccess = true;
311  result.push_back( candidate );
312  }
313  }
314 
315  bFoundCandidate = getNext( candidate );
316  }
317 
318  return bSuccess;
319 }
void clear()
Erases the entire container.
Definition: ossimString.h:432
void setFlags(int flags)
bool getFirst(ossimFilename &filename, int flags=OSSIM_DIR_DEFAULT)
void findAllFilesThatMatch(std::vector< ossimFilename > &result, const ossimString &regularExpressionPattern, int flags=OSSIM_DIR_DEFAULT)
bool isDir() const
bool getNext(ossimFilename &filename) const
void push_back(char c)
Equivalent to insert(end(), c).
Definition: ossimString.h:905
std::string::size_type length() const
Definition: ossimString.h:408
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
bool isFile() const
void compile(const char *)
bool open(const ossimFilename &dir)
bool isOpened() const
ossimFilename dirCat(const ossimFilename &file) const
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
ossimFilename file() const
bool read(ossimFilename &filename)
bool find(const char *)
ossimDirectoryData * theData
bool findCaseInsensitiveEquivalents(const ossimFilename &filename, std::vector< ossimFilename > &result, bool bExcludeExactMatch=true)