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#include "filesystem.hpp"
19#include "fileinfo.h"
20
21namespace fs = ghc::filesystem;
22
23size_t FileInfo::size() const
24{
25 std::error_code ec;
26 size_t result = static_cast<size_t>(fs::file_size(fs::path(m_name),ec));
27 return ec ? 0 : result;
28}
29
30bool FileInfo::exists() const
31{
32 std::error_code ec;
33 bool result = fs::exists(fs::path(m_name),ec);
34 return !ec && result;
35}
36
38{
39 std::error_code ec;
40 fs::file_status status = fs::status(m_name,ec);
41 return !ec && (status.permissions() & fs::perms::owner_write)!=fs::perms::none;
42}
43
45{
46 std::error_code ec;
47 fs::file_status status = fs::status(m_name,ec);
48 return !ec && (status.permissions() & fs::perms::owner_read)!=fs::perms::none;
49}
50
52{
53 std::error_code ec;
54 fs::file_status status = fs::status(m_name,ec);
55 return !ec && (status.permissions() & fs::perms::owner_exec)!=fs::perms::none;
56}
57
59{
60 return fs::path(m_name).is_relative();
61}
62
63bool FileInfo::isFile() const
64{
65 std::error_code ec;
66 fs::file_status status = fs::status(m_name,ec);
67 return !ec && fs::is_regular_file(std::move(status));
68}
69
70bool FileInfo::isDir() const
71{
72 std::error_code ec;
73 fs::file_status status = fs::status(m_name,ec);
74 return !ec && fs::is_directory(std::move(status));
75}
76
78{
79 std::error_code ec;
80 fs::file_status status = fs::symlink_status(m_name,ec);
81 return !ec && fs::is_symlink(std::move(status));
82}
83
84std::string FileInfo::readLink() const
85{
86 std::error_code ec;
87 fs::path targetPath = fs::read_symlink(fs::path(m_name));
88 return !ec ? targetPath.string() : std::string();
89}
90
91std::string FileInfo::filePath() const
92{
93 return m_name;
94}
95
96void FileInfo::correctPath(std::string &s)
97{
98 std::replace( s.begin(), s.end(), '\\', '/' );
99}
100
101std::string FileInfo::absFilePath() const
102{
103 std::string result;
104 std::error_code ec;
105 fs::path path(m_name);
106 if (!path.is_relative())
107 {
108 result = path.lexically_normal().string();
109 }
110 else
111 {
112 result = (fs::current_path(ec) / m_name).lexically_normal().string();
113 }
114 correctPath(result);
115 return result;
116}
117
118std::string FileInfo::fileName() const
119{
120 return fs::path(m_name).filename().string();
121}
122
123std::string FileInfo::baseName() const
124{
125 std::string s = fileName();
126 size_t pos = s.find('.');
127 return pos!=std::string::npos ? s.substr(0,pos) : s;
128}
129
130std::string FileInfo::extension(bool complete) const
131{
132 std::string fn = fileName();
133 size_t pos = complete ? fn.find('.') : fn.rfind('.');
134 return pos!=std::string::npos ? fn.substr(pos+1) : std::string();
135}
136
137std::string FileInfo::dirPath(bool absPath) const
138{
139 std::string result;
140 if (absPath)
141 {
142 result = absFilePath();
143 }
144 else
145 {
146 result = m_name;
147 correctPath(result);
148 }
149 size_t pos = result.rfind('/');
150 if (pos==std::string::npos)
151 {
152 return ".";
153 }
154 else if (pos==0)
155 {
156 return "/";
157 }
158 else
159 {
160 return result.substr(0,pos);
161 }
162}
163
std::string m_name
Definition fileinfo.h:44
std::string readLink() const
Definition fileinfo.cpp:84
bool isRelative() const
Definition fileinfo.cpp:58
bool isSymLink() const
Definition fileinfo.cpp:77
bool isExecutable() const
Definition fileinfo.cpp:51
bool exists() const
Definition fileinfo.cpp:30
bool isWritable() const
Definition fileinfo.cpp:37
size_t size() const
Definition fileinfo.cpp:23
std::string extension(bool complete) const
Definition fileinfo.cpp:130
std::string fileName() const
Definition fileinfo.cpp:118
bool isReadable() const
Definition fileinfo.cpp:44
bool isDir() const
Definition fileinfo.cpp:70
static void correctPath(std::string &s)
Definition fileinfo.cpp:96
bool isFile() const
Definition fileinfo.cpp:63
std::string dirPath(bool absPath=true) const
Definition fileinfo.cpp:137
std::string filePath() const
Definition fileinfo.cpp:91
std::string baseName() const
Definition fileinfo.cpp:123
std::string absFilePath() const
Definition fileinfo.cpp:101