Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.11


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

Interpreting class diagrams

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

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

Apache Logo