-
Notifications
You must be signed in to change notification settings - Fork 31
/
Utils.cpp
executable file
·163 lines (156 loc) · 4.42 KB
/
Utils.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
/*
* RDPShare: Windows Desktop Sharing remote control interface
*
* Copyright 2016 Simul Piscator
*
* RDPShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RDPShare 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RDPShare. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Utils.h"
#include <sstream>
#include <Shlwapi.h>
std::string
StringReplace(const std::string& what, const std::string& with, const std::string& in)
{
std::string s = in;
for (size_t pos = s.npos; (pos = s.find(what)) != s.npos; )
s = s.substr(0, pos) + with + s.substr(pos + what.length());
return s;
}
std::string
GetErrorText(DWORD err)
{
DWORD winerr = err;
if (HRESULT_FACILITY(err) == FACILITY_WIN32)
winerr = HRESULT_CODE(err);
else if (err & 0xffff0000)
winerr = 0;
std::ostringstream oss;
oss << "Error 0x" << std::hex << err;
std::string s = oss.str();
char* text = nullptr;
if (winerr)
::FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, winerr, 0, (LPSTR)&text, 0, 0
);
if (text)
{
s.append(":\n").append(text);
::HeapFree(::GetProcessHeap(), 0, text);
}
return s;
}
std::string GetProgramName()
{
char buf1[MAX_PATH + 1];
::GetModuleFileNameA(NULL, buf1, sizeof(buf1));
int length = ::GetLongPathNameA(buf1, nullptr, 0);
std::vector<char> buf2(length);
::GetLongPathNameA(buf1, buf2.data(), length);
const char* begin = ::PathFindFileNameA(buf2.data()), *end = ::PathFindExtensionA(begin);
return std::string(begin, end - begin);
}
bool
GetScreenRect(unsigned int screen, RECT* pRect)
{
if (screen == 0)
{
pRect->left = ::GetSystemMetrics(SM_XVIRTUALSCREEN);
pRect->top = ::GetSystemMetrics(SM_YVIRTUALSCREEN);
pRect->right = pRect->left + ::GetSystemMetrics(SM_CXVIRTUALSCREEN);
pRect->bottom = pRect->top + ::GetSystemMetrics(SM_CYVIRTUALSCREEN);
return true;
}
struct MonitorEnum
{
static BOOL CALLBACK Proc(HMONITOR, HDC, LPRECT pRect, LPARAM pData)
{
MonitorEnum* this_ = (MonitorEnum*)pData;
if (--this_->count == 0)
{
*this_->pRect = *pRect;
return FALSE;
}
return TRUE;
}
unsigned int count;
RECT* pRect;
} monitorEnum = { screen, pRect };
::EnumDisplayMonitors(NULL, nullptr, &MonitorEnum::Proc, (LPARAM)&monitorEnum);
return monitorEnum.count == 0;
}
int
SetScreenSize(unsigned int screen, int width, int height)
{
if (screen == 0)
return false;
struct MonitorEnum
{
static BOOL CALLBACK Proc(HMONITOR hMonitor, HDC, LPRECT, LPARAM pData)
{
MonitorEnum* this_ = (MonitorEnum*)pData;
if (--this_->count == 0)
{
MONITORINFOEXA info = {};
info.cbSize = sizeof(MONITORINFOEXA);
if (::GetMonitorInfoA(hMonitor, &info))
{
DEVMODEA mode = {};
mode.dmSize = sizeof(DEVMODEA);
if(::EnumDisplaySettingsA(info.szDevice, ENUM_CURRENT_SETTINGS, &mode))
{
mode.dmPelsWidth = this_->width;
mode.dmPelsHeight = this_->height;
this_->result = ::ChangeDisplaySettingsExA(info.szDevice, &mode, NULL, 0, NULL);
}
}
return FALSE;
}
return TRUE;
}
unsigned int count;
int width, height;
int result;
} monitorEnum = { screen, width, height, DISP_CHANGE_BADPARAM };
::EnumDisplayMonitors(NULL, nullptr, &MonitorEnum::Proc, (LPARAM)&monitorEnum);
return monitorEnum.result;
}
std::string UrlEscape_(const std::string& in, const std::string& allow)
{
std::string out;
for (auto c : in)
{
if (::isalnum(c) || allow.find(c) != allow.npos)
out += c;
else
{
out += '%';
std::ostringstream oss;
oss.width(2);
oss.fill('0');
oss << std::hex << int(c);
out += oss.str();
}
}
return out;
}
std::istream&
QuotedString::Unserialize( std::istream& is )
{
std::string& self = *static_cast<std::string*>(this);
is >> std::ws;
if (is.peek() == '"')
return std::getline(is.ignore(), self, '"');
return is >> self;
}