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