Doxygen
Toggle main menu visibility
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
28
size_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
35
bool
FileInfo::exists
()
const
36
{
37
std::error_code ec;
38
bool
result = fs::exists(
toFsPath
(
m_name
),ec);
39
return
!ec && result;
40
}
41
42
bool
FileInfo::isWritable
()
const
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
49
bool
FileInfo::isReadable
()
const
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
56
bool
FileInfo::isExecutable
()
const
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
63
bool
FileInfo::isRelative
()
const
64
{
65
return
toFsPath
(
m_name
).is_relative();
66
}
67
68
bool
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
75
bool
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
82
bool
FileInfo::isSymLink
()
const
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
89
std::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
96
std::string
FileInfo::filePath
()
const
97
{
98
return
m_name
;
99
}
100
101
void
FileInfo::correctPath
(std::string &s)
102
{
103
std::replace( s.begin(), s.end(),
'\\'
,
'/'
);
104
}
105
106
std::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
123
std::string
FileInfo::fileName
()
const
124
{
125
return
fromFsPath
(
toFsPath
(
m_name
).filename());
126
}
127
128
std::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
135
std::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
142
std::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
FileInfo::m_name
std::string m_name
Definition
fileinfo.h:108
FileInfo::readLink
std::string readLink() const
Definition
fileinfo.cpp:89
FileInfo::isRelative
bool isRelative() const
Definition
fileinfo.cpp:63
FileInfo::isSymLink
bool isSymLink() const
Definition
fileinfo.cpp:82
FileInfo::isExecutable
bool isExecutable() const
Definition
fileinfo.cpp:56
FileInfo::exists
bool exists() const
Definition
fileinfo.cpp:35
FileInfo::isWritable
bool isWritable() const
Definition
fileinfo.cpp:42
FileInfo::size
size_t size() const
Definition
fileinfo.cpp:28
FileInfo::extension
std::string extension(bool complete) const
Definition
fileinfo.cpp:135
FileInfo::fileName
std::string fileName() const
Definition
fileinfo.cpp:123
FileInfo::isReadable
bool isReadable() const
Definition
fileinfo.cpp:49
FileInfo::isDir
bool isDir() const
Definition
fileinfo.cpp:75
FileInfo::correctPath
static void correctPath(std::string &s)
Definition
fileinfo.cpp:101
FileInfo::isFile
bool isFile() const
Definition
fileinfo.cpp:68
FileInfo::dirPath
std::string dirPath(bool absPath=true) const
Definition
fileinfo.cpp:142
FileInfo::filePath
std::string filePath() const
Definition
fileinfo.cpp:96
FileInfo::baseName
std::string baseName() const
Definition
fileinfo.cpp:128
FileInfo::absFilePath
std::string absFilePath() const
Definition
fileinfo.cpp:106
fileinfo.h
fswrapper.h
Selects the filesystem implementation used by doxygen.
fromFsPath
std::string fromFsPath(const fs::path &p)
Converts a path into a UTF-8 encoded string.
Definition
fswrapper.h:78
toFsPath
fs::path toFsPath(const std::string &s)
Converts a UTF-8 encoded string into a path.
Definition
fswrapper.h:64
src
fileinfo.cpp
Generated by
1.19.0