aboutsummaryrefslogtreecommitdiffstats
path: root/vncviewer/parameters.cxx
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2024-11-07 10:37:53 +0100
committerPierre Ossman <ossman@cendio.se>2024-11-07 10:37:53 +0100
commitf7507aea98b1a428d02fe5c41d25ee69dd5436bb (patch)
tree49f9349a1d7441874d1cb6d4428e2bcb0d63b422 /vncviewer/parameters.cxx
parent7508e9887de022e127d8fadb9f6a6bd8e9778864 (diff)
parent2b7857283b834391266e414adcff8c20f8fe3067 (diff)
downloadtigervnc-f7507aea98b1a428d02fe5c41d25ee69dd5436bb.tar.gz
tigervnc-f7507aea98b1a428d02fe5c41d25ee69dd5436bb.zip
Merge branch 'stdexcept' of github.com:CendioOssman/tigervnc
Diffstat (limited to 'vncviewer/parameters.cxx')
-rw-r--r--vncviewer/parameters.cxx135
1 files changed, 72 insertions, 63 deletions
diff --git a/vncviewer/parameters.cxx b/vncviewer/parameters.cxx
index c75cad8b..a6229a34 100644
--- a/vncviewer/parameters.cxx
+++ b/vncviewer/parameters.cxx
@@ -33,7 +33,9 @@
#include "parameters.h"
#include <os/os.h>
-#include <rfb/Exception.h>
+
+#include <rdr/Exception.h>
+
#include <rfb/LogWriter.h>
#include <rfb/SecurityClient.h>
#include <rfb/util.h>
@@ -305,20 +307,20 @@ static void setKeyString(const char *_name, const char *_value, HKEY* hKey) {
wchar_t name[buffersize];
unsigned size = fl_utf8towc(_name, strlen(_name)+1, name, buffersize);
if (size >= buffersize)
- throw Exception(_("The name of the parameter is too large"));
+ throw std::invalid_argument(_("The name of the parameter is too large"));
char encodingBuffer[buffersize];
if (!encodeValue(_value, encodingBuffer, buffersize))
- throw Exception(_("The parameter is too large"));
+ throw std::invalid_argument(_("The parameter is too large"));
wchar_t value[buffersize];
size = fl_utf8towc(encodingBuffer, strlen(encodingBuffer)+1, value, buffersize);
if (size >= buffersize)
- throw Exception(_("The parameter is too large"));
+ throw std::invalid_argument(_("The parameter is too large"));
LONG res = RegSetValueExW(*hKey, name, 0, REG_SZ, (BYTE*)&value, (wcslen(value)+1)*2);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception("RegSetValueExW", res);
+ throw rdr::win32_error("RegSetValueExW", res);
}
@@ -330,11 +332,11 @@ static void setKeyInt(const char *_name, const int _value, HKEY* hKey) {
unsigned size = fl_utf8towc(_name, strlen(_name)+1, name, buffersize);
if (size >= buffersize)
- throw Exception(_("The name of the parameter is too large"));
+ throw std::out_of_range(_("The name of the parameter is too large"));
LONG res = RegSetValueExW(*hKey, name, 0, REG_DWORD, (BYTE*)&value, sizeof(DWORD));
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception("RegSetValueExW", res);
+ throw rdr::win32_error("RegSetValueExW", res);
}
@@ -347,7 +349,7 @@ static bool getKeyString(const char* _name, char* dest, size_t destSize, HKEY* h
unsigned size = fl_utf8towc(_name, strlen(_name)+1, name, buffersize);
if (size >= buffersize)
- throw Exception(_("The name of the parameter is too large"));
+ throw std::out_of_range(_("The name of the parameter is too large"));
value = new WCHAR[destSize];
valuesize = destSize;
@@ -355,7 +357,7 @@ static bool getKeyString(const char* _name, char* dest, size_t destSize, HKEY* h
if (res != ERROR_SUCCESS){
delete [] value;
if (res != ERROR_FILE_NOT_FOUND)
- throw rdr::Win32Exception("RegQueryValueExW", res);
+ throw rdr::win32_error("RegQueryValueExW", res);
// The value does not exist, defaults will be used.
return false;
}
@@ -365,14 +367,14 @@ static bool getKeyString(const char* _name, char* dest, size_t destSize, HKEY* h
delete [] value;
if (size >= destSize) {
delete [] utf8val;
- throw Exception(_("The parameter is too large"));
+ throw std::out_of_range(_("The parameter is too large"));
}
bool ret = decodeValue(utf8val, dest, destSize);
delete [] utf8val;
if (!ret)
- throw Exception(_("Invalid format or too large value"));
+ throw std::invalid_argument(_("Invalid format or too large value"));
return true;
}
@@ -387,12 +389,12 @@ static bool getKeyInt(const char* _name, int* dest, HKEY* hKey) {
unsigned size = fl_utf8towc(_name, strlen(_name)+1, name, buffersize);
if (size >= buffersize)
- throw Exception(_("The name of the parameter is too large"));
+ throw std::out_of_range(_("The name of the parameter is too large"));
LONG res = RegQueryValueExW(*hKey, name, nullptr, nullptr, (LPBYTE)&value, &dwordsize);
if (res != ERROR_SUCCESS){
if (res != ERROR_FILE_NOT_FOUND)
- throw rdr::Win32Exception("RegQueryValueExW", res);
+ throw rdr::win32_error("RegQueryValueExW", res);
// The value does not exist, defaults will be used.
return false;
}
@@ -407,12 +409,12 @@ static void removeValue(const char* _name, HKEY* hKey) {
unsigned size = fl_utf8towc(_name, strlen(_name)+1, name, buffersize);
if (size >= buffersize)
- throw Exception(_("The name of the parameter is too large"));
+ throw std::out_of_range(_("The name of the parameter is too large"));
LONG res = RegDeleteValueW(*hKey, name);
if (res != ERROR_SUCCESS) {
if (res != ERROR_FILE_NOT_FOUND)
- throw rdr::Win32Exception("RegDeleteValueW", res);
+ throw rdr::win32_error("RegDeleteValueW", res);
// The value does not exist, no need to remove it.
return;
}
@@ -426,7 +428,7 @@ void saveHistoryToRegKey(const list<string>& serverHistory) {
&hKey, nullptr);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception(_("Failed to create registry key"), res);
+ throw rdr::win32_error(_("Failed to create registry key"), res);
unsigned index = 0;
assert(SERVER_HISTORY_SIZE < 100);
@@ -440,14 +442,14 @@ void saveHistoryToRegKey(const list<string>& serverHistory) {
setKeyString(indexString, entry.c_str(), &hKey);
index++;
}
- } catch (Exception& e) {
+ } catch (std::exception& e) {
RegCloseKey(hKey);
throw;
}
res = RegCloseKey(hKey);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception(_("Failed to close registry key"), res);
+ throw rdr::win32_error(_("Failed to close registry key"), res);
}
static void saveToReg(const char* servername) {
@@ -459,14 +461,14 @@ static void saveToReg(const char* servername) {
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr,
&hKey, nullptr);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception(_("Failed to create registry key"), res);
+ throw rdr::win32_error(_("Failed to create registry key"), res);
try {
setKeyString("ServerName", servername, &hKey);
- } catch (Exception& e) {
+ } catch (std::exception& e) {
RegCloseKey(hKey);
- throw Exception(_("Failed to save \"%s\": %s"),
- "ServerName", e.str());
+ throw std::runtime_error(format(_("Failed to save \"%s\": %s"),
+ "ServerName", e.what()));
}
for (size_t i = 0; i < sizeof(parameterArray)/sizeof(VoidParameter*); i++) {
@@ -478,12 +480,13 @@ static void saveToReg(const char* servername) {
} else if (dynamic_cast<BoolParameter*>(parameterArray[i]) != nullptr) {
setKeyInt(parameterArray[i]->getName(), (int)*(BoolParameter*)parameterArray[i], &hKey);
} else {
- throw Exception(_("Unknown parameter type"));
+ throw std::logic_error(_("Unknown parameter type"));
}
- } catch (Exception& e) {
+ } catch (std::exception& e) {
RegCloseKey(hKey);
- throw Exception(_("Failed to save \"%s\": %s"),
- parameterArray[i]->getName(), e.str());
+ throw std::runtime_error(format(_("Failed to save \"%s\": %s"),
+ parameterArray[i]->getName(),
+ e.what()));
}
}
@@ -493,16 +496,17 @@ static void saveToReg(const char* servername) {
for (size_t i = 0; i < sizeof(readOnlyParameterArray)/sizeof(VoidParameter*); i++) {
try {
removeValue(readOnlyParameterArray[i]->getName(), &hKey);
- } catch (Exception& e) {
+ } catch (std::exception& e) {
RegCloseKey(hKey);
- throw Exception(_("Failed to remove \"%s\": %s"),
- readOnlyParameterArray[i]->getName(), e.str());
+ throw std::runtime_error(format(_("Failed to remove \"%s\": %s"),
+ readOnlyParameterArray[i]->getName(),
+ e.what()));
}
}
res = RegCloseKey(hKey);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception(_("Failed to close registry key"), res);
+ throw rdr::win32_error(_("Failed to close registry key"), res);
}
list<string> loadHistoryFromRegKey() {
@@ -518,7 +522,7 @@ list<string> loadHistoryFromRegKey() {
return serverHistory;
}
- throw rdr::Win32Exception(_("Failed to open registry key"), res);
+ throw rdr::win32_error(_("Failed to open registry key"), res);
}
unsigned index;
@@ -533,10 +537,10 @@ list<string> loadHistoryFromRegKey() {
if (!getKeyString(indexString, servernameBuffer,
buffersize, &hKey))
break;
- } catch (Exception& e) {
+ } catch (std::exception& e) {
// Just ignore this entry and try the next one
vlog.error(_("Failed to read server history entry %d: %s"),
- (int)index, e.str());
+ (int)index, e.what());
continue;
}
@@ -545,7 +549,7 @@ list<string> loadHistoryFromRegKey() {
res = RegCloseKey(hKey);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception(_("Failed to close registry key"), res);
+ throw rdr::win32_error(_("Failed to close registry key"), res);
return serverHistory;
}
@@ -569,12 +573,12 @@ static void getParametersFromReg(VoidParameter* parameters[],
if (getKeyInt(parameters[i]->getName(), &intValue, hKey))
((BoolParameter*)parameters[i])->setParam(intValue);
} else {
- throw Exception(_("Unknown parameter type"));
+ throw std::logic_error(_("Unknown parameter type"));
}
- } catch(Exception& e) {
+ } catch(std::exception& e) {
// Just ignore this entry and continue with the rest
vlog.error(_("Failed to read parameter \"%s\": %s"),
- parameters[i]->getName(), e.str());
+ parameters[i]->getName(), e.what());
}
}
}
@@ -592,7 +596,7 @@ static char* loadFromReg() {
return nullptr;
}
- throw rdr::Win32Exception(_("Failed to open registry key"), res);
+ throw rdr::win32_error(_("Failed to open registry key"), res);
}
const size_t buffersize = 256;
@@ -602,9 +606,9 @@ static char* loadFromReg() {
try {
if (getKeyString("ServerName", servernameBuffer, buffersize, &hKey))
snprintf(servername, buffersize, "%s", servernameBuffer);
- } catch(Exception& e) {
+ } catch(std::exception& e) {
vlog.error(_("Failed to read parameter \"%s\": %s"),
- "ServerName", e.str());
+ "ServerName", e.what());
strcpy(servername, "");
}
@@ -614,7 +618,7 @@ static char* loadFromReg() {
res = RegCloseKey(hKey);
if (res != ERROR_SUCCESS)
- throw rdr::Win32Exception(_("Failed to close registry key"), res);
+ throw rdr::win32_error(_("Failed to close registry key"), res);
return servername;
}
@@ -637,7 +641,7 @@ void saveViewerParameters(const char *filename, const char *servername) {
const char* configDir = os::getvncconfigdir();
if (configDir == nullptr)
- throw Exception(_("Could not determine VNC config directory path"));
+ throw std::runtime_error(_("Could not determine VNC config directory path"));
snprintf(filepath, sizeof(filepath), "%s/default.tigervnc", configDir);
} else {
@@ -648,7 +652,7 @@ void saveViewerParameters(const char *filename, const char *servername) {
FILE* f = fopen(filepath, "w+");
if (!f) {
std::string msg = format(_("Could not open \"%s\""), filepath);
- throw rdr::PosixException(msg.c_str(), errno);
+ throw rdr::posix_error(msg.c_str(), errno);
}
fprintf(f, "%s\n", IDENTIFIER_STRING);
@@ -656,8 +660,9 @@ void saveViewerParameters(const char *filename, const char *servername) {
if (!encodeValue(servername, encodingBuffer, buffersize)) {
fclose(f);
- throw Exception(_("Failed to save \"%s\": %s"),
- "ServerName", _("Could not encode parameter"));
+ throw std::runtime_error(format(_("Failed to save \"%s\": %s"),
+ "ServerName",
+ _("Could not encode parameter")));
}
fprintf(f, "ServerName=%s\n", encodingBuffer);
@@ -666,9 +671,9 @@ void saveViewerParameters(const char *filename, const char *servername) {
if (!encodeValue(*(StringParameter*)param,
encodingBuffer, buffersize)) {
fclose(f);
- throw Exception(_("Failed to save \"%s\": %s"),
- param->getName(),
- _("Could not encode parameter"));
+ throw std::runtime_error(format(_("Failed to save \"%s\": %s"),
+ param->getName(),
+ _("Could not encode parameter")));
}
fprintf(f, "%s=%s\n", ((StringParameter*)param)->getName(), encodingBuffer);
} else if (dynamic_cast<IntParameter*>(param) != nullptr) {
@@ -677,9 +682,9 @@ void saveViewerParameters(const char *filename, const char *servername) {
fprintf(f, "%s=%d\n", ((BoolParameter*)param)->getName(), (int)*(BoolParameter*)param);
} else {
fclose(f);
- throw Exception(_("Failed to save \"%s\": %s"),
- param->getName(),
- _("Unknown parameter type"));
+ throw std::logic_error(format(_("Failed to save \"%s\": %s"),
+ param->getName(),
+ _("Unknown parameter type")));
}
}
fclose(f);
@@ -698,7 +703,7 @@ static bool findAndSetViewerParameterFromValue(
if (dynamic_cast<StringParameter*>(parameters[i]) != nullptr) {
if (strcasecmp(line, ((StringParameter*)parameters[i])->getName()) == 0) {
if(!decodeValue(value, decodingBuffer, sizeof(decodingBuffer)))
- throw Exception(_("Invalid format or too large value"));
+ throw std::runtime_error(_("Invalid format or too large value"));
((StringParameter*)parameters[i])->setParam(decodingBuffer);
return false;
}
@@ -716,7 +721,7 @@ static bool findAndSetViewerParameterFromValue(
}
} else {
- throw Exception(_("Unknown parameter type"));
+ throw std::logic_error(_("Unknown parameter type"));
}
}
@@ -742,7 +747,7 @@ char* loadViewerParameters(const char *filename) {
const char* configDir = os::getvncconfigdir();
if (configDir == nullptr)
- throw Exception(_("Could not determine VNC config directory path"));
+ throw std::runtime_error(_("Could not determine VNC config directory path"));
snprintf(filepath, sizeof(filepath), "%s/default.tigervnc", configDir);
} else {
@@ -755,7 +760,7 @@ char* loadViewerParameters(const char *filename) {
if (!filename)
return nullptr; // Use defaults.
std::string msg = format(_("Could not open \"%s\""), filepath);
- throw rdr::PosixException(msg.c_str(), errno);
+ throw rdr::posix_error(msg.c_str(), errno);
}
int lineNr = 0;
@@ -770,13 +775,16 @@ char* loadViewerParameters(const char *filename) {
fclose(f);
std::string msg = format(_("Failed to read line %d in "
"file \"%s\""), lineNr, filepath);
- throw rdr::PosixException(msg.c_str(), errno);
+ throw rdr::posix_error(msg.c_str(), errno);
}
if (strlen(line) == (sizeof(line) - 1)) {
fclose(f);
- throw Exception(_("Failed to read line %d in file %s: %s"),
- lineNr, filepath, _("Line too long"));
+ throw std::runtime_error(format("%s: %s",
+ format(_("Failed to read line %d "
+ "in file \"%s\""),
+ lineNr, filepath).c_str(),
+ _("Line too long")));
}
// Make sure that the first line of the file has the file identifier string
@@ -785,8 +793,9 @@ char* loadViewerParameters(const char *filename) {
continue;
fclose(f);
- throw Exception(_("Configuration file %s is in an invalid format"),
- filepath);
+ throw std::runtime_error(format(_("Configuration file %s is in "
+ "an invalid format"),
+ filepath));
}
// Skip empty lines and comments
@@ -820,7 +829,7 @@ char* loadViewerParameters(const char *filename) {
if (strcasecmp(line, "ServerName") == 0) {
if(!decodeValue(value, decodingBuffer, sizeof(decodingBuffer)))
- throw Exception(_("Invalid format or too large value"));
+ throw std::runtime_error(_("Invalid format or too large value"));
snprintf(servername, sizeof(decodingBuffer), "%s", decodingBuffer);
invalidParameterName = false;
@@ -833,10 +842,10 @@ char* loadViewerParameters(const char *filename) {
value, line);
}
}
- } catch(Exception& e) {
+ } catch(std::exception& e) {
// Just ignore this entry and continue with the rest
vlog.error(_("Failed to read line %d in file %s: %s"),
- lineNr, filepath, e.str());
+ lineNr, filepath, e.what());
continue;
}