added entries and readFilename
fluxgen fluxgen
2 files changed,
39 insertions(+),
16 deletions(-)
M
src/DirHelper.cc
→
src/DirHelper.cc
@@ -19,11 +19,12 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// 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 @@
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 (dir == 0) + return false; + 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) // successfull loading? + return false; - return false; + // get number of entries + while (read()) + m_num_entries++; + + rewind(); // go back to start + + return true; }
M
src/DirHelper.hh
→
src/DirHelper.hh
@@ -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 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// 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 "NotCopyable.hh" + #include <sys/types.h> #include <dirent.h> +#include <string> -#include "NotCopyable.hh" - -/** - Wrapper class for DIR * routines -*/ +/// 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(); - void close(); + /// 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