Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.10

DirectoryEnumerator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #if !defined(DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680)
17 #define DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680
18 
19 
20 
21 // Base header file. Must be first.
23 
24 
25 
26 #if defined(_MSC_VER)
27 #include <io.h>
28 #else
29 #include <unistd.h>
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #endif
34 
35 
36 
37 #include <functional>
38 #include <iterator>
39 #include <cstring>
40 
41 #include "xercesc/framework/MemoryManager.hpp"
42 
43 
44 
48 
49 
50 
51 XALAN_CPP_NAMESPACE_BEGIN
52 
53 
54 
55 XALAN_USING_XERCES(MemoryManager)
56 
57 
58 
59 #if defined(_MSC_VER)
60 
61 struct FindFileStruct : public _wfinddata_t
62 {
63 
64  enum eAttributes
65  {
66  eAttributeArchive = _A_ARCH,
67  eAttributeDirectory = _A_SUBDIR,
68  eAttributeHidden = _A_HIDDEN,
69  eAttributeNormal = _A_NORMAL,
70  eReadOnly = _A_RDONLY,
71  eSystem = _A_SYSTEM
72  };
73 
74 public:
75 
81  const XalanDOMChar*
82  getName() const
83  {
84  return name;
85  }
86 
92  bool
93  isDirectory() const
94  {
95  return attrib & eAttributeDirectory ? true : false;
96  }
97 
98  bool
99  isSelfOrParent() const
100  {
101  if (isDirectory() == false)
102  {
103  return false;
104  }
105  else if (name[0] == '.')
106  {
107  if (name[1] == '\0')
108  {
109  return true;
110  }
111  else if (name[1] == '.' &&
112  name[2] == '\0')
113  {
114  return true;
115  }
116  else
117  {
118  return false;
119  }
120  }
121  else
122  {
123  return false;
124  }
125  }
126 };
127 
128 #else
129 
130 struct FindFileStruct : public dirent
131 {
132 public:
133 
139  const char* getName() const
140  {
141  return d_name;
142  }
143 
149  bool isDirectory() const
150  {
151 #if defined(__SunOS_5_10) && (__SUNPRO_CC >= 0x570)
152  struct stat64 stat_Info;
153 
154  const int retCode = stat64(d_name, &stat_Info);
155 #else
156  struct stat stat_Info;
157 
158  const int retCode = stat(d_name, &stat_Info);
159 #endif
160 
161  return retCode == -1 ? false : S_ISDIR(stat_Info.st_mode);
162  }
163 
164  bool
166  {
167  if (isDirectory() == false)
168  {
169  return false;
170  }
171  else if (d_name[0] == '.')
172  {
173  if (d_name[1] == '\0')
174  {
175  return true;
176  }
177  else if (d_name[1] == '.' &&
178  d_name[2] == '\0')
179  {
180  return true;
181  }
182  else
183  {
184  return false;
185  }
186  }
187  else
188  {
189  return false;
190  }
191  }
192 };
193 
194 #endif
195 
196 
197 
198 #if defined(XALAN_NO_STD_NAMESPACE)
199 struct DirectoryFilterPredicate : public unary_function<FindFileStruct, bool>
200 #else
201 struct DirectoryFilterPredicate : public std::unary_function<FindFileStruct, bool>
202 #endif
203 {
204  result_type
205  operator()(const argument_type& theFindData) const
206  {
207  return theFindData.isDirectory();
208  }
209 };
210 
211 
212 
213 #if defined(XALAN_NO_STD_NAMESPACE)
214 struct FilesOnlyFilterPredicate : public unary_function<FindFileStruct, bool>
215 #else
216 struct FilesOnlyFilterPredicate : public std::unary_function<FindFileStruct, bool>
217 #endif
218 {
219  result_type
220  operator()(const argument_type& theFindData) const
221  {
222  DirectoryFilterPredicate theDirectoryPredicate;
223 
224  return !theDirectoryPredicate(theFindData);
225 
226  }
227 };
228 
229 
230 
231 template<class OutputIteratorType,
232  class FilterPredicateType,
233  class StringType,
234  class StringConversionFunction>
235 void
237  MemoryManager& theMemoryManager,
238  const StringType& theFullSearchSpec,
239  OutputIteratorType theOutputIterator,
240  FilterPredicateType theFilterPredicate,
241  StringConversionFunction theConversionFunction,
243  bool fIncludeSelfAndParent)
244 #else
245  bool fIncludeSelfAndParent = false)
246 #endif
247 {
248 #if defined(_MSC_VER)
249  FindFileStruct theFindData;
250 
251  #ifdef _WIN64
252  typedef intptr_t theHandleType;
253  #else
254  typedef long theHandleType;
255  #endif
256 
257 #pragma warning(push)
258 #pragma warning(disable: 4244)
259  theHandleType theSearchHandle =
260  _wfindfirst(
261  const_cast<wchar_t*>(theConversionFunction(theFullSearchSpec)),
262  &theFindData);
263 #pragma warning(pop)
264 
265  if (theSearchHandle != -1)
266  {
267 
268  try
269  {
270  do
271  {
272  if ((fIncludeSelfAndParent == true || theFindData.isSelfOrParent() == false) &&
273  theFilterPredicate(theFindData) == true)
274  {
275  *theOutputIterator = StringType(theFindData.getName(), theMemoryManager);
276  }
277  }
278  while(_wfindnext(theSearchHandle,
279  &theFindData) == 0);
280  }
281  catch(...)
282  {
283  _findclose(theSearchHandle);
284 
285  throw;
286  }
287 
288  _findclose(theSearchHandle);
289  }
290 
291 
292 #else
293 
294  CharVectorType theTargetVector(theMemoryManager);
295 
296  TranscodeToLocalCodePage(theFullSearchSpec, theTargetVector, false);
297 
298  const CharVectorType::size_type theSize = theTargetVector.size();
299  int indexSuffix=0, indexName=0;
300  bool target_Dir = false;
301 
302  if (theSize > 0)
303  {
304  if (theTargetVector.back() == '*')
305  {
306  target_Dir = true;
307  theTargetVector.pop_back();
308 
309  if (theSize == 1)
310  {
311  theTargetVector.push_back('.');
312  }
313 
314  }
315  else
316  {
317  target_Dir = false;
318 
319  while(theTargetVector.back() != '*')
320  {
321  theTargetVector.pop_back();
322  indexSuffix++;
323  }
324 
325  theTargetVector.pop_back();
326  while(theTargetVector.back() != '/')
327  {
328  theTargetVector.pop_back();
329  indexName++;
330  }
331  }
332 
333  theTargetVector.push_back('\0');
334 
335  const char* const theSpec = c_str(theTargetVector);
336  assert(theSpec != 0);
337 
338  XalanDOMString theName(theMemoryManager);
339  XalanDOMString theSuffix(theMemoryManager);
340  if ( !target_Dir )
341  {
342 #if defined(XALAN_STRICT_ANSI_HEADERS)
343  using std::strlen;
344 #endif
345 
346  int lenSpec = strlen(theSpec);
347  theFullSearchSpec.substr(theName, lenSpec, indexName);
348  theFullSearchSpec.substr(theSuffix, lenSpec+indexName+1, indexSuffix);
349  }
350 
351  DIR* const theDirectory = opendir(theSpec);
352 
353  if (theDirectory != 0)
354  {
355  chdir(theSpec);
356  try
357  {
358  const FindFileStruct* theEntry =
359  (FindFileStruct*)readdir(theDirectory);
360 
361  while(theEntry != 0)
362  {
363  if ((fIncludeSelfAndParent == true || theEntry->isSelfOrParent() == false))
364  {
365  if (theFilterPredicate(*theEntry) == true)
366  {
367  if( target_Dir )
368  {
369  *theOutputIterator = StringType(theEntry->getName(), theMemoryManager);
370  }
371  else
372  {
373  XalanDOMString Getname(theEntry->getName(), theMemoryManager);
374  int Check_1 = Getname.compare(theName);
375  XalanDOMString GetnameSuffix(theMemoryManager);
376  Getname.substr(GetnameSuffix, Getname.size() -indexSuffix, indexSuffix);
377  int Check_2 = GetnameSuffix.compare(theSuffix);
378  if ( Check_1 == 1 && (!Check_2) )
379  {
380  *theOutputIterator = StringType(theEntry->getName(), theMemoryManager);
381  }
382  }
383  }
384  }
385  theEntry = (FindFileStruct*)readdir(theDirectory);
386  } //while
387  }//try
388 
389  catch(...)
390  {
391  closedir(theDirectory);
392 
393  throw;
394  }
395  if( target_Dir )
396  chdir("..");
397  else
398  chdir("../..");
399  closedir(theDirectory);
400  }
401  }
402 
403 #endif
404 }
405 
406 
407 
408 template<class OutputIteratorType,
409  class FilterPredicateType,
410  class StringType,
411  class StringConversionFunction>
412 void
414  MemoryManager& theMemoryManager,
415  const StringType& theDirectory,
416  const StringType& theSearchSpec,
417  OutputIteratorType theOutputIterator,
418  FilterPredicateType theFilterPredicate,
419  StringConversionFunction theConversionFunction,
421  bool fIncludeSelfAndParent)
422 #else
423  bool fIncludeSelfAndParent = false)
424 #endif
425 {
426  StringType theFullSearchSpec(theDirectory, theMemoryManager);
427 
428  theFullSearchSpec += theSearchSpec;
429 
431  theMemoryManager,
432  theFullSearchSpec,
433  theOutputIterator,
434  theFilterPredicate,
435  theConversionFunction,
436  fIncludeSelfAndParent);
437 }
438 
439 
440 
441 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
442 template<class CollectionType, class StringType>
444 {
445  CollectionType
446  operator()(const StringType& theDirectory) const
447  {
448  CollectionType theCollection;
449 
450  operator()(theDirectory,
451  theCollection);
452 
453  return theCollection;
454  }
455 
456  void
457  operator()(
458  const StringType&,
459  const CollectionType&) const
460  {
461  }
462 };
463 #else
464 template<class CollectionType,
465  class StringType = XalanDOMString,
466  class FilterPredicateType = FilesOnlyFilterPredicate,
467  class StringConversionFunction = c_wstr_functor>
468 #if defined(XALAN_NO_STD_NAMESPACE)
469 struct DirectoryEnumeratorFunctor : public unary_function<StringType, CollectionType>
470 #else
471 struct DirectoryEnumeratorFunctor : public std::unary_function<StringType, CollectionType>
472 #endif
473 {
474 #if defined(XALAN_NO_STD_NAMESPACE)
475  typedef unary_function<StringType, CollectionType> BaseClassType;
476 #else
477  typedef std::unary_function<StringType, CollectionType> BaseClassType;
478 #endif
479 
480  typedef typename BaseClassType::result_type result_type;
481  typedef typename BaseClassType::argument_type argument_type;
482 
483  explicit
485  MemoryManager& theMemoryManager,
486  bool fIncludeSelfAndParent = false) :
487  m_includeSelfAndParent(fIncludeSelfAndParent),
488  m_memoryManager(theMemoryManager)
489  {
490  }
491 
492  void
494  const argument_type& theFullSearchSpec,
495  CollectionType& theCollection) const
496  {
497  XALAN_USING_STD(back_inserter)
498 
500  m_memoryManager,
501  theFullSearchSpec,
502  XALAN_STD_QUALIFIER back_inserter(theCollection),
503  m_filterPredicate,
504  m_conversionFunction,
505  m_includeSelfAndParent);
506  }
507 
509  operator()(const argument_type& theFullSearchSpec) const
510  {
511  result_type theCollection;
512 
513  operator()(
514  theFullSearchSpec,
515  theCollection);
516 
517  return theCollection;
518  }
519 
520  void
522  const argument_type& theDirectory,
523  const argument_type& theSearchSpec,
524  CollectionType& theCollection) const
525  {
527  m_memoryManager,
528  theDirectory,
529  theSearchSpec,
530  XALAN_STD_QUALIFIER back_inserter(theCollection),
531  m_filterPredicate,
532  m_conversionFunction,
533  m_includeSelfAndParent);
534  }
535 
538  const argument_type& theDirectory,
539  const argument_type& theSearchSpec) const
540  {
541  result_type theCollection;
542 
543  operator()(
544  theDirectory,
545  theSearchSpec,
546  theCollection);
547 
548  return theCollection;
549  }
550 
551 private:
552 
553  FilterPredicateType m_filterPredicate;
554 
555  StringConversionFunction m_conversionFunction;
556 
557  const bool m_includeSelfAndParent;
558 
559  MemoryManager& m_memoryManager;
560 };
561 #endif
562 
563 
564 
565 XALAN_CPP_NAMESPACE_END
566 
567 
568 
569 #endif // DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680

Interpreting class diagrams

Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.

dot

Xalan-C++ XSLT Processor Version 1.10
Copyright © 1999-2004 The Apache Software Foundation. All Rights Reserved.

Apache Logo