-
Notifications
You must be signed in to change notification settings - Fork 3
/
native.cpp
107 lines (96 loc) · 2.99 KB
/
native.cpp
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
#include <string>
#include <stdexcept>
#if defined(__linux__) || defined(__APPLE__)
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
namespace scpak
{
extern const char pathsep = '/';
void createDirectory(const char *path)
{
int result = mkdir(path, 0777);
if (result == -1)
throw std::runtime_error("failed to create directory: " + std::string(path));
}
bool pathExists(const char *path)
{
return access(path, F_OK) == 0;
}
int getFileSize(const char *path)
{
unsigned long filesize = -1;
struct stat statbuf;
if (stat(path, &statbuf) < 0)
throw std::runtime_error("failed to get call stat: " + std::string(path));
filesize = statbuf.st_size;
return static_cast<int>(filesize);
}
bool isDirectory(const char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) < 0)
throw std::runtime_error("failed to get call stat: " + std::string(path));
return S_ISDIR(statbuf.st_mode);
}
bool isNormalFile(const char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) < 0)
throw std::runtime_error("failed to get call stat: " + std::string(path));
return S_ISREG(statbuf.st_mode);
}
}
#elif defined(_WIN32)
# include <windows.h>
namespace scpak
{
extern const char pathsep = '\\';
void createDirectory(const char *path)
{
int result = CreateDirectoryA(path, NULL);
if (result == 0)
throw std::runtime_error("failed to get file size: " + std::string(path));
}
bool pathExists(const char *path)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFileA(path, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return false;
else
{
FindClose(hFind);
return true;
}
}
int getFileSize(const char *path)
{
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
hFind = FindFirstFileA(path, &fileInfo);
if (hFind == INVALID_HANDLE_VALUE)
throw std::runtime_error("failed to get file size: " + std::string(path));
int size = fileInfo.nFileSizeLow;
FindClose(hFind);
return size;
}
bool isDirectory(const char *path)
{
DWORD attributes = GetFileAttributesA(path);
if (attributes == INVALID_FILE_ATTRIBUTES)
throw std::runtime_error("failed to get file attribute: " + std::string(path));
return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
bool isNormalFile(const char *path)
{
DWORD attributes = GetFileAttributesA(path);
if (attributes == INVALID_FILE_ATTRIBUTES)
throw std::runtime_error("failed to get file attribute: " + std::string(path));
return (attributes & FILE_ATTRIBUTE_NORMAL) != 0 || (attributes & FILE_ATTRIBUTE_ARCHIVE) != 0;
}
}
#else
# error scpak: Not a supported platform.
#endif