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
#include "filesystem.hpp"
19
#include "
fileinfo.h
"
20
21
namespace
fs = ghc::filesystem;
22
23
size_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
30
bool
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
37
bool
FileInfo::isWritable
()
const
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
44
bool
FileInfo::isReadable
()
const
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
51
bool
FileInfo::isExecutable
()
const
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
58
bool
FileInfo::isRelative
()
const
59
{
60
return
fs::path(
m_name
).is_relative();
61
}
62
63
bool
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
70
bool
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
77
bool
FileInfo::isSymLink
()
const
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
84
std::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
91
std::string
FileInfo::filePath
()
const
92
{
93
return
m_name
;
94
}
95
96
void
FileInfo::correctPath
(std::string &s)
97
{
98
std::replace( s.begin(), s.end(),
'\\'
,
'/'
);
99
}
100
101
std::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
118
std::string
FileInfo::fileName
()
const
119
{
120
return
fs::path(
m_name
).filename().string();
121
}
122
123
std::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
130
std::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
137
std::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
FileInfo::m_name
std::string m_name
Definition
fileinfo.h:44
FileInfo::readLink
std::string readLink() const
Definition
fileinfo.cpp:84
FileInfo::isRelative
bool isRelative() const
Definition
fileinfo.cpp:58
FileInfo::isSymLink
bool isSymLink() const
Definition
fileinfo.cpp:77
FileInfo::isExecutable
bool isExecutable() const
Definition
fileinfo.cpp:51
FileInfo::exists
bool exists() const
Definition
fileinfo.cpp:30
FileInfo::isWritable
bool isWritable() const
Definition
fileinfo.cpp:37
FileInfo::size
size_t size() const
Definition
fileinfo.cpp:23
FileInfo::extension
std::string extension(bool complete) const
Definition
fileinfo.cpp:130
FileInfo::fileName
std::string fileName() const
Definition
fileinfo.cpp:118
FileInfo::isReadable
bool isReadable() const
Definition
fileinfo.cpp:44
FileInfo::isDir
bool isDir() const
Definition
fileinfo.cpp:70
FileInfo::correctPath
static void correctPath(std::string &s)
Definition
fileinfo.cpp:96
FileInfo::isFile
bool isFile() const
Definition
fileinfo.cpp:63
FileInfo::dirPath
std::string dirPath(bool absPath=true) const
Definition
fileinfo.cpp:137
FileInfo::filePath
std::string filePath() const
Definition
fileinfo.cpp:91
FileInfo::baseName
std::string baseName() const
Definition
fileinfo.cpp:123
FileInfo::absFilePath
std::string absFilePath() const
Definition
fileinfo.cpp:101
fileinfo.h
src
fileinfo.cpp
Generated by
1.17.0