zipios  2.3.2
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-2022 Made to Order Software Corp. All Rights Reserved
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 
35 
36 namespace zipios
37 {
38 
39 
40 namespace
41 {
42 
43 
58 std::string pruneTrailingSeparator(std::string path)
59 {
60  if(path.size() > 0)
61  {
62  if(path[path.size() - 1] == g_separator)
63  {
64  path.erase(path.size() - 1);
65  }
66  }
67 
68  return path;
69 }
70 
71 
72 } // no name namespace
73 
74 
75 
102 FilePath::FilePath(std::string const & path)
103  : m_path(pruneTrailingSeparator(path))
104 {
105 }
106 
107 
119 void FilePath::check() const
120 {
121  if(!m_checked)
122  {
123  m_checked = true;
124 
134  m_stat = {};
135  m_exists = stat(m_path.c_str(), &m_stat) == 0;
136  }
137 }
138 
139 
149 FilePath & FilePath::operator = (std::string const & path)
150 {
152  m_checked = false;
153  return *this;
154 }
155 
156 
163 FilePath::operator std::string () const
164 {
165  return m_path;
166 }
167 
168 
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 
217 bool FilePath::operator == (char const * rhs) const
218 {
219  return m_path == rhs;
220 }
221 
222 
235 bool operator == (char const * lhs, FilePath const & rhs)
236 {
237  return lhs == rhs.m_path;
238 }
239 
240 
251 bool FilePath::operator == (std::string const & rhs) const
252 {
253  return m_path == rhs;
254 }
255 
256 
268 bool operator == (std::string const & lhs, FilePath const & rhs)
269 {
270  return lhs == rhs.m_path;
271 }
272 
273 
291 bool FilePath::operator == (FilePath const & rhs) const
292 {
293  return m_path == rhs.m_path;
294 }
295 
296 
302 {
303  m_path.clear();
304  m_checked = false;
305 }
306 
307 
315 std::string FilePath::filename() const
316 {
317  std::string::size_type const pos(m_path.find_last_of(g_separator));
318  if(pos != std::string::npos)
319  {
320  return m_path.substr(pos + 1);
321  }
322 
323  return m_path;
324 }
325 
326 
336 size_t FilePath::length() const
337 {
338  return m_path.length();
339 }
340 
341 
356 size_t FilePath::size() const
357 {
358  return length();
359 }
360 
361 
381 bool FilePath::empty() const
382 {
383  return m_path.empty();
384 }
385 
386 
394 bool FilePath::exists() const
395 {
396  check();
397  return m_exists;
398 }
399 
400 
409 {
410  check();
411  return m_exists && S_ISREG(m_stat.st_mode);
412 }
413 
414 
423 {
424  check();
425  return m_exists && S_ISDIR(m_stat.st_mode);
426 }
427 
428 
437 {
438  check();
439  return m_exists && S_ISCHR(m_stat.st_mode);
440 }
441 
442 
451 {
452  check();
453  return m_exists && S_ISBLK(m_stat.st_mode);
454 }
455 
456 
464 bool FilePath::isSocket() const
465 {
466  check();
467  return m_exists && S_ISSOCK(m_stat.st_mode);
468 }
469 
470 
478 bool FilePath::isFifo() const
479 {
480  check();
481  return m_exists && S_ISFIFO(m_stat.st_mode);
482 }
483 
484 
504 size_t FilePath::fileSize() const
505 {
506  check();
507  return m_stat.st_size;
508 }
509 
510 
522 {
523  check();
524  return m_stat.st_mtime;
525 }
526 
527 
538 std::ostream & operator << (std::ostream & os, FilePath const & path)
539 {
540  os << static_cast<std::string>(path);
541  return os;
542 }
543 
544 } // namespace
545 
546 // Local Variables:
547 // mode: cpp
548 // indent-tabs-mode: nil
549 // c-basic-offset: 4
550 // tab-width: 4
551 // End:
552 
553 // vim: ts=4 sw=4 et
bool isCharSpecial() const
Check whether the file is a character special file.
Definition: filepath.cpp:436
std::time_t lastModificationTime() const
Get the last modification time of the file.
Definition: filepath.cpp:521
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
size_t length() const
Get the length of the string.
Definition: filepath.cpp:336
std::string pruneTrailingSeparator(std::string path)
Prune the trailing separator if present.
Definition: filepath.cpp:58
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:315
FilePath(std::string const &path=std::string())
Initialize a FilePath object.
Definition: filepath.cpp:102
bool isFifo() const
Check whether the file is a pipe.
Definition: filepath.cpp:478
bool isBlockSpecial() const
Check whether the file is a block special file.
Definition: filepath.cpp:450
void clear()
Clear the filename.
Definition: filepath.cpp:301
os_stat_t m_stat
Definition: filepath.hpp:79
bool operator==(char const *rhs) const
Check whether two FilePath represent the same file.
Definition: filepath.cpp:217
size_t fileSize() const
Get the size of the file.
Definition: filepath.cpp:504
FilePath & operator=(std::string const &path)
Replace the path with a new path.
Definition: filepath.cpp:149
void check() const
Read the file mode.
Definition: filepath.cpp:119
std::string m_path
Definition: filepath.hpp:78
bool isDirectory() const
Check whether the file is a directory.
Definition: filepath.cpp:422
char const g_separator
The character used as the filename separator.
bool isRegular() const
Check whether the file is a regular file.
Definition: filepath.cpp:408
Define the zipios::FilePath class.
Handle a file path and name and its statistics.
Definition: filepath.hpp:46
FilePath operator+(FilePath const &name) const
Append the a child name to this path.
Definition: filepath.cpp:185
bool exists() const
Check whether the file exists.
Definition: filepath.cpp:394
Various functions used throughout the library.
size_t size() const
Get the length of the string.
Definition: filepath.cpp:356
bool empty() const
Check whether the filename is empty.
Definition: filepath.cpp:381
bool isSocket() const
Check whether the file is a socket.
Definition: filepath.cpp:464
bool operator==(char const *lhs, FilePath const &rhs)
Check whether two FilePath represent the same file.
Definition: filepath.cpp:235