zipios++  2.0.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 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
137  memset(&m_stat, 0, sizeof(m_stat));
138  m_exists = stat(m_path.c_str(), &m_stat) == 0;
139  }
140 }
141 
142 
152 FilePath& FilePath::operator = (std::string const& path)
153 {
155  return *this;
156 }
157 
158 
165 FilePath::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 
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 
304 std::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 
325 size_t FilePath::length() const
326 {
327  return m_path.length();
328 }
329 
330 
345 size_t FilePath::size() const
346 {
347  return length();
348 }
349 
350 
358 bool FilePath::exists() const
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 
428 bool FilePath::isSocket() const
429 {
430  check();
431  return m_exists && S_ISSOCK(m_stat.st_mode);
432 }
433 
434 
442 bool FilePath::isFifo() const
443 {
444  check();
445  return m_exists && S_ISFIFO(m_stat.st_mode);
446 }
447 
448 
468 size_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 
502 std::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
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:304
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:372
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:400
bool isSocket() const
Check whether the file is a socket.
Definition: filepath.cpp:428
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:485
bool isBlockSpecial() const
Check whether the file is a block special file.
Definition: filepath.cpp:414
FilePath & operator=(std::string const &path)
Replace the path with a new path.
Definition: filepath.cpp:152
std::string m_path
Definition: filepath.hpp:76
bool isDirectory() const
Check whether the file is a directory.
Definition: filepath.cpp:386
bool operator==(char const *rhs) const
Check whether two FilePath represent the same file.
Definition: filepath.cpp:217
char const g_separator
The character used as the filename separator.
bool exists() const
Check whether the file exists.
Definition: filepath.cpp:358
size_t fileSize() const
Get the size of the file.
Definition: filepath.cpp:468
size_t size() const
Get the length of the string.
Definition: filepath.cpp:345
bool isFifo() const
Check whether the file is a pipe.
Definition: filepath.cpp:442
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:325
FilePath operator+(FilePath const &name) const
Append the a child name to this path.
Definition: filepath.cpp:185
void check() const
Read the file mode.
Definition: filepath.cpp:124
bool operator==(char const *lhs, FilePath const &rhs)
Check whether two FilePath represent the same file.
Definition: filepath.cpp:235