Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.6

Main Page | 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  * The Apache Software License, Version 1.1
00003  *
00004  *
00005  * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights 
00006  * reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer. 
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in
00017  *    the documentation and/or other materials provided with the
00018  *    distribution.
00019  *
00020  * 3. The end-user documentation included with the redistribution,
00021  *    if any, must include the following acknowledgment:  
00022  *       "This product includes software developed by the
00023  *        Apache Software Foundation (http://www.apache.org/)."
00024  *    Alternately, this acknowledgment may appear in the software itself,
00025  *    if and wherever such third-party acknowledgments normally appear.
00026  *
00027  * 4. The names "Xalan" and "Apache Software Foundation" must
00028  *    not be used to endorse or promote products derived from this
00029  *    software without prior written permission. For written 
00030  *    permission, please contact apache@apache.org.
00031  *
00032  * 5. Products derived from this software may not be called "Apache",
00033  *    nor may "Apache" appear in their name, without prior written
00034  *    permission of the Apache Software Foundation.
00035  *
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00039  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00040  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00041  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00042  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00043  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00045  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00046  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00047  * SUCH DAMAGE.
00048  * ====================================================================
00049  *
00050  * This software consists of voluntary contributions made by many
00051  * individuals on behalf of the Apache Software Foundation and was
00052  * originally based on software copyright (c) 1999, International
00053  * Business Machines, Inc., http://www.ibm.com.  For more
00054  * information on the Apache Software Foundation, please see
00055  * <http://www.apache.org/>.
00056  */
00057 #if !defined(DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680)
00058 #define DIRECTORY_ENUMERATOR_HEADER_GUARD_1357924680
00059 
00060 
00061 
00062 // Base header file.  Must be first.
00063 #include <xalanc/PlatformSupport/PlatformSupportDefinitions.hpp>
00064 
00065 
00066 
00067 #if defined(_MSC_VER)
00068 #include <io.h>
00069 #else
00070 #include <dirent.h>
00071 #endif
00072 
00073 
00074 
00075 #include <functional>
00076 #include <iterator>
00077 
00078 
00079 
00080 #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
00081 #include <xalanc/PlatformSupport/XalanUnicode.hpp>
00082 
00083 
00084 
00085 XALAN_CPP_NAMESPACE_BEGIN
00086 
00087 
00088 
00089 #if defined(_MSC_VER)
00090 
00091 class FindFileStruct : public _wfinddata_t
00092 {
00093 
00094     enum eAttributes
00095     {
00096         eAttributeArchive = _A_ARCH,
00097         eAttributeDirectory = _A_SUBDIR,
00098         eAttributeHidden = _A_HIDDEN,
00099         eAttributeNormal = _A_NORMAL,
00100         eReadOnly = _A_RDONLY,
00101         eSystem = _A_SYSTEM
00102     };
00103 
00104 public:
00105 
00111     const XalanDOMChar*
00112     getName() const
00113     {
00114         return name;
00115     }
00116 
00122     bool
00123     isDirectory() const
00124     {
00125         return attrib & eAttributeDirectory ? true : false;
00126     }
00127 
00128     bool
00129     isSelfOrParent() const
00130     {
00131         if (isDirectory() == false)
00132         {
00133             return false;
00134         }
00135         else if (name[0] == XalanUnicode::charFullStop)
00136         {
00137             if (name[1] == 0)
00138             {
00139                 return true;
00140             }
00141             else if (name[1] == XalanUnicode::charFullStop &&
00142                      name[2] == 0)
00143             {
00144                 return true;
00145             }
00146             else
00147             {
00148                 return false;
00149             }
00150         }
00151         else
00152         {
00153             return false;
00154         }
00155     }
00156 };
00157 
00158 #else
00159 
00160 class FindFileStruct : public dirent
00161 {
00162 public:
00163 
00169     const char* getName() const
00170     {
00171         return d_name;
00172     }
00173 
00179     bool isDirectory() const
00180     {
00181 #if defined(AIX) || defined(HPUX) || defined(SOLARIS) || defined(OS390) || defined(TRU64)
00182         return false;
00183 #else       
00184         return d_type == DT_DIR;        
00185 #endif      
00186     }
00187 };
00188 
00189 #endif
00190 
00191 
00192 
00193 #if defined(XALAN_NO_STD_NAMESPACE)
00194 struct DirectoryFilterPredicate : public unary_function<FindFileStruct, bool>
00195 #else
00196 struct DirectoryFilterPredicate : public std::unary_function<FindFileStruct, bool>
00197 #endif
00198 {
00199     result_type
00200     operator()(const argument_type& theFindData) const
00201     {
00202         return theFindData.isDirectory();
00203     }
00204 };
00205 
00206 
00207 
00208 #if defined(XALAN_NO_STD_NAMESPACE)
00209 struct FilesOnlyFilterPredicate : public unary_function<FindFileStruct, bool>
00210 #else
00211 struct FilesOnlyFilterPredicate : public std::unary_function<FindFileStruct, bool>
00212 #endif
00213 {
00214     result_type
00215     operator()(const argument_type& theFindData) const
00216     {
00217         DirectoryFilterPredicate        theDirectoryPredicate;
00218 
00219         return !theDirectoryPredicate(theFindData);
00220                
00221     }
00222 };
00223 
00224 
00225 
00226 template<class OutputIteratorType,
00227          class FilterPredicateType,
00228          class StringType,
00229          class StringConversionFunction>
00230 void
00231 EnumerateDirectory(
00232             const StringType&           theFullSearchSpec,
00233             OutputIteratorType          theOutputIterator,
00234             FilterPredicateType         theFilterPredicate,
00235             StringConversionFunction    theConversionFunction,
00236 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00237             bool                        fIncludeSelfAndParent)
00238 #else
00239             bool                        fIncludeSelfAndParent = false)
00240 #endif
00241 {
00242 #if defined(_MSC_VER)
00243     FindFileStruct      theFindData;
00244 
00245     long    theSearchHandle = _wfindfirst(const_cast<wchar_t*>(theConversionFunction(theFullSearchSpec)),
00246                                           &theFindData);
00247 
00248     if (theSearchHandle != -1)
00249     {
00250         try
00251         {
00252             do
00253             {
00254                 if ((fIncludeSelfAndParent == true || theFindData.isSelfOrParent() == false) &&
00255                     theFilterPredicate(theFindData) == true)
00256                 {
00257                     *theOutputIterator = StringType(theFindData.getName());
00258                 }
00259             }
00260             while(_wfindnext(theSearchHandle,
00261                              &theFindData) == 0);
00262         }
00263         catch(...)
00264         {
00265             _findclose(theSearchHandle);
00266 
00267             throw;
00268         }
00269 
00270         _findclose(theSearchHandle);
00271     }
00272 
00273     
00274 #else
00275     // Do nothing for now...
00276     // Unsupported platform!!!
00277 #endif
00278 }
00279 
00280 
00281 
00282 template<class OutputIteratorType,
00283          class FilterPredicateType,
00284          class StringType,
00285          class StringConversionFunction>
00286 void
00287 EnumerateDirectory(
00288             const StringType&           theDirectory,
00289             const StringType&           theSearchSpec,
00290             OutputIteratorType          theOutputIterator,
00291             FilterPredicateType         theFilterPredicate,
00292             StringConversionFunction    theConversionFunction,
00293 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00294             bool                        fIncludeSelfAndParent)
00295 #else
00296             bool                        fIncludeSelfAndParent = false)
00297 #endif
00298 {
00299     StringType  theFullSearchSpec(theDirectory);
00300 
00301     theFullSearchSpec += theSearchSpec;
00302 
00303     EnumerateDirectory(theFullSearchSpec, theOutputIterator, theFilterPredicate, theConversionFunction, fIncludeSelfAndParent);
00304 }
00305 
00306 
00307 
00308 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00309 template<class CollectionType, class StringType>
00310 struct DirectoryEnumeratorFunctor
00311 {
00312     CollectionType
00313     operator()(const StringType&    theDirectory) const
00314     {
00315         CollectionType      theCollection;
00316 
00317         operator()(theDirectory,
00318                theCollection);
00319 
00320         return theCollection;
00321     }
00322 
00323     void
00324     operator()(
00325         const StringType&,
00326         const CollectionType&) const
00327     {
00328     }
00329 };
00330 #else
00331 template<class CollectionType,
00332      class StringType = XalanDOMString,
00333      class FilterPredicateType = FilesOnlyFilterPredicate,
00334      class StringConversionFunction = c_wstr_functor>
00335 #if defined(XALAN_NO_STD_NAMESPACE)
00336 struct DirectoryEnumeratorFunctor : public unary_function<StringType, CollectionType>
00337 #else
00338 struct DirectoryEnumeratorFunctor : public std::unary_function<StringType, CollectionType>
00339 #endif
00340 {
00341 #if defined(XALAN_NO_STD_NAMESPACE)
00342     typedef unary_function<StringType, CollectionType>  BaseClassType;
00343 #else
00344     typedef std::unary_function<StringType, CollectionType> BaseClassType;
00345 #endif
00346 
00347     typedef typename BaseClassType::result_type     result_type;
00348     typedef typename BaseClassType::argument_type   argument_type;
00349 
00350     explicit
00351     DirectoryEnumeratorFunctor(bool     fIncludeSelfAndParent = false) :
00352         m_includeSelfAndParent(fIncludeSelfAndParent)
00353     {
00354     }
00355             
00356     void
00357     operator()(
00358             const argument_type&    theFullSearchSpec,
00359             CollectionType&         theCollection) const
00360     {
00361         XALAN_USING_STD(back_inserter)
00362 
00363         EnumerateDirectory(
00364                 theFullSearchSpec,
00365                 XALAN_STD_QUALIFIER back_inserter(theCollection),
00366                 m_filterPredicate,
00367                 m_conversionFunction,
00368                 m_includeSelfAndParent);
00369     }
00370 
00371     result_type
00372     operator()(const argument_type&     theFullSearchSpec) const
00373     {
00374         result_type     theCollection;
00375 
00376         operator()(
00377                 theFullSearchSpec,
00378                 theCollection);
00379 
00380         return theCollection;
00381     }
00382 
00383     void
00384     operator()(
00385             const argument_type&    theDirectory,
00386             const argument_type&    theSearchSpec,
00387             CollectionType&         theCollection) const
00388     {
00389         EnumerateDirectory(
00390                 theDirectory,
00391                 theSearchSpec,
00392                 XALAN_STD_QUALIFIER back_inserter(theCollection),
00393                 m_filterPredicate,
00394                 m_conversionFunction,
00395                 m_includeSelfAndParent);
00396     }
00397 
00398     result_type
00399     operator()(
00400             const argument_type&    theDirectory,
00401             const argument_type&    theSearchSpec) const
00402     {
00403         result_type     theCollection;
00404 
00405         operator()(
00406                 theDirectory,
00407                 theSearchSpec,
00408                 theCollection);
00409 
00410         return theCollection;
00411     }
00412 
00413 private:
00414 
00415     FilterPredicateType         m_filterPredicate;
00416 
00417     StringConversionFunction    m_conversionFunction;
00418 
00419     const bool                  m_includeSelfAndParent;
00420 };
00421 #endif
00422 
00423 
00424 
00425 XALAN_CPP_NAMESPACE_END
00426 
00427 
00428 
00429 #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.6
Copyright © 2000, 2001, 2002, 2003 The Apache Software Foundation. All Rights Reserved.