summaryrefslogtreecommitdiffabout
path: root/libical/src/libicalss/icalset.c
Side-by-side diff
Diffstat (limited to 'libical/src/libicalss/icalset.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libical/src/libicalss/icalset.c528
1 files changed, 327 insertions, 201 deletions
diff --git a/libical/src/libicalss/icalset.c b/libical/src/libicalss/icalset.c
index 2120609..0ad2269 100644
--- a/libical/src/libicalss/icalset.c
+++ b/libical/src/libicalss/icalset.c
@@ -20,348 +20,474 @@
This program is free software; you can redistribute it and/or modify
it under the terms of either:
The LGPL as published by the Free Software Foundation, version
2.1, available at: http://www.fsf.org/copyleft/lesser.html
Or:
The Mozilla Public License Version 1.0. You may obtain a copy of
the License at http://www.mozilla.org/MPL/
The Original Code is eric. The Initial Developer of the Original
Code is Eric Busboom
======================================================================*/
#include "ical.h"
#include "icalset.h"
#include "icalfileset.h"
#include "icalfilesetimpl.h"
#include "icaldirset.h"
#include "icaldirsetimpl.h"
#include <stdlib.h>
-/*#include "icalheapset.h"*/
-/*#include "icalmysqlset.h"*/
-
-#define ICALSET_ID "set "
-
-struct icalset_fp {
- void (*free)(icalset* set);
- const char* (*path)(icalset* set);
- void (*mark)(icalset* set);
- icalerrorenum (*commit)(icalset* set);
- icalerrorenum (*add_component)(icalset* set, icalcomponent* comp);
- icalerrorenum (*remove_component)(icalset* set, icalcomponent* comp);
- int (*count_components)(icalset* set,
- icalcomponent_kind kind);
- icalerrorenum (*select)(icalset* set, icalcomponent* gauge);
- void (*clear)(icalset* set);
- icalcomponent* (*fetch)(icalset* set, const char* uid);
- icalcomponent* (*fetch_match)(icalset* set, icalcomponent *comp);
- int (*has_uid)(icalset* set, const char* uid);
- icalerrorenum (*modify)(icalset* set, icalcomponent *old,
- icalcomponent *new);
- icalcomponent* (*get_current_component)(icalset* set);
- icalcomponent* (*get_first_component)(icalset* set);
- icalcomponent* (*get_next_component)(icalset* set);
-};
-
-struct icalset_fp icalset_dirset_fp = {
+#include <string.h>
+#include <errno.h>
+
+#ifdef WITH_BDB4
+#include "icalbdbset.h"
+#include "icalbdbsetimpl.h"
+#endif
+
+/* #define _DLOPEN_TEST */
+#ifdef _DLOPEN_TEST
+#include <sys/types.h>
+#include <dlfcn.h>
+#include <dirent.h>
+#endif
+
+static icalset icalset_dirset_init = {
+ ICAL_DIR_SET,
+ sizeof(icaldirset),
+ NULL,
+ icaldirset_init,
icaldirset_free,
icaldirset_path,
icaldirset_mark,
icaldirset_commit,
icaldirset_add_component,
icaldirset_remove_component,
icaldirset_count_components,
icaldirset_select,
icaldirset_clear,
icaldirset_fetch,
icaldirset_fetch_match,
icaldirset_has_uid,
icaldirset_modify,
icaldirset_get_current_component,
icaldirset_get_first_component,
- icaldirset_get_next_component
+ icaldirset_get_next_component,
+ icaldirset_begin_component,
+ icaldirsetiter_to_next,
+ icaldirsetiter_to_prior
};
-struct icalset_fp icalset_fileset_fp = {
+static icalset icalset_fileset_init = {
+ ICAL_FILE_SET,
+ sizeof(icalfileset),
+ NULL,
+ icalfileset_init,
icalfileset_free,
icalfileset_path,
icalfileset_mark,
icalfileset_commit,
icalfileset_add_component,
icalfileset_remove_component,
icalfileset_count_components,
icalfileset_select,
icalfileset_clear,
icalfileset_fetch,
icalfileset_fetch_match,
icalfileset_has_uid,
icalfileset_modify,
icalfileset_get_current_component,
icalfileset_get_first_component,
- icalfileset_get_next_component
+ icalfileset_get_next_component,
+ icalfileset_begin_component,
+ icalfilesetiter_to_next,
+ NULL
};
-struct icalset_impl {
+#ifdef WITH_BDB4
+static icalset icalset_bdbset_init = {
+ ICAL_BDB_SET,
+ sizeof(icalbdbset),
+ NULL,
+ icalbdbset_init,
+ icalbdbset_free,
+ icalbdbset_path,
+ icalbdbset_mark,
+ icalbdbset_commit,
+ icalbdbset_add_component,
+ icalbdbset_remove_component,
+ icalbdbset_count_components,
+ icalbdbset_select,
+ icalbdbset_clear,
+ icalbdbset_fetch,
+ icalbdbset_fetch_match,
+ icalbdbset_has_uid,
+ icalbdbset_modify,
+ icalbdbset_get_current_component,
+ icalbdbset_get_first_component,
+ icalbdbset_get_next_component,
+ icalbdbset_begin_component,
+ icalbdbsetiter_to_next,
+ NULL
+};
+#endif
- char id[5]; /* "set " */
+#ifdef _DLOPEN_TEST
+static int icalset_init_done = 0;
+static pvl_list icalset_kinds = 0;
- void *derived_impl;
- struct icalset_fp *fp;
-};
+typedef icalset *(*fptr)(void);
-/* Figure out what was actually passed in as the set. This could be a
- set or and of the derived types such as dirset or fileset. Note
- this routine returns a value, not a reference, to avoid memory
- leaks in the methods */
-struct icalset_impl icalset_get_impl(icalset* set)
-{
- struct icalset_impl impl;
-
- memset(&impl,0,sizeof(impl));
- icalerror_check_arg_re( (set!=0),"set",impl);
-
- if(strcmp((char*)set,ICALSET_ID)==0) {
- /* It is actually a set, so just sent the reference back out. */
- return *(struct icalset_impl*)set;
- } else if(strcmp((char*)set,ICALFILESET_ID)==0) {
- /* Make a new set from the fileset */
- impl.fp = &icalset_fileset_fp;
- impl.derived_impl = set;
- strcpy(impl.id,ICALFILESET_ID);/* HACK. Is this necessary? */
- return impl;
- } else if(strcmp((char*)set,ICALDIRSET_ID)==0) {
- /* Make a new set from the dirset */
- impl.fp = &icalset_dirset_fp;
- impl.derived_impl = set;
- strcpy(impl.id,ICALDIRSET_ID);/* HACK. Is this necessary? */
- return impl;
- } else {
- /* The type of set is unknown, so throw an error */
- icalerror_assert((0),"Unknown set type");
- return impl;
- }
+/**
+ * Try to load the file and register any icalset found within.
+ */
+static int load(const char *file) {
+
+ void *modh;
+ fptr inith;
+ icalset *icalset_init_ptr;
+
+ if ((modh = dlopen(file, RTLD_NOW)) == 0) {
+ perror("dlopen");
+ return 0;
+ }
+
+ if ((inith = (fptr)dlsym(modh, "InitModule")) == 0) {
+ perror("dlsym");
+ return 0;
+ }
+
+ while ((icalset_init_ptr = ((inith)())) != 0) {
+ pvl_push(icalset_kinds, &icalset_init_ptr);
+ }
+
+ return 1;
}
+/**
+ * Look in the given directory for files called mod_*.o and try to
+ * load them.
+ */
+int icalset_loaddir(const char *path) {
+ DIR *d;
+ struct dirent *dp;
+ char buf[PATH_MAX],
+ *bufptr;
+ int tot = 0;
-struct icalset_impl* icalset_new_impl()
-{
-
- struct icalset_impl* impl;
+ strcpy(buf, path);
+ bufptr = buf + strlen(buf);
- if ( ( impl = (struct icalset_impl*)
- malloc(sizeof(struct icalset_impl))) == 0) {
- icalerror_set_errno(ICAL_NEWFAILED_ERROR);
- return 0;
- }
+ if (*(bufptr-1) != '/')
+ *bufptr++ = '/';
- strcpy(impl->id,ICALSET_ID);
+ if ((d = opendir(path)) == 0) {
+ perror("opendir");
+ return 0;
+ }
- impl->derived_impl = 0;
- impl->fp = 0;
+ while ((dp = readdir(d)) != 0) {
+ if (strncmp(dp->d_name, "mod_", 4)) continue;
- return impl;
-}
+ strcpy(bufptr, dp->d_name);
-struct icalset_impl* icalset_new_file_from_ref(icalfileset *fset)
-{
- struct icalset_impl *impl = icalset_new_impl();
+ load(buf);
+ tot++;
+ }
+ (void)closedir(d);
- icalerror_check_arg_rz( (fset!=0),"fset");
+ return 1;
+}
- if(impl == 0){
- free(impl);
- return 0;
- }
+int icalset_register_class(icalset *set);
- impl->derived_impl = fset;
+static void icalset_init(void) {
+ assert(icalset_kinds == 0);
+ icalset_kinds = pvl_newlist();
- if (impl->derived_impl == 0){
- free(impl);
- return 0;
- }
+ pvl_push(icalset_kinds, &icalset_fileset_init);
+ pvl_push(icalset_kinds, &icalset_dirset_init);
+#ifdef WITH_BDB4
+ pvl_push(icalset_kinds, &icalset_bdb4set_init);
+#endif
- impl->fp = &icalset_fileset_fp;
+#ifdef EXT_PATH
+ icalset_loaddir(EXT_PATH);
+#endif
- return (struct icalset_impl*)impl;
+ icalset_init_done++;
}
-icalset* icalset_new_file(const char* path)
-{
- icalfileset *fset = icalfileset_new(path);
+int icalset_register_class(icalset *set) {
- if(fset == 0){
- return 0;
- }
+ if (!icalset_init_done)
+ icalset_init();
- return (icalset*)icalset_new_file_from_ref(fset);
+ pvl_push(icalset_kinds, set);
+ return 1;
}
-icalset* icalset_new_dir_from_ref(icaldirset *dset)
-{
+#endif
- struct icalset_impl *impl = icalset_new_impl();
+icalset* icalset_new(icalset_kind kind, const char* dsn, void* options) {
+ icalset *data = NULL;
+ icalset *ret = NULL;
- icalerror_check_arg_rz( (dset!=0),"dset");
+#ifdef _DLOPEN_TEST
+ pvl_elem e;
+ icalset *impl;
- if(impl == 0){
- return 0;
+ if (!icalset_init_done)
+ icalset_init();
+
+ for(e = pvl_head(icalset_kinds); e!=0; e = pvl_next(e)) {
+ impl = (icalset*)pvl_data(e);
+ if (impl->kind == kind)
+ break;
+ }
+ if (e == 0) {
+ icalerror_set_errno(ICAL_UNIMPLEMENTED_ERROR);
+ return(NULL);
}
- impl->derived_impl = dset;
+ data = (icalset*)malloc(impl->size);
+ if (data == 0) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ errno = ENOMEM;
+ return 0;
+ }
- if (impl->derived_impl == 0){
- free(impl);
+ /* The first member of the derived class must be an icalset. */
+ memset(data,0,impl->size);
+ /* *data = *impl; */
+ memcpy(data, impl, sizeof(icalset));
+
+ data->dsn = strdup(dsn);
+#else
+ switch(kind) {
+ case ICAL_FILE_SET:
+ data = (icalset*) malloc(sizeof(icalfileset));
+ if (data == 0) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ errno = ENOMEM;
+ return 0;
+ }
+ memset(data,0,sizeof(icalfileset));
+ *data = icalset_fileset_init;
+ break;
+ case ICAL_DIR_SET:
+ data = (icalset*) malloc(sizeof(icaldirset));
+ if (data == 0) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ errno = ENOMEM;
+ return 0;
+ }
+ memset(data,0,sizeof(icaldirset));
+ *data = icalset_dirset_init;
+ break;
+#ifdef WITH_BDB4
+ case ICAL_BDB_SET:
+ data = (icalset*) malloc(sizeof(icalbdbset));
+ if (data == 0) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ errno = ENOMEM;
return 0;
}
+ memset(data,0,sizeof(icalbdbset));
+ *data = icalset_bdbset_init;
+ break;
+#endif
+
+ default:
+ icalerror_set_errno(ICAL_UNIMPLEMENTED_ERROR);
+ /** unimplemented **/
+ return(NULL);
+ }
+
+ if ( data == 0) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ return 0;
+ }
+ data->kind = kind;
+ data->dsn = strdup(dsn);
+#endif
- impl->fp = &icalset_dirset_fp;
+ /** call the implementation specific initializer **/
+ if ((ret = data->init(data, dsn, options)) == NULL)
+ icalset_free(data);
- return impl;
+ return ret;
}
-icalset* icalset_new_dir(const char* path)
+icalset* icalset_new_file(const char* path)
{
- icaldirset *dset = icaldirset_new(path);
-
- if(dset == 0){
- return 0;
- }
+ return icalset_new(ICAL_FILE_SET, path, NULL);
+}
- return icalset_new_dir_from_ref(dset);
+icalset* icalset_new_file_writer(const char* path)
+{
+ return icalfileset_new_writer(path);
}
-icalset* icalset_new_heap(void)
+icalset* icalset_new_file_reader(const char* path)
{
- struct icalset_impl *impl = icalset_new_impl();
+ return icalfileset_new_reader(path);
+}
- if(impl == 0){
- free(impl);
- return 0;
- }
+icalset* icalset_new_dir(const char* path)
+{
+ return icalset_new(ICAL_DIR_SET, path, NULL);
+}
- return 0;
+icalset* icalset_new_dir_writer(const char* path)
+{
+ return icaldirset_new_writer(path);
}
-icalset* icalset_new_mysql(const char* path)
+icalset* icalset_new_dir_reader(const char* path)
{
- struct icalset_impl *impl = icalset_new_impl();
+ return icaldirset_new_reader(path);
+}
- if(impl == 0){
- free(impl);
- return 0;
- }
- return 0;
-}
+
+/* Functions for built-in methods */
+
+/**
+ * free memory associated with this icalset
+ * automatically calls the implementation specific free routine
+ */
void icalset_free(icalset* set)
{
- struct icalset_impl impl = icalset_get_impl(set);
- (*(impl.fp->free))(impl.derived_impl);
+ if (set->free)
+ set->free(set);
- if(strcmp((char*)set,ICALSET_ID)) {
- free(set);
- }
-}
+ if (set->dsn)
+ free(set->dsn);
-const char* icalset_path(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->path))(impl.derived_impl);
+ free(set);
}
-void icalset_mark(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- (*(impl.fp->mark))(impl.derived_impl);
+
+const char* icalset_path(icalset* set) {
+ return set->path(set);
}
-icalerrorenum icalset_commit(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->commit))(impl.derived_impl);
+void icalset_mark(icalset* set) {
+ set->mark(set);
}
-icalerrorenum icalset_add_component(icalset* set, icalcomponent* comp)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->add_component))(impl.derived_impl,comp);
+icalerrorenum icalset_commit(icalset* set) {
+ return set->commit(set);
}
-icalerrorenum icalset_remove_component(icalset* set, icalcomponent* comp)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->remove_component))(impl.derived_impl,comp);
+icalerrorenum icalset_add_component(icalset* set, icalcomponent* comp) {
+ return set->add_component(set,comp);
}
-int icalset_count_components(icalset* set,icalcomponent_kind kind)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->count_components))(impl.derived_impl,kind);
+icalerrorenum icalset_remove_component(icalset* set, icalcomponent* comp) {
+ return set->remove_component(set,comp);
}
-icalerrorenum icalset_select(icalset* set, icalcomponent* gauge)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->select))(impl.derived_impl,gauge);
+int icalset_count_components(icalset* set,icalcomponent_kind kind) {
+ return set->count_components(set,kind);
}
-void icalset_clear(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- (*(impl.fp->clear))(impl.derived_impl);
+icalerrorenum icalset_select(icalset* set, icalgauge* gauge) {
+ return set->select(set, gauge);
}
-icalcomponent* icalset_fetch(icalset* set, const char* uid)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->fetch))(impl.derived_impl,uid);
+void icalset_clear(icalset* set) {
+ set->clear(set);
}
-icalcomponent* icalset_fetch_match(icalset* set, icalcomponent *comp)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->fetch_match))(impl.derived_impl,comp);
+icalcomponent* icalset_fetch(icalset* set, const char* uid) {
+ return set->fetch(set, uid);
}
+icalcomponent* icalset_fetch_match(icalset* set, icalcomponent *comp) {
+ return set->fetch_match(set, comp);
+}
-int icalset_has_uid(icalset* set, const char* uid)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->has_uid))(impl.derived_impl,uid);
+int icalset_has_uid(icalset* set, const char* uid) {
+ return set->has_uid(set, uid);
}
icalerrorenum icalset_modify(icalset* set, icalcomponent *old,
- icalcomponent *new)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->modify))(impl.derived_impl,old,new);
+ icalcomponent *new) {
+ return set->modify(set, old, new);
}
-icalcomponent* icalset_get_current_component(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->get_current_component))(impl.derived_impl);
+icalcomponent* icalset_get_current_component(icalset* set) {
+ return set->get_current_component(set);
}
-icalcomponent* icalset_get_first_component(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->get_first_component))(impl.derived_impl);
+icalcomponent* icalset_get_first_component(icalset* set) {
+ return set->get_first_component(set);
}
-icalcomponent* icalset_get_next_component(icalset* set)
-{
- struct icalset_impl impl = icalset_get_impl(set);
- return (*(impl.fp->get_next_component))(impl.derived_impl);
+icalcomponent* icalset_get_next_component(icalset* set) {
+ return set->get_next_component(set);
+}
+
+icalsetiter icalsetiter_null = {{ICAL_NO_COMPONENT, 0}, 0};
+
+icalsetiter icalset_begin_component(icalset* set,
+ icalcomponent_kind kind, icalgauge* gauge) {
+ return set->icalset_begin_component(set, kind, gauge);
+}
+
+icalcomponent* icalsetiter_next(icalsetiter* itr) {
+
+ icalcomponent* c = 0;
+ icalerror_check_arg_rz( (itr != NULL), "i");
+
+ do {
+ c = icalcompiter_next(&(itr->iter));
+ if(c != 0 && (itr->gauge == 0 ||
+ icalgauge_compare(itr->gauge, c) == 1)){
+ return c;
+ }
+ } while (c != 0);
+
+ return 0;
}
+icalcomponent* icalsetiter_prior(icalsetiter* i) {
+
+ icalcomponent* c = 0;
+ icalerror_check_arg_rz( (i != NULL), "i" );
+
+ do {
+ c = icalcompiter_prior(&(i->iter));
+ if(c != 0 && (i->gauge == 0 ||
+ icalgauge_compare(i->gauge, c) == 1)){
+ return c;
+ }
+ } while (c != 0);
+
+ return 0;
+}
+icalcomponent* icalsetiter_deref(icalsetiter* i) {
+ icalerror_check_arg_rz( (i != NULL), "i" );
+ return (icalcompiter_deref(&(i->iter)));
+}
+/* for subclasses that use multiple clusters that require specialized cluster traversal */
+icalcomponent* icalsetiter_to_next(icalset* set, icalsetiter* i)
+{
+ return set->icalsetiter_to_next(set, i);
+}
+icalcomponent* icalsetiter_to_prior(icalset* set, icalsetiter* i)
+{
+ return set->icalsetiter_to_prior(set, i);
+}