Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.8

Main Page | Directories | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

DirectoryEnumerator.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 1999-2004 The Apache Software Foundation.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #if !defined(DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680)
00017 #define DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680
00018 
00019 
00020 
00021 // Base header file.  Must be first.
00022 #include <xalanc/PlatformSupport/PlatformSupportDefinitions.hpp>
00023 
00024 
00025 
00026 #if defined(_MSC_VER)
00027 #include <io.h>
00028 #else
00029 #include <dirent.h>
00030 #endif
00031 
00032 
00033 
00034 #include <functional>
00035 #include <iterator>
00036 
00037 
00038 
00039 #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
00040 #include <xalanc/PlatformSupport/XalanUnicode.hpp>
00041 
00042 
00043 
00044 XALAN_CPP_NAMESPACE_BEGIN
00045 
00046 
00047 
00048 #if defined(_MSC_VER)
00049 
00050 struct FindFileStruct : public _wfinddata_t
00051 {
00052 
00053     enum eAttributes
00054     {
00055         eAttributeArchive = _A_ARCH,
00056         eAttributeDirectory = _A_SUBDIR,
00057         eAttributeHidden = _A_HIDDEN,
00058         eAttributeNormal = _A_NORMAL,
00059         eReadOnly = _A_RDONLY,
00060         eSystem = _A_SYSTEM
00061     };
00062 
00063 public:
00064 
00070     const XalanDOMChar*
00071     getName() const
00072     {
00073         return name;
00074     }
00075 
00081     bool
00082     isDirectory() const
00083     {
00084         return attrib & eAttributeDirectory ? true : false;
00085     }
00086 
00087     bool
00088     isSelfOrParent() const
00089     {
00090         if (isDirectory() == false)
00091         {
00092             return false;
00093         }
00094         else if (name[0] == '.')
00095         {
00096             if (name[1] == '\0')
00097             {
00098                 return true;
00099             }
00100             else if (name[1] == '.' &&
00101                      name[2] == '\0')
00102             {
00103                 return true;
00104             }
00105             else
00106             {
00107                 return false;
00108             }
00109         }
00110         else
00111         {
00112             return false;
00113         }
00114     }
00115 };
00116 
00117 #else
00118 
00119 struct FindFileStruct : public dirent
00120 {
00121 public:
00122 
00128     const char* getName() const
00129     {
00130         return d_name;
00131     }
00132 
00138     bool isDirectory() const
00139     {
00140 #if defined(AIX) || defined(HPUX) || defined(SOLARIS) || defined(OS390) || defined(OS400) || defined(TRU64)  || defined(CYGWIN)
00141         return false;
00142 #else       
00143         if (d_type == DT_DIR || d_type == DT_UNKNOWN)
00144         {
00145             return true;
00146         }
00147         else
00148         {
00149             return false;
00150         }
00151 #endif      
00152     }
00153 
00154     bool
00155     isSelfOrParent() const
00156     {
00157 #if defined(AIX) || defined(HPUX) || defined(SOLARIS) || defined(OS390) || defined(OS400) || defined(TRU64)
00158         return false;
00159 #else       
00160         if (isDirectory() == false)
00161         {
00162             return false;
00163         }
00164         else if (d_name[0] == '.')
00165         {
00166             if (d_name[1] == '\0')
00167             {
00168                 return true;
00169             }
00170             else if (d_name[1] == '.' &&
00171                      d_name[2] == '\0')
00172             {
00173                 return true;
00174             }
00175             else
00176             {
00177                 return false;
00178             }
00179         }
00180         else
00181         {
00182             return false;
00183         }
00184 #endif
00185     }
00186 };
00187 
00188 #endif
00189 
00190 
00191 
00192 #if defined(XALAN_NO_STD_NAMESPACE)
00193 struct DirectoryFilterPredicate : public unary_function<FindFileStruct, bool>
00194 #else
00195 struct DirectoryFilterPredicate : public std::unary_function<FindFileStruct, bool>
00196 #endif
00197 {
00198     result_type
00199     operator()(const argument_type& theFindData) const
00200     {
00201         return theFindData.isDirectory();
00202     }
00203 };
00204 
00205 
00206 
00207 #if defined(XALAN_NO_STD_NAMESPACE)
00208 struct FilesOnlyFilterPredicate : public unary_function<FindFileStruct, bool>
00209 #else
00210 struct FilesOnlyFilterPredicate : public std::unary_function<FindFileStruct, bool>
00211 #endif
00212 {
00213     result_type
00214     operator()(const argument_type& theFindData) const
00215     {
00216         DirectoryFilterPredicate        theDirectoryPredicate;
00217 
00218         return !theDirectoryPredicate(theFindData);
00219                
00220     }
00221 };
00222 
00223 
00224 
00225 template<class OutputIteratorType,
00226          class FilterPredicateType,
00227          class StringType,
00228          class StringConversionFunction>
00229 void
00230 EnumerateDirectory(
00231             const StringType&           theFullSearchSpec,
00232             OutputIteratorType          theOutputIterator,
00233             FilterPredicateType         theFilterPredicate,
00234             StringConversionFunction    theConversionFunction,
00235 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00236             bool                        fIncludeSelfAndParent)
00237 #else
00238             bool                        fIncludeSelfAndParent = false)
00239 #endif
00240 {
00241 #if defined(_MSC_VER)
00242     FindFileStruct      theFindData;
00243 
00244     long    theSearchHandle = _wfindfirst(const_cast<wchar_t*>(theConversionFunction(theFullSearchSpec)),
00245                                           &theFindData);
00246 
00247     if (theSearchHandle != -1)
00248     {
00249         try
00250         {
00251             do
00252             {
00253                 if ((fIncludeSelfAndParent == true || theFindData.isSelfOrParent() == false) &&
00254                     theFilterPredicate(theFindData) == true)
00255                 {
00256                     *theOutputIterator = StringType(theFindData.getName());
00257                 }
00258             }
00259             while(_wfindnext(theSearchHandle,
00260                              &theFindData) == 0);
00261         }
00262         catch(...)
00263         {
00264             _findclose(theSearchHandle);
00265 
00266             throw;
00267         }
00268 
00269         _findclose(theSearchHandle);
00270     }
00271 
00272     
00273 #elif defined(LINUX)
00274 
00275     CharVectorType  theTargetVector;
00276 
00277     TranscodeToLocalCodePage(theFullSearchSpec, theTargetVector, false);
00278 
00279     const CharVectorType::size_type     theSize = theTargetVector.size();
00280 
00281     if (theSize > 0)
00282     {
00283         if (theTargetVector.back() == '*')
00284         {
00285             theTargetVector.pop_back();
00286 
00287             if (theSize == 1)
00288             {
00289                 theTargetVector.push_back('.');
00290             }
00291         }
00292 
00293         theTargetVector.push_back('\0');
00294 
00295         const char* const   theSpec = c_str(theTargetVector);
00296         assert(theSpec != 0);
00297 
00298         DIR* const  theDirectory = opendir(theSpec);
00299 
00300         if (theDirectory != 0)
00301         {
00302             try
00303             {
00304                 const FindFileStruct*   theEntry =
00305                     (FindFileStruct*)readdir(theDirectory);
00306     
00307                 while(theEntry != 0)
00308                 {
00309                     if ((fIncludeSelfAndParent == true || theEntry->isSelfOrParent() == false) &&
00310                         theFilterPredicate(*theEntry) == true)
00311                     {
00312                         *theOutputIterator = StringType(theEntry->getName());
00313                     }
00314 
00315                     theEntry = (FindFileStruct*)readdir(theDirectory);
00316                 }
00317             }
00318             catch(...)
00319             {
00320                 closedir(theDirectory);
00321 
00322                 throw;
00323             }
00324 
00325             closedir(theDirectory);
00326         }
00327     }
00328 #else
00329     // Do nothing for now...
00330     // Unsupported platform!!!
00331 #endif
00332 }
00333 
00334 
00335 
00336 template<class OutputIteratorType,
00337          class FilterPredicateType,
00338          class StringType,
00339          class StringConversionFunction>
00340 void
00341 EnumerateDirectory(
00342             const StringType&           theDirectory,
00343             const StringType&           theSearchSpec,
00344             OutputIteratorType          theOutputIterator,
00345             FilterPredicateType         theFilterPredicate,
00346             StringConversionFunction    theConversionFunction,
00347 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00348             bool                        fIncludeSelfAndParent)
00349 #else
00350             bool                        fIncludeSelfAndParent = false)
00351 #endif
00352 {
00353     StringType  theFullSearchSpec(theDirectory);
00354 
00355     theFullSearchSpec += theSearchSpec;
00356 
00357     EnumerateDirectory(theFullSearchSpec, theOutputIterator, theFilterPredicate, theConversionFunction, fIncludeSelfAndParent);
00358 }
00359 
00360 
00361 
00362 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00363 template<class CollectionType, class StringType>
00364 struct DirectoryEnumeratorFunctor
00365 {
00366     CollectionType
00367     operator()(const StringType&    theDirectory) const
00368     {
00369         CollectionType      theCollection;
00370 
00371         operator()(theDirectory,
00372                theCollection);
00373 
00374         return theCollection;
00375     }
00376 
00377     void
00378     operator()(
00379         const StringType&,
00380         const CollectionType&) const
00381     {
00382     }
00383 };
00384 #else
00385 template<class CollectionType,
00386      class StringType = XalanDOMString,
00387      class FilterPredicateType = FilesOnlyFilterPredicate,
00388      class StringConversionFunction = c_wstr_functor>
00389 #if defined(XALAN_NO_STD_NAMESPACE)
00390 struct DirectoryEnumeratorFunctor : public unary_function<StringType, CollectionType>
00391 #else
00392 struct DirectoryEnumeratorFunctor : public std::unary_function<StringType, CollectionType>
00393 #endif
00394 {
00395 #if defined(XALAN_NO_STD_NAMESPACE)
00396     typedef unary_function<StringType, CollectionType>  BaseClassType;
00397 #else
00398     typedef std::unary_function<StringType, CollectionType> BaseClassType;
00399 #endif
00400 
00401     typedef typename BaseClassType::result_type     result_type;
00402     typedef typename BaseClassType::argument_type   argument_type;
00403 
00404     explicit
00405     DirectoryEnumeratorFunctor(bool     fIncludeSelfAndParent = false) :
00406         m_includeSelfAndParent(fIncludeSelfAndParent)
00407     {
00408     }
00409             
00410     void
00411     operator()(
00412             const argument_type&    theFullSearchSpec,
00413             CollectionType&         theCollection) const
00414     {
00415         XALAN_USING_STD(back_inserter)
00416 
00417         EnumerateDirectory(
00418                 theFullSearchSpec,
00419                 XALAN_STD_QUALIFIER back_inserter(theCollection),
00420                 m_filterPredicate,
00421                 m_conversionFunction,
00422                 m_includeSelfAndParent);
00423     }
00424 
00425     result_type
00426     operator()(const argument_type&     theFullSearchSpec) const
00427     {
00428         result_type     theCollection;
00429 
00430         operator()(
00431                 theFullSearchSpec,
00432                 theCollection);
00433 
00434         return theCollection;
00435     }
00436 
00437     void
00438     operator()(
00439             const argument_type&    theDirectory,
00440             const argument_type&    theSearchSpec,
00441             CollectionType&         theCollection) const
00442     {
00443         EnumerateDirectory(
00444                 theDirectory,
00445                 theSearchSpec,
00446                 XALAN_STD_QUALIFIER back_inserter(theCollection),
00447                 m_filterPredicate,
00448                 m_conversionFunction,
00449                 m_includeSelfAndParent);
00450     }
00451 
00452     result_type
00453     operator()(
00454             const argument_type&    theDirectory,
00455             const argument_type&    theSearchSpec) const
00456     {
00457         result_type     theCollection;
00458 
00459         operator()(
00460                 theDirectory,
00461                 theSearchSpec,
00462                 theCollection);
00463 
00464         return theCollection;
00465     }
00466 
00467 private:
00468 
00469     FilterPredicateType         m_filterPredicate;
00470 
00471     StringConversionFunction    m_conversionFunction;
00472 
00473     const bool                  m_includeSelfAndParent;
00474 };
00475 #endif
00476 
00477 
00478 
00479 XALAN_CPP_NAMESPACE_END
00480 
00481 
00482 
00483 #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.

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