Skip to content

Commit

Permalink
another try to clean up glib dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tagoh committed Oct 5, 2012
1 parent 0a1d171 commit ade8cca
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 93 deletions.
3 changes: 3 additions & 0 deletions liblangtag/langtag.h
Expand Up @@ -14,9 +14,12 @@
#define __LANGTAG_H__ #define __LANGTAG_H__


#define __LANGTAG_H__INSIDE #define __LANGTAG_H__INSIDE
#include <liblangtag/lt-error.h>
#include <liblangtag/lt-database.h> #include <liblangtag/lt-database.h>
#include <liblangtag/lt-ext-module.h> #include <liblangtag/lt-ext-module.h>
#include <liblangtag/lt-extension.h> #include <liblangtag/lt-extension.h>
#include <liblangtag/lt-list.h>
#include <liblangtag/lt-string.h>
#include <liblangtag/lt-tag.h> #include <liblangtag/lt-tag.h>
#undef __LANGTAG_H__INSIDE #undef __LANGTAG_H__INSIDE


Expand Down
4 changes: 4 additions & 0 deletions liblangtag/lt-error.h
Expand Up @@ -10,6 +10,10 @@
* Lesser General Public License or the Mozilla Public * Lesser General Public License or the Mozilla Public
* License, as specified in the README file. * License, as specified in the README file.
*/ */
#if !defined (__LANGTAG_H__INSIDE) && !defined (__LANGTAG_COMPILATION)
#error "Only <liblangtag/langtag.h> can be included directly."
#endif

#ifndef __LT_ERROR_H__ #ifndef __LT_ERROR_H__
#define __LT_ERROR_H__ #define __LT_ERROR_H__


Expand Down
93 changes: 57 additions & 36 deletions liblangtag/lt-ext-module.c
Expand Up @@ -16,6 +16,7 @@


#include <ctype.h> #include <ctype.h>
#include <dirent.h> #include <dirent.h>
#include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
Expand Down Expand Up @@ -201,36 +202,43 @@ lt_ext_module_load(lt_ext_module_t *module)
lt_string_t *fullname = lt_string_new(NULL); lt_string_t *fullname = lt_string_new(NULL);
char *filename = lt_strdup_printf("liblangtag-ext-%s." G_MODULE_SUFFIX, char *filename = lt_strdup_printf("liblangtag-ext-%s." G_MODULE_SUFFIX,
module->name); module->name);
char **path_list, *s, *path = NULL; char *path_list, *p, *s, *path;
const char *env = getenv("LANGTAG_EXT_MODULE_PATH"); const char *env = getenv("LANGTAG_EXT_MODULE_PATH");
int i;
lt_bool_t retval = FALSE; lt_bool_t retval = FALSE;
size_t len; size_t len;


if (!env) { if (!env) {
path_list = g_strsplit( path_list = strdup(
#ifdef GNOME_ENABLE_DEBUG #ifdef GNOME_ENABLE_DEBUG
BUILDDIR G_DIR_SEPARATOR_S "liblangtag" G_DIR_SEPARATOR_S "extensions" G_SEARCHPATH_SEPARATOR_S BUILDDIR LT_DIR_SEPARATOR_S "liblangtag" LT_DIR_SEPARATOR_S "extensions" LT_SEARCHPATH_SEPARATOR_S
BUILDDIR G_DIR_SEPARATOR_S "liblangtag" G_DIR_SEPARATOR_S "extensions" G_DIR_SEPARATOR_S ".libs" G_SEARCHPATH_SEPARATOR_S BUILDDIR LT_DIR_SEPARATOR_S "liblangtag" LT_DIR_SEPARATOR_S "extensions" LT_DIR_SEPARATOR_S ".libs" LT_SEARCHPATH_SEPARATOR_S
#endif #endif
LANGTAG_EXT_MODULE_PATH, LANGTAG_EXT_MODULE_PATH);
G_SEARCHPATH_SEPARATOR_S,
-1);
} else { } else {
path_list = g_strsplit(env, G_SEARCHPATH_SEPARATOR_S, -1); path_list = strdup(env);
} }


for (i = 0; path_list[i] != NULL && !retval; i++) { s = path_list;
s = path_list[i]; do {

if (!s)
while (*s && isspace(*s)) break;
p = strchr(s, LT_SEARCHPATH_SEPARATOR);
if (p == s) {
s++; s++;
len = strlen(s); continue;
while (len > 0 && isspace(s[len - 1])) }
path = s;
if (p) {
*p = 0;
p++;
}
s = p;
while (*path && isspace(*path))
path++;
len = strlen(path);
while (len > 0 && isspace(path[len - 1]))
len--; len--;
if (path) path[len] = 0;
free(path);
path = strndup(s, len);
if (path[0] != 0) { if (path[0] != 0) {
lt_string_clear(fullname); lt_string_clear(fullname);
if (!lt_string_append_filename(fullname, path, filename, NULL)) { if (!lt_string_append_filename(fullname, path, filename, NULL)) {
Expand Down Expand Up @@ -274,14 +282,13 @@ lt_ext_module_load(lt_ext_module_t *module)
retval = TRUE; retval = TRUE;
} }
} }
} } while (1);
if (!retval) if (!retval)
lt_warning("No such modules: %s", module->name); lt_warning("No such modules: %s", module->name);


lt_string_unref(fullname); lt_string_unref(fullname);
free(path);
free(filename); free(filename);
g_strfreev(path_list); free(path_list);


return retval; return retval;
} }
Expand Down Expand Up @@ -374,7 +381,8 @@ lt_ext_module_new(const char *name)
retval = lt_mem_alloc_object(sizeof (lt_ext_module_t)); retval = lt_mem_alloc_object(sizeof (lt_ext_module_t));


if (retval) { if (retval) {
char *filename = g_path_get_basename(name), *module = NULL; char *n = strdup(name);
char *filename = basename(n), *module = NULL;
static const char *prefix = "liblangtag-ext-"; static const char *prefix = "liblangtag-ext-";
static size_t prefix_len = 0; static size_t prefix_len = 0;
char singleton_c; char singleton_c;
Expand All @@ -399,7 +407,7 @@ lt_ext_module_new(const char *name)
lt_mem_add_ref(&retval->parent, retval->name, lt_mem_add_ref(&retval->parent, retval->name,
(lt_destroy_func_t)free); (lt_destroy_func_t)free);


g_free(filename); free(n);


if (!lt_ext_module_load(retval)) { if (!lt_ext_module_load(retval)) {
lt_ext_module_unref(retval); lt_ext_module_unref(retval);
Expand Down Expand Up @@ -555,28 +563,40 @@ lt_ext_modules_load(void)
{ {
#ifdef ENABLE_GMODULE #ifdef ENABLE_GMODULE
const char *env = getenv("LANGTAG_EXT_MODULE_PATH"); const char *env = getenv("LANGTAG_EXT_MODULE_PATH");
char **path_list; char *path_list, *s, *p, *path;
int i;
size_t suffix_len = strlen(G_MODULE_SUFFIX) + 1; size_t suffix_len = strlen(G_MODULE_SUFFIX) + 1;


if (__lt_ext_module_initialized) if (__lt_ext_module_initialized)
return; return;
if (!env) { if (!env) {
path_list = g_strsplit( path_list = strdup(
#ifdef GNOME_ENABLE_DEBUG #ifdef GNOME_ENABLE_DEBUG
BUILDDIR G_DIR_SEPARATOR_S "liblangtag" G_DIR_SEPARATOR_S "extensions" G_SEARCHPATH_SEPARATOR_S BUILDDIR LT_DIR_SEPARATOR_S "liblangtag" LT_DIR_SEPARATOR_S "extensions" LT_SEARCHPATH_SEPARATOR_S
BUILDDIR G_DIR_SEPARATOR_S "liblangtag" G_DIR_SEPARATOR_S "extensions" G_DIR_SEPARATOR_S ".libs" G_SEARCHPATH_SEPARATOR_S BUILDDIR LT_DIR_SEPARATOR_S "liblangtag" LT_DIR_SEPARATOR_S "extensions" LT_DIR_SEPARATOR_S ".libs" LT_SEARCHPATH_SEPARATOR_S
#endif #endif
LANGTAG_EXT_MODULE_PATH, LANGTAG_EXT_MODULE_PATH);
G_SEARCHPATH_SEPARATOR_S,
-1);
} else { } else {
path_list = g_strsplit(env, G_SEARCHPATH_SEPARATOR_S, -1); path_list = strdup(env);
} }
for (i = 0; path_list[i] != NULL; i++) { s = path_list;
do {
DIR *dir; DIR *dir;


dir = opendir(path_list[i]); if (!s)
break;
p = strchr(s, LT_SEARCHPATH_SEPARATOR);
if (s == p) {
s++;
continue;
}
path = s;
if (p) {
*p = 0;
p++;
}
s = p;

dir = opendir(path);
if (dir) { if (dir) {
struct dirent dent, *dresult; struct dirent dent, *dresult;
size_t len; size_t len;
Expand All @@ -594,8 +614,9 @@ lt_ext_modules_load(void)
} }
closedir(dir); closedir(dir);
} }
} } while (1);
g_strfreev(path_list);
free(path_list);
#endif /* ENABLE_GMODULE */ #endif /* ENABLE_GMODULE */
__lt_ext_default_handler = lt_ext_module_new_with_data("default", __lt_ext_default_handler = lt_ext_module_new_with_data("default",
&__default_funcs); &__default_funcs);
Expand Down
4 changes: 4 additions & 0 deletions liblangtag/lt-list.h
Expand Up @@ -10,6 +10,10 @@
* Lesser General Public License or the Mozilla Public * Lesser General Public License or the Mozilla Public
* License, as specified in the README file. * License, as specified in the README file.
*/ */
#if !defined (__LANGTAG_H__INSIDE) && !defined (__LANGTAG_COMPILATION)
#error "Only <liblangtag/langtag.h> can be included directly."
#endif

#ifndef __LT_LIST_H__ #ifndef __LT_LIST_H__
#define __LT_LIST_H__ #define __LT_LIST_H__


Expand Down
12 changes: 8 additions & 4 deletions liblangtag/lt-macros.h
Expand Up @@ -99,11 +99,15 @@


/* Macros for path separator */ /* Macros for path separator */
#ifdef _WIN32 #ifdef _WIN32
# define LT_DIR_SEPARATOR_S "\\" # define LT_DIR_SEPARATOR_S "\\"
# define LT_DIR_SEPARATOR '\\' # define LT_DIR_SEPARATOR '\\'
# define LT_SEARCHPATH_SEPARATOR_S ";"
# define LT_SEARCHPATH_SEPARATOR ';'
#else #else
# define LT_DIR_SEPARATOR_S "/" # define LT_DIR_SEPARATOR_S "/"
# define LT_DIR_SEPARATOR '/' # define LT_DIR_SEPARATOR '/'
# define LT_SEARCHPATH_SEPARATOR_S ":"
# define LT_SEARCHPATH_SEPARATOR ':'
#endif #endif


/* Macros for min/max */ /* Macros for min/max */
Expand Down
1 change: 0 additions & 1 deletion tests/Makefile.am
Expand Up @@ -8,7 +8,6 @@ INCLUDES = \
-DTEST_DATADIR="\"$(abs_top_builddir)/data\"" \ -DTEST_DATADIR="\"$(abs_top_builddir)/data\"" \
-DTEST_MODDIR="\"$(abs_top_builddir)/liblangtag/extensions/.libs\"" \ -DTEST_MODDIR="\"$(abs_top_builddir)/liblangtag/extensions/.libs\"" \
-D__LANGTAG_COMPILATION \ -D__LANGTAG_COMPILATION \
$(GLIB_CFLAGS) \
$(CHECK_CFLAGS) \ $(CHECK_CFLAGS) \
$(NULL) $(NULL)
DEPS = \ DEPS = \
Expand Down
61 changes: 17 additions & 44 deletions tests/main.c
Expand Up @@ -18,80 +18,53 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <check.h> #include <check.h>
#include <glib-object.h> #include "langtag.h"
#include "lt-messages.h"
#include "main.h" #include "main.h"


extern Suite *tester_suite(void); extern Suite *tester_suite(void);


static GLogFunc old_logger = NULL; static lt_message_func_t old_logger = NULL;
static GError *error = NULL; static lt_error_t *error = NULL;
G_LOCK_DEFINE_STATIC(err);


/* /*
* Private functions * Private functions
*/ */
static void static void
logger(const gchar *log_domain, logger(lt_message_type_t type,
GLogLevelFlags log_level, lt_message_flags_t flags,
const gchar *message, lt_message_category_t category,
gpointer user_data) const char *message,
lt_pointer_t user_data)
{ {
gchar *prev = NULL; lt_error_set(&error, type, message);

G_LOCK (err);

if (error) {
prev = g_strdup_printf("\n %s", error->message);
g_error_free(error);
error = NULL;
}
g_set_error(&error, TESTER_ERROR, log_level,
"%s%s", message,
(prev ? prev : ""));
g_free(prev);

G_UNLOCK (err);
} }


static void static void
init(int argc, init(int argc,
char **argv) char **argv)
{ {
old_logger = g_log_set_default_handler(logger, NULL); old_logger = lt_message_set_default_handler(logger, NULL);
} }


static void static void
fini(void) fini(void)
{ {
tester_pop_error();
if (old_logger) if (old_logger)
g_log_set_default_handler(old_logger, NULL); lt_message_set_default_handler(old_logger, NULL);
} }


/* /*
* Public functions * Public functions
*/ */
GQuark void
tester_get_error_quark(void)
{
GQuark quark = 0;

if (!quark)
quark = g_quark_from_static_string("tester-error");

return quark;
}

gchar *
tester_pop_error(void) tester_pop_error(void)
{ {
gchar *retval = NULL; if (lt_error_is_set(error, LT_ERR_ANY)) {

lt_error_print(error, LT_ERR_ANY);
if (error) { lt_error_clear(error);
retval = g_strdup(error->message);
g_clear_error(&error);
} }

return retval;
} }


int int
Expand Down
15 changes: 7 additions & 8 deletions tests/main.h
Expand Up @@ -15,9 +15,9 @@
#define __TEST_MAIN_H__ #define __TEST_MAIN_H__


#include <check.h> #include <check.h>
#include <glib.h> #include <liblangtag/langtag.h>


G_BEGIN_DECLS LT_BEGIN_DECLS


#define TESTER_ERROR tester_get_error_quark() #define TESTER_ERROR tester_get_error_quark()
#define TDEF(fn) START_TEST (test_ ## fn) #define TDEF(fn) START_TEST (test_ ## fn)
Expand All @@ -26,12 +26,11 @@ G_BEGIN_DECLS
#define TNUL(obj) fail_unless((obj) != NULL, "Failed to create an object") #define TNUL(obj) fail_unless((obj) != NULL, "Failed to create an object")




void setup (void); void setup (void);
void teardown (void); void teardown (void);
Suite *tester_suite (void); Suite *tester_suite (void);
GQuark tester_get_error_quark(void); void tester_pop_error(void);
gchar *tester_pop_error (void) G_GNUC_MALLOC;


G_END_DECLS LT_END_DECLS


#endif /* __TEST_MAIN_H__ */ #endif /* __TEST_MAIN_H__ */

0 comments on commit ade8cca

Please sign in to comment.