2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file pluginConfig.c
19 * @author Dawid Kozinski (d.kozinski@samsung.com)
21 * @brief Plugin configuration parser done in C99.
26 #include "pluginConfig.h"
35 #include "pluginConfigLog.h"
37 #define UNUSED(x) (void)(x)
39 /*********************************************************************************
41 ******************************************************************************/
44 /******************************************************************************
46 * Implicit Declarations of trimming functions
47 * this only describes and assumes allocated elsewhere (trimming.c)
49 ******************************************************************************/
51 * @brief trim from start, i.e. left side
52 * Trimming leading whitespace from string
57 extern char *plugin_internal_ltrim(char *s);
60 * @brief trim from end, i.e. right side
61 * Trimming trailing whitespace from string
66 extern char *plugin_internal_rtrim(char *s);
69 * @brief trim from both ends
70 * Trimming leading and trailing whitespace from string
74 extern char *plugin_internal_trim(char *s);
76 /******************************************************************************
78 ******************************************************************************/
80 /******************************************************************************
81 * [unique name][cloud_adaptor_handle*]
82 ******************************************************************************/
84 * @typedef plugin_internal_MapStrStr
86 * |---------------------------|-----------------------------------------------|
87 * | KEY : char* | VALUE : char* |
88 * |---------------------------|-----------------------------------------------|
90 * |---------------------------|-----------------------------------------------|
92 * |---------------------------|-----------------------------------------------|
94 * |---------------------------|-----------------------------------------------|
96 typedef GHashTable plugin_internal_MapStrStr;
99 * @typedef plugin_internal_MapStrSection
101 * |---------------------------|-----------------------------------------------|
102 * | SECTION NAME : char* | SECTION PTR : MapStrStr * |
103 * |---------------------------|-----------------------------------------------|
105 * |---------------------------|-----------------------------------------------|
107 * |---------------------------|-----------------------------------------------|
109 * |---------------------------|-----------------------------------------------|
111 typedef GHashTable plugin_internal_MapStrSection;
114 * @typedef plugin_internal_ListSection
116 * |------------------------------------------|
117 * | SECTION PTR : MapStrStr * |
118 * |------------------------------------------|
120 * |------------------------------------------|
122 * |------------------------------------------|
124 * |------------------------------------------|
126 typedef GList plugin_internal_ListSection;
128 /******************************************************************************
129 * ConfigParserState struct
130 ******************************************************************************/
131 typedef struct plugin_internal_ConfigParserState {
132 char *current_section_name;
133 plugin_internal_MapStrStr *current_section;
136 /******************************************************************************
138 ******************************************************************************/
139 typedef struct plugin_internal_ConfigData {
143 * @brief Filepath to the read configuration file.
148 * @brief Describes layout of the data in the file.
150 PluginConfigType type;
153 * @brief Variable that stores the all sections of configuration,
154 * meant to be accessed through the field 'configuration'.
156 plugin_internal_ListSection *sections;
159 * @brief Variable that stores the whole configuration.
161 plugin_internal_MapStrSection *configuration;
162 } plugin_internal_ConfigData;
164 /******************************************************************************
165 * Declarations (methods of ConfigData class)
166 ******************************************************************************/
168 /******************************************************************************
170 ******************************************************************************/
173 * @brief Function which is called when a data element of configuration map
174 * being a member of plugin_internal_ConfigData struct
175 * (@see plugin_internal_ConfigData) is destroyed.
176 * @see plugin_internal_MapStrSection
178 * @param data pointer to the user data
182 void plugin_internal_ConfigData_cbMapStrSectionFreeData(gpointer data);
185 * @brief Function passed to g_hash_table_foreach().
186 * Function is called for each key/value pair.
189 * @param value the value corresponding to the key
190 * @param user_data user data passed to g_hash_table_foreach()
194 void plugin_internal_ConfigData_cbMapStrSectionEntryCopy(gpointer key,
199 * @brief Function passed to g_hash_table_foreach().
200 * Function is called for each key/value pair.
203 * @param value the value corresponding to the key
204 * @param user_data user data passed to g_hash_table_foreach()
208 void plugin_internal_ConfigData_cbMapStrSectionCopy(gpointer key,
212 * @brief Function passed to g_list_foreach().
213 * The function called for each element's data.
215 * @param data the element's data.
216 * @param user_data user data passed to g_list_foreach() or g_slist_foreach().
220 gpointer plugin_internal_ConfigData_cbListSectionCopy(gconstpointer data,
224 * @brief The function to be called to free each element's data.
225 * The function which is called when a data element is destroyed.
226 * It is passed the pointer to the data element and should free any memory and
227 * resources allocated for it.
229 * @param data the data element.
233 void plugin_internal_ConfigData_cbDestroySectionsList(gpointer data);
235 /******************************************************************************
236 * Constructing object
237 ******************************************************************************/
239 * @brief Contruct object of type plugin_internal_ConfigData
240 * @return plugin_internal_ConfigData*
242 /*plugin_internal_ConfigData *plugin_internal_ConfigData_new(); */
245 * @brief Contruct object of type plugin_internal_ConfigData and loads
246 * configuration from the specified file.
248 * @param filepath a path to the configuration file
249 * @param type expected type of configuration file, this value
250 * determines how the file is parsed
253 plugin_internal_ConfigData *
254 plugin_internal_ConfigData_new(const char *filepath, PluginConfigType type);
257 * @brief Copy constructor.
263 plugin_internal_ConfigData *
264 plugin_internal_ConfigData_copy(const plugin_internal_ConfigData *source);
267 /******************************************************************************
269 ******************************************************************************/
271 * @brief Removes configuration from memory, rendering this configuration invalid.
276 void plugin_internal_ConfigData_clear(plugin_internal_ConfigData *self);
283 void plugin_internal_ConfigData_delete(plugin_internal_ConfigData *self);
285 /******************************************************************************
287 ******************************************************************************/
290 * @brief parse line of INI file
292 * @param self pointer to object of type plugin_internal_ConfigData
294 * @param line pointer to the string containing line of file
298 void plugin_internal_ConfigData_parseLineIni(plugin_internal_ConfigData *self,
299 ConfigParserState *state,
302 /* TODO Provide implementation */
304 * @todo Provide implementation
306 * @brief parse line of GIT config file
308 * @param self pointer to object of type plugin_internal_ConfigData
310 * @param line pointer to the string containing line of file
314 void plugin_internal_ConfigData_parseLineGit(plugin_internal_ConfigData *self,
315 ConfigParserState *state,
318 /* TODO Provide implementation */
320 * @todo Provide implementation
322 * @brief parse line of CSV config file
324 * @param self pointer to object of type plugin_internal_ConfigData
326 * @param line pointer to the string containing line of file
327 * @param separator ...
331 void plugin_internal_ConfigData_parseLineCsv(plugin_internal_ConfigData *self,
332 ConfigParserState *state,
340 * @param self pointer to object of type plugin_internal_ConfigData
346 plugin_internal_ConfigData_hasSection(const plugin_internal_ConfigData *self,
347 const char *section);
350 * @brief This method assumes that the given section exists. Use hasSection()
351 * first, or use hasSectionAndKey() instead.
353 * @param self pointer to object of type plugin_internal_ConfigData
354 * @param section name of the configuration section
355 * @param key name of the key within that section
356 * @return int 1 if such key exists
360 plugin_internal_ConfigData_hasKey(const plugin_internal_ConfigData *self,
367 * @param self pointer to object of type plugin_internal_ConfigData
374 plugin_internal_ConfigData_hasSectionAndKey(const plugin_internal_ConfigData *self,
381 * @param self pointer to object of type plugin_internal_ConfigData
382 * @return const char*
386 plugin_internal_ConfigData_getFilepath(const plugin_internal_ConfigData *self);
391 * @param self pointer to object of type plugin_internal_ConfigData
392 * @return PluginConfigType
396 plugin_internal_ConfigData_getType(const plugin_internal_ConfigData *self);
401 * @param self pointer to object of type plugin_internal_ConfigData
402 * @param section_name ...
403 * @param section_key ...
404 * @return const char*
408 plugin_internal_ConfigData_getEntry(const plugin_internal_ConfigData *self,
409 const char *section_name,
410 const char *section_key);
412 /******************************************************************************
413 * Definitions (methods of ConfigData class)
414 ******************************************************************************/
416 /******************************************************************************/
418 void plugin_internal_ConfigData_cbMapStrSectionFreeData(gpointer data)
420 /* TODO Is it enough ? */
426 /******************************************************************************/
427 plugin_internal_ConfigData *plugin_internal_ConfigData_new()
429 plugin_internal_ConfigData *self;
430 self = (plugin_internal_ConfigData *)calloc(1, sizeof(plugin_internal_ConfigData));
432 GMutex *data_mutex = (GMutex *)calloc(1, sizeof(GMutex));
433 g_mutex_init(data_mutex);
435 self->data_mutex = data_mutex;
436 self->filepath = NULL;
437 self->type = CCT_INI;
438 self->sections = NULL;
439 self->configuration = NULL;
441 self->configuration = g_hash_table_new_full(g_str_hash, /* Hash function */
442 g_str_equal, /* Comparator */
443 plugin_internal_ConfigData_cbMapStrSectionFreeData, /* Key destructor */
444 plugin_internal_ConfigData_cbMapStrSectionFreeData); /* Val destructor */
450 /******************************************************************************/
452 plugin_internal_ConfigData *
453 plugin_internal_ConfigData_new(const char *filepath, PluginConfigType type)
457 if (NULL == filepath) {
461 if (CCT_INVALID == type) {
465 plugin_internal_ConfigData *self;
466 self = (plugin_internal_ConfigData *)calloc(1, sizeof(plugin_internal_ConfigData));
472 GMutex *data_mutex = (GMutex *)calloc(1, sizeof(GMutex));
473 if (NULL == data_mutex) {
478 g_mutex_init(data_mutex);
480 self->data_mutex = data_mutex;
481 self->filepath = strdup(filepath);
482 self->type = CCT_INI;
483 self->sections = NULL; /* sections.reserve(128); */
484 self->configuration = NULL;
486 self->configuration =
487 g_hash_table_new_full(g_str_hash, /* Hash function */
488 g_str_equal, /* Comparator */
489 plugin_internal_ConfigData_cbMapStrSectionFreeData, /* Key destructor */
490 plugin_internal_ConfigData_cbMapStrSectionFreeData);/* Val destructor */
497 fp = fopen(filepath, "r");
500 /* exit(EXIT_FAILURE); */
502 if (self->data_mutex) {
503 g_mutex_clear(self->data_mutex);
504 free(self->data_mutex);
507 free(self->filepath);
508 if (self->configuration)
509 g_hash_table_destroy(self->configuration);
515 ConfigParserState state;
517 while ((read = getline(&line, &len, fp)) != -1) {
518 /* printf("Retrieved line of length %zu :\n", read); */
519 /* printf("%s", line); */
521 if (strlen(line) == 0)
526 plugin_internal_ConfigData_parseLineIni(self, &state, line);
529 plugin_internal_ConfigData_parseLineGit(self, &state, line);
532 plugin_internal_ConfigData_parseLineCsv(self, &state, line, ',');
535 plugin_internal_ConfigData_parseLineCsv(self, &state, line, '\t');
538 plugin_internal_ConfigData_parseLineCsv(self, &state, line, ':');
540 case CCT_CSV_SEMICOLON:
541 plugin_internal_ConfigData_parseLineCsv(self, &state, line, ';');
551 /* printf("configuration file %s was loaded\n", filepath); */
558 /******************************************************************************/
560 void plugin_internal_ConfigData_cbMapStrSectionEntryCopy(gpointer key,
566 plugin_internal_MapStrStr *copy_section = (plugin_internal_MapStrStr *)user_data;
567 g_hash_table_insert(copy_section, strdup((char *)key), strdup((char *)value));
572 /******************************************************************************/
574 void plugin_internal_ConfigData_cbMapStrSectionCopy(gpointer key,
580 plugin_internal_MapStrSection *copy_configuration = (plugin_internal_MapStrSection *)user_data;
582 plugin_internal_MapStrStr *section = (plugin_internal_MapStrStr *)value;
584 plugin_internal_MapStrStr *copy_section =
585 g_hash_table_new_full(g_str_hash, /* Hash function */
586 g_str_equal, /* Comparator */
587 plugin_internal_ConfigData_cbMapStrSectionFreeData, /* Key destructor */
588 plugin_internal_ConfigData_cbMapStrSectionFreeData); /* Val destructor */
590 g_hash_table_foreach(section, (GHFunc)plugin_internal_ConfigData_cbMapStrSectionEntryCopy, copy_section);
592 g_hash_table_insert(copy_configuration, strdup((char *)key), copy_section);
597 /******************************************************************************/
599 gpointer plugin_internal_ConfigData_cbListSectionCopy(gconstpointer src,
606 plugin_internal_MapStrStr *section = (plugin_internal_MapStrStr *)src;
608 plugin_internal_MapStrStr *copy_section =
609 g_hash_table_new_full(g_str_hash, /* Hash function */
610 g_str_equal, /* Comparator */
611 plugin_internal_ConfigData_cbMapStrSectionFreeData, /* Key destructor */
612 plugin_internal_ConfigData_cbMapStrSectionFreeData); /* Val destructor */
614 g_hash_table_foreach(section,
615 (GHFunc)plugin_internal_ConfigData_cbMapStrSectionEntryCopy,
622 /******************************************************************************/
625 plugin_internal_ConfigData *
626 plugin_internal_ConfigData_copy(const plugin_internal_ConfigData *source)
630 if (NULL == source) {
634 plugin_internal_ConfigData *self;
635 self = (plugin_internal_ConfigData *)calloc(1, sizeof(plugin_internal_ConfigData));
637 GMutex *data_mutex = (GMutex *)calloc(1, sizeof(GMutex));
638 g_mutex_init(data_mutex);
640 g_mutex_lock(source->data_mutex);
642 self->data_mutex = data_mutex;
643 self->filepath = strdup(source->filepath);
644 self->type = source->type;
646 self->sections = NULL;
648 self->configuration =
649 g_hash_table_new_full(g_str_hash, /* Hash function */
650 g_str_equal, /* Comparator */
651 plugin_internal_ConfigData_cbMapStrSectionFreeData, /* Key destructor */
652 plugin_internal_ConfigData_cbMapStrSectionFreeData);/* Val destructor */
654 /* copy source->configuration to self->configuration */
655 g_hash_table_foreach(source->configuration,
656 (GHFunc)plugin_internal_ConfigData_cbMapStrSectionCopy,
657 self->configuration);
659 /* copy source->sections to self->sections */
660 self->sections = g_list_copy_deep(source->sections,
661 plugin_internal_ConfigData_cbListSectionCopy,
664 g_mutex_unlock(source->data_mutex);
670 /******************************************************************************/
672 void plugin_internal_ConfigData_delete(plugin_internal_ConfigData *self)
677 plugin_internal_ConfigData_clear(self);
683 /******************************************************************************
685 ******************************************************************************/
688 plugin_internal_ConfigData_parseLineIni(plugin_internal_ConfigData *self,
689 ConfigParserState *state,
694 if (NULL == self || NULL == line) {
701 if (line[0] == '[') {
702 line = plugin_internal_trim((char *)line);
704 char *begin = strchr((char *)line, '[');
705 char *end = strrchr((char *)line, ']');
708 printf("Invalid section name %p\n", line);
712 int section_name_len = end - begin - 1;
714 char *section_name = (char *)calloc(1, section_name_len + 1);
715 if (NULL == section_name) {
719 strncpy(section_name, line + 1, section_name_len);
720 section_name[section_name_len] = '\0';
722 plugin_internal_MapStrStr *section =
723 g_hash_table_new_full(g_str_hash, /* Hash function */
724 g_str_equal, /* Comparator */
725 plugin_internal_ConfigData_cbMapStrSectionFreeData, /* Key destructor */
726 plugin_internal_ConfigData_cbMapStrSectionFreeData); /* Val destructor */
728 self->sections = g_list_append(self->sections, section);
729 /*sections.push_back(MapStrStr()); */
731 /*MapStrStr *section = &(sections[sections.size() - 1]); */
732 g_hash_table_insert(self->configuration, strdup(section_name), section);
734 /* configuration[section_name] = section; */
736 state->current_section_name = section_name;
737 state->current_section = section;
739 /* ---------------------------------------------------------------------
740 * |0|1|2|3|4|5|6|7|8|
741 * ---------------------------------------------------------------------
742 * |K|E|Y|1|=|V|A|L|1|
743 * ---------------------------------------------------------------------
749 line = plugin_internal_trim((char *)line);
750 const char *pch = strchr((char *)line, '=');
753 int len = strlen(line);
754 int key_len = pch - line + 1 ; /* +'\0' */
755 int value_len = len - key_len + 1; /* +'\0' */
757 char *key = (char *)calloc(1, key_len);
758 char *value = (char *)calloc(1, value_len);
760 if (NULL != key && NULL != value) {
761 strncpy(key, line, key_len - 1);
762 key[key_len - 1] = '\0';
764 strncpy(value, pch + 1, value_len - 1);
765 value[value_len - 1] = '\0';
767 g_hash_table_insert(state->current_section, strdup(key), strdup(value));
783 /******************************************************************************/
786 plugin_internal_ConfigData_parseLineGit(plugin_internal_ConfigData *self,
787 ConfigParserState *state,
802 /******************************************************************************/
805 plugin_internal_ConfigData_parseLineCsv(plugin_internal_ConfigData *self,
806 ConfigParserState *state,
823 /******************************************************************************/
825 void plugin_internal_ConfigData_cbDestroySectionsList(gpointer data)
829 plugin_internal_MapStrStr *section = (plugin_internal_MapStrStr *)data;
830 g_hash_table_destroy(section);
835 /******************************************************************************/
837 void plugin_internal_ConfigData_clear(plugin_internal_ConfigData *self)
845 free(self->filepath);
847 self->type = CCT_INVALID;
849 g_hash_table_destroy(self->configuration);
850 self->configuration = NULL;
851 g_list_free_full(self->sections, plugin_internal_ConfigData_cbDestroySectionsList);
856 /******************************************************************************/
859 plugin_internal_ConfigData_hasKey(const plugin_internal_ConfigData *self,
860 const char *section_name,
861 const char *section_key)
865 if (NULL == self || NULL == self->configuration) {
869 g_mutex_lock(self->data_mutex);
871 gconstpointer lookup_key = (gconstpointer)section_name;
872 gpointer orig_key = NULL; /* key of self->configuration */
873 gpointer value = NULL; /* value of self->configuration */
875 gboolean result = g_hash_table_lookup_extended(self->configuration,
880 if (TRUE == result && value) {
881 plugin_internal_MapStrStr *ptrMapSection = (plugin_internal_MapStrStr *)value ;
883 lookup_key = (gconstpointer)section_key;
884 orig_key = NULL; /* key of ptrMapSection */
885 value = NULL; /* value of ptrMapSection */
887 result = g_hash_table_lookup_extended(ptrMapSection,
893 g_mutex_unlock(self->data_mutex);
899 /******************************************************************************
901 ******************************************************************************/
904 plugin_internal_ConfigData_hasSection(const plugin_internal_ConfigData *self,
905 const char *section_name)
913 g_mutex_lock(self->data_mutex);
915 gconstpointer lookup_key = (gconstpointer)section_name;
916 gpointer orig_key = NULL; /* key of self->configuration */
917 gpointer value = NULL; /* value of self->configuration */
919 gboolean result = g_hash_table_lookup_extended(self->configuration,
924 g_mutex_unlock(self->data_mutex);
930 /******************************************************************************/
933 plugin_internal_ConfigData_hasSectionAndKey(const plugin_internal_ConfigData *self,
934 const char *section_name,
935 const char *section_key)
943 g_mutex_lock(self->data_mutex);
945 gconstpointer lookup_key = (gconstpointer)section_name;
946 gpointer orig_key = NULL; /* key of self->configuration */
947 gpointer value = NULL; /* value of self->configuration */
949 gboolean result = g_hash_table_lookup_extended(self->configuration,
954 if (TRUE == result && value) {
955 plugin_internal_MapStrStr *ptrMapSection = (plugin_internal_MapStrStr *)value ;
956 lookup_key = (gconstpointer)section_key;
957 orig_key = NULL; /* key of ptrMapSection */
958 value = NULL; /* value of ptrMapSection */
960 result = g_hash_table_lookup_extended(ptrMapSection,
966 g_mutex_unlock(self->data_mutex);
972 /******************************************************************************/
975 plugin_internal_ConfigData_getFilepath(const plugin_internal_ConfigData *self)
983 g_mutex_lock(self->data_mutex);
985 const char *result = self->filepath;
987 g_mutex_unlock(self->data_mutex);
993 /******************************************************************************/
996 plugin_internal_ConfigData_getType(const plugin_internal_ConfigData *self)
1004 g_mutex_lock(self->data_mutex);
1006 PluginConfigType result = self->type;
1008 g_mutex_unlock(self->data_mutex);
1014 /******************************************************************************/
1017 plugin_internal_ConfigData_getEntry(const plugin_internal_ConfigData *self,
1018 const char *section_name,
1019 const char *section_key)
1027 g_mutex_lock(self->data_mutex);
1029 gconstpointer lookup_key = (gconstpointer)section_name;
1030 gpointer orig_key = NULL; /* key of self->configuration */
1031 gpointer value = NULL; /* value of self->configuration */
1033 gboolean result = g_hash_table_lookup_extended(self->configuration,
1038 if (TRUE == result && value) {
1039 plugin_internal_MapStrStr *ptrMapSection = (plugin_internal_MapStrStr *)value ;
1041 lookup_key = (gconstpointer)section_key;
1042 orig_key = NULL; /* key of ptrMapSection */
1043 value = NULL; /* value of ptrMapSection */
1045 result = g_hash_table_lookup_extended(ptrMapSection,
1051 g_mutex_unlock(self->data_mutex);
1054 return (char *)value;
1057 /******************************************************************************
1059 ******************************************************************************/
1061 * @brief General-purpose, class for loading and reading configuration from
1062 * simple plaintext files.
1064 typedef struct plugin_Config {
1066 plugin_internal_ConfigData *_configuration;
1069 /******************************************************************************
1070 * Declarations (methods of Config class)
1071 ******************************************************************************/
1074 * @brief Construct object of type plugin_Config
1076 * @return plugin_Config*
1079 plugin_Config *plugin_Config_new();
1082 * @brief Destroy object of type plugin_Config
1084 * @param self pointer to object of type plugin_Config
1088 void plugin_Config_delete(plugin_Config *self);
1091 * @brief Loads configuration to memory and/or gets raw data from configuration.
1093 * @param self pointer to object of type plugin_Config
1094 * @param section will return value attached to a key from this section
1095 * @param key will return value attached to this key
1100 plugin_Config_getRaw(plugin_Config *self, const char *section, const char *key);
1103 * @brief Loads the configuration from a given file into memory.
1105 * @param self pointer to object of type plugin_Config
1106 * @param filepath path to the configuration file that will be loaded
1107 * @param type expected type of configuration file, this value determines
1108 * how the file is parsed
1109 * @return int return 1 if successful, 0 otherwise
1113 plugin_Config_load(plugin_Config *self, const char *filepath, PluginConfigType type);
1116 * @brief Unloads the configuration from the memory.
1118 * The configuration is automatically unloaded when the Config object is
1119 * destoyed, so this method does not have to be executed unless you need
1120 * to extremely lower memory usage and few bytes matter.
1122 * @param self pointer to object of type plugin_Config
1125 static void plugin_Config_unload(plugin_Config *self);
1128 * @brief Checks wheteher config file has been loaded.
1130 * @return int return 1 if successful, 0 otherwise
1132 static bool plugin_Config_isLoaded(plugin_Config *self);
1135 * @brief From loaded configuration, gets string value attached to given key.
1137 * @param self pointer to object of type plugin_Config
1138 * @param section ...
1140 * @return char* value attached to the key from the section
1144 plugin_Config_getString(plugin_Config *self, const char *section, const char *key);
1147 * @brief From loaded configuration, gets integer value attached to given key.
1149 * @param self pointer to object of type plugin_Config
1150 * @param section will return value attached to a key from this section
1151 * @param key will return value attached to this key
1155 int plugin_Config_getInt(plugin_Config *self, const char *section, const char *key);
1158 * @brief From _loaded configuration, gets double value attached to given key.
1160 * @param self pointer to object of type plugin_Config
1161 * @param section will return value attached to a key from this section
1162 * @param key will return value attached to this key
1166 double getDouble(plugin_Config *self, const char *section, const char *key);
1168 /******************************************************************************
1169 * Definitiions (methods of Config class)
1170 ******************************************************************************/
1172 plugin_Config *plugin_Config_new()
1176 plugin_Config *self;
1177 self = (plugin_Config *)calloc(1, sizeof(plugin_Config));
1183 self->_loaded = false;
1184 self->_configuration = NULL;
1190 /******************************************************************************/
1192 void plugin_Config_delete(plugin_Config *self)
1196 if (self && self->_configuration) {
1197 free(self->_configuration);
1198 self->_configuration = NULL;
1200 plugin_config_error("Invalid parameter");
1207 /******************************************************************************/
1210 plugin_Config_getRaw(plugin_Config *self, const char *section, const char *key)
1214 const char *ret = NULL;
1216 if (self && self->_configuration) {
1217 if (plugin_internal_ConfigData_hasSectionAndKey(
1218 (const plugin_internal_ConfigData *)self->_configuration, section, key)) {
1219 ret = plugin_internal_ConfigData_getEntry((const plugin_internal_ConfigData *)self->_configuration,
1224 plugin_config_error("Invalid parameter");
1231 /******************************************************************************/
1233 int plugin_Config_load(plugin_Config *self, const char *filepath, PluginConfigType type)
1239 if (self->_configuration) {
1240 plugin_internal_ConfigData_delete(self->_configuration);
1241 self->_configuration = NULL;
1244 if (filepath && strlen(filepath) > 0) {
1245 self->_configuration = plugin_internal_ConfigData_new(filepath, type);
1247 if (self->_configuration) {
1253 plugin_config_error("Invalid parameter");
1260 /******************************************************************************/
1262 void plugin_Config_unload(plugin_Config *self)
1266 if (self && self->_configuration) {
1267 plugin_internal_ConfigData_delete(self->_configuration);
1268 self->_configuration = NULL;
1269 self->_loaded = false;
1271 plugin_config_error("Invalid parameter");
1277 /******************************************************************************/
1279 bool plugin_Config_isLoaded(plugin_Config *self)
1286 ret = self->_loaded;
1288 plugin_config_error("Invalid parameter");
1295 /******************************************************************************/
1298 plugin_Config_getString(plugin_Config *self, const char *section, const char *key)
1300 plugin_config_debug_func();
1302 return plugin_Config_getRaw(self, section, key);
1306 /******************************************************************************/
1308 int plugin_Config_getInt(plugin_Config *self, const char *section, const char *key)
1310 plugin_config_debug_func();
1312 const char *result = plugin_Config_getRaw(self, section, key);
1313 if (NULL == result) {
1317 return atoi(result);
1321 /******************************************************************************/
1323 double plugin_Config_getDouble(plugin_Config *self, const char *section, const char *key)
1325 plugin_config_debug_func();
1327 const char *result = plugin_Config_getRaw(self, section, key);
1328 if (NULL == result) {
1332 return atof(result);
1335 /******************************************************************************
1336 * C API implementation
1337 ******************************************************************************/
1339 /******************************************************************************/
1340 ConfigHandle plugin_config_create()
1342 plugin_config_debug_func();
1344 return (ConfigHandle)plugin_Config_new();
1347 /******************************************************************************/
1348 void plugin_config_delete(ConfigHandle config_handle)
1352 plugin_Config_delete((plugin_Config *)config_handle);
1357 /******************************************************************************/
1358 void plugin_config_load(ConfigHandle config_handle, const char *filepath,
1359 PluginConfigType type)
1363 if (config_handle) {
1364 plugin_Config *pConfig = (plugin_Config *)(config_handle);
1365 plugin_Config_load(pConfig, filepath, type);
1367 plugin_config_error("Invalid parameter");
1373 /******************************************************************************/
1374 void plugin_config_unload(ConfigHandle config_handle)
1378 if (config_handle) {
1379 plugin_Config *pConfig = (plugin_Config *)(config_handle);
1380 plugin_Config_unload(pConfig);
1382 plugin_config_error("Invalid parameter");
1388 /******************************************************************************/
1389 const char *plugin_config_get_string(ConfigHandle config_handle,
1390 const char *section, const char *key)
1394 const char *val = NULL;
1396 if (config_handle) {
1397 plugin_Config *pConfig = (plugin_Config *)(config_handle);
1398 val = plugin_Config_getString(pConfig, section, key);
1400 plugin_config_error("Invalid parameter");
1407 /******************************************************************************/
1408 int plugin_config_get_int(ConfigHandle config_handle, const char *section, const char *key)
1414 if (config_handle) {
1415 plugin_Config *pConfig = (plugin_Config *)(config_handle);
1416 ret = plugin_Config_getInt(pConfig, section, key);
1418 plugin_config_error("Invalid parameter");
1425 /******************************************************************************/
1426 double plugin_config_get_double(ConfigHandle config_handle, const char *section, const char *key)
1432 if (config_handle) {
1433 plugin_Config *pConfig = (plugin_Config *)(config_handle);
1434 ret = plugin_Config_getDouble(pConfig, section, key);
1436 plugin_config_error("Invalid parameter");
1443 /******************************************************************************/
1444 bool plugin_config_is_loaded(ConfigHandle config_handle)
1449 if (config_handle) {
1450 plugin_Config *pConfig = (plugin_Config *)(config_handle);
1451 ret = plugin_Config_isLoaded(pConfig);
1453 plugin_config_error("Invalid parameter");