summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2003-02-15 01:42:17 (GMT)
committerfluxgen <fluxgen>2003-02-15 01:42:17 (GMT)
commitf6117a751497283c810d8f732eaee657c1e9c34f (patch)
tree5ffd45a6138584153468da25756e7d178aed97da
parent7061805dfd4a2ba19fda52d67485c99be20ad4d0 (diff)
downloadfluxbox_lack-f6117a751497283c810d8f732eaee657c1e9c34f.zip
fluxbox_lack-f6117a751497283c810d8f732eaee657c1e9c34f.tar.bz2
added entries and readFilename
-rw-r--r--src/DirHelper.cc33
-rw-r--r--src/DirHelper.hh22
2 files changed, 39 insertions, 16 deletions
diff --git a/src/DirHelper.cc b/src/DirHelper.cc
index 4098e3e..19f7612 100644
--- a/src/DirHelper.cc
+++ b/src/DirHelper.cc
@@ -19,11 +19,12 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22// $Id: DirHelper.cc,v 1.1 2002/12/02 19:44:25 fluxgen Exp $ 22// $Id: DirHelper.cc,v 1.2 2003/02/15 01:42:17 fluxgen Exp $
23 23
24#include "DirHelper.hh" 24#include "DirHelper.hh"
25 25
26DirHelper::DirHelper(const char *dir):m_dir(0) { 26DirHelper::DirHelper(const char *dir):m_dir(0),
27m_num_entries(0) {
27 if (dir != 0) 28 if (dir != 0)
28 open(dir); 29 open(dir);
29} 30}
@@ -45,23 +46,39 @@ struct dirent *DirHelper::read() {
45 return readdir(m_dir); 46 return readdir(m_dir);
46} 47}
47 48
49std::string DirHelper::readFilename() {
50 dirent *ent = read();
51 if (ent == 0)
52 return "";
53 return (ent->d_name ? ent->d_name : "");
54}
55
48void DirHelper::close() { 56void DirHelper::close() {
49 if (m_dir != 0) { 57 if (m_dir != 0) {
50 closedir(m_dir); 58 closedir(m_dir);
51 m_dir = 0; 59 m_dir = 0;
60 m_num_entries = 0;
52 } 61 }
53} 62}
54 63
64
55bool DirHelper::open(const char *dir) { 65bool DirHelper::open(const char *dir) {
56 if (m_dir != 0)
57 close();
58 if (dir == 0) 66 if (dir == 0)
59 return false; 67 return false;
60 68
69 if (m_dir != 0)
70 close();
71
61 m_dir = opendir(dir); 72 m_dir = opendir(dir);
62 if (m_dir != 0) // successfull loading? 73 if (m_dir == 0) // successfull loading?
63 return true; 74 return false;
75
76 // get number of entries
77 while (read())
78 m_num_entries++;
79
80 rewind(); // go back to start
64 81
65 return false; 82 return true;
66} 83}
67 84
diff --git a/src/DirHelper.hh b/src/DirHelper.hh
index df6b301..c41066b 100644
--- a/src/DirHelper.hh
+++ b/src/DirHelper.hh
@@ -1,5 +1,5 @@
1// DirHelper.hh 1// DirHelper.hh
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen at users.sourceforge.net) 2// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
3// 3//
4// Permission is hereby granted, free of charge, to any person obtaining a 4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"), 5// copy of this software and associated documentation files (the "Software"),
@@ -19,29 +19,35 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22// $Id: DirHelper.hh,v 1.1 2002/12/02 19:44:24 fluxgen Exp $ 22// $Id: DirHelper.hh,v 1.2 2003/02/15 01:41:50 fluxgen Exp $
23 23
24#ifndef DIRHELPER_HH 24#ifndef DIRHELPER_HH
25#define DIRHELPER_HH 25#define DIRHELPER_HH
26 26
27#include "NotCopyable.hh"
28
27#include <sys/types.h> 29#include <sys/types.h>
28#include <dirent.h> 30#include <dirent.h>
31#include <string>
29 32
30#include "NotCopyable.hh" 33/// Wrapper class for DIR * routines
31
32/**
33 Wrapper class for DIR * routines
34*/
35class DirHelper: private FbTk::NotCopyable { 34class DirHelper: private FbTk::NotCopyable {
36public: 35public:
37 explicit DirHelper(const char *dir = 0); 36 explicit DirHelper(const char *dir = 0);
38 ~DirHelper(); 37 ~DirHelper();
38
39 void rewind(); 39 void rewind();
40 /// gets next dirent info struct in directory
40 struct dirent * read(); 41 struct dirent * read();
41 void close(); 42 /// reads next filename in directory
43 std::string readFilename();
44 void close();
42 bool open(const char *dir); 45 bool open(const char *dir);
46 /// @return number of entries in the directory
47 size_t entries() const { return m_num_entries; }
43private: 48private:
44 DIR *m_dir; 49 DIR *m_dir;
50 size_t m_num_entries; ///< number of file entries in directory
45}; 51};
46 52
47#endif // DIRHELPER_HH 53#endif // DIRHELPER_HH