/* Value Setting */ void Dictionary_setValueForKey(SCDictionary* self, SCValue* value, const char* key) { SCDictionary* temp = _Dictionary_findKey(self, key); if (temp) { Value_free(temp->value); temp->value = value; return; } temp = _Dictionary_findLast(self); if (temp == self && !temp->key) { self->key = strdup(key); self->value = value; return; } self = Dictionary_new(); self->tree_left = temp; temp->tree_right = self; self->key = strdup(key); self->value = value; }
/** * \brief Read a configuration file into a Dictionary * * Parse the configuration file as described above and store all the key/value * pairs into a Dictionary which is returned. * * \param filename The file to read * \return A Dictionary mapping the key/value pairs or NULL if an error occurs */ Dictionary* Config_readFile(const char* filename) { FILE* config_file = fopen(filename, "r"); Dictionary* config = NULL; char* option; char* value; int i, tmp = 0; char line[MAX_LINE]; bool comment; /* Reset config_errno to success */ config_errno = CONFIG_SUCCESS; config_lineno = 0; /* Configuration file opened alright? */ if(config_file == NULL) { config_errno = CONFIG_EFILEACCESS; goto config_error; } /* Create the dictionary object */ config = Dictionary_new(); /* Read in line by line until EOF to get each configuration option */ while(true) { line[0] = '\0'; /* Read until we find a non empty line */ while(strcmp(line, "") == 0) { i = 0; comment = false; /* Read line */ while(true) { tmp = fgetc(config_file); /* End of input/line */ if(tmp == EOF || tmp == '\n') { break; } /* Start of a comment */ if(tmp == '#') { comment = true; } /* If we're in a comment discard characters */ if(!comment) { line[i++] = tmp; } /* Ran out of space. Line too long */ if(i >= MAX_LINE) { config_errno = CONFIG_ELINETOOLONG; goto config_error; } } config_lineno++; line[i] = '\0'; /* Strip white space from entire line */ Util_strip(line); /* If the line is empty and we have reached the end of file then return */ if(tmp == EOF && strcmp(line, "") == 0) { fclose(config_file); return config; } } /* Split the line */ option = value = line; for(i = 0; line[i] != '\0'; i++) { if(line[i] == '=') { line[i] = '\0'; value = line + i + 1; break; } } /* Never found '=' in the line */ if(option == value) { config_errno = CONFIG_EPARSE; goto config_error; } /* Strip spaces from components */ Util_strip(option); Util_strip(value); /* Store the pair */ Dictionary_set(config, option, strdup(value)); } config_error: if(config != NULL) { List* options = Dictionary_getKeys(config); char* option; /* Free memory allocated to config values */ while(List_getSize(options)) { option = List_remove(options, 0); free(Dictionary_get(config, option)); } List_destroy(options); Dictionary_destroy(config); } if(config_file != NULL) { fclose(config_file); } return NULL; }
/** * \brief Process the configuration file * * Read the configuration file and populate the configuration dictionary */ static void Hub_Config_processConfig(void) { Dictionary* temp_config; List* options; char* option; char* value; /* Initialize config table with default options */ config = Dictionary_new(); for(int i = 0; i < sizeof(valid_options) / sizeof(valid_options[0]); i++) { Dictionary_set(config, valid_options[i].option, strdup(valid_options[i].default_value)); } /* Locate a configuration file */ if(Hub_Config_chooseConfigFile() == false) { Hub_Logging_log(WARNING, "Could not find configuration file! Continuing with default configuration!"); return; } /* Attempt to read the configuration file */ temp_config = Config_readFile(hub_config_file); /* Error handling for Config_readFile */ if(temp_config == NULL) { switch(Config_getError()) { case CONFIG_EFILEACCESS: Hub_Logging_log(WARNING, Util_format("Failed to open configuration file: %s", strerror(errno))); break; case CONFIG_ELINETOOLONG: Hub_Logging_log(CRITICAL, Util_format("Line exceeded maximum allowable length at line %d", Config_getLineNumber())); break; case CONFIG_EPARSE: Hub_Logging_log(CRITICAL, Util_format("Parse error occured on line %d", Config_getLineNumber())); break; default: Hub_Logging_log(CRITICAL, "Unknown error occured while reading configuration file"); break; } /* Bail out and exit */ Hub_exitError(); } /* Get the list of configuration options in the file */ options = Dictionary_getKeys(temp_config); /* Check each configuration option and if it is valid store it in the real configuration table */ while(List_getSize(options)) { /* Remove the first item from the list */ option = List_remove(options, 0); value = Dictionary_get(temp_config, option); if(Dictionary_exists(config, option)) { /* Valid option, free old value, store new value */ free(Dictionary_get(config, option)); Dictionary_set(config, option, value); } else { Hub_Logging_log(WARNING, Util_format("Unknown configuration option '%s'", option)); } } List_destroy(options); Dictionary_destroy(temp_config); }
/** * \brief Var component initialization * \private */ void Var_init(void) { ro_cache = Dictionary_new(); subscriptions = Dictionary_new(); initialized = true; }