Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 10th, 2005, 2:55 PM   #1
c0ldshadow
Unverified User
 
c0ldshadow's Avatar
 
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0 c0ldshadow is on a distinguished road
callback example for file/directory enumeration

hey people,great forum,glad most of my dev shed friends are here now too... ive been clicking your adsense ads lol; hopefully that helps u some support this kick ass site.

this is a mini script i wrote that i mainly wrote to try to consolidate my understand of callbacks. basically what this program does is enumerate all files and folders on the c:\ drive, and after the function has returned, a vector of strings passed by reference to a function is used to display this data. function pointers can be passed to the function so one can do a particular operation on each file or folder AS IT IS FOUND rather than having to wait for the entire function to return to do stuff.... this might be a terribly written program or pointless use of callbacks... i really don't know...

there is a MONSTER thunderstorm rolling in like a lion so im just going to post this code before we lose power lol...

questions/comments/suggestions about code sample welcomed... no urgency for respond lol,just looking for some advice and starting out using this forum,peace, --ave

/*
    Fileproc demonstrates how to enumerate all files and folders on a drive.
    Copyright (C) 2005  Avery Tarasov

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
BOOL __stdcall dirMod(const char *,unsigned &);
BOOL __stdcall fileMod(const char *,unsigned &);
class fileproc
{
public:
	fileproc();
	typedef BOOL (__stdcall *diskMarshall)(const char *,unsigned &);
	bool fprocEnumFiles(const std::string &,std::vector<std::string> &,diskMarshall,diskMarshall);
	~fileproc();
private:
	unsigned idxDir;
	unsigned idxFile;
};
fileproc::fileproc()
{
	idxDir=0;
	idxFile=0;
}
bool fileproc::fprocEnumFiles(const std::string &root,std::vector <std::string> &invec,diskMarshall dMod,diskMarshall fMod)
{
	HANDLE search=0;
	WIN32_FIND_DATA wfd;
	std::string wc=root+"*";
	search=FindFirstFile(wc.c_str(),&wfd);
	if(search==INVALID_HANDLE_VALUE)
	{
		return false;
	}
	BOOL rcode=TRUE;
	for(;rcode;rcode=FindNextFile(search,&wfd))
	{
		if(wfd.cFileName[0]=='.')
		{
			continue;
		}
		if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
		{
			std::string subdir=root+wfd.cFileName+"\\";
			++idxDir;
			dMod(subdir.c_str(),idxDir);
			fprocEnumFiles(subdir.c_str(),invec,dMod,fMod);
		}
		else
		{
			std::string current=root+wfd.cFileName;
			invec.push_back(current.c_str());
			++idxFile;
			fMod(current.c_str(),idxFile);
		}
	}
	if(!FindClose(search))
	{
		return false;
	}
	return true;
}
fileproc::~fileproc()
{

}
int main()
{
    fileproc fp;
	std::vector <std::string> asd;
	fp.fprocEnumFiles("C:\\",asd,&dirMod,&fileMod);
	std::cout<<"now here is our vec's data.."<<std::endl;
	system("pause");
	for(unsigned i=0;i<asd.size();++i)
	{
		std::cout<<asd[i]<<std::endl;
	}
	std::cout<<asd.size()<<" files found"<<std::endl;
	system("pause");
	return 0;
}
BOOL __stdcall dirMod(const char *cp,unsigned &i)
{
	std::cout<<cp<<" is # "<<i<<" dir found"<<std::endl;
	return 0;
}
BOOL __stdcall fileMod(const char *cp,unsigned &i)
{
	std::cout<<cp<<" is # "<<i<<" file found"<<std::endl;
	return 0;
}
__________________
DeepTide

The way is shut.
It was made by those who are dead
and the Dead keep it.
The way is shut.
c0ldshadow is offline   Reply With Quote
Old Jun 10th, 2005, 6:41 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Looks great, mate. Good to have ya here.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC