summaryrefslogtreecommitdiff
path: root/libpeas
diff options
context:
space:
mode:
authorChristian Hergert <chergert@redhat.com>2020-01-19 17:21:30 -0800
committerChristian Hergert <chergert@redhat.com>2020-01-19 17:24:35 -0800
commitec20d0fac1aa90ed17bbce1dd3ce9354638eef84 (patch)
treea057c7b63fa4194e0ff41efc519c64bb3457c366 /libpeas
parenta0664d356601ff035904d902e1b34c4b2337e0cc (diff)
downloadlibpeas-ec20d0fac1aa90ed17bbce1dd3ce9354638eef84.tar.gz
i18n: simplify i18n for use within libpeas
We don't need a public (but not exposed via headers) function in our ABI to do gettext. Instead, we can just wrap _() to use g_dgettext() with a predefined GETTEXT_PACKAGE. Additionally, instead of requiring callers to check/initialize the libpeas gettext textdomain, we can use a static constructor to set that up once at startup. Related !24 Fixes #35
Diffstat (limited to 'libpeas')
-rw-r--r--libpeas/gconstructor.h94
-rw-r--r--libpeas/peas-engine.c2
-rw-r--r--libpeas/peas-extension-set.c2
-rw-r--r--libpeas/peas-i18n-priv.h32
-rw-r--r--libpeas/peas-i18n.c43
-rw-r--r--libpeas/peas-i18n.h75
-rw-r--r--libpeas/peas-plugin-info.c3
7 files changed, 146 insertions, 105 deletions
diff --git a/libpeas/gconstructor.h b/libpeas/gconstructor.h
new file mode 100644
index 0000000..df98f83
--- /dev/null
+++ b/libpeas/gconstructor.h
@@ -0,0 +1,94 @@
+/*
+ If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
+ destructors, in a sane way, including e.g. on library unload. If not you're on
+ your own.
+
+ Some compilers need #pragma to handle this, which does not work with macros,
+ so the way you need to use this is (for constructors):
+
+ #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+ #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
+ #endif
+ G_DEFINE_CONSTRUCTOR(my_constructor)
+ static void my_constructor(void) {
+ ...
+ }
+
+*/
+
+#ifndef __GTK_DOC_IGNORE__
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
+
+#define G_HAS_CONSTRUCTORS 1
+
+#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
+#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
+
+#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
+/* Visual studio 2008 and later has _Pragma */
+
+#define G_HAS_CONSTRUCTORS 1
+
+#define G_DEFINE_CONSTRUCTOR(_func) \
+ static void _func(void); \
+ static int _func ## _wrapper(void) { _func(); return 0; } \
+ __pragma(section(".CRT$XCU",read)) \
+ __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
+
+#define G_DEFINE_DESTRUCTOR(_func) \
+ static void _func(void); \
+ static int _func ## _constructor(void) { atexit (_func); return 0; } \
+ __pragma(section(".CRT$XCU",read)) \
+ __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
+
+#elif defined (_MSC_VER)
+
+#define G_HAS_CONSTRUCTORS 1
+
+/* Pre Visual studio 2008 must use #pragma section */
+#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
+#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
+
+#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
+ section(".CRT$XCU",read)
+#define G_DEFINE_CONSTRUCTOR(_func) \
+ static void _func(void); \
+ static int _func ## _wrapper(void) { _func(); return 0; } \
+ __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
+
+#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
+ section(".CRT$XCU",read)
+#define G_DEFINE_DESTRUCTOR(_func) \
+ static void _func(void); \
+ static int _func ## _constructor(void) { atexit (_func); return 0; } \
+ __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
+
+#elif defined(__SUNPRO_C)
+
+/* This is not tested, but i believe it should work, based on:
+ * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
+ */
+
+#define G_HAS_CONSTRUCTORS 1
+
+#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
+#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
+
+#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
+ init(_func)
+#define G_DEFINE_CONSTRUCTOR(_func) \
+ static void _func(void);
+
+#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
+ fini(_func)
+#define G_DEFINE_DESTRUCTOR(_func) \
+ static void _func(void);
+
+#else
+
+/* constructors not supported for this compiler */
+
+#endif
+
+#endif /* __GTK_DOC_IGNORE__ */
diff --git a/libpeas/peas-engine.c b/libpeas/peas-engine.c
index b623738..b3f4857 100644
--- a/libpeas/peas-engine.c
+++ b/libpeas/peas-engine.c
@@ -25,7 +25,7 @@
#include <string.h>
-#include "peas-i18n.h"
+#include "peas-i18n-priv.h"
#include "peas-engine.h"
#include "peas-engine-priv.h"
#include "peas-plugin-info-priv.h"
diff --git a/libpeas/peas-extension-set.c b/libpeas/peas-extension-set.c
index 3bbeb09..baeb4a4 100644
--- a/libpeas/peas-extension-set.c
+++ b/libpeas/peas-extension-set.c
@@ -25,7 +25,7 @@
#include "peas-extension-set.h"
-#include "peas-i18n.h"
+#include "peas-i18n-priv.h"
#include "peas-introspection.h"
#include "peas-plugin-info.h"
#include "peas-marshal.h"
diff --git a/libpeas/peas-i18n-priv.h b/libpeas/peas-i18n-priv.h
new file mode 100644
index 0000000..9b01948
--- /dev/null
+++ b/libpeas/peas-i18n-priv.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
+ * All rights reserved.
+ *
+ * libpeas is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * libpeas is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __PEAS_I18N_PRIV_H__
+#define __PEAS_I18N_PRIV_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define _(s) g_dgettext(GETTEXT_PACKAGE, s)
+#define I_(s) g_intern_string(s)
+
+G_END_DECLS
+
+#endif /* __PEAS_I18N_PRIV_H__ */
diff --git a/libpeas/peas-i18n.c b/libpeas/peas-i18n.c
index ceb2b10..f4e9469 100644
--- a/libpeas/peas-i18n.c
+++ b/libpeas/peas-i18n.c
@@ -21,38 +21,29 @@
#include <config.h>
#endif
+#include <glib/gi18n.h>
#include <string.h>
-#include "peas-i18n.h"
#include "peas-dirs.h"
-/**
- * peas_gettext:
- * @msgid: The string to be translated
- *
- * Returns the translated string from the libpeas translations.
- * This is an internal function and should only be used by
- * the internals of libpeas (such as libpeas or libpeas-gtk).
- *
- * Returns: the transation of @msgid to the current locale
- */
-const gchar *
-peas_gettext (const gchar *msgid)
-{
- static gboolean initialized = FALSE;
+#include "gconstructor.h"
- if (G_UNLIKELY (!initialized))
- {
- gchar *locale_dir;
-
- locale_dir = peas_dirs_get_locale_dir ();
+#if defined (G_HAS_CONSTRUCTORS)
+# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
+# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(peas_init_ctor)
+# endif
+G_DEFINE_CONSTRUCTOR(peas_init_ctor)
+#else
+# error Your platform/compiler is missing constructor support
+#endif
- (void) bindtextdomain (GETTEXT_PACKAGE, locale_dir);
- g_free (locale_dir);
+static void
+peas_init_ctor (void)
+{
+ gchar *locale_dir = peas_dirs_get_locale_dir ();
- (void) bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- initialized = TRUE;
- }
+ bindtextdomain (GETTEXT_PACKAGE, locale_dir);
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- return g_dgettext (GETTEXT_PACKAGE, msgid);
+ g_free (locale_dir);
}
diff --git a/libpeas/peas-i18n.h b/libpeas/peas-i18n.h
deleted file mode 100644
index 664c068..0000000
--- a/libpeas/peas-i18n.h
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
- * All rights reserved.
- *
- * libpeas is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * libpeas is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * Handles all of the internationalization configuration options.
- * Author: Tom Tromey <tromey@creche.cygnus.com>
- *
- * This is a modified version of gtksourceview-i18n.h
- */
-
-#ifndef __PEAS_18N_H__
-#define __PEAS_18N_H__
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include "peas-version-macros.h"
-
-G_BEGIN_DECLS
-
-#ifdef ENABLE_NLS
-# include <libintl.h>
-# undef _
-# define _(String) peas_gettext (String)
-# undef N_
-# ifdef gettext_noop
-# define N_(String) gettext_noop (String)
-# else
-# define N_(String) (String)
-# endif
-#else
-/* Stubs that do something close enough. */
-# undef textdomain
-# define textdomain(String) (String)
-# undef gettext
-# define gettext(String) (String)
-# undef dgettext
-# define dgettext(Domain,Message) (Message)
-# undef dcgettext
-# define dcgettext(Domain,Message,Type) (Message)
-# undef bindtextdomain
-# define bindtextdomain(Domain,Directory) (Domain)
-# undef bind_textdomain_codeset
-# define bind_textdomain_codeset(Domain,CodeSet) (Domain)
-# undef _
-# define _(String) (String)
-# undef N_
-# define N_(String) (String)
-#endif
-
-PEAS_AVAILABLE_IN_ALL
-const gchar *peas_gettext (const char *msgid) G_GNUC_FORMAT(1);
-
-/* not really I18N-related, but also a string marker macro */
-#define I_(string) g_intern_static_string (string)
-
-G_END_DECLS
-
-#endif /* __PEAS_I18N_H__ */
diff --git a/libpeas/peas-plugin-info.c b/libpeas/peas-plugin-info.c
index 333e80f..6040004 100644
--- a/libpeas/peas-plugin-info.c
+++ b/libpeas/peas-plugin-info.c
@@ -23,9 +23,8 @@
#include "config.h"
#include <string.h>
-#include <glib.h>
-#include "peas-i18n.h"
+#include "peas-i18n-priv.h"
#include "peas-plugin-info-priv.h"
#include "peas-utils.h"