Xalan-C++ API Documentation

The Xalan C++ XSLT Processor Version 1.10

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

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 <unistd.h>
00030 #include <dirent.h>
00031 #include <sys/stat.h>
00032 #include <errno.h>
00033 #endif
00034 
00035 
00036 
00037 #include <functional>
00038 #include <iterator>
00039 
00040 
00041 #include "xercesc/framework/MemoryManager.hpp"
00042 
00043 
00044 
00045 #include "xalanc/PlatformSupport/XalanFileOutputStream.hpp"
00046 #include "xalanc/PlatformSupport/DOMStringHelper.hpp"
00047 #include "xalanc/PlatformSupport/XalanUnicode.hpp"
00048 
00049 
00050 
00051 XALAN_CPP_NAMESPACE_BEGIN
00052 
00053 
00054 
00055 XALAN_USING_XERCES(MemoryManager)
00056 
00057 
00058 
00059 #if defined(_MSC_VER)
00060 
00061 struct FindFileStruct : public _wfinddata_t
00062 {
00063 
00064     enum eAttributes
00065     {
00066         eAttributeArchive = _A_ARCH,
00067         eAttributeDirectory = _A_SUBDIR,
00068         eAttributeHidden = _A_HIDDEN,
00069         eAttributeNormal = _A_NORMAL,
00070         eReadOnly = _A_RDONLY,
00071         eSystem = _A_SYSTEM
00072     };
00073 
00074 public:
00075 
00081     const XalanDOMChar*
00082     getName() const
00083     {
00084         return name;
00085     }
00086 
00092     bool
00093     isDirectory() const
00094     {
00095         return attrib & eAttributeDirectory ? true : false;
00096     }
00097 
00098     bool
00099     isSelfOrParent() const
00100     {
00101         if (isDirectory() == false)
00102         {
00103             return false;
00104         }
00105         else if (name[0] == '.')
00106         {
00107             if (name[1] == '\0')
00108             {
00109                 return true;
00110             }
00111             else if (name[1] == '.' &&
00112                      name[2] == '\0')
00113             {
00114                 return true;
00115             }
00116             else
00117             {
00118                 return false;
00119             }
00120         }
00121         else
00122         {
00123             return false;
00124         }
00125     }
00126 };
00127 
00128 #else
00129 
00130 struct FindFileStruct : public dirent
00131 {
00132 public:
00133 
00139     const char* getName() const
00140     {
00141         return d_name;
00142     }
00143 
00149     bool isDirectory() const
00150     {
00151 #if defined(__SunOS_5_10) && (__SUNPRO_CC >= 0x570)
00152             struct stat64 stat_Info;
00153 
00154             const int   retCode = stat64(d_name, &stat_Info);
00155 #else
00156             struct  stat stat_Info;
00157         
00158             const int   retCode = stat(d_name, &stat_Info);
00159 #endif
00160 
00161         return retCode == -1 ? false : S_ISDIR(stat_Info.st_mode);
00162     }
00163 
00164     bool
00165     isSelfOrParent() const
00166     {       
00167         if (isDirectory() == false)
00168         {
00169             return false;
00170         }
00171         else if (d_name[0] == '.')
00172         {
00173             if (d_name[1] == '\0')
00174             {
00175                 return true;
00176             }
00177             else if (d_name[1] == '.' &&
00178                      d_name[2] == '\0')
00179             {
00180                 return true;
00181             }
00182             else
00183             {
00184                 return false;
00185             }
00186         }
00187         else
00188         {
00189             return false;
00190         }
00191     }
00192 };
00193 
00194 #endif
00195 
00196 
00197 
00198 #if defined(XALAN_NO_STD_NAMESPACE)
00199 struct DirectoryFilterPredicate : public unary_function<FindFileStruct, bool>
00200 #else
00201 struct DirectoryFilterPredicate : public std::unary_function<FindFileStruct, bool>
00202 #endif
00203 {
00204     result_type
00205     operator()(const argument_type& theFindData) const
00206     {
00207         return theFindData.isDirectory();
00208     }
00209 };
00210 
00211 
00212 
00213 #if defined(XALAN_NO_STD_NAMESPACE)
00214 struct FilesOnlyFilterPredicate : public unary_function<FindFileStruct, bool>
00215 #else
00216 struct FilesOnlyFilterPredicate : public std::unary_function<FindFileStruct, bool>
00217 #endif
00218 {
00219     result_type
00220     operator()(const argument_type& theFindData) const
00221     {
00222         DirectoryFilterPredicate        theDirectoryPredicate;
00223 
00224         return !theDirectoryPredicate(theFindData);
00225                
00226     }
00227 };
00228 
00229 
00230 
00231 template<class OutputIteratorType,
00232          class FilterPredicateType,
00233          class StringType,
00234          class StringConversionFunction>
00235 void
00236 EnumerateDirectory(
00237             MemoryManager&              theMemoryManager,
00238             const StringType&           theFullSearchSpec,
00239             OutputIteratorType          theOutputIterator,
00240             FilterPredicateType         theFilterPredicate,
00241             StringConversionFunction    theConversionFunction,
00242 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00243             bool                        fIncludeSelfAndParent)
00244 #else
00245             bool                        fIncludeSelfAndParent = false)
00246 #endif
00247 {
00248 #if defined(_MSC_VER)
00249     FindFileStruct      theFindData;
00250     
00251     #ifdef _WIN64
00252         typedef intptr_t    theHandleType;
00253     #else
00254         typedef long    theHandleType;
00255     #endif
00256 
00257 #pragma warning(push)
00258 #pragma warning(disable: 4244)
00259     theHandleType   theSearchHandle =
00260         _wfindfirst(
00261             const_cast<wchar_t*>(theConversionFunction(theFullSearchSpec)),
00262             &theFindData);
00263 #pragma warning(pop)
00264 
00265     if (theSearchHandle != -1)
00266     {
00267   
00268         try
00269         {
00270             do
00271             {
00272                 if ((fIncludeSelfAndParent == true || theFindData.isSelfOrParent() == false) &&
00273                     theFilterPredicate(theFindData) == true)
00274                 {
00275                     *theOutputIterator = StringType(theFindData.getName(), theMemoryManager);
00276                 }
00277             }
00278             while(_wfindnext(theSearchHandle,
00279                              &theFindData) == 0);
00280         }
00281         catch(...)
00282         {
00283             _findclose(theSearchHandle);
00284 
00285             throw;
00286         }
00287 
00288         _findclose(theSearchHandle);
00289     }
00290 
00291 
00292 #else   
00293 
00294     CharVectorType  theTargetVector(theMemoryManager);
00295 
00296     TranscodeToLocalCodePage(theFullSearchSpec, theTargetVector, false);
00297 
00298     const CharVectorType::size_type     theSize = theTargetVector.size();
00299     int  indexSuffix=0, indexName=0;
00300     bool target_Dir = false; 
00301 
00302     if (theSize > 0)
00303     {
00304         if (theTargetVector.back() == '*')
00305         {
00306             target_Dir = true;
00307             theTargetVector.pop_back();
00308 
00309             if (theSize == 1)
00310             {
00311                 theTargetVector.push_back('.');
00312             }
00313         
00314         }
00315         else
00316         {
00317             target_Dir = false;
00318 
00319             while(theTargetVector.back() != '*')
00320             {
00321                 theTargetVector.pop_back();
00322                 indexSuffix++;
00323             }
00324 
00325             theTargetVector.pop_back();
00326             while(theTargetVector.back() != '/')
00327             { 
00328                 theTargetVector.pop_back();
00329                 indexName++;
00330             }
00331         }      
00332 
00333         theTargetVector.push_back('\0');
00334 
00335         const char* const   theSpec = c_str(theTargetVector);
00336         assert(theSpec != 0);
00337         
00338         XalanDOMString      theName(theMemoryManager);
00339         XalanDOMString      theSuffix(theMemoryManager);
00340         if ( !target_Dir )
00341         {
00342 #if defined(XALAN_STRICT_ANSI_HEADERS)
00343             using std::strlen;
00344 #endif
00345 
00346             int lenSpec = strlen(theSpec); 
00347             theFullSearchSpec.substr(theName, lenSpec, indexName); 
00348             theFullSearchSpec.substr(theSuffix, lenSpec+indexName+1, indexSuffix);
00349         }
00350 
00351         DIR* const  theDirectory = opendir(theSpec);
00352 
00353         if (theDirectory != 0)
00354         {
00355             chdir(theSpec);
00356             try
00357             {
00358                 const FindFileStruct*   theEntry =
00359                     (FindFileStruct*)readdir(theDirectory);
00360     
00361                 while(theEntry != 0)
00362                 {
00363                     if ((fIncludeSelfAndParent == true || theEntry->isSelfOrParent() == false))
00364                     {
00365                         if (theFilterPredicate(*theEntry) == true)
00366                         {
00367                             if( target_Dir )
00368                             {
00369                                 *theOutputIterator = StringType(theEntry->getName(), theMemoryManager);
00370                             }
00371                             else
00372                             {
00373                                 XalanDOMString  Getname(theEntry->getName(), theMemoryManager);
00374                                 int Check_1 = Getname.compare(theName);
00375                                 XalanDOMString  GetnameSuffix(theMemoryManager);
00376                                 Getname.substr(GetnameSuffix, Getname.size() -indexSuffix, indexSuffix);            
00377                                 int Check_2 = GetnameSuffix.compare(theSuffix);
00378                                 if ( Check_1 == 1 && (!Check_2) )
00379                                 {
00380                                 *theOutputIterator = StringType(theEntry->getName(), theMemoryManager);
00381                                 }
00382                             }
00383                         }
00384                     }
00385                     theEntry = (FindFileStruct*)readdir(theDirectory);
00386                 } //while
00387             }//try
00388 
00389             catch(...)
00390             {
00391                 closedir(theDirectory);
00392 
00393                 throw;
00394             }
00395             if( target_Dir )
00396                            chdir("..");
00397                          else
00398                            chdir("../..");
00399             closedir(theDirectory);
00400         }
00401     }
00402 
00403 #endif
00404 }
00405 
00406 
00407 
00408 template<class OutputIteratorType,
00409          class FilterPredicateType,
00410          class StringType,
00411          class StringConversionFunction>
00412 void
00413 EnumerateDirectory(
00414             MemoryManager&              theMemoryManager,
00415             const StringType&           theDirectory,
00416             const StringType&           theSearchSpec,
00417             OutputIteratorType          theOutputIterator,
00418             FilterPredicateType         theFilterPredicate,
00419             StringConversionFunction    theConversionFunction,
00420 #if defined(XALAN_TEMPLATE_FUNCTION_NO_DEFAULT_PARAMETERS)
00421             bool                        fIncludeSelfAndParent)
00422 #else
00423             bool                        fIncludeSelfAndParent = false)
00424 #endif
00425 {
00426     StringType  theFullSearchSpec(theDirectory, theMemoryManager);
00427 
00428     theFullSearchSpec += theSearchSpec;
00429 
00430     EnumerateDirectory(
00431         theMemoryManager,
00432         theFullSearchSpec,
00433         theOutputIterator,
00434         theFilterPredicate,
00435         theConversionFunction,
00436         fIncludeSelfAndParent);
00437 }
00438 
00439 
00440 
00441 #if defined(XALAN_NO_DEFAULT_TEMPLATE_ARGUMENTS)
00442 template<class CollectionType, class StringType>
00443 struct DirectoryEnumeratorFunctor
00444 {
00445     CollectionType
00446     operator()(const StringType&    theDirectory) const
00447     {
00448         CollectionType      theCollection;
00449 
00450         operator()(theDirectory,
00451                theCollection);
00452 
00453         return theCollection;
00454     }
00455 
00456     void
00457     operator()(
00458         const StringType&,
00459         const CollectionType&) const
00460     {
00461     }
00462 };
00463 #else
00464 template<class CollectionType,
00465      class StringType = XalanDOMString,
00466      class FilterPredicateType = FilesOnlyFilterPredicate,
00467      class StringConversionFunction = c_wstr_functor>
00468 #if defined(XALAN_NO_STD_NAMESPACE)
00469 struct DirectoryEnumeratorFunctor : public unary_function<StringType, CollectionType>
00470 #else
00471 struct DirectoryEnumeratorFunctor : public std::unary_function<StringType, CollectionType>
00472 #endif
00473 {
00474 #if defined(XALAN_NO_STD_NAMESPACE)
00475     typedef unary_function<StringType, CollectionType>  BaseClassType;
00476 #else
00477     typedef std::unary_function<StringType, CollectionType> BaseClassType;
00478 #endif
00479 
00480     typedef typename BaseClassType::result_type     result_type;
00481     typedef typename BaseClassType::argument_type   argument_type;
00482 
00483     explicit
00484     DirectoryEnumeratorFunctor(
00485                 MemoryManager&  theMemoryManager,
00486                 bool            fIncludeSelfAndParent = false) :
00487         m_includeSelfAndParent(fIncludeSelfAndParent),
00488         m_memoryManager(theMemoryManager)
00489     {
00490     }
00491             
00492     void
00493     operator()(
00494             const argument_type&    theFullSearchSpec,
00495             CollectionType&         theCollection) const
00496     {
00497         XALAN_USING_STD(back_inserter)
00498 
00499         EnumerateDirectory(
00500             m_memoryManager,
00501             theFullSearchSpec,
00502             XALAN_STD_QUALIFIER back_inserter(theCollection),
00503             m_filterPredicate,
00504             m_conversionFunction,
00505             m_includeSelfAndParent);
00506     }
00507 
00508     result_type
00509     operator()(const argument_type&     theFullSearchSpec) const
00510     {
00511         result_type     theCollection;
00512 
00513         operator()(
00514                 theFullSearchSpec,
00515                 theCollection);
00516 
00517         return theCollection;
00518     }
00519 
00520     void
00521     operator()(
00522             const argument_type&    theDirectory,
00523             const argument_type&    theSearchSpec,
00524             CollectionType&         theCollection) const
00525     {
00526         EnumerateDirectory(
00527             m_memoryManager,
00528             theDirectory,
00529             theSearchSpec,
00530             XALAN_STD_QUALIFIER back_inserter(theCollection),
00531             m_filterPredicate,
00532             m_conversionFunction,
00533             m_includeSelfAndParent);
00534     }
00535 
00536     result_type
00537     operator()(
00538             const argument_type&    theDirectory,
00539             const argument_type&    theSearchSpec) const
00540     {
00541         result_type     theCollection;
00542 
00543         operator()(
00544                 theDirectory,
00545                 theSearchSpec,
00546                 theCollection);
00547 
00548         return theCollection;
00549     }
00550 
00551 private:
00552 
00553     FilterPredicateType         m_filterPredicate;
00554 
00555     StringConversionFunction    m_conversionFunction;
00556 
00557     const bool                  m_includeSelfAndParent;
00558 
00559     MemoryManager&              m_memoryManager;
00560 };
00561 #endif
00562 
00563 
00564 
00565 XALAN_CPP_NAMESPACE_END
00566 
00567 
00568 
00569 #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.

dot

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

Apache Logo