Skip to content

Commit fa7f796

Browse files
committed
Refactor file system functions && Add isDirPath function
1 parent 80541ab commit fa7f796

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

libraries/libim/common.h

+29-6
Original file line numberDiff line numberDiff line change
@@ -216,30 +216,53 @@ namespace libim {
216216
return svpext == ext;
217217
}
218218

219-
inline bool isFilePath(const std::string& path)
219+
inline bool isFilePath(const std::filesystem::path& path, OptionalRef<std::error_code> ec = std::nullopt)
220220
{
221-
return path.find_last_of(".") != std::string::npos;
221+
using namespace std::filesystem;
222+
if (ec) {
223+
return is_regular_file(path, *ec) || path.has_extension();
224+
}
225+
return is_regular_file(path) || path.has_extension();
226+
}
227+
228+
inline bool isFilePath(const std::string& path, OptionalRef<std::error_code> ec)
229+
{
230+
return isFilePath(std::filesystem::path(path), ec);
222231
}
223232

224233
inline bool fileExists(const std::filesystem::path& filePath)
225234
{
226-
if(filePath.empty()) {
235+
if(filePath.empty() || !isFilePath(filePath)) {
227236
return false;
228237
}
229238

230239
return std::filesystem::exists(filePath);
231240
}
232241

242+
inline bool isDirPath(const std::filesystem::path& path, OptionalRef<std::error_code> ec = std::nullopt)
243+
{
244+
using namespace std::filesystem;
245+
if (ec) {
246+
return is_directory(path, *ec) || !isFilePath(path);
247+
}
248+
return is_directory(path) || !isFilePath(path);
249+
}
250+
251+
inline bool isDirPath(const std::string& path, OptionalRef<std::error_code> ec)
252+
{
253+
return isDirPath(std::filesystem::path(path), ec);
254+
}
255+
233256
inline bool dirExists(const std::filesystem::path& dirPath)
234257
{
235-
if(dirPath.empty()) {
258+
if(dirPath.empty() || isFilePath(dirPath)) {
236259
return false;
237260
}
238261
else if(!isNativePath(dirPath.string())) {
239262
return dirExists(getNativePath(dirPath.string()));
240263
}
241264

242-
return std::filesystem::exists(dirPath);
265+
return std::filesystem::exists(dirPath);
243266
}
244267

245268
inline bool makeDir(const std::filesystem::path& dirName)
@@ -298,7 +321,7 @@ namespace libim {
298321
return removeFile(file, ec);
299322
}
300323

301-
inline bool renameFile(const std::filesystem::path& from, const std::filesystem::path&& to, bool override = true)
324+
inline bool renameFile(const std::filesystem::path& from, const std::filesystem::path& to, bool override = true)
302325
{
303326
if(fileExists(to) && !override) {
304327
return false;

0 commit comments

Comments
 (0)