From e6e6eaaaf39a4f1fc0b614b08875ff60b87f5e40 Mon Sep 17 00:00:00 2001 From: Micke Prag Date: Sun, 2 Mar 2008 17:33:33 +0000 Subject: [PATCH] Added TelldusSettings::getStringSetting() and TelldusSettings::setStringSetting() in TelldusSettingsWinRegistry. With this the Windows and Linux port can reuse most of the logic. --- driver/TellUsbD101/TellUsbD101.cpp | 10 +- driver/TellUsbD101/TellUsbD101.def | 2 +- .../TellUsbD101/settings/TelldusSettings.cpp | 43 +++ .../settings/TelldusSettingsConfuse.cpp | 47 ---- .../settings/TelldusSettingsWinRegistry.cpp | 254 ++++-------------- 5 files changed, 107 insertions(+), 249 deletions(-) diff --git a/driver/TellUsbD101/TellUsbD101.cpp b/driver/TellUsbD101/TellUsbD101.cpp index e9aa95d3..3ec1ffeb 100644 --- a/driver/TellUsbD101/TellUsbD101.cpp +++ b/driver/TellUsbD101/TellUsbD101.cpp @@ -218,9 +218,9 @@ bool WINAPI devSetModel(int intDeviceId, char* strNewModel){ return blnSuccess; } -/*bool WINAPI devSetArguments(int intDeviceId, char* strArguments){ - - vector vArguments; +bool WINAPI devSetArguments(int intDeviceId, char* strArguments){ + return false; +/* vector vArguments; //int intArguments[] = new int[]; //bort? try{ char* strTemp = strtok(strArguments, ","); @@ -235,8 +235,8 @@ bool WINAPI devSetModel(int intDeviceId, char* strNewModel){ catch(exception e){ handleException(e); return false; - } -}*/ + }*/ +} int WINAPI devGetArgument(int intDeviceId, int intArgumentIndex){ int intReturn; diff --git a/driver/TellUsbD101/TellUsbD101.def b/driver/TellUsbD101/TellUsbD101.def index cb3d80b9..fc32ffca 100644 --- a/driver/TellUsbD101/TellUsbD101.def +++ b/driver/TellUsbD101/TellUsbD101.def @@ -12,7 +12,7 @@ EXPORTS devSetName @8 devSetVendor @9 devSetModel @10 -; devSetArguments @11 + devSetArguments @11 devAddDevice @12 devRemoveDevice @13 diff --git a/driver/TellUsbD101/settings/TelldusSettings.cpp b/driver/TellUsbD101/settings/TelldusSettings.cpp index f5d81e49..94e43a47 100644 --- a/driver/TellUsbD101/settings/TelldusSettings.cpp +++ b/driver/TellUsbD101/settings/TelldusSettings.cpp @@ -34,3 +34,46 @@ Device* TelldusSettings::getDevice(int intDeviceId, int intDongleIndex){ } return dev; } + +/* +* Get the name of the device +*/ +char* TelldusSettings::getName(int intDeviceId){ + return getStringSetting(intDeviceId, "name"); +} + +/* +* Set the name of the device +*/ +bool TelldusSettings::setName(int intDeviceId, char* strNewName){ + return setStringSetting(intDeviceId, "name", strNewName); +} + + +/* +* Get the device vendor +*/ +char* TelldusSettings::getVendor(int intDeviceId){ + return getStringSetting(intDeviceId, "vendor"); +} + +/* +* Set the device vendor +*/ +bool TelldusSettings::setVendor(int intDeviceId, char* strVendor){ + return setStringSetting(intDeviceId, "vendor", strVendor); +} + +/* +* Get the device model +*/ +char* TelldusSettings::getModel(int intDeviceId){ + return getStringSetting(intDeviceId, "model"); +} + +/* +* Set the device model +*/ +bool TelldusSettings::setModel(int intDeviceId, char* strModel){ + return setStringSetting(intDeviceId, "model", strModel); +} diff --git a/driver/TellUsbD101/settings/TelldusSettingsConfuse.cpp b/driver/TellUsbD101/settings/TelldusSettingsConfuse.cpp index 676ddccb..3e6c7ce3 100644 --- a/driver/TellUsbD101/settings/TelldusSettingsConfuse.cpp +++ b/driver/TellUsbD101/settings/TelldusSettingsConfuse.cpp @@ -60,53 +60,6 @@ Device* TelldusSettings::getDevice(int intDeviceId){ return NULL; } -/* -* Get the name of the device -*/ -char* TelldusSettings::getName(int intDeviceId){ - return getStringSetting(intDeviceId, "name"); -} - -/* -* Set the name of the device -*/ -bool TelldusSettings::setName(int intDeviceId, char* strNewName){ - bool blnSuccess = true; - setStringSetting(intDeviceId, "name", strNewName); - return blnSuccess; -} - -/* -* Get the device vendor -*/ -char* TelldusSettings::getVendor(int intDeviceId){ - return getStringSetting(intDeviceId, "vendor"); -} - -/* -* Set the device vendor -*/ -bool TelldusSettings::setVendor(int intDeviceId, char* strVendor){ - bool blnSuccess = true; - setStringSetting(intDeviceId, "vendor", strVendor); - return blnSuccess; -} - -/* -* Get the device model -*/ -char* TelldusSettings::getModel(int intDeviceId){ - return getStringSetting(intDeviceId, "model"); -} - -/* -* Set the device model -*/ -bool TelldusSettings::setModel(int intDeviceId, char* strVendor){ - bool blnSuccess = true; - setStringSetting(intDeviceId, "model", strVendor); - return blnSuccess; -} int TelldusSettings::getDeviceId(int intDeviceIndex){ if (intDeviceIndex >= getNumberOfDevices()) { //Out of bounds diff --git a/driver/TellUsbD101/settings/TelldusSettingsWinRegistry.cpp b/driver/TellUsbD101/settings/TelldusSettingsWinRegistry.cpp index d4b4aca7..6644b8cb 100644 --- a/driver/TellUsbD101/settings/TelldusSettingsWinRegistry.cpp +++ b/driver/TellUsbD101/settings/TelldusSettingsWinRegistry.cpp @@ -90,202 +90,6 @@ Device* TelldusSettings::getDevice(int intDeviceId){ } } -/* -* Get the name of the device -*/ -char* TelldusSettings::getName(int intDeviceId){ - - char* strReturn = ""; - - try{ - std::ostringstream ssRegPath; - ssRegPath << d->strRegPathDevice << intDeviceId; - string strCompleteRegPath = ssRegPath.str(); - long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &d->hk); - - if(lnExists == ERROR_SUCCESS){ - DWORD dwLength; - char* Buff = new char[d->intMaxRegValueLength]; - - long lngStatus = RegQueryValueEx(d->hk, "Name", NULL, NULL, (LPBYTE)Buff, &dwLength); - if(lngStatus == ERROR_MORE_DATA){ - Buff = new char[dwLength]; - lngStatus = RegQueryValueEx(d->hk, "Name", NULL, NULL, (LPBYTE)Buff, &dwLength); - } - strReturn = Buff; - } - else{ - throw exception(); //couldn't open reg key - } - RegCloseKey(d->hk); - } - catch(...){ - strReturn = ""; - } - return strReturn; -} - -/* -* Set the name of the device -*/ -bool TelldusSettings::setName(int intDeviceId, char* strNewName){ - - bool blnSuccess = true; - try{ - - std::ostringstream ssRegPath; - ssRegPath << d->strRegPathDevice << intDeviceId; - string strCompleteRegPath = ssRegPath.str(); - long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_WRITE, &d->hk); - - if(lnExists == ERROR_SUCCESS){ - d->intMaxRegValueLength = (int)strlen(strNewName); - RegSetValueEx(d->hk, "Name", 0, REG_SZ, (LPBYTE)strNewName, d->intMaxRegValueLength); - } - else{ - throw exception(); //couldn't open reg key - } - RegCloseKey(d->hk); - - } - catch(...){ - blnSuccess = false; - } - return blnSuccess; -} - -/* -* Get the device vendor -*/ -char* TelldusSettings::getVendor(int intDeviceId){ - - char* strReturn = ""; - - try{ - std::ostringstream ssRegPath; - ssRegPath << d->strRegPathDevice << intDeviceId; - string strCompleteRegPath = ssRegPath.str(); - long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, (LPCSTR)strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &d->hk); - - if(lnExists == ERROR_SUCCESS){ - DWORD dwLength; - char* Buff = new char[d->intMaxRegValueLength]; - - long lngStatus = RegQueryValueEx(d->hk, (LPCSTR)"Vendor", NULL, NULL, (LPBYTE)Buff, &dwLength); - if(lngStatus == ERROR_MORE_DATA){ - Buff = new char[dwLength]; - lngStatus = RegQueryValueEx(d->hk, (LPCSTR)"Vendor", NULL, NULL, (LPBYTE)Buff, &dwLength); - } - - strReturn = Buff; - } - else{ - throw exception(); //couldn't open reg key - } - RegCloseKey(d->hk); - } - catch(exception e){ - strReturn = ""; - ofstream errorfile("c:\\errorlog.txt", ios::app); - if(errorfile){ - errorfile << e.what() << endl; - errorfile.close(); - } - } - return strReturn; -} - -/* -* Set the device vendor -*/ -bool TelldusSettings::setVendor(int intDeviceId, char* strVendor){ - - bool blnSuccess = true; - try{ - - std::ostringstream ssRegPath; - ssRegPath << d->strRegPathDevice << intDeviceId; - string strCompleteRegPath = ssRegPath.str(); - long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_WRITE, &d->hk); - - if(lnExists == ERROR_SUCCESS){ - d->intMaxRegValueLength = (int)strlen(strVendor); - RegSetValueEx(d->hk, "Vendor", 0, REG_SZ, (LPBYTE)strVendor, d->intMaxRegValueLength); - } - else{ - throw exception(); //couldn't open reg key - } - RegCloseKey(d->hk); - } - catch(...){ - blnSuccess = false; - } - return blnSuccess; -} - -/* -* Get the device model -*/ -char* TelldusSettings::getModel(int intDeviceId){ - - char* strReturn = ""; - - try{ - std::ostringstream ssRegPath; - ssRegPath << d->strRegPathDevice << intDeviceId; - string strCompleteRegPath = ssRegPath.str(); - long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &d->hk); - - if(lnExists == ERROR_SUCCESS){ - DWORD dwLength; - char* Buff = new char[d->intMaxRegValueLength]; - - long lngStatus = RegQueryValueEx(d->hk, "Model", NULL, NULL, (LPBYTE)Buff, &dwLength); - if(lngStatus == ERROR_MORE_DATA){ - - Buff = new char[dwLength]; - lngStatus = RegQueryValueEx(d->hk, "Model", NULL, NULL, (LPBYTE)Buff, &dwLength); - } - strReturn = Buff; - } - else{ - throw exception(); //couldn't open reg key - } - RegCloseKey(d->hk); - } - catch(...){ - strReturn = ""; - } - return strReturn; -} - -/* -* Set the device model -*/ -bool TelldusSettings::setModel(int intDeviceId, char* strVendor){ - - bool blnSuccess = true; - try{ - - std::ostringstream ssRegPath; - ssRegPath << d->strRegPathDevice << intDeviceId; - string strCompleteRegPath = ssRegPath.str(); - long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_WRITE, &d->hk); - - if(lnExists == ERROR_SUCCESS){ - d->intMaxRegValueLength = (int)strlen(strVendor); - RegSetValueEx(d->hk, "Model", 0, REG_SZ, (LPBYTE)strVendor, d->intMaxRegValueLength); - } - else{ - throw exception(); //couldn't open reg key - } - RegCloseKey(d->hk); - } - catch(...){ - blnSuccess = false; - } - return blnSuccess; -} int TelldusSettings::getDeviceId(int intDeviceIndex){ int intReturn = -1; @@ -586,6 +390,64 @@ bool TelldusSettings::removeDevice(int intDeviceId){ return blnSuccess; } +char *TelldusSettings::getStringSetting(int intDeviceId, const char* name) { + char* strReturn = ""; + + try{ + std::ostringstream ssRegPath; + ssRegPath << d->strRegPathDevice << intDeviceId; + string strCompleteRegPath = ssRegPath.str(); + long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &d->hk); + + if(lnExists == ERROR_SUCCESS){ + DWORD dwLength; + char* Buff = new char[d->intMaxRegValueLength]; + + long lngStatus = RegQueryValueEx(d->hk, name, NULL, NULL, (LPBYTE)Buff, &dwLength); + if(lngStatus == ERROR_MORE_DATA){ + Buff = new char[dwLength]; + lngStatus = RegQueryValueEx(d->hk, name, NULL, NULL, (LPBYTE)Buff, &dwLength); + } + strReturn = Buff; + } + else{ + throw exception(); //couldn't open reg key + } + RegCloseKey(d->hk); + } + catch(...){ + strReturn = ""; + } + return strReturn; +} + +bool TelldusSettings::setStringSetting(int intDeviceId, const char* name, const char *value) { + + bool blnSuccess = true; + try{ + + std::ostringstream ssRegPath; + ssRegPath << d->strRegPathDevice << intDeviceId; + string strCompleteRegPath = ssRegPath.str(); + long lnExists = RegOpenKeyEx(HKEY_CURRENT_USER, strCompleteRegPath.c_str(), 0, KEY_WRITE, &d->hk); + + if(lnExists == ERROR_SUCCESS){ + d->intMaxRegValueLength = (int)strlen(value); + RegSetValueEx(d->hk, name, 0, REG_SZ, (LPBYTE)value, d->intMaxRegValueLength); + } + else{ + throw exception(); //couldn't open reg key + } + RegCloseKey(d->hk); + + } + catch(...){ + blnSuccess = false; + } + return blnSuccess; + +} + //only for debug reasons void TelldusSettings::debugLog(char* debugstring){ ofstream debugfile("c:\\telldusdebug.txt", ios::app);