/*--- Implementations of exported functions -----------------------------*/ extern io_parameter_t io_parameter_get(char *fname) { parse_ini_t ini; io_parameter_t params = NULL; if ((ini = local_getIni(fname)) != NULL) { params = local_allocStruct(); local_initStruct(params); local_readRequired(params, ini); parse_ini_close(&ini); local_printStruct(stderr, params); } return params; }
static makeMask_t local_getMama(void) { parse_ini_t ini; makeMask_t mama; ini = parse_ini_open(localIniFileName); if (ini == NULL) { fprintf(stderr, "FATAL: Could not open %s for reading.\n", localIniFileName); exit(EXIT_FAILURE); } mama = makeMask_newFromIni(ini, localSectionName); parse_ini_close(&ini); return mama; }
extern parse_ini_t parse_ini_open(const char *fname) { parse_ini_t dummy = NULL; local_section_t sec = NULL; local_key_t key = NULL; char *line = NULL; size_t n = 0; size_t line_len = 0; int i; /* Get memory */ dummy = xmalloc(sizeof(parse_ini_struct_t)); /* Try to open the file */ dummy->f = fopen(fname, "r"); if (dummy->f == NULL) { xfree(dummy); return NULL; } /* Copy the filename over */ dummy->fname = xstrdup(fname); /* Some init stuff */ dummy->sections = NULL; dummy->num_sections = 0; dummy->cursec = -1; /* Parse the file */ while (xgetline(&line, &n, dummy->f) > 0) { /* Check for empty (only blanks) line, wipe it */ i = 0; line_len = strlen(line); while ((line[i] != '\0') && (isblank(line[i]))) i++; if ((size_t)i == line_len - 1) { line[0] = '\0'; } switch (line[0]) { case '\0': /* This is an empty line, we do nothing. */ case '#': /* Ignore comment lines */ break; case '[': /* This is a new section */ dummy->num_sections++; /* Get memory for it */ dummy->sections = xrealloc(dummy->sections, dummy->num_sections * sizeof(local_section_struct_t)); /* Use a shortcut to access the current section */ sec = dummy->sections + dummy->num_sections - 1; /* Initialize the section structure correctly */ sec->name = local_parse_secname(line); sec->num_keys = 0; sec->keys = NULL; /* Verify the section structure */ if (sec->name == NULL) { /* Wrong file format. We die. */ fprintf(stderr, "Could not parse '%s'\n", line); xfree(line); parse_ini_close(&dummy); return NULL; } break; default: /* This is potentially a key */ if (sec == NULL) { /* Huh.. we haven't had a section yet, so lets * ignore this gibberish. */ } else { /* Okay, it is a new key */ sec->num_keys++; /* Get memory for it */ sec->keys = xrealloc(sec->keys, sec->num_keys * sizeof(local_key_struct_t)); /* Use a shortcut */ key = sec->keys + (sec->num_keys - 1); /* Initialize the key correctly */ local_parse_key(line, &(key->key_name), &(key->value)); /* Check that it worked */ if ((key->key_name == NULL) || (key->value == NULL)) { /* Wrong file format. We die. */ fprintf(stderr, "Could not parse '%s'\n", line); xfree(line); parse_ini_close(&dummy); return NULL; } /* This key has not yet been requested */ key->requested = false; } break; } /* switch */ } /* while */ /* Clean */ fclose(dummy->f); dummy->f = NULL; xfree(line); /* And done */ return dummy; } /* parse_ini_open */