Click here to Skip to main content
12,350,610 members (24,045 online)
Click here to Skip to main content

Stats

103.9K views
618 downloads
42 bookmarked
Posted

Win32 file name iteration STL way

, 21 Nov 2004
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

Share

About the Author

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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 21 Nov 2004
Article Copyright 2004 by bektek
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid