aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
index 9093875..23fda50 100644
--- a/src/common.c
+++ b/src/common.c
@@ -380,3 +380,108 @@ SLAPT_BOOL_T slapt_disk_space_check (const char *path,double space_needed)
return SLAPT_TRUE;
}
+slapt_list_t *slapt_init_list(void)
+{
+ slapt_list_t *list = slapt_malloc(sizeof *list);
+ list->items = slapt_malloc(sizeof *list->items);
+ list->count = 0;
+
+ return list;
+}
+
+void slapt_add_list_item(slapt_list_t *list,const char *item)
+{
+ char **realloc_tmp;
+
+ realloc_tmp =
+ realloc( list->items, sizeof *list->items * (list->count + 1) );
+
+ if ( realloc_tmp == NULL )
+ return;
+
+ list->items = realloc_tmp;
+ list->items[ list->count ] = strndup(item, strlen(item));
+ list->items[ list->count ][strlen(item)] = '\0';
+ ++list->count;
+
+}
+
+void slapt_remove_list_item(slapt_list_t *list, const char *item)
+{
+ unsigned int i = 0;
+ char *tmp = NULL;
+
+ while (i < list->count) {
+ if ( strcmp(item,list->items[i]) == 0 && tmp == NULL ) {
+ tmp = list->items[i];
+ }
+ if ( tmp != NULL && (i+1 < list->count) ) {
+ list->items[i] = list->items[i + 1];
+ }
+ ++i;
+ }
+ if ( tmp != NULL ) {
+ char **realloc_tmp;
+ int count = list->count - 1;
+ if ( count < 1 )
+ count = 1;
+
+ free(tmp);
+
+ realloc_tmp = realloc(list->items, sizeof *list->items * count);
+ if ( realloc_tmp != NULL ) {
+ list->items = realloc_tmp;
+ if (list->count > 0)
+ --list->count;
+ }
+
+ }
+}
+
+void slapt_free_list(slapt_list_t *list)
+{
+ unsigned int i;
+
+ for (i = 0; i < list->count; ++i) {
+ free(list->items[i]);
+ }
+ free(list->items);
+ free(list);
+}
+
+slapt_list_t *slapt_parse_delimited_list(char *line, char delim)
+{
+ slapt_list_t *list = slapt_init_list();
+ int count = 0, position = 0, len = strlen(line);
+
+ while (isspace(line[position]) != 0)
+ ++position;
+
+ while (position < len) {
+ char *start = line + position;
+ char *end = NULL, *ptr = NULL;
+ int start_len = strlen(start), end_len = 0;
+
+ if (strchr(start,delim) != NULL) {
+ if (line[position] == delim || isspace(line[position]) != 0) {
+ ++position;
+ continue;
+ }
+ end = strchr(start,delim);
+ }
+
+ if (end != NULL)
+ end_len = strlen(end);
+
+ ptr = strndup(start, start_len - end_len);
+
+ slapt_add_list_item(list, ptr);
+ free(ptr);
+ ++count;
+
+ position += start_len - end_len;
+ }
+
+ return list;
+}
+