filepath.cpp
Go to the documentation of this file.
1/*
2 Zipios++ - a small C++ library that provides easy access to .zip files.
3
4 Copyright (C) 2000-2007 Thomas Sondergaard
5 Copyright (C) 2015-2017 Made to Order Software Corporation
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
30#include "zipios/filepath.hpp"
31
32#include "zipios_common.hpp"
33
34#include <memory.h>
35
36
37namespace zipios
38{
39
40
41namespace
42{
43
44
59std::string pruneTrailingSeparator(std::string path)
60{
61 if(path.size() > 0)
62 {
63 if(path[path.size() - 1] == g_separator)
64 {
65 path.erase(path.size() - 1);
66 }
67 }
68
69 return path;
70}
71
72
73} // no name namespace
74
75
76
103FilePath::FilePath(std::string const& path)
104 : m_path(pruneTrailingSeparator(path))
105 //, m_stat() -- see below
106 //, m_checked(false) -- auto-init
107 //, m_exists(false) -- auto-init
108{
109 memset(&m_stat, 0, sizeof(m_stat));
110}
111
112
124void FilePath::check() const
125{
126 if(!m_checked)
127 {
128 m_checked = true;
129
137 memset(&m_stat, 0, sizeof(m_stat));
138 m_exists = stat(m_path.c_str(), &m_stat) == 0;
139 }
140}
141
142
152FilePath& FilePath::operator = (std::string const& path)
153{
154 m_path = pruneTrailingSeparator(path);
155 return *this;
156}
157
158
165FilePath::operator std::string () const
166{
167 return m_path;
168}
169
170
186{
187 if(m_path.empty())
188 {
189 return rhs;
190 }
191
192 if(rhs.m_path.empty())
193 {
194 return *this;
195 }
196
197 if(rhs.m_path[0] == g_separator)
198 {
199 return m_path + rhs.m_path;
200 }
201
202 return m_path + g_separator + rhs.m_path;
203}
204
205
217bool FilePath::operator == (char const *rhs) const
218{
219 return m_path == rhs;
220}
221
222
235bool operator == (char const *lhs, FilePath const& rhs)
236{
237 return lhs == rhs.m_path;
238}
239
240
251bool FilePath::operator == (std::string const& rhs) const
252{
253 return m_path == rhs;
254}
255
256
268bool operator == (std::string const& lhs, FilePath const& rhs)
269{
270 return lhs == rhs.m_path;
271}
272
273
291bool FilePath::operator == (FilePath const& rhs) const
292{
293 return m_path == rhs.m_path;
294}
295
296
304std::string FilePath::filename() const
305{
306 std::string::size_type const pos(m_path.find_last_of(g_separator));
307 if(pos != std::string::npos)
308 {
309 return m_path.substr(pos + 1);
310 }
311
312 return m_path;
313}
314
315
325size_t FilePath::length() const
326{
327 return m_path.length();
328}
329
330
345size_t FilePath::size() const
346{
347 return length();
348}
349
350
359{
360 check();
361 return m_exists;
362}
363
364
373{
374 check();
375 return m_exists && S_ISREG(m_stat.st_mode);
376}
377
378
387{
388 check();
389 return m_exists && S_ISDIR(m_stat.st_mode);
390}
391
392
401{
402 check();
403 return m_exists && S_ISCHR(m_stat.st_mode);
404}
405
406
415{
416 check();
417 return m_exists && S_ISBLK(m_stat.st_mode);
418}
419
420
429{
430 check();
431 return m_exists && S_ISSOCK(m_stat.st_mode);
432}
433
434
443{
444 check();
445 return m_exists && S_ISFIFO(m_stat.st_mode);
446}
447
448
468size_t FilePath::fileSize() const
469{
470 check();
471 return m_stat.st_size;
472}
473
474
486{
487 check();
488 return m_stat.st_mtime;
489}
490
491
502std::ostream& operator << (std::ostream& os, FilePath const& path)
503{
504 os << static_cast<std::string>(path);
505 return os;
506}
507
508} // namespace
509
510// Local Variables:
511// mode: cpp
512// indent-tabs-mode: nil
513// c-basic-offset: 4
514// tab-width: 4
515// End:
516
517// vim: ts=4 sw=4 et
Handle a file path and name and its statistics.
Definition: filepath.hpp:47
bool isFifo() const
Check whether the file is a pipe.
Definition: filepath.cpp:442
void check() const
Read the file mode.
Definition: filepath.cpp:124
FilePath operator+(FilePath const &name) const
Append the a child name to this path.
Definition: filepath.cpp:185
bool isCharSpecial() const
Check whether the file is a character special file.
Definition: filepath.cpp:400
bool isBlockSpecial() const
Check whether the file is a block special file.
Definition: filepath.cpp:414
std::string m_path
Definition: filepath.hpp:76
friend bool operator==(char const *lhs, FilePath const &rhs)
Check whether two FilePath represent the same file.
Definition: filepath.cpp:235
os_stat_t m_stat
Definition: filepath.hpp:77
FilePath(std::string const &path="")
Initialize a FilePath object.
Definition: filepath.cpp:103
bool isDirectory() const
Check whether the file is a directory.
Definition: filepath.cpp:386
std::time_t lastModificationTime() const
Get the last modification time of the file.
Definition: filepath.cpp:485
size_t fileSize() const
Get the size of the file.
Definition: filepath.cpp:468
bool exists() const
Check whether the file exists.
Definition: filepath.cpp:358
FilePath & operator=(std::string const &path)
Replace the path with a new path.
Definition: filepath.cpp:152
size_t size() const
Get the length of the string.
Definition: filepath.cpp:345
bool isRegular() const
Check whether the file is a regular file.
Definition: filepath.cpp:372
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:304
bool isSocket() const
Check whether the file is a socket.
Definition: filepath.cpp:428
size_t length() const
Get the length of the string.
Definition: filepath.cpp:325
Define the zipios::FilePath class.
std::string pruneTrailingSeparator(std::string path)
Prune the trailing separator if present.
Definition: filepath.cpp:59
The zipios namespace includes the Zipios++ library definitions.
Definition: backbuffer.cpp:36
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
bool operator==(char const *lhs, FilePath const &rhs)
Check whether two FilePath represent the same file.
Definition: filepath.cpp:235
char const g_separator
The character used as the filename separator.
Various functions used throughout the library.