From c7351455575348d9d9847604069ee15132f2d8e0 Mon Sep 17 00:00:00 2001 From: crunchy Date: Tue, 29 Oct 2002 20:03:33 +0000 Subject: a special crunchy checkin: no testing;-) --- mpiosh/config.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 mpiosh/config.c (limited to 'mpiosh/config.c') diff --git a/mpiosh/config.c b/mpiosh/config.c new file mode 100644 index 0000000..330bfea --- /dev/null +++ b/mpiosh/config.c @@ -0,0 +1,162 @@ +/* config.c + * + * Author: Andreas Buesching + * + * $Id: config.c,v 1.1 2002/10/29 20:03:35 crunchy Exp $ + * + * Copyright (C) 2001 Andreas Büsching + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" +#include "global.h" + +#include +#include +#include + +struct mpiosh_config_t * +mpiosh_config_new(void) +{ + struct mpiosh_config_t * cfg = malloc(sizeof(struct mpiosh_config_t)); + char *filename; + char *tmp; + struct stat st; + + cfg->prompt_int = cfg->prompt_ext = NULL; + cfg->default_mem = MPIO_INTERNAL_MEM; + + filename = malloc(strlen(CONFIG_GLOBAL) + strlen(CONFIG_FILE) + 1); + filename[0] = '\0'; + strcat(filename, CONFIG_GLOBAL); + strcat(filename, CONFIG_FILE); + tmp = cfg_resolve_path(filename); + + if (stat(tmp, &st) != -1) + cfg->handle_global = + cfg_handle_new_with_filename(tmp, 1); + + free(tmp), free(filename); + filename = malloc(strlen(CONFIG_USER) + strlen(CONFIG_FILE) + 1); + filename[0] = '\0'; + strcat(filename, CONFIG_USER); + strcat(filename, CONFIG_FILE); + tmp = cfg_resolve_path(filename); + + if (stat(tmp, &st) != -1) + cfg->handle_user = + cfg_handle_new_with_filename(tmp, 1); + else + cfg->handle_user = + cfg_handle_new_with_filename(tmp, 0); + + free(tmp), free(filename); + + return cfg; +} + +const char * +mpiosh_config_read_key(struct mpiosh_config_t *config, const char *group, + const char *key) +{ + char *value = NULL; + + if (config->handle_user) + value = (char *)cfg_key_get_value(config->handle_user, + group, key); + else if (config->handle_global) + value = (char *)cfg_key_get_value(config->handle_global, + group, key); + + return value; +} + +void +mpiosh_config_free(struct mpiosh_config_t *config) +{ + cfg_close(config->handle_global); + cfg_close(config->handle_user); + free(config->prompt_int); + free(config->prompt_ext); + free(config); +} + +int +mpiosh_config_read(struct mpiosh_config_t *config) +{ + if (config) { + const char *value; + + value = mpiosh_config_read_key(config, "mpiosh", "prompt_int"); + if (value) { + config->prompt_int = strdup(value); + } else { + config->prompt_int = strdup(PROMPT_INT); + } + + value = mpiosh_config_read_key(config, "mpiosh", "prompt_ext"); + if (value) { + config->prompt_ext = strdup(value); + } else { + config->prompt_ext = strdup(PROMPT_EXT); + } + + value = mpiosh_config_read_key(config, "mpiosh", "default_mem"); + if (value) { + if (!strcmp("internal", value)) { + config->default_mem = MPIO_INTERNAL_MEM; + } else if (!strcmp("external", value)) { + config->default_mem = MPIO_EXTERNAL_MEM; + } + } + } + + return 1; +} + +int +mpiosh_config_write(struct mpiosh_config_t *config) +{ + DIR *dir; + char *path = cfg_resolve_path(CONFIG_USER); + + if ((dir = opendir(path)) == NULL) + mkdir(path, 0777); + else + closedir(dir); + + free(path); + + if (config->handle_user) { + cfg_key_set_value(config->handle_user, + "mpiosh", "prompt_int", config->prompt_int); + cfg_key_set_value(config->handle_user, + "mpiosh", "prompt_ext", config->prompt_ext); + if (config->default_mem == MPIO_EXTERNAL_MEM) + cfg_key_set_value(config->handle_user, + "mpiosh", "default_mem", "external"); + else + cfg_key_set_value(config->handle_user, + "mpiosh", "default_mem", "internal"); + + cfg_save(config->handle_user, 0); + } + + return 1; +} + + +/* end of config.c */ -- cgit v1.2.3