Doxygen
Loading...
Searching...
No Matches
fileinfo.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2021 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#define NOMINMAX
17#define WIN32_LEAN_AND_MEAN // optional
18
19// own header
20#include "fileinfo.h"
21
22// other includes
23#include "filesystem.hpp"
24
25namespace fs = ghc::filesystem;
26
27size_t FileInfo::size() const
28{
29 std::error_code ec;
30 size_t result = static_cast<size_t>(fs::file_size(fs::path(m_name),ec));
31 return ec ? 0 : result;
32}
33
34bool FileInfo::exists() const
35{
36 std::error_code ec;
37 bool result = fs::exists(fs::path(m_name),ec);
38 return !ec && result;
39}
40
42{
43 std::error_code ec;
44 fs::file_status status = fs::status(m_name,ec);
45 return !ec && (status.permissions() & fs::perms::owner_write)!=fs::perms::none;
46}
47
49{
50 std::error_code ec;
51 fs::file_status status = fs::status(m_name,ec);
52 return !ec && (status.permissions() & fs::perms::owner_read)!=fs::perms::none;
53}
54
56{
57 std::error_code ec;
58 fs::file_status status = fs::status(m_name,ec);
59 return !ec && (status.permissions() & fs::perms::owner_exec)!=fs::perms::none;
60}
61
63{
64 return fs::path(m_name).is_relative();
65}
66
67bool FileInfo::isFile() const
68{
69 std::error_code ec;
70 fs::file_status status = fs::status(m_name,ec);
71 return !ec && fs::is_regular_file(std::move(status));
72}
73
74bool FileInfo::isDir() const
75{
76 std::error_code ec;
77 fs::file_status status = fs::status(m_name,ec);
78 return !ec && fs::is_directory(std::move(status));
79}
80
82{
83 std::error_code ec;
84 fs::file_status status = fs::symlink_status(m_name,ec);
85 return !ec && fs::is_symlink(std::move(status));
86}
87
88std::string FileInfo::readLink() const
89{
90 std::error_code ec;
91 fs::path targetPath = fs::read_symlink(fs::path(m_name));
92 return !ec ? targetPath.string() : std::string();
93}
94
95std::string FileInfo::filePath() const
96{
97 return m_name;
98}
99
100void FileInfo::correctPath(std::string &s)
101{
102 std::replace( s.begin(), s.end(), '\\', '/' );
103}
104
105std::string FileInfo::absFilePath() const
106{
107 std::string result;
108 std::error_code ec;
109 fs::path path(m_name);
110 if (!path.is_relative())
111 {
112 result = path.lexically_normal().string();
113 }
114 else
115 {
116 result = (fs::current_path(ec) / m_name).lexically_normal().string();
117 }
118 correctPath(result);
119 return result;
120}
121
122std::string FileInfo::fileName() const
123{
124 return fs::path(m_name).filename().string();
125}
126
127std::string FileInfo::baseName() const
128{
129 std::string s = fileName();
130 size_t pos = s.find('.');
131 return pos!=std::string::npos ? s.substr(0,pos) : s;
132}
133
134std::string FileInfo::extension(bool complete) const
135{
136 std::string fn = fileName();
137 size_t pos = complete ? fn.find('.') : fn.rfind('.');
138 return pos!=std::string::npos ? fn.substr(pos+1) : std::string();
139}
140
141std::string FileInfo::dirPath(bool absPath) const
142{
143 std::string result;
144 if (absPath)
145 {
146 result = absFilePath();
147 }
148 else
149 {
150 result = m_name;
151 correctPath(result);
152 }
153 size_t pos = result.rfind('/');
154 if (pos==std::string::npos)
155 {
156 return ".";
157 }
158 else if (pos==0)
159 {
160 return "/";
161 }
162 else
163 {
164 return result.substr(0,pos);
165 }
166}
167
std::string m_name
Definition fileinfo.h:108
std::string readLink() const
Definition fileinfo.cpp:88
bool isRelative() const
Definition fileinfo.cpp:62
bool isSymLink() const
Definition fileinfo.cpp:81
bool isExecutable() const
Definition fileinfo.cpp:55
bool exists() const
Definition fileinfo.cpp:34
bool isWritable() const
Definition fileinfo.cpp:41
size_t size() const
Definition fileinfo.cpp:27
std::string extension(bool complete) const
Definition fileinfo.cpp:134
std::string fileName() const
Definition fileinfo.cpp:122
bool isReadable() const
Definition fileinfo.cpp:48
bool isDir() const
Definition fileinfo.cpp:74
static void correctPath(std::string &s)
Definition fileinfo.cpp:100
bool isFile() const
Definition fileinfo.cpp:67
std::string dirPath(bool absPath=true) const
Definition fileinfo.cpp:141
std::string filePath() const
Definition fileinfo.cpp:95
std::string baseName() const
Definition fileinfo.cpp:127
std::string absFilePath() const
Definition fileinfo.cpp:105