Xalan-C++ API Reference 1.12.0
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
54namespace XALAN_CPP_NAMESPACE {
55
56
57
58using xercesc::MemoryManager;
59
60
61
62#if defined(_MSC_VER)
63
64struct 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
77public:
78
79 /**
80 * Retrieve name of file
81 *
82 * @return file name
83 */
84 const XalanDOMChar*
85 getName() const
86 {
87 return const_cast<XalanDOMChar*>(reinterpret_cast<const XalanDOMChar*>(&name[0]));
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
133struct FindFileStruct : public dirent
134{
135public:
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
202{
203 bool
205 {
206 return theFindData.isDirectory();
207 }
208};
209
210
211
223
224
225
226template<class OutputIteratorType,
227 class FilterPredicateType,
228 class StringType,
229 class StringConversionFunction>
230void
232 MemoryManager& theMemoryManager,
239#else
240 bool fIncludeSelfAndParent = false)
241#endif
242{
243#if defined(_MSC_VER)
245
246 #ifdef _WIN64
247 typedef intptr_t theHandleType;
248 #else
249 typedef long theHandleType;
250 #endif
251
252#pragma warning(push)
253#pragma warning(disable: 4244)
256 reinterpret_cast<wchar_t*>(const_cast<XalanDOMChar*>(theConversionFunction(theFullSearchSpec))),
257 &theFindData);
258#pragma warning(pop)
259
260 if (theSearchHandle != -1)
261 {
262
263 try
264 {
265 do
266 {
267 if ((fIncludeSelfAndParent == true || theFindData.isSelfOrParent() == false) &&
269 {
271 }
272 }
274 &theFindData) == 0);
275 }
276 catch(...)
277 {
279
280 throw;
281 }
282
284 }
285
286
287#else
288
290
292
294 int indexSuffix=0, indexName=0;
295 bool target_Dir = false;
296
297 if (theSize > 0)
298 {
299 if (theTargetVector.back() == '*')
300 {
301 target_Dir = true;
302 theTargetVector.pop_back();
303
304 if (theSize == 1)
305 {
306 theTargetVector.push_back('.');
307 }
308
309 }
310 else
311 {
312 target_Dir = false;
313
314 while(theTargetVector.back() != '*')
315 {
316 theTargetVector.pop_back();
317 indexSuffix++;
318 }
319
320 theTargetVector.pop_back();
321 while(theTargetVector.back() != '/')
322 {
323 theTargetVector.pop_back();
324 indexName++;
325 }
326 }
327
328 theTargetVector.push_back('\0');
329
330 const char* const theSpec = c_str(theTargetVector);
331 assert(theSpec != 0);
332
335 if ( !target_Dir )
336 {
337 using std::strlen;
338
339 int lenSpec = strlen(theSpec);
342 }
343
345
346 if (theDirectory != 0)
347 {
348 chdir(theSpec);
349 try
350 {
351 const FindFileStruct* theEntry =
353
354 while(theEntry != 0)
355 {
356 if ((fIncludeSelfAndParent == true || theEntry->isSelfOrParent() == false))
357 {
358 if (theFilterPredicate(*theEntry) == true)
359 {
360 if( target_Dir )
361 {
363 }
364 else
365 {
367 int Check_1 = Getname.compare(theName);
370 int Check_2 = GetnameSuffix.compare(theSuffix);
371 if ( Check_1 == 1 && (!Check_2) )
372 {
374 }
375 }
376 }
377 }
379 } //while
380 }//try
381
382 catch(...)
383 {
385
386 throw;
387 }
388 if( target_Dir )
389 chdir("..");
390 else
391 chdir("../..");
393 }
394 }
395
396#endif
397}
398
399
400
401template<class OutputIteratorType,
402 class FilterPredicateType,
403 class StringType,
404 class StringConversionFunction>
405void
431
432
433
434#if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
435template<class CollectionType, class StringType>
436struct DirectoryEnumeratorFunctor
437{
438 CollectionType
439 operator()(const StringType& theDirectory) const
440 {
441 CollectionType theCollection;
442
443 operator()(theDirectory,
444 theCollection);
445
446 return theCollection;
447 }
448
449 void
450 operator()(
451 const StringType&,
452 const CollectionType&) const
453 {
454 }
455};
456#else
457template<class CollectionType,
458 class StringType = XalanDOMString,
459 class FilterPredicateType = FilesOnlyFilterPredicate,
460 class StringConversionFunction = c_wstr_functor>
462{
463 explicit
465 MemoryManager& theMemoryManager,
466 bool fIncludeSelfAndParent = false) :
467 m_includeSelfAndParent(fIncludeSelfAndParent),
468 m_memoryManager(theMemoryManager)
469 {
470 }
471
472 void
475 CollectionType& theCollection) const
476 {
477 using std::back_inserter;
478
480 m_memoryManager,
482 std::back_inserter(theCollection),
483 m_filterPredicate,
484 m_conversionFunction,
485 m_includeSelfAndParent);
486 }
487
488 CollectionType
490 {
491 CollectionType theCollection;
492
493 operator()(
496
497 return theCollection;
498 }
499
500 void
504 CollectionType& theCollection) const
505 {
507 m_memoryManager,
510 std::back_inserter(theCollection),
511 m_filterPredicate,
512 m_conversionFunction,
513 m_includeSelfAndParent);
514 }
515
516 CollectionType
519 const StringType& theSearchSpec) const
520 {
521 CollectionType theCollection;
522
523 operator()(
527
528 return theCollection;
529 }
530
531private:
532
533 FilterPredicateType m_filterPredicate;
534
535 StringConversionFunction m_conversionFunction;
536
537 const bool m_includeSelfAndParent;
538
539 MemoryManager& m_memoryManager;
540};
541#endif
542
543
544
545}
546
547
548
549#endif // DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
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...
const char * c_str(const CharVectorType &theString)
Get the underlying representation of the target CharVectorType as a null-terminated string.
void EnumerateDirectory(MemoryManager &theMemoryManager, const StringType &theFullSearchSpec, OutputIteratorType theOutputIterator, FilterPredicateType theFilterPredicate, StringConversionFunction theConversionFunction, bool fIncludeSelfAndParent=false)
DirectoryEnumeratorFunctor(MemoryManager &theMemoryManager, bool fIncludeSelfAndParent=false)
CollectionType operator()(const StringType &theFullSearchSpec) const
void operator()(const StringType &theDirectory, const StringType &theSearchSpec, CollectionType &theCollection) const
CollectionType operator()(const StringType &theDirectory, const StringType &theSearchSpec) const
void operator()(const StringType &theFullSearchSpec, CollectionType &theCollection) const
bool operator()(const FindFileStruct &theFindData) const
bool operator()(const FindFileStruct &theFindData) const
bool isDirectory() const
Determine whether file is a directory.
const char * getName() const
Retrieve name of file.