![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|