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