aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-04-15 00:36:20 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-04-15 01:03:35 -0400
commit7dbbe725e8b59a7ed813342bd5cb47c81bc2cf1c (patch)
tree64a325354ca84a29855ff30027a6220524848bcd
parent0db784a48f28a240e2a768d2773ba7cd4833a92d (diff)
downloadstasis-7dbbe725e8b59a7ed813342bd5cb47c81bc2cf1c.tar.gz
Add ini_has_key() and expose ini_section_search() function
* ini_section_search now has three matching modes _EXACT, _BEGINS, and _SUBSTR
-rw-r--r--include/ini.h20
-rw-r--r--src/ini.c62
2 files changed, 73 insertions, 9 deletions
diff --git a/include/ini.h b/include/ini.h
index a444665..fa2bd98 100644
--- a/include/ini.h
+++ b/include/ini.h
@@ -9,6 +9,9 @@
#define INI_WRITE_PRESERVE 1 ///< Dump INI data. Template strings are
#define INI_SETVAL_APPEND 0
#define INI_SETVAL_REPLACE 1
+#define INI_SEARCH_EXACT 0
+#define INI_SEARCH_BEGINS 1
+#define INI_SEARCH_SUBSTR 2
///< expanded to preserve runtime state.
#define INIVAL_TYPE_INT 1 ///< Integer
@@ -90,6 +93,23 @@ struct INIFILE {
struct INIFILE *ini_open(const char *filename);
/**
+ *
+ * @param ini
+ * @param value
+ * @return
+ */
+struct INISection *ini_section_search(struct INIFILE **ini, unsigned mode, char *value);
+
+/**
+ *
+ * @param ini
+ * @param section
+ * @param key
+ * @return
+ */
+int ini_has_key(struct INIFILE *ini, const char *section, const char *key);
+
+/**
* Assign value to a section key
* @param ini
* @param type INI_SETVAL_APPEND or INI_SETVAL_REPLACE
diff --git a/src/ini.c b/src/ini.c
index 9698923..2f94b5b 100644
--- a/src/ini.c
+++ b/src/ini.c
@@ -16,12 +16,22 @@ void ini_section_init(struct INIFILE **ini) {
(*ini)->section = calloc((*ini)->section_count + 1, sizeof(**(*ini)->section));
}
-struct INISection *ini_section_search(struct INIFILE **ini, char *value) {
+struct INISection *ini_section_search(struct INIFILE **ini, unsigned mode, char *value) {
struct INISection *result = NULL;
for (size_t i = 0; i < (*ini)->section_count; i++) {
if ((*ini)->section[i]->key != NULL) {
- if (!strcmp((*ini)->section[i]->key, value)) {
- result = (*ini)->section[i];
+ if (mode == INI_SEARCH_EXACT) {
+ if (!strcmp((*ini)->section[i]->key, value)) {
+ result = (*ini)->section[i];
+ }
+ } else if (mode == INI_SEARCH_BEGINS) {
+ if (startswith((*ini)->section[i]->key, value)) {
+ result = (*ini)->section[i];
+ }
+ } else if (mode == INI_SEARCH_SUBSTR) {
+ if (strstr((*ini)->section[i]->key, value)) {
+ result = (*ini)->section[i];
+ }
}
}
}
@@ -29,7 +39,7 @@ struct INISection *ini_section_search(struct INIFILE **ini, char *value) {
}
int ini_data_init(struct INIFILE **ini, char *section_name) {
- struct INISection *section = ini_section_search(ini, section_name);
+ struct INISection *section = ini_section_search(ini, INI_SEARCH_EXACT, section_name);
if (section == NULL) {
return 1;
}
@@ -37,10 +47,29 @@ int ini_data_init(struct INIFILE **ini, char *section_name) {
return 0;
}
+int ini_has_key(struct INIFILE *ini, const char *section_name, const char *key) {
+ if (!ini || !section_name || !key) {
+ return 0;
+ }
+ struct INISection *section = ini_section_search(&ini, INI_SEARCH_EXACT, section_name);
+ if (!section) {
+ return 0;
+ }
+ for (size_t i = 0; i < section->data_count; i++) {
+ const struct INIData *data = section->data[i];
+ if (data && data->key) {
+ if (!strcmp(data->key, key)) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
struct INIData *ini_data_get(struct INIFILE *ini, char *section_name, char *key) {
struct INISection *section = NULL;
- section = ini_section_search(&ini, section_name);
+ section = ini_section_search(&ini, INI_SEARCH_EXACT, section_name);
if (!section) {
return NULL;
}
@@ -61,7 +90,10 @@ struct INIData *ini_getall(struct INIFILE *ini, char *section_name) {
struct INIData *result = NULL;
static size_t i = 0;
- section = ini_section_search(&ini, section_name);
+ section = ini_section_search(&ini, INI_SEARCH_EXACT, section_name);
+ if (!section) {
+ return NULL;
+ }
if (i == section->data_count) {
i = 0;
return NULL;
@@ -141,7 +173,7 @@ int ini_getval(struct INIFILE *ini, char *section_name, char *key, int type, uni
}
int ini_data_append(struct INIFILE **ini, char *section_name, char *key, char *value) {
- struct INISection *section = ini_section_search(ini, section_name);
+ struct INISection *section = ini_section_search(ini, INI_SEARCH_EXACT, section_name);
if (section == NULL) {
return 1;
}
@@ -191,19 +223,31 @@ int ini_data_append(struct INIFILE **ini, char *section_name, char *key, char *v
int ini_setval(struct INIFILE **ini, unsigned type, char *section_name, char *key, char *value) {
struct INISection *section = ini_section_search(ini, INI_SEARCH_EXACT, section_name);
if (section == NULL) {
- return 1;
+ // no section
+ return -1;
}
if (ini_has_key(*ini, section_name, key)) {
if (!type) {
- ini_data_append(ini, section_name, key, value);
+ if (ini_data_append(ini, section_name, key, value)) {
+ // append failed
+ return -1;
+ }
} else {
struct INIData *data = ini_data_get(*ini, section_name, key);
if (data) {
guard_free(data->value);
data->value = strdup(value);
+ if (!data->value) {
+ // allocation failed
+ return -1;
+ }
+ } else {
+ // getting data failed
+ return -1;
}
}
}
+ return 0;
}
int ini_section_create(struct INIFILE **ini, char *key) {