libDwm-0.9.45
DwmDirectoryEntry.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2011, 2016, 2017
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMDIRECTORYENTRY_HH_
42#define _DWMDIRECTORYENTRY_HH_
43
44extern "C" {
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <dirent.h>
48}
49
50#include <mutex>
51#include <string>
52#include <vector>
53
54namespace Dwm {
55
56 //--------------------------------------------------------------------------
57 // pre-declare DirectoryEntry
58 //--------------------------------------------------------------------------
59 class DirectoryEntry;
60
61 //--------------------------------------------------------------------------
63 //--------------------------------------------------------------------------
64 class DirectoryEntryFunctor
65 {
66 public:
67 virtual bool operator () (DirectoryEntry & dirEntry)
68 {
69 return false;
70 }
71 };
72
73 //--------------------------------------------------------------------------
75 //--------------------------------------------------------------------------
77 {
78 public:
79 //------------------------------------------------------------------------
81 //------------------------------------------------------------------------
82 typedef enum {
83 e_typeUnknown = 0x00,
84 e_typeFifo = 0x01,
85 e_typeCharacterSpecial = 0x02,
86 e_typeDirectory = 0x04,
87 e_typeBlockSpecial = 0x08,
88 e_typeRegular = 0x10,
89 e_typeSymbolicLink = 0x20,
90 e_typeSocket = 0x40
91 } TypeEnum;
92
93 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
96 DirectoryEntry(const std::string & path);
97
98 //------------------------------------------------------------------------
100 //------------------------------------------------------------------------
101 DirectoryEntry(const std::string & path, const DirectoryEntry *parent);
102
103 //------------------------------------------------------------------------
105 //------------------------------------------------------------------------
106 TypeEnum Type() const;
107
108 //------------------------------------------------------------------------
110 //------------------------------------------------------------------------
111 const std::string & Path() const;
112
113 std::string RelativePath() const;
114
115 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
118 std::string BaseName() const;
119
120 //------------------------------------------------------------------------
122 //------------------------------------------------------------------------
123 std::string DirName() const;
124
125 //------------------------------------------------------------------------
129 //------------------------------------------------------------------------
130 std::string RealPath() const;
131
132 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 std::string Extension() const;
138
139 //------------------------------------------------------------------------
142 //------------------------------------------------------------------------
143 const DirectoryEntry *Parent() const;
144
145 //------------------------------------------------------------------------
147 //------------------------------------------------------------------------
148 bool Exists() const;
149
150 //------------------------------------------------------------------------
152 //------------------------------------------------------------------------
153 bool IsDirectory() const;
154
155 //------------------------------------------------------------------------
159 //------------------------------------------------------------------------
160 bool GetChildren(std::vector<DirectoryEntry> & children,
161 uint8_t typeMask = 0xFF) const;
162
163 bool GetChildren(std::vector<DirectoryEntry> & children,
164 DirectoryEntryFunctor & dirEntryFunc) const;
165
166 uint32_t Recurse(DirectoryEntryFunctor & dirEntryFunc,
167 bool depthFirst = false,
168 int maxDepth = -1);
169
170 protected:
171 static std::mutex _readdirMtx;
172 std::string _path;
173 const DirectoryEntry *_parent;
174 TypeEnum _type;
175
176 static TypeEnum GetType(const struct stat & statStruct);
177 static TypeEnum GetType(const struct dirent & dirEntry);
178
179 uint32_t Recurse(DirectoryEntry & de,
180 DirectoryEntryFunctor & dirEntryFunc,
181 int depth, bool depthFirst, int maxDepth);
182 };
183
184} // namespace Dwm
185
186#endif // _DWMDIRECTORYENTRY_HH_
187
188
189//---------------------------- emacs settings -----------------------------
190// Local Variables:
191// mode: C++
192// tab-width: 2
193// indent-tabs-mode: nil
194// c-basic-offset: 2
195// End:
196//-------------------------------------------------------------------------
Encapsulates a directory entry (file, directory, FIFO, et. al.).
Definition DwmDirectoryEntry.hh:77
DirectoryEntry(const std::string &path, const DirectoryEntry *parent)
Construct from a path and a parent.
TypeEnum Type() const
Returns the DirectoryEntry's type.
bool IsDirectory() const
Returns true if the DirectoryEntry is a directory.
bool Exists() const
Returns true if the DirectoryEntry exists in the filesystem.
std::string BaseName() const
Returns the final component of the DirectoryEntry's path.
bool GetChildren(std::vector< DirectoryEntry > &children, uint8_t typeMask=0xFF) const
If the DirectoryEntry is a directory and has children, fills children with its direct children and re...
std::string DirName() const
Returns the name of the directory in which DirectoryEntry resides.
DirectoryEntry(const std::string &path)
Construct from a path.
std::string Extension() const
If the DirectoryEntry has dots ('.
TypeEnum
The types of DirectoryEntry objects.
Definition DwmDirectoryEntry.hh:82
const std::string & Path() const
Returns the DirectoryEntry's path.
const DirectoryEntry * Parent() const
If the DirectoryEntry has a parent, returns a pointer to it.
std::string RealPath() const
If the DirectoryEntry exists in the filesystem, returns its path with all symlinks,...