/* * 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; }
DLLIMPORT void cfg_print(cfg_t *cfg, FILE *fp) { cfg_print_indent(cfg, fp, 0); }
DLLIMPORT void cfg_opt_print_indent(cfg_opt_t *opt, FILE *fp, int indent) { assert(opt && fp); if(opt->type == CFGT_SEC) { cfg_t *sec; unsigned int i; for(i = 0; i < cfg_opt_size(opt); i++) { sec = cfg_opt_getnsec(opt, i); cfg_indent(fp, indent); if(is_set(CFGF_TITLE, opt->flags)) fprintf(fp, "%s \"%s\" {\n", opt->name, cfg_title(sec)); else fprintf(fp, "%s {\n", opt->name); cfg_print_indent(sec, fp, indent + 1); cfg_indent(fp, indent); fprintf(fp, "}\n"); } } else if(opt->type != CFGT_FUNC && opt->type != CFGT_NONE) { if(is_set(CFGF_LIST, opt->flags)) { unsigned int i; cfg_indent(fp, indent); fprintf(fp, "%s = {", opt->name); if(opt->nvalues) { if(opt->pf) opt->pf(opt, 0, fp); else cfg_opt_nprint_var(opt, 0, fp); for(i = 1; i < opt->nvalues; i++) { fprintf(fp, ", "); if(opt->pf) opt->pf(opt, i, fp); else cfg_opt_nprint_var(opt, i, fp); } } fprintf(fp, "}"); } else { cfg_indent(fp, indent); /* comment out the option if is not set */ if(opt->simple_value) { if(opt->type == CFGT_STR && *((char **)opt->simple_value) == 0) fprintf(fp, "# "); } else { if(cfg_opt_size(opt) == 0 || ( opt->type == CFGT_STR && (opt->values[0]->string == 0 || opt->values[0]->string[0] == 0))) fprintf(fp, "# "); } fprintf(fp, "%s = ", opt->name); if(opt->pf) opt->pf(opt, 0, fp); else cfg_opt_nprint_var(opt, 0, fp); } fprintf(fp, "\n"); } else if(opt->pf) { cfg_indent(fp, indent); opt->pf(opt, 0, fp); fprintf(fp, "\n"); } }