added entries and readFilename

This commit is contained in:
fluxgen 2003-02-15 01:42:17 +00:00
parent 7061805dfd
commit f6117a7514
2 changed files with 41 additions and 18 deletions

View file

@ -19,11 +19,12 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: DirHelper.cc,v 1.1 2002/12/02 19:44:25 fluxgen Exp $
// $Id: DirHelper.cc,v 1.2 2003/02/15 01:42:17 fluxgen Exp $
#include "DirHelper.hh"
DirHelper::DirHelper(const char *dir):m_dir(0) {
DirHelper::DirHelper(const char *dir):m_dir(0),
m_num_entries(0) {
if (dir != 0)
open(dir);
}
@ -45,23 +46,39 @@ struct dirent *DirHelper::read() {
return readdir(m_dir);
}
std::string DirHelper::readFilename() {
dirent *ent = read();
if (ent == 0)
return "";
return (ent->d_name ? ent->d_name : "");
}
void DirHelper::close() {
if (m_dir != 0) {
closedir(m_dir);
m_dir = 0;
m_num_entries = 0;
}
}
bool DirHelper::open(const char *dir) {
if (m_dir != 0)
close();
if (dir == 0)
return false;
m_dir = opendir(dir);
if (m_dir != 0) // successfull loading?
return true;
if (m_dir != 0)
close();
m_dir = opendir(dir);
if (m_dir == 0) // successfull loading?
return false;
// get number of entries
while (read())
m_num_entries++;
rewind(); // go back to start
return true;
}

View file

@ -1,5 +1,5 @@
// DirHelper.hh
// Copyright (c) 2002 Henrik Kinnunen (fluxgen at users.sourceforge.net)
// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
@ -19,29 +19,35 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// $Id: DirHelper.hh,v 1.1 2002/12/02 19:44:24 fluxgen Exp $
// $Id: DirHelper.hh,v 1.2 2003/02/15 01:41:50 fluxgen Exp $
#ifndef DIRHELPER_HH
#define DIRHELPER_HH
#include <sys/types.h>
#include <dirent.h>
#include "NotCopyable.hh"
/**
Wrapper class for DIR * routines
*/
#include <sys/types.h>
#include <dirent.h>
#include <string>
/// Wrapper class for DIR * routines
class DirHelper: private FbTk::NotCopyable {
public:
explicit DirHelper(const char *dir = 0);
~DirHelper();
void rewind();
/// gets next dirent info struct in directory
struct dirent * read();
/// reads next filename in directory
std::string readFilename();
void close();
bool open(const char *dir);
/// @return number of entries in the directory
size_t entries() const { return m_num_entries; }
private:
DIR *m_dir;
size_t m_num_entries; ///< number of file entries in directory
};
#endif // DIRHELPER_HH