zipios  2.2.0
Zipios – a small C++ library that provides easy access to .zip files.
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-2019 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 
37 namespace zipios
38 {
39 
40 
41 namespace
42 {
43 
44 
59 std::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 
103 FilePath::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 
124 void FilePath::check() const
125 {
126  if(!m_checked)
127  {
128  m_checked = true;
129 
139  memset(&m_stat, 0, sizeof(m_stat));
140  m_exists = stat(m_path.c_str(), &m_stat) == 0;
141  }
142 }
143 
144 
154 FilePath& FilePath::operator = (std::string const& path)
155 {
157  return *this;
158 }
159 
160 
167 FilePath::operator std::string () const
168 {
169  return m_path;
170 }
171 
172 
188 {
189  if(m_path.empty())
190  {
191  return rhs;
192  }
193 
194  if(rhs.m_path.empty())
195  {
196  return *this;
197  }
198 
199  if(rhs.m_path[0] == g_separator)
200  {
201  return m_path + rhs.m_path;
202  }
203 
204  return m_path + g_separator + rhs.m_path;
205 }
206 
207 
219 bool FilePath::operator == (char const *rhs) const
220 {
221  return m_path == rhs;
222 }
223 
224 
237 bool operator == (char const *lhs, FilePath const& rhs)
238 {
239  return lhs == rhs.m_path;
240 }
241 
242 
253 bool FilePath::operator == (std::string const& rhs) const
254 {
255  return m_path == rhs;
256 }
257 
258 
270 bool operator == (std::string const& lhs, FilePath const& rhs)
271 {
272  return lhs == rhs.m_path;
273 }
274 
275 
293 bool FilePath::operator == (FilePath const& rhs) const
294 {
295  return m_path == rhs.m_path;
296 }
297 
298 
306 std::string FilePath::filename() const
307 {
308  std::string::size_type const pos(m_path.find_last_of(g_separator));
309  if(pos != std::string::npos)
310  {
311  return m_path.substr(pos + 1);
312  }
313 
314  return m_path;
315 }
316 
317 
327 size_t FilePath::length() const
328 {
329  return m_path.length();
330 }
331 
332 
347 size_t FilePath::size() const
348 {
349  return length();
350 }
351 
352 
360 bool FilePath::exists() const
361 {
362  check();
363  return m_exists;
364 }
365 
366 
375 {
376  check();
377  return m_exists && S_ISREG(m_stat.st_mode);
378 }
379 
380 
389 {
390  check();
391  return m_exists && S_ISDIR(m_stat.st_mode);
392 }
393 
394 
403 {
404  check();
405  return m_exists && S_ISCHR(m_stat.st_mode);
406 }
407 
408 
417 {
418  check();
419  return m_exists && S_ISBLK(m_stat.st_mode);
420 }
421 
422 
430 bool FilePath::isSocket() const
431 {
432  check();
433  return m_exists && S_ISSOCK(m_stat.st_mode);
434 }
435 
436 
444 bool FilePath::isFifo() const
445 {
446  check();
447  return m_exists && S_ISFIFO(m_stat.st_mode);
448 }
449 
450 
470 size_t FilePath::fileSize() const
471 {
472  check();
473  return m_stat.st_size;
474 }
475 
476 
488 {
489  check();
490  return m_stat.st_mtime;
491 }
492 
493 
504 std::ostream& operator << (std::ostream& os, FilePath const& path)
505 {
506  os << static_cast<std::string>(path);
507  return os;
508 }
509 
510 } // namespace
511 
512 // Local Variables:
513 // mode: cpp
514 // indent-tabs-mode: nil
515 // c-basic-offset: 4
516 // tab-width: 4
517 // End:
518 
519 // vim: ts=4 sw=4 et
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:306
std::string pruneTrailingSeparator(std::string path)
Prune the trailing separator if present.
Definition: filepath.cpp:59
bool isRegular() const
Check whether the file is a regular file.
Definition: filepath.cpp:374
FilePath(std::string const &path="")
Initialize a FilePath object.
Definition: filepath.cpp:103
bool isCharSpecial() const
Check whether the file is a character special file.
Definition: filepath.cpp:402
bool isSocket() const
Check whether the file is a socket.
Definition: filepath.cpp:430
os_stat_t m_stat
Definition: filepath.hpp:77
std::time_t lastModificationTime() const
Get the last modification time of the file.
Definition: filepath.cpp:487
bool isBlockSpecial() const
Check whether the file is a block special file.
Definition: filepath.cpp:416
FilePath & operator=(std::string const &path)
Replace the path with a new path.
Definition: filepath.cpp:154
std::string m_path
Definition: filepath.hpp:76
bool isDirectory() const
Check whether the file is a directory.
Definition: filepath.cpp:388
bool operator==(char const *rhs) const
Check whether two FilePath represent the same file.
Definition: filepath.cpp:219
char const g_separator
The character used as the filename separator.
bool exists() const
Check whether the file exists.
Definition: filepath.cpp:360
size_t fileSize() const
Get the size of the file.
Definition: filepath.cpp:470
size_t size() const
Get the length of the string.
Definition: filepath.cpp:347
bool isFifo() const
Check whether the file is a pipe.
Definition: filepath.cpp:444
Define the zipios::FilePath class.
Handle a file path and name and its statistics.
Definition: filepath.hpp:46
Various functions used throughout the library.
size_t length() const
Get the length of the string.
Definition: filepath.cpp:327
FilePath operator+(FilePath const &name) const
Append the a child name to this path.
Definition: filepath.cpp:187
void check() const
Read the file mode.
Definition: filepath.cpp:124