Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.4

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

DirectoryEnumerator.hpp

Go to the documentation of this file.
00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  *
00005  * Copyright (c) 1999 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 <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 <PlatformSupport/DOMStringHelper.hpp>
00081 #include <PlatformSupport/XalanUnicode.hpp>
00082 
00083 
00084 
00085 #if defined(_MSC_VER)
00086 
00087 class FindFileStruct : public _wfinddata_t
00088 {
00089 
00090     enum eAttributes
00091     {
00092         eAttributeArchive = _A_ARCH,
00093         eAttributeDirectory = _A_SUBDIR,
00094         eAttributeHidden = _A_HIDDEN,
00095         eAttributeNormal = _A_NORMAL,
00096         eReadOnly = _A_RDONLY,
00097         eSystem = _A_SYSTEM
00098     };
00099 
00100 public:
00101 
00107     const XalanDOMChar*
00108     getName() const
00109     {
00110         return name;
00111     }
00112 
00118     bool
00119     isDirectory() const
00120     {
00121         return attrib & eAttributeDirectory ? true : false;
00122     }
00123 
00124     bool
00125     isSelfOrParent() const
00126     {
00127         if (isDirectory() == false)
00128         {
00129             return false;
00130         }
00131         else if (name[0] == XalanUnicode::charFullStop)
00132         {
00133             if (name[1] == 0)
00134             {
00135                 return true;
00136             }
00137             else if (name[1] == XalanUnicode::charFullStop &&
00138                      name[2] == 0)
00139             {
00140                 return true;
00141             }
00142             else
00143             {
00144                 return false;
00145             }
00146         }
00147         else
00148         {
00149             return false;
00150         }
00151     }
00152 };
00153 
00154 #else
00155 
00156 class FindFileStruct : public dirent
00157 {
00158 public:
00159 
00165     const char* getName() const
00166     {
00167         return d_name;
00168     }
00169 
00175     bool isDirectory() const
00176     {
00177 #if defined(AIX) || defined(HPUX) || defined(SOLARIS) || defined(OS390) || defined(TRU64)
00178         return false;
00179 #else       
00180         return d_type == DT_DIR;        
00181 #endif      
00182     }
00183 };
00184 
00185 #endif
00186 
00187 
00188 
00189 #if defined(XALAN_NO_NAMESPACES)
00190 struct DirectoryFilterPredicate : public unary_function<FindFileStruct, bool>
00191 #else
00192 struct DirectoryFilterPredicate : public std::unary_function<FindFileStruct, bool>
00193 #endif
00194 {
00195     result_type
00196     operator()(const argument_type& theFindData) const
00197     {
00198         return theFindData.isDirectory();
00199     }
00200 };
00201 
00202 
00203 
00204 #if defined(XALAN_NO_NAMESPACES)
00205 struct FilesOnlyFilterPredicate : public unary_function<FindFileStruct, bool>
00206 #else
00207 struct FilesOnlyFilterPredicate : public std::unary_function<FindFileStruct, bool>
00208 #endif
00209 {
00210     result_type
00211     operator()(const argument_type& theFindData) const
00212     {
00213         DirectoryFilterPredicate        theDirectoryPredicate;
00214 
00215         return !theDirectoryPredicate(theFindData);
00216                
00217     }
00218 };
00219 
00220 
00221 
00222 template<class OutputIteratorType,
00223          class FilterPredicateType,
00224          class StringType,
00225          class StringConversionFunction>
00226 void
00227 EnumerateDirectory(
00228             const StringType&           theFullSearchSpec,
00229             OutputIteratorType          theOutputIterator,
00230             FilterPredicateType         theFilterPredicate,
00231             StringConversionFunction    theConversionFunction,
00232 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00233             bool                        fIncludeSelfAndParent)
00234 #else
00235             bool                        fIncludeSelfAndParent = false)
00236 #endif
00237 {
00238 #if defined(_MSC_VER)
00239     FindFileStruct      theFindData;
00240 
00241     long    theSearchHandle = _wfindfirst(const_cast<wchar_t*>(theConversionFunction(theFullSearchSpec)),
00242                                           &theFindData);
00243 
00244     if (theSearchHandle != -1)
00245     {
00246         try
00247         {
00248             do
00249             {
00250                 if ((fIncludeSelfAndParent == true || theFindData.isSelfOrParent() == false) &&
00251                     theFilterPredicate(theFindData) == true)
00252                 {
00253                     *theOutputIterator = StringType(theFindData.getName());
00254                 }
00255             }
00256             while(_wfindnext(theSearchHandle,
00257                              &theFindData) == 0);
00258         }
00259         catch(...)
00260         {
00261             _findclose(theSearchHandle);
00262 
00263             throw;
00264         }
00265 
00266         _findclose(theSearchHandle);
00267     }
00268 
00269     
00270 #else
00271     // Do nothing for now...
00272     // Unsupported platform!!!
00273 #endif
00274 }
00275 
00276 
00277 
00278 template<class OutputIteratorType,
00279          class FilterPredicateType,
00280          class StringType,
00281          class StringConversionFunction>
00282 void
00283 EnumerateDirectory(
00284             const StringType&           theDirectory,
00285             const StringType&           theSearchSpec,
00286             OutputIteratorType          theOutputIterator,
00287             FilterPredicateType         theFilterPredicate,
00288             StringConversionFunction    theConversionFunction,
00289 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00290             bool                        fIncludeSelfAndParent)
00291 #else
00292             bool                        fIncludeSelfAndParent = false)
00293 #endif
00294 {
00295     StringType  theFullSearchSpec(theDirectory);
00296 
00297     theFullSearchSpec += theSearchSpec;
00298 
00299     EnumerateDirectory(theFullSearchSpec, theOutputIterator, theFilterPredicate, theConversionFunction, fIncludeSelfAndParent);
00300 }
00301 
00302 
00303 
00304 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00305 template<class CollectionType, class StringType>
00306 struct DirectoryEnumeratorFunctor
00307 {
00308     CollectionType
00309     operator()(const StringType&    theDirectory) const
00310     {
00311         CollectionType      theCollection;
00312 
00313         operator()(theDirectory,
00314                theCollection);
00315 
00316         return theCollection;
00317     }
00318 
00319     void
00320     operator()(
00321         const StringType&,
00322         const CollectionType&) const
00323     {
00324     }
00325 };
00326 #else
00327 template<class CollectionType,
00328      class StringType = XalanDOMString,
00329      class FilterPredicateType = FilesOnlyFilterPredicate,
00330      class StringConversionFunction = c_wstr_functor>
00331 #if defined(XALAN_NO_NAMESPACES)
00332 struct DirectoryEnumeratorFunctor : public unary_function<StringType, CollectionType>
00333 #else
00334 struct DirectoryEnumeratorFunctor : public std::unary_function<StringType, CollectionType>
00335 #endif
00336 {
00337 #if defined(XALAN_NO_NAMESPACES)
00338     typedef unary_function<StringType, CollectionType>  BaseClassType;
00339 #else
00340     typedef std::unary_function<StringType, CollectionType> BaseClassType;
00341 #endif
00342 
00343     typedef typename BaseClassType::result_type     result_type;
00344     typedef typename BaseClassType::argument_type   argument_type;
00345 
00346     explicit
00347     DirectoryEnumeratorFunctor(bool     fIncludeSelfAndParent = false) :
00348         m_includeSelfAndParent(fIncludeSelfAndParent)
00349     {
00350     }
00351             
00352     void
00353     operator()(
00354             const argument_type&    theFullSearchSpec,
00355             CollectionType&         theCollection) const
00356     {
00357 #if !defined(XALAN_NO_NAMESPACES)
00358         using std::back_inserter;
00359 #endif
00360 
00361         EnumerateDirectory(theFullSearchSpec,
00362                            back_inserter(theCollection),
00363                            m_filterPredicate,
00364                            m_conversionFunction,
00365                            m_includeSelfAndParent);
00366     }
00367 
00368     result_type
00369     operator()(const argument_type&     theFullSearchSpec) const
00370     {
00371         result_type     theCollection;
00372 
00373         operator()(
00374                 theFullSearchSpec,
00375                 theCollection);
00376 
00377         return theCollection;
00378     }
00379 
00380     void
00381     operator()(
00382             const argument_type&    theDirectory,
00383             const argument_type&    theSearchSpec,
00384             CollectionType&         theCollection) const
00385     {
00386 #if !defined(XALAN_NO_NAMESPACES)
00387         using std::back_inserter;
00388 #endif
00389 
00390         EnumerateDirectory(
00391                 theDirectory,
00392                 theSearchSpec,
00393                 back_inserter(theCollection),
00394                 m_filterPredicate,
00395                 m_conversionFunction,
00396                 m_includeSelfAndParent);
00397     }
00398 
00399     result_type
00400     operator()(
00401             const argument_type&    theDirectory,
00402             const argument_type&    theSearchSpec) const
00403     {
00404         result_type     theCollection;
00405 
00406         operator()(
00407                 theDirectory,
00408                 theSearchSpec,
00409                 theCollection);
00410 
00411         return theCollection;
00412     }
00413 
00414 private:
00415 
00416     FilterPredicateType         m_filterPredicate;
00417 
00418     StringConversionFunction    m_conversionFunction;
00419 
00420     const bool                  m_includeSelfAndParent;
00421 };
00422 #endif
00423 
00424 
00425 #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.4
Copyright © 2000, 2001, 2002 The Apache Software Foundation. All Rights Reserved.