environment.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "environment.h"
  2. #include "data/bootstrap.h"
  3. /** @brief Default context.
  4. *
  5. * The actual default context IRI used throughout the application may use a
  6. * value from the `LSUP_DEFAULT_CTX_URI` environment variable.
  7. */
  8. #define DEFAULT_CTX_LABEL LSUP_NS "default"
  9. /*
  10. * External variables.
  11. */
  12. LSUP_NSMap *LSUP_default_nsm = NULL;
  13. LSUP_Term *LSUP_default_ctx = NULL;
  14. LSUP_Buffer *LSUP_default_ctx_buf = NULL;
  15. /*
  16. * API
  17. */
  18. LSUP_rc
  19. LSUP_init (void)
  20. {
  21. if (LSUP_IS_INIT) {
  22. LOG_RC (LSUP_NOACTION);
  23. return LSUP_NOACTION;
  24. }
  25. #ifdef DEBUG
  26. // In debug mode, always use max logging.
  27. int loglevel = LOG_TRACE;
  28. #else
  29. char *_loglevel = getenv ("LSUP_LOGLEVEL");
  30. int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
  31. #endif
  32. log_set_level (loglevel);
  33. // Default namespace map.
  34. LSUP_default_nsm = LSUP_nsmap_new();
  35. if (UNLIKELY (!LSUP_default_nsm)) return LSUP_ERROR;
  36. for (int i = 0; init_nsmap[i][0] != NULL; i++)
  37. LSUP_nsmap_add (LSUP_default_nsm, init_nsmap[i][0], init_nsmap[i][1]);
  38. // Default context URI.
  39. char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
  40. if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL;
  41. LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, NULL);
  42. if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR;
  43. LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx);
  44. if (UNLIKELY (!LSUP_default_ctx_buf)) return LSUP_ERROR;
  45. // Initialize term cache.
  46. LSUP_term_cache = LSUP_term_set_new();
  47. if (UNLIKELY (!LSUP_term_cache)) return LSUP_MEM_ERR;
  48. // Create and cache default literal datatype key.
  49. // This will be done only once in the program, so no need to check for
  50. // duplicates.
  51. // This is the last operation that can fail in this function, and it is
  52. // the indicator for LSUP_IS_INIT.
  53. LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL);
  54. LSUP_rc rc = LSUP_term_set_add (
  55. LSUP_term_cache, LSUP_default_datatype, NULL);
  56. PRCCK (rc);
  57. log_info ("LSUP environment initialized.");
  58. // Set automatic teardown TODO Is this a good idea?
  59. atexit (LSUP_done);
  60. return LSUP_OK;
  61. }
  62. void
  63. LSUP_done (void)
  64. {
  65. if (!LSUP_IS_INIT) return;
  66. // Free default NS map and context.
  67. LSUP_buffer_free (LSUP_default_ctx_buf);
  68. LSUP_term_free (LSUP_default_ctx);
  69. LSUP_nsmap_free (LSUP_default_nsm);
  70. // Free ID cache, including default literal datatype.
  71. hashmap_free (LSUP_term_cache);
  72. LSUP_default_datatype = NULL; // This causes LSUP_IS_INIT to return false.
  73. log_info ("LSUP environment torn down.");
  74. }
  75. /*
  76. * Extern inline prototypes.
  77. */
  78. bool LSUP_is_init (void);