Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC

Win32 file name iteration STL way

Rate me:
Please Sign up or sign in to vote.
4.96/5 (20 votes)
21 Nov 20041 min read 131.2K   668   43  
This simple class shows how to iterate file names by using STL iterator interface.
#include <windows.h>
#include <iterator>
#include <string>

class win32_file_iterator : 
	public std::iterator<std::input_iterator_tag, std::string>
{
private:
	class internal_handle_data{
	public:
		internal_handle_data():_h(NULL), _ref(0){}
		void setHandle(HANDLE handle){ _h = handle; }
		HANDLE getHandle(){ return _h; }
		void incRef(){ _ref++; }
		unsigned decRef(){ return --_ref; }
		operator HANDLE(){ return _h; }
	private:
		HANDLE _h;
		unsigned _ref;
	};

public:
	win32_file_iterator(std::string strfilter, bool bFullPath = false, int flag = FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_NORMAL|FILE_ATTRIBUTE_DIRECTORY):
	  _bEnd(false), _bFullPath(bFullPath), _flag(flag){
        HANDLE h = ::FindFirstFile(strfilter.c_str(), &_wfd);
		_handle.setHandle(h);

		if(h == INVALID_HANDLE_VALUE){
			_bEnd = true;
		}else{
			_handle.incRef();
			std::string::size_type n1 = strfilter.find_last_of("\\");
			_strroot = strfilter.substr(0,n1+1);
			_chkvalid(_wfd);
		}
	}

	win32_file_iterator():_bEnd(true){}

	win32_file_iterator(win32_file_iterator& rhs){
		
		_handle = rhs._handle;
		_handle.incRef();

		_flag = rhs._flag;
		_bFullPath = rhs._bFullPath;
		_bEnd = rhs._bEnd;
		_wfd = rhs._wfd;
		_strfname = rhs._strfname;
		_strroot = rhs._strroot;		
	}

	~win32_file_iterator(){
		if(_handle.decRef() == 0 && _handle.getHandle() != NULL ){
			FindClose(_handle);
		}
		
	}

	reference operator*(){
		return _strfname;
	}

	bool operator==(const win32_file_iterator& rhs) const{
		return (_bEnd == rhs._bEnd);
	}
	bool operator!=(const win32_file_iterator& rhs) const{
		return (_bEnd != rhs._bEnd);
	}

	//Prefix
	win32_file_iterator& operator++(){
		_findnext();
		return *this;
	}
	//Posfix
	win32_file_iterator& operator++(int){
		_findnext();
		return *this;
	}

private:
	void _findnext(){
		BOOL b = ::FindNextFile(_handle, &_wfd);
		if(b){
			_chkvalid(_wfd);
		}else{
			_bEnd = true;
		}
	}

	void _chkvalid(WIN32_FIND_DATA& _wfd){
		if(_wfd.dwFileAttributes & _flag){
			_getval(_wfd);
		}
		else{
			_findnext();
		}
	}
	void _getval(WIN32_FIND_DATA& wfd){
		if(_bFullPath)
			_strfname = _strroot+ wfd.cFileName;
		else
			_strfname = wfd.cFileName;
	}

private:
	int _flag;
	bool _bFullPath;
	bool _bEnd;
	internal_handle_data _handle;
	//HANDLE _handle;
	WIN32_FIND_DATA _wfd;
	std::string _strroot;
	std::string _strfname;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
study, study, That's all I can say Smile | :)

Comments and Discussions