123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "environment.h"
- #include "data/bootstrap.h"
- /** @brief Default context.
- *
- * The actual default context IRI used throughout the application may use a
- * value from the `LSUP_DEFAULT_CTX_URI` environment variable.
- */
- #define DEFAULT_CTX_LABEL LSUP_NS "default"
- /*
- * External variables.
- */
- LSUP_NSMap *LSUP_default_nsm = NULL;
- LSUP_Term *LSUP_default_ctx = NULL;
- LSUP_Buffer *LSUP_default_ctx_buf = NULL;
- /*
- * API
- */
- LSUP_rc
- LSUP_init (void)
- {
- if (LSUP_IS_INIT) {
- LOG_RC (LSUP_NOACTION);
- return LSUP_NOACTION;
- }
- #ifdef DEBUG
- // In debug mode, always use max logging.
- int loglevel = LOG_TRACE;
- #else
- char *_loglevel = getenv ("LSUP_LOGLEVEL");
- int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
- #endif
- log_set_level (loglevel);
- // Default namespace map.
- LSUP_default_nsm = LSUP_nsmap_new();
- if (UNLIKELY (!LSUP_default_nsm)) return LSUP_ERROR;
- for (int i = 0; init_nsmap[i][0] != NULL; i++)
- LSUP_nsmap_add (LSUP_default_nsm, init_nsmap[i][0], init_nsmap[i][1]);
- // Default context URI.
- char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
- if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL;
- LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, NULL);
- if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR;
- LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx);
- if (UNLIKELY (!LSUP_default_ctx_buf)) return LSUP_ERROR;
- // Initialize term cache.
- LSUP_term_cache = LSUP_term_set_new();
- if (UNLIKELY (!LSUP_term_cache)) return LSUP_MEM_ERR;
- // Create and cache default literal datatype key.
- // This will be done only once in the program, so no need to check for
- // duplicates.
- // This is the last operation that can fail in this function, and it is
- // the indicator for LSUP_IS_INIT.
- LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL);
- LSUP_rc rc = LSUP_term_set_add (
- LSUP_term_cache, LSUP_default_datatype, NULL);
- PRCCK (rc);
- log_info ("LSUP environment initialized.");
- // Set automatic teardown TODO Is this a good idea?
- atexit (LSUP_done);
- return LSUP_OK;
- }
- void
- LSUP_done (void)
- {
- if (!LSUP_IS_INIT) return;
- // Free default NS map and context.
- LSUP_buffer_free (LSUP_default_ctx_buf);
- LSUP_term_free (LSUP_default_ctx);
- LSUP_nsmap_free (LSUP_default_nsm);
- // Free ID cache, including default literal datatype.
- hashmap_free (LSUP_term_cache);
- LSUP_default_datatype = NULL; // This causes LSUP_IS_INIT to return false.
- log_info ("LSUP environment torn down.");
- }
- /*
- * Extern inline prototypes.
- */
- bool LSUP_is_init (void);
|