1 /*-------------------------------------------------------------------------
2 * C-Pluff, a plug-in framework for C
3 * Copyright 2007 Johannes Lehtinen
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *-----------------------------------------------------------------------*/
25 * Core framework functions
45 /* ------------------------------------------------------------------------
47 * ----------------------------------------------------------------------*/
49 /// Number of initializations
50 static int initialized = 0;
55 static cpi_mutex_t *framework_mutex = NULL;
57 #elif !defined(NDEBUG)
59 /// Framework locking count
60 static int framework_locked = 0;
64 /// Fatal error handler, or NULL for default
65 static cp_fatal_error_func_t fatal_error_handler = NULL;
68 /* ------------------------------------------------------------------------
69 * Function definitions
70 * ----------------------------------------------------------------------*/
72 CP_C_API const char *cp_get_version(void) {
76 CP_C_API const char *cp_get_host_type(void) {
80 CP_HIDDEN void cpi_lock_framework(void) {
81 #if defined(CP_THREADS)
82 cpi_lock_mutex(framework_mutex);
83 #elif !defined(NDEBUG)
88 CP_HIDDEN void cpi_unlock_framework(void) {
89 #if defined(CP_THREADS)
90 cpi_unlock_mutex(framework_mutex);
91 #elif !defined(NDEBUG)
92 assert(framework_locked > 0);
97 static void reset(void) {
99 if (framework_mutex != NULL) {
100 cpi_destroy_mutex(framework_mutex);
101 framework_mutex = NULL;
106 CP_C_API cp_status_t cp_init(void) {
107 cp_status_t status = CP_OK;
109 // Initialize if necessary
112 bindtextdomain(PACKAGE, CP_DATADIR CP_FNAMESEP_STR "locale");
114 if ((framework_mutex = cpi_create_mutex()) == NULL) {
115 status = CP_ERR_RESOURCE;
119 #ifdef DLOPEN_LIBTOOL
121 status = CP_ERR_RESOURCE;
129 // Rollback on failure
130 if (status != CP_OK) {
137 CP_C_API void cp_destroy(void) {
138 assert(initialized > 0);
142 assert(framework_mutex == NULL || !cpi_is_mutex_locked(framework_mutex));
144 assert(!framework_locked);
146 cpi_destroy_all_contexts();
147 #ifdef DLOPEN_LIBTOOL
154 CP_C_API void cp_set_fatal_error_handler(cp_fatal_error_func_t error_handler) {
155 fatal_error_handler = error_handler;
158 CP_HIDDEN void cpi_fatalf(const char *msg, ...) {
164 va_start(params, msg);
165 vsnprintf(fmsg, sizeof(fmsg), msg, params);
167 fmsg[sizeof(fmsg)/sizeof(char) - 1] = '\0';
169 // Call error handler or print the error message
170 if (fatal_error_handler != NULL) {
171 fatal_error_handler(fmsg);
173 fprintf(stderr, _("C-Pluff: FATAL ERROR: %s\n"), fmsg);
176 // Abort if still alive
180 CP_HIDDEN void cpi_fatal_null_arg(const char *arg, const char *func) {
181 cpi_fatalf(_("Argument %s has illegal NULL value in call to function %s."), arg, func);