#ifndef INCLUDED_L3_FILES_H #define INCLUDED_L3_FILES_H // *Filenames Wildcarding* Copyright (C) Krzysztof Bosak 1999-04-14 // All rights reserved. // kbosak@box43.pl // http://www.kbosak.prv.pl #include #include #include "l3_array.h" #include "l3_stack.h" #include "l3_str.h" class InvalidPathEx {}; // Path is meanigless or too long (system limitation) after expanding to full path. #ifdef _MSC_VER // Visual C++ //////////////////////////////////////////////////////////////// #include class SmartFindNext { intptr_t _handle; public: inline SmartFindNext(char * const path, _finddata_t * const finddata) { assert(path!=NULL); assert(finddata!=NULL); _handle=_findfirst(path, finddata); } inline bool FindNext(_finddata_t * const finddata) const { return _findnext(_handle, finddata)==0; } inline ~SmartFindNext() { _findclose(_handle); } inline bool IsGoodHandle() const { return _handle!=-1L; } }; class DirectoryScanner { TextString _path; basearray_s _buffer_full_path; TextString _full_path; growing_arraystack<_finddata_t> _find_data; public: explicit DirectoryScanner(const TextString& path) : _path(path), _buffer_full_path(_MAX_PATH) { assert(_buffer_full_path.size()==_MAX_PATH); _path=path; //cout<<"Path="<<_path.c_str()<=2 && path[last_path_index-1]=='.' && path[last_path_index-2]=='*') { fast_flag=true; } else if(last_path_index>=1 && path[last_path_index-1]!='.') { fast_flag=true; } } } if(do_recursion==true && dirs_only==false) {// directory\*.txt in case of recursion should mean *.* for dirs and *.txt for files _splitpath(path.c_str(), &_split_drive[0], &_split_dir[0], &_split_fname[0], &_split_ext[0]); //if(strcmp(&_split_fname[0], "*")==0 && strcmp(&_split_ext[0], ".*")==0) if(fast_flag==true) { //cout<<"FASTER...\n"; //assert(0); _do_recursion(path, true, false); } else { const TextString const1=&_split_fname[0]; const TextString const2=&_split_ext[0]; _makepath(&_split_path[0], &_split_drive[0], &_split_dir[0], "*", NULL); path=&_split_path[0]; //cout<<"NotFinished="<& finddata_stack=fe.FileNames(); while(!finddata_stack.empty()) { TextString subdirpath=finddata_stack.top(); finddata_stack.pop(); subdirpath+="\\"; subdirpath+=const1; subdirpath+=const2; //cout<& FileNames() { return _find_names; } inline const growing_arraystack& FileNames() const { return _find_names; } private: inline void _do_recursion(const TextString& path, bool do_recursion, bool dirs_only) { DirectoryScanner ds(path); growing_arraystack<_finddata_t> &gs=ds.FindData(); while(!gs.empty()) { const _finddata_t finddata=gs.top(); gs.pop(); if(strcmp(finddata.name, ".")!=0 && strcmp(finddata.name, "..")!=0) { if(dirs_only==false) { if(finddata.attrib!=_A_SUBDIR) { //_find_names.push(finddata.name);// push filename only _splitpath(path.c_str(), &_split_drive[0], &_split_dir[0], &_split_fname[0], &_split_ext[0]); _makepath(&_split_path[0], &_split_drive[0], &_split_dir[0], finddata.name, NULL); _find_names.push(&_split_path[0]);// push full path } } else { if(finddata.attrib==_A_SUBDIR) { _splitpath(path.c_str(), &_split_drive[0], &_split_dir[0], &_split_fname[0], &_split_ext[0]); _makepath(&_split_path[0], &_split_drive[0], &_split_dir[0], finddata.name, NULL); _find_names.push(&_split_path[0]);// push full path of directory } } if(do_recursion==true) { //cout< _find_names; public: inline FilenamesExtractor(const TextString& path, bool, bool) : _single_filename(path) { _find_names.push(_single_filename); } inline const TextString& Path() const { return _single_filename; } inline growing_arraystack& FileNames() { return _find_names; } inline const growing_arraystack& FileNames() const { return _find_names; } }; #endif // MSC_VER ////////////////////////////////////////////////////////////// /* int main(int argc, char *argv[]) { try { if(argc!=2) { cerr<<"Bad number of command line arguments.\n"; exit(1); } cout<<"argc: "<& finddata_stack=ds.FileNames(); while(!finddata_stack.empty()) { cout<