int Settings::setIntSetting(Node type, int intDeviceId, const std::wstring &name, int value, bool parameter) { // already locked if (d->cfg == 0) { return TELLSTICK_ERROR_PERMISSION_DENIED; } std::string strType = getNodeString(type); cfg_t *cfg_device; for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) { cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i); if (cfg_getint(cfg_device, "id") == intDeviceId) { if (parameter) { cfg_t *cfg_parameters = cfg_getsec(cfg_device, "parameters"); cfg_setint(cfg_parameters, TelldusCore::wideToString(name).c_str(), value); } else { cfg_setint(cfg_device, TelldusCore::wideToString(name).c_str(), value); } FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle if (!fp) { return TELLSTICK_ERROR_PERMISSION_DENIED; } cfg_print(d->cfg, fp); fclose(fp); return TELLSTICK_SUCCESS; } } return TELLSTICK_ERROR_DEVICE_NOT_FOUND; }
/* * Remove a device */ int Settings::removeNode(Node type, int intNodeId) { TelldusCore::MutexLocker locker(&mutex); FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle if (!fp) { return TELLSTICK_ERROR_PERMISSION_DENIED; } std::string strType = getNodeString(type); // Print all opts for(int i = 0; d->cfg->opts[i].name; i++) { // Check if it isn't a device section if (strcmp(d->cfg->opts[i].name, strType.c_str()) != 0) { cfg_opt_print(&d->cfg->opts[i], fp); } else { // Print all sections except the one to remove cfg_t *cfg_node; for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) { cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i); if (cfg_getint(cfg_node, "id") != intNodeId) { // This isn't the one to skip fprintf(fp, "%s {\n", strType.c_str()); cfg_print_indent(cfg_node, fp, 1); fprintf(fp, "}\n"); } } } } fclose(fp); // Re-read config-file cfg_free(d->cfg); readConfig(&d->cfg); return TELLSTICK_SUCCESS; }
/** * Fill the variable passed by reference with the value found at the specified path in the XML. * If the path does not exist in the XML, the variable is untouched. * @param path Path to retrieve. The path "a.b.c" corresponds to the xml structure \<a\>\<b\>\<c\>1\</c\>\</b\>\</a\> * @param ref Variable to fill the the value found at path. * @return true in case of success (path is found). Else false. */ bool XMLConfParser::getValue(std::string path, char *ref) { xmlNodePtr n = findPathNode(path); if(n){ std::string s = getNodeString(n); strcpy(ref, s.data()); fReadSuccess++; return true; } return false; }
/** * Fill the variable passed by reference with the value found at the specified path in the XML. * If the path does not exist in the XML, the variable is untouched. * @param path Path to retrieve. The path "a.b.c" corresponds to the xml structure \<a\>\<b\>\<c\>1\</c\>\</b\>\</a\> * @param ref Variable to fill the the value found at path. * @return true in case of success (path is found). Else false. */ bool XMLConfParser::getValue(std::string path, std::string& ref) { xmlNodePtr n = findPathNode(path); if(n){ std::string s = getNodeString(n); ref = s; fReadSuccess++; return true; } return false; }
int Settings::getIntSetting(Node type, int intDeviceId, const std::wstring &name, bool parameter) const { // already locked if (d->cfg == 0) { return 0; } std::string strType = getNodeString(type); cfg_t *cfg_node; for(int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) { cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i); if (cfg_getint(cfg_node, "id") == intDeviceId) { if (parameter) { cfg_node = cfg_getsec(cfg_node, "parameters"); } return cfg_getint(cfg_node, TelldusCore::wideToString(name).c_str()); } } return 0; }
/** * Fill the variable passed by reference with the value found at the specified path in the XML. * If the path does not exist in the XML, the variable is untouched. * @warning Fill the error stack * @param path Path to retrieve. The path "a.b.c" corresponds to the xml structure \<a\>\<b\>\<c\>1\</c\>\</b\>\</a\> * @param ref Variable to fill the the value found at path. * @return true in case of success (path is found and the value can be transformed into int). Else false. */ bool XMLConfParser::getValue(std::string path, int& ref) { xmlNodePtr n = findPathNode(path); if(n){ char* endptr; std::string s = getNodeString(n); trim(s, "\n\t\r "); int val = strtol(s.data(), &endptr, 0); if(!*endptr){ ref = val; fReadSuccess++; return true; } else{ std::stringstream ss; ss << "Unable to convert " << s << " to integer"; fErrorStack.addError(ss); } } return false; }
std::wstring Settings::getStringSetting(Node type, int intNodeId, const std::wstring &name, bool parameter) const { // already locked if (d->cfg == 0) { return L""; } std::string strType = getNodeString(type); cfg_t *cfg_device; for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) { cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i); if (cfg_getint(cfg_device, "id") == intNodeId) { if (parameter) { cfg_device = cfg_getsec(cfg_device, "parameters"); } std::wstring setting; char *cSetting = cfg_getstr(cfg_device, TelldusCore::wideToString(name).c_str()); if (cSetting) { setting = TelldusCore::charToWstring(cSetting); } return setting; } } return L""; }