This repository has been archived by the owner on Jan 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgvs_system.cpp
172 lines (129 loc) · 3.54 KB
/
lgvs_system.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
#include "lgvs_system.h"
#include <algorithm>
#include <windows.h>
#include <shlwapi.h>
namespace lgvs {
// (static)
std::wstring System::get_current_dir()
{
const DWORD MAX_BUFFER_SIZE = MAX_PATH;
WCHAR buffer[MAX_BUFFER_SIZE];
DWORD api_result = 0;
api_result = ::GetCurrentDirectoryW(MAX_BUFFER_SIZE, buffer);
if (api_result > 0 && api_result < MAX_BUFFER_SIZE)
return std::wstring(buffer, api_result);
return std::wstring();
}
// (static)
std::wstring System::get_system_dir()
{
const DWORD MAX_BUFFER_SIZE = MAX_PATH;
WCHAR buffer[MAX_BUFFER_SIZE];
DWORD api_result = 0;
api_result = ::GetSystemDirectoryW(buffer, MAX_BUFFER_SIZE);
if (api_result > 0 && api_result < MAX_BUFFER_SIZE)
return std::wstring(buffer, api_result);
return std::wstring();
}
// (static)
bool System::is_file_exists(const std::wstring& path)
{
bool result = false;
WIN32_FIND_DATAW wfd;
HANDLE fh = INVALID_HANDLE_VALUE;
fh = ::FindFirstFileW(path.c_str(), &wfd);
if (fh != INVALID_HANDLE_VALUE) {
result = true;
::FindClose(fh);
}
return result;
}
// (static)
std::wstring System::combine_paths(
const std::wstring& path1,
const std::wstring& path2)
{
if (path1.empty())
return path2;
if (path2.empty())
return path1;
std::wstring result;
result.reserve(path1.size() + path2.size() + 1);
result = path1;
if (!is_path_separator (result[result.size() - 1]))
result += L'\\';
result += path2;
class SlashPredicate {
public:
static bool is_slash(wchar_t ch)
{
return ch == L'/';
}
}; // class SlashPredicate
std::replace_if(result.begin(), result.end(),
SlashPredicate::is_slash, L'\\');
return result;
}
// (static)
std::wstring System::combine_paths(
const std::wstring& path1,
const std::wstring& path2,
const std::wstring& path3)
{
return combine_paths(combine_paths(path1, path2), path3);
}
// (static)
bool System::is_path_separator(wchar_t ch)
{
return ch == L'\\' || ch == L'/';
}
// (static)
std::wstring System::extract_file_name(const std::wstring& path)
{
auto pos = path.find_last_of(L"\\/");
if (pos != path.npos)
return path.substr(pos + 1);
return path;
}
// (static)
std::wstring System::change_file_extension(
const std::wstring& path,
const std::wstring& new_extension)
{
if (new_extension.size() <= 1)
return path;
auto pos = path.find_last_of(L'.');
if (pos != path.npos) {
std::wstring new_path(path);
new_path.erase(pos);
new_path += new_extension;
return new_path;
}
return path;
}
// (static)
size_t System::get_page_size()
{
static size_t result = 0;
if (result != 0)
return result;
SYSTEM_INFO si;
::GetSystemInfo(&si);
result = si.dwPageSize;
return result;
}
} // namespace lgvs