forked from vencabkk/opencl-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
executable file
·119 lines (97 loc) · 2.68 KB
/
Utils.cpp
File metadata and controls
executable file
·119 lines (97 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// Created by Vaclav Samec on 5/7/15 AD.
// Copyright (c) 2015 Venca. All rights reserved.
//
#include "Utils.h"
#include "Profiler.h"
#include <sys/types.h>
#if defined(WIN32)
#include <direct.h>
#include <Windows.h>
#elif defined(__APPLE__) || defined(__MACOSX)
#include <dirent.h>
#include <mach-o/dyld.h>
#include <sys/stat.h>
#endif
std::unordered_map<std::string, clock_t> Profiler::m_clocks;
bool Utils::isDirectory(const std::string& path)
{
struct stat info{};
if (stat(path.c_str(), &info) != 0)
{
return false;
}
return (info.st_mode & S_IFDIR) != 0;
}
bool Utils::createDirectory(const std::string& path)
{
#if defined(__APPLE__) || defined(__MACOSX)
return mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
#else
return _mkdir(path.c_str()) == 0;
#endif
}
void Utils::getFilesDir(const std::string& path, std::vector<std::string>& files, const std::string& mask)
{
files.reserve(64);
#if defined(__APPLE__) || defined(__MACOSX)
DIR* dirFile = opendir( path.c_str());
if ( dirFile )
{
struct dirent* hFile;
errno = 0;
while (( hFile = readdir( dirFile )) != nullptr )
{
if ( !strcmp( hFile->d_name, "." )) continue;
if ( !strcmp( hFile->d_name, ".." )) continue;
if ( hFile->d_name[0] == '.' ) continue;
if (strstr( hFile->d_name, mask.c_str()))
{
files.push_back(path + "/" + hFile->d_name);
}
}
closedir( dirFile );
}
#elif defined(WIN32)
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((path + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const std::string file_name = file_data.cFileName;
const std::string full_file_name = path + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
if (file_name.rfind(mask) != std::string::npos)
{
files.push_back(full_file_name);
}
} while (FindNextFile(dir, &file_data));
FindClose(dir);
#endif
}
std::string Utils::getExeDir()
{
#if defined(WIN32)
HMODULE hModule = GetModuleHandleW(NULL);
char path[MAX_PATH] = {0};
GetModuleFileName(hModule, path, MAX_PATH);
#elif defined(__APPLE__) || defined(__MACOSX)
char path[1024] = {0};
uint32_t size = sizeof(path);
_NSGetExecutablePath(path, &size);
#endif
std::string filename = path;
if (!filename.empty())
{
const size_t last_slash_idx = filename.find_last_of("/\\");
if (std::string::npos != last_slash_idx)
{
return filename.substr(0, last_slash_idx);
}
}
return std::string();
}