void attrs_map_parse(attrs_map *map, char *str) { char *pair, *name, *value, *header_str, *original_ptr; header_str = strdup(str); original_ptr = header_str; while(isspace(*header_str)) header_str++; while((pair = strsep(&header_str, ";")) && pair != NULL) { name = strsep(&pair, "="); value = strsep(&pair, "="); attrs_map_add(map, str_trim(name), str_trim(str_strip_quotes(value))); } free(original_ptr); }
void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) { FILE* file; int line_num = 0; char linebuf[MAX_CONFIG_LINE]; file= fopen(fn, "r"); if (!file) { if (errno==ENOENT) return; warning(_("failed to open configuration file '%.255s' for reading: %s"), fn, strerror(errno)); return; } while (fgets(linebuf, sizeof(linebuf), file)) { char* opt; const struct cmdinfo *cip; int l; line_num++; if ((linebuf[0] == '#') || (linebuf[0] == '\n') || (linebuf[0] == '\0')) continue; l=strlen(linebuf); if (linebuf[l - 1] == '\n') linebuf[l - 1] = '\0'; for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ; if (*opt == '\0') opt=NULL; else { *opt++ = '\0'; if (*opt=='=') opt++; while (isspace(*opt)) opt++; opt = str_strip_quotes(opt); if (opt == NULL) config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf); } for (cip=cmdinfos; cip->olong || cip->oshort; cip++) { if (!cip->olong) continue; if (!strcmp(cip->olong,linebuf)) break; l=strlen(cip->olong); if ((cip->takesvalue==2) && (linebuf[l]=='-') && !opt && !strncmp(linebuf,cip->olong,l)) { opt=linebuf+l+1; break; } } if (!cip->olong) config_error(fn, line_num, _("unknown option '%s'"), linebuf); if (cip->takesvalue) { if (!opt) config_error(fn, line_num, _("'%s' needs a value"), linebuf); if (cip->call) cip->call(cip,opt); else *cip->sassignto = m_strdup(opt); } else { if (opt) config_error(fn, line_num, _("'%s' does not take a value"), linebuf); if (cip->call) cip->call(cip,NULL); else *cip->iassignto= cip->arg; } } if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn); if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn); }