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
// other includes
23
#include "filesystem.hpp"
24
25
namespace
fs = ghc::filesystem;
26
27
size_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
34
bool
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
41
bool
FileInfo::isWritable
()
const
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
48
bool
FileInfo::isReadable
()
const
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
55
bool
FileInfo::isExecutable
()
const
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
62
bool
FileInfo::isRelative
()
const
63
{
64
return
fs::path(
m_name
).is_relative();
65
}
66
67
bool
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
74
bool
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
81
bool
FileInfo::isSymLink
()
const
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
88
std::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
95
std::string
FileInfo::filePath
()
const
96
{
97
return
m_name
;
98
}
99
100
void
FileInfo::correctPath
(std::string &s)
101
{
102
std::replace( s.begin(), s.end(),
'\\'
,
'/'
);
103
}
104
105
std::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
122
std::string
FileInfo::fileName
()
const
123
{
124
return
fs::path(
m_name
).filename().string();
125
}
126
127
std::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
134
std::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
141
std::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
FileInfo::m_name
std::string m_name
Definition
fileinfo.h:108
FileInfo::readLink
std::string readLink() const
Definition
fileinfo.cpp:88
FileInfo::isRelative
bool isRelative() const
Definition
fileinfo.cpp:62
FileInfo::isSymLink
bool isSymLink() const
Definition
fileinfo.cpp:81
FileInfo::isExecutable
bool isExecutable() const
Definition
fileinfo.cpp:55
FileInfo::exists
bool exists() const
Definition
fileinfo.cpp:34
FileInfo::isWritable
bool isWritable() const
Definition
fileinfo.cpp:41
FileInfo::size
size_t size() const
Definition
fileinfo.cpp:27
FileInfo::extension
std::string extension(bool complete) const
Definition
fileinfo.cpp:134
FileInfo::fileName
std::string fileName() const
Definition
fileinfo.cpp:122
FileInfo::isReadable
bool isReadable() const
Definition
fileinfo.cpp:48
FileInfo::isDir
bool isDir() const
Definition
fileinfo.cpp:74
FileInfo::correctPath
static void correctPath(std::string &s)
Definition
fileinfo.cpp:100
FileInfo::isFile
bool isFile() const
Definition
fileinfo.cpp:67
FileInfo::dirPath
std::string dirPath(bool absPath=true) const
Definition
fileinfo.cpp:141
FileInfo::filePath
std::string filePath() const
Definition
fileinfo.cpp:95
FileInfo::baseName
std::string baseName() const
Definition
fileinfo.cpp:127
FileInfo::absFilePath
std::string absFilePath() const
Definition
fileinfo.cpp:105
fileinfo.h
src
fileinfo.cpp
Generated by
1.19.0