aboutsummaryrefslogtreecommitdiff
path: root/src/DirHelper.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/DirHelper.cc')
-rw-r--r--src/DirHelper.cc84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/DirHelper.cc b/src/DirHelper.cc
new file mode 100644
index 0000000..19f7612
--- /dev/null
+++ b/src/DirHelper.cc
@@ -0,0 +1,84 @@
1// DirHelper.cc
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen at users.sourceforge.net)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: DirHelper.cc,v 1.2 2003/02/15 01:42:17 fluxgen Exp $
23
24#include "DirHelper.hh"
25
26DirHelper::DirHelper(const char *dir):m_dir(0),
27m_num_entries(0) {
28 if (dir != 0)
29 open(dir);
30}
31
32DirHelper::~DirHelper() {
33 if (m_dir != 0)
34 close();
35}
36
37void DirHelper::rewind() {
38 if (m_dir != 0)
39 rewinddir(m_dir);
40}
41
42struct dirent *DirHelper::read() {
43 if (m_dir == 0)
44 return 0;
45
46 return readdir(m_dir);
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
56void DirHelper::close() {
57 if (m_dir != 0) {
58 closedir(m_dir);
59 m_dir = 0;
60 m_num_entries = 0;
61 }
62}
63
64
65bool DirHelper::open(const char *dir) {
66 if (dir == 0)
67 return false;
68
69 if (m_dir != 0)
70 close();
71
72 m_dir = opendir(dir);
73 if (m_dir == 0) // successfull loading?
74 return false;
75
76 // get number of entries
77 while (read())
78 m_num_entries++;
79
80 rewind(); // go back to start
81
82 return true;
83}
84