diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-10 10:14:07 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-10 10:14:07 -0500 |
commit | 217bd310b9da9ea39e0ff41473b27b0b2fc1d5e8 (patch) | |
tree | a240980fbd82900d39d9d35e91c6565360bd89ce | |
parent | e9e16cc286ae1bd89af50d0c29e00866e1826b6f (diff) | |
download | spmc-217bd310b9da9ea39e0ff41473b27b0b2fc1d5e8.tar.gz |
Start implementing configuration
-rw-r--r-- | config.c | 32 | ||||
-rw-r--r-- | spm.c | 9 | ||||
-rw-r--r-- | spm.h | 17 |
3 files changed, 35 insertions, 23 deletions
@@ -55,7 +55,7 @@ int isquoted(char *sptr) { return 1; } -Config **config_read(const char *filename) { +ConfigItem **config_read(const char *filename) { const char sep = '='; char line[CONFIG_BUFFER_SIZE]; FILE *fp = fopen(filename, "r"); @@ -64,7 +64,7 @@ Config **config_read(const char *filename) { return NULL; } int record_initial = 2; - Config **config = (Config **) calloc(record_initial, sizeof(Config *)); + ConfigItem **config = (ConfigItem **) calloc(record_initial, sizeof(ConfigItem *)); int record = 0; @@ -95,8 +95,8 @@ Config **config_read(const char *filename) { size_t key_length = strcspn(lptr, &sep); size_t value_length = strlen(sep_pos); - // Allocate a Config record - config[record] = (Config *)calloc(1, sizeof(Config)); + // Allocate a ConfigItem record + config[record] = (ConfigItem *)calloc(1, sizeof(ConfigItem)); config[record]->key = (char *)calloc(key_length + 1, sizeof(char)); config[record]->value = (char *)calloc(value_length + 1, sizeof(char)); @@ -137,40 +137,40 @@ Config **config_read(const char *filename) { // increment record count record++; // Expand config by another record - config = (Config **)reallocarray(config, record + record_initial + 1, sizeof(Config *)); + config = (ConfigItem **)reallocarray(config, record + record_initial + 1, sizeof(ConfigItem *)); } return config; } -void config_free(Config **config) { - for (int i = 0; config[i] != NULL; i++) { - free(config[i]); +void config_free(ConfigItem **item) { + for (int i = 0; item[i] != NULL; i++) { + free(item[i]); } - free(config); + free(item); } /// If the configuration contains `key` return a pointer to that record -/// \param config pointer to array of config records +/// \param item pointer to array of config records /// \param key search for key in config records /// \return success=pointer to record, failure=NULL -Config *config_get(Config **config, const char *key) { - for (int i = 0; config[i] != NULL; i++) { - if (!strcmp(config[i]->key, key)) { - return config[i]; +ConfigItem *config_get(ConfigItem **item, const char *key) { + for (int i = 0; item[i] != NULL; i++) { + if (!strcmp(item[i]->key, key)) { + return item[i]; } } return NULL; } void config_test(void) { - Config **config = config_read("program.conf"); + ConfigItem **config = config_read("program.conf"); printf("Data Parsed:\n"); for (int i = 0; config[i] != NULL; i++) { printf("key: '%s', value: '%s'\n", config[i]->key, config[i]->value); } printf("Testing config_get():\n"); - Config *cptr = NULL; + ConfigItem *cptr = NULL; if ((cptr = config_get(config, "integer_value"))) { printf("%s = %d\n", cptr->key, atoi(cptr->value)); } @@ -1191,6 +1191,15 @@ int install(const char *destroot, const char *_package) { free(ucd); } +int init_config_global() { + SPM_GLOBAL.user_config_basedir = get_user_conf_dir(); + SPM_GLOBAL.user_config_file = get_user_config_file(); + SPM_GLOBAL.package_dir = realpath(PKG_DIR, NULL); + if (SPM_GLOBAL.user_config_file) { + config_read(SPM_GLOBAL.user_config_file); + } +} + int main(int argc, char *argv[]) { // not much to see here yet // at the moment this will all be random tests, for better or worse @@ -36,12 +36,19 @@ #define SHELL_BENCHMARK 1 << 2 typedef struct { + char *key; + char *value; +} ConfigItem; + +typedef struct { char *platform; char *arch; char *package_dir; char *user_config_basedir; char *user_config_file; + ConfigItem **config; } spm_vars; +static spm_vars SPM_GLOBAL; typedef struct { int count; @@ -99,17 +106,13 @@ void check_runtime_environment(void); // config.c #define CONFIG_BUFFER_SIZE 1024 -typedef struct { - char *key; - char *value; -} Config; - char *lstrip(char *sptr); char *strip(char *sptr); int isempty(char *sptr); int isquoted(char *sptr); -Config **config_read(const char *filename); -void config_free(Config **config); +ConfigItem **config_read(const char *filename); +ConfigItem *config_get(ConfigItem **item, const char *key); +void config_free(ConfigItem **item); void config_test(void); #endif //SPM_SPM_H |