[gameprogrammer] Re: Linux _findfirst equivalent
- From: "Enrico Zschemisch" <enrico.zschemisch@xxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Mon, 26 Jun 2006 17:44:11 +0200
Hi,
sorry for the delay, I had exams the last two weeks and now some holidays.
Attached you can find the request method. I had to guess the FileList class and
the hash stuff is missing. Source is tested on Debian testing with G++-4 and
should work on every Unix/Linux.
Feel free to request more features or ask questions :-)
Greetings, Enrico
--
Echte DSL-Flatrate dauerhaft für 0,- Euro*!
"Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#include <fts.h>
using std::string; using std::cout; using std::endl;
using std::vector;
class FileList
{
public:
FileList() {}
~FileList()
{
m_vFiles.clear();
}
void AddFilesFromDirectory(const char *applicationDirectory,
const char *subDirectory, bool writeHashInsteadOfData);
void Dump(void)
{
vector<FileEntry>::iterator filename = m_vFiles.begin();
cout << "File List: " << endl;
for(; filename != m_vFiles.end(); filename++)
{
cout << "\tFilename: " << (*filename).name << " Size: "
<< (*filename).size << "Byte" << endl;
}
}
void AddFile(const std::string &filename, const char *data, const
size_t DataSize, const size_t FileSize)
{
FileEntry entry;
entry.name = filename;
entry.size = FileSize;
m_vFiles.push_back(entry);
}
private:
typedef struct sFileEntry
{
string name;
size_t size;
} FileEntry;
vector<FileEntry> m_vFiles;
};
void FileList::AddFilesFromDirectory(const char *applicationDirectory, const
char *subDirectory, bool writeHashInsteadOfData)
{
// C++ strings make handling much easier
std::string RootDirectory = "./";
if(NULL != applicationDirectory)
{
RootDirectory = applicationDirectory;
}
// Make sure the path is finished with a slash
if('/' != RootDirectory[RootDirectory.length()-1])
{
RootDirectory.append("/");
}
if(NULL != subDirectory)
{
RootDirectory += subDirectory;
}
// Make sure the path is finished with a slash
// have to do this again, because "subDirectory" might be without slash
if('/' != RootDirectory[RootDirectory.length()-1])
{
RootDirectory.append("/");
}
char *DirectoryList[] = { (char*)RootDirectory.c_str(), NULL };
// Don't set FTS_NOSTAT or you won't get filesize so easily!
FTS *fts = fts_open(DirectoryList, FTS_LOGICAL, NULL);
if(NULL != fts)
{
FTSENT *entry = NULL;
while((entry = fts_read(fts)) != NULL)
{
if(entry->fts_info & FTS_F)
{
const string Filename = entry->fts_path;
// includes path+filename
const size_t Filesize =
entry->fts_statp->st_size; // size in bytes
// filter out hidden files
if(Filename.length() > 0 &&
entry->fts_namelen > 0 &&
'.' != entry->fts_name[0])
{
// This is not exactly what Kevin
needs, need to talk to him
if(true == writeHashInsteadOfData)
{
// calculate hash value here
}
else
{
this->AddFile(Filename, NULL,
Filesize, Filesize);
}
}
}
}
}
else
{
// mhm, some error handling should go here!
cout << "No FTS!" << endl;
}
fts_close(fts);
}
int main(int argc, char **argv)
{
FileList list;
if(1 == argc)
{
list.AddFilesFromDirectory("/home/enrico/", "Desktop/", false);
}
else
{
list.AddFilesFromDirectory(argv[1], "", false);
}
list.Dump();
return 0;
}
- Follow-Ups:
- [gameprogrammer] Re: Linux _findfirst equivalent
- From: Kevin Jenkins
Other related posts:
- » [gameprogrammer] Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- » [gameprogrammer] Re: Linux _findfirst equivalent
- [gameprogrammer] Re: Linux _findfirst equivalent
- From: Kevin Jenkins