static void wftk_session_free (WFTK_SESSION * sess); WFTK_EXPORT XML * wftk_session_alloc () { WFTK_SESSION * sess; XML * ret = xml_create ("wftk-session"); sess = (WFTK_SESSION *) malloc (sizeof (struct wftk_session)); memset ((void *) sess, '\0', sizeof (struct wftk_session)); xml_setbin (ret, sess, (XML_SETBIN_FREE_FN *)wftk_session_free); return ret; } static void wftk_session_free (WFTK_SESSION * sess) { WFTK_ADAPTOR_LIST * list; WFTK_CACHE_LIST * cachelist; if (!sess) return; while (sess->ads) { wftk_free_adaptor (NULL, sess->ads->ad); list = sess->ads->next; free (sess->ads); sess->ads = list; } while (sess->cache) { xml_free (sess->cache->cached); cachelist = sess->cache->next; free(sess->cache); sess->cache = cachelist; } if (sess->user) xml_free (sess->user); if (sess->context) xml_free (sess->context); if (sess->config) xml_free (sess->config); if (sess->datasheet) xml_free (sess->datasheet); if (sess->procdef) xml_free (sess->procdef); if (sess->values) xml_free (sess->values); } WFTK_EXPORT void * wftk_session_init (XML * session) { WFTK_SESSION * sess; XML * mark; if (!session) return NULL; if (!xml_is (session, "wftk-session")) { mark = xml_search (session, "wftk-session", NULL, NULL); if (!mark) { mark = xml_create ("wftk-session"); xml_prepend (session, xml_createtext ("\n")); xml_prepend (session, mark); xml_prepend (session, xml_createtext ("\n")); } session = mark; } sess = xml_getbin (session); if (!sess) { sess = (WFTK_SESSION *) malloc (sizeof (struct wftk_session)); memset ((void *) sess, '\0', sizeof (struct wftk_session)); xml_setbin (session, sess, (XML_SETBIN_FREE_FN *)wftk_session_free); } return sess; } WFTK_EXPORT void wftk_session_cleanup (XML * session) { XML * mark; WFTK_SESSION * sess; if (xml_is (session, "wftk-session")) { sess = xml_getbin (session); if (sess) { wftk_session_free (sess); xml_setbin (session, NULL, NULL); } } else { mark = xml_search (session, "wftk-session", NULL, NULL); if (mark) xml_delete (mark); } } |
WFTK_EXPORT void wftk_session_configure (XML * session, XML * config) { WFTK_SESSION * sess = wftk_session_init (session); sess->config = config; } WFTK_EXPORT void wftk_session_setlookup (XML * session, WFTK_MODULE_LOOKUP_FN fn) { WFTK_SESSION * sess = wftk_session_init (session); sess->static_module_lookup = fn; } |
wftk_session_current
call associates a workflow object (in XML format) with the current session so that
subsequent calls to the library needn't look the object up again. Ideally this should be transparent. That makes freeing
up these objects somewhat confusing -- if you're not using a session, you have to do it yourself; otherwise, the session will
track things for you. This stuff may get replaced at some point, it's hard to say. It's been going strong for a year now.
WFTK_EXPORT void wftk_session_current (XML * session, XML * object) { WFTK_SESSION * sess = wftk_session_init (session); if (!sess) return; if (!object) return; if (xml_is (object, "datasheet")) { sess->datasheet = object; return; } if (xml_is (object, "procdef")) { sess->procdef = object; return; } } |
wftk_session_cachecheck
function (get it?) checks for the key but doesn't delete it or cache it, either way. This
is used to determine whether we're going to need to read something from the filesystem or not. (In that case, if read, the parser
will return a brand-new XML structure, so it wouldn't be the same one as the key which got cached. Not practical.)
WFTK_EXPORT XML * wftk_session_cache (XML * session, XML * key, int * flag) { WFTK_SESSION * sess = wftk_session_init (session); WFTK_CACHE_LIST * list; XML_ATTR * attr; if (flag) *flag = 0; if (!session) return key; if (!key) return key; return key; /* TODO: figure out whether this is worth saving, and how not to do it wrong. */ list = sess->cache; while (list) { if (xml_is (key, xml_name (list->cached))) { attr = xml_attrfirst (key); while (attr) { if (strcmp (xml_attrvalue (attr), xml_attrval (list->cached, xml_attrname(attr)))) break; attr = xml_attrnext (attr); } if (!attr) break; } list = list->next; } if (list) { if (flag) *flag = 1; xml_free (key); return list->cached; } list = sess->cache; sess->cache = (WFTK_CACHE_LIST *) malloc (sizeof (WFTK_CACHE_LIST)); sess->cache->cached = key; sess->cache->next = list; return key; } WFTK_EXPORT XML * wftk_session_cachecheck (XML * session, XML * key) { WFTK_SESSION * sess = wftk_session_init (session); WFTK_CACHE_LIST * list; XML_ATTR * attr; if (!session) return NULL; if (!key) return NULL; list = sess->cache; while (list) { if (xml_is (key, xml_name(list->cached))) { attr = xml_attrfirst (key); while (attr) { if (strcmp (xml_attrvalue (attr), xml_attrval (list->cached, xml_attrname(attr)))) break; attr = xml_attrnext (attr); } if (!attr) break; } list = list->next; } if (list) { return list->cached; /* Found. */ } return NULL; /* Not found. */ } |
wftk_session_setuser
creates a user structure and assigns the given userid to it. More will certainly
be done with this later. This function is not intended to include authentication of the userid. The wftk_session_getuser
just returns the user structure with no further ado. Of course, the caller may then change said structure (providing a
way to do that authentication.)
WFTK_EXPORT void wftk_session_setuser (XML * session, char * userid) { WFTK_SESSION * sess = wftk_session_init (session); if (!session) return; if (sess->user) { xml_free (sess->user); } sess->user = xml_create ("user"); xml_set (sess->user, "id", userid); } WFTK_EXPORT void wftk_session_storeuser (XML * session, XML * user) { WFTK_SESSION * sess = wftk_session_init (session); if (!session) return; if (sess->user) { xml_free (sess->user); } sess->user = user; } WFTK_EXPORT XML * wftk_session_getuser (XML * session) { WFTK_SESSION * sess = wftk_session_init (session); if (!session) return NULL; return (sess->user); } |
wftk_session_setcontext
takes a context object and places it in the current session,
freeing any previously stored context if necessary. The wftk_session_getcontext
then
simply retrieves a pointer to that context. A NULL context may be passed.
WFTK_EXPORT void wftk_session_setcontext (XML * session, XML * context) { WFTK_SESSION * sess = wftk_session_init (session); if (!session) return; if (sess->context) { xml_free (sess->context); } sess->context = context; } WFTK_EXPORT XML * wftk_session_getcontext (XML * session) { WFTK_SESSION * sess = wftk_session_init (session); if (!session) return NULL; return (sess->context); } |
WFTK_EXPORT XML * wftk_session_stashvalue (XML * session, const char * value) { WFTK_SESSION * sess = wftk_session_init (session); XML * holder; if (!sess->values) sess->values = xml_create ("list"); holder = xml_create ("value"); xml_set (holder, "value", value); xml_append (sess->values, holder); return holder; } WFTK_EXPORT void wftk_session_freevalue (XML * session, const char * value) { WFTK_SESSION * sess = wftk_session_init (session); XML_ATTR * attribute; XML * pointer = xml_firstelem (sess->values); while (pointer) { attribute = xml_attrfirst (pointer); while (attribute) { if (xml_attrvalue (attribute) == value) { /* Note test of pointer equality! */ xml_delete (pointer); return; } attribute = xml_attrnext (attribute); } pointer = xml_nextelem (pointer); } } |
This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2002, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. |