/**
 * lt_script_db_new:
 *
 * Create a new instance of a #lt_script_db_t.
 *
 * Returns: (transfer full): a new instance of #lt_script_db_t.
 */
lt_script_db_t *
lt_script_db_new(void)
{
	lt_script_db_t *retval = lt_mem_alloc_object(sizeof (lt_script_db_t));

	if (retval) {
		lt_error_t *err = NULL;
		lt_script_t *le;

		LT_ITER_TMPL_INIT (&retval->parent, _lt_script_db);

		retval->script_entries = lt_trie_new();
		lt_mem_add_ref((lt_mem_t *)retval, retval->script_entries,
			       (lt_destroy_func_t)lt_trie_unref);

		le = lt_script_create();
		lt_script_set_tag(le, "*");
		lt_script_set_name(le, "Wildcard entry");
		lt_trie_replace(retval->script_entries,
				lt_script_get_tag(le),
				le,
				(lt_destroy_func_t)lt_script_unref);
		le = lt_script_create();
		lt_script_set_tag(le, "");
		lt_script_set_name(le, "Empty entry");
		lt_trie_replace(retval->script_entries,
				lt_script_get_tag(le),
				le,
				(lt_destroy_func_t)lt_script_unref);

		retval->xml = lt_xml_new();
		if (!retval->xml) {
			lt_script_db_unref(retval);
			retval = NULL;
			goto bail;
		}
		lt_mem_add_ref((lt_mem_t *)retval, retval->xml,
			       (lt_destroy_func_t)lt_xml_unref);

		lt_script_db_parse(retval, &err);
		if (lt_error_is_set(err, LT_ERR_ANY)) {
			lt_error_print(err, LT_ERR_ANY);
			lt_script_db_unref(retval);
			retval = NULL;
			lt_error_unref(err);
		}
	}
  bail:

	return retval;
}
Exemple #2
0
/**
 * lt_region_db_new:
 *
 * Create a new instance of a #lt_region_db_t.
 *
 * Returns: (transfer full): a new instance of #lt_region_db_t.
 */
lt_region_db_t *
lt_region_db_new(void)
{
	lt_region_db_t *retval = lt_mem_alloc_object(sizeof (lt_region_db_t));

	if (retval) {
		GError *err = NULL;
		lt_region_t *le;

		retval->region_entries = g_hash_table_new_full(g_str_hash,
							       g_str_equal,
							       g_free,
							       (GDestroyNotify)lt_region_unref);
		lt_mem_add_ref(&retval->parent, retval->region_entries,
			       (lt_destroy_func_t)g_hash_table_destroy);

		le = lt_region_create();
		lt_region_set_tag(le, "*");
		lt_region_set_name(le, "Wildcard entry");
		g_hash_table_replace(retval->region_entries,
				     g_strdup(lt_region_get_tag(le)),
				     le);
		le = lt_region_create();
		lt_region_set_tag(le, "");
		lt_region_set_name(le, "Empty entry");
		g_hash_table_replace(retval->region_entries,
				     g_strdup(lt_region_get_tag(le)),
				     le);

		retval->xml = lt_xml_new();
		if (!retval->xml) {
			lt_region_db_unref(retval);
			retval = NULL;
			goto bail;
		}
		lt_mem_add_ref(&retval->parent, retval->xml,
			       (lt_destroy_func_t)lt_xml_unref);

		lt_region_db_parse(retval, &err);
		if (err) {
			g_printerr(err->message);
			lt_region_db_unref(retval);
			retval = NULL;
			g_error_free(err);
		}
	}
  bail:

	return retval;
}
Exemple #3
0
/*< private >*/
static gboolean
lt_xml_read_subtag_registry(lt_xml_t  *xml,
			    GError   **error)
{
	gchar *regfile = NULL;
	xmlParserCtxtPtr xmlparser;
	xmlDocPtr doc = NULL;
	GError *err = NULL;

	g_return_val_if_fail (xml != NULL, FALSE);

#ifdef GNOME_ENABLE_DEBUG
	regfile = g_build_filename(BUILDDIR, "data", "language-subtag-registry.xml", NULL);
	if (!g_file_test(regfile, G_FILE_TEST_EXISTS)) {
		g_free(regfile);
#endif
	regfile = g_build_filename(lt_db_get_datadir(), "language-subtag-registry.xml", NULL);
#ifdef GNOME_ENABLE_DEBUG
	}
#endif
	xmlparser = xmlNewParserCtxt();
	if (!xmlparser) {
		g_set_error(&err, LT_ERROR, LT_ERR_OOM,
			    "Unable to create an instance of xmlParserCtxt.");
		goto bail;
	}
	doc = xmlCtxtReadFile(xmlparser, regfile, "UTF-8", 0);
	if (!doc) {
		g_set_error(&err, LT_ERROR, LT_ERR_FAIL_ON_XML,
			    "Unable to read the xml file: %s",
			    regfile);
		goto bail;
	}
	xml->subtag_registry = doc;
	lt_mem_add_ref(&xml->parent, xml->subtag_registry,
		       (lt_destroy_func_t)xmlFreeDoc);

  bail:
	g_free(regfile);
	if (xmlparser)
		xmlFreeParserCtxt(xmlparser);

	xmlCleanupParser();

	if (err) {
		if (error)
			*error = g_error_copy(err);
		else
			g_warning(err->message);
		g_error_free(err);

		return FALSE;
	}

	return TRUE;
}
void
lt_region_set_preferred_tag(lt_region_t *region,
			    const char  *subtag)
{
	lt_return_if_fail (region != NULL);
	lt_return_if_fail (subtag != NULL);

	if (region->preferred_tag)
		lt_mem_delete_ref(&region->parent, region->preferred_tag);
	region->preferred_tag = strdup(subtag);
	lt_mem_add_ref(&region->parent, region->preferred_tag, free);
}
void
lt_region_set_name(lt_region_t *region,
		   const char  *description)
{
	lt_return_if_fail (region != NULL);
	lt_return_if_fail (description != NULL);

	if (region->description)
		lt_mem_delete_ref(&region->parent, region->description);
	region->description = strdup(description);
	lt_mem_add_ref(&region->parent, region->description, free);
}
Exemple #6
0
void
lt_grandfathered_set_preferred_tag(lt_grandfathered_t *grandfathered,
				   const gchar        *subtag)
{
	g_return_if_fail (grandfathered != NULL);
	g_return_if_fail (subtag != NULL);

	if (grandfathered->preferred_tag)
		lt_mem_remove_ref(&grandfathered->parent, grandfathered->preferred_tag);
	grandfathered->preferred_tag = g_strdup(subtag);
	lt_mem_add_ref(&grandfathered->parent, grandfathered->preferred_tag,
		       (lt_destroy_func_t)g_free);
}
Exemple #7
0
void
lt_grandfathered_set_name(lt_grandfathered_t *grandfathered,
			  const gchar        *description)
{
	g_return_if_fail (grandfathered != NULL);
	g_return_if_fail (description != NULL);

	if (grandfathered->description)
		lt_mem_remove_ref(&grandfathered->parent, grandfathered->description);
	grandfathered->description = g_strdup(description);
	lt_mem_add_ref(&grandfathered->parent, grandfathered->description,
		       (lt_destroy_func_t)g_free);
}
Exemple #8
0
void
lt_redundant_set_preferred_tag(lt_redundant_t *redundant,
			       const gchar    *subtag)
{
	g_return_if_fail (redundant != NULL);
	g_return_if_fail (subtag != NULL);

	if (redundant->preferred_tag)
		lt_mem_remove_ref(&redundant->parent, redundant->preferred_tag);
	redundant->preferred_tag = g_strdup(subtag);
	lt_mem_add_ref(&redundant->parent, redundant->preferred_tag,
		       (lt_destroy_func_t)g_free);
}
Exemple #9
0
void
lt_redundant_set_name(lt_redundant_t *redundant,
		      const gchar    *description)
{
	g_return_if_fail (redundant != NULL);
	g_return_if_fail (description != NULL);

	if (redundant->description)
		lt_mem_remove_ref(&redundant->parent, redundant->description);
	redundant->description = g_strdup(description);
	lt_mem_add_ref(&redundant->parent, redundant->description,
		       (lt_destroy_func_t)g_free);
}
Exemple #10
0
lt_list_t *
lt_list_prepend(lt_list_t         *list,
		lt_pointer_t       data,
		lt_destroy_func_t  func)
{
	lt_list_t *l = lt_list_new();

	l->value = data;
	l->next = list;
	lt_mem_add_ref(&l->parent, l, _lt_list_update);
	if (func)
		lt_mem_add_ref(&l->parent, data, func);
	if (list) {
		l->prev = list->prev;
		if (list->prev)
			list->prev->next = l;
		list->prev = l;
	} else {
		l->prev = NULL;
	}

	return l;
}
static lt_ext_module_t *
lt_ext_module_new_with_data(const char                  *name,
			    const lt_ext_module_funcs_t *funcs)
{
	lt_ext_module_t *retval;

	lt_return_val_if_fail (name != NULL, NULL);
	lt_return_val_if_fail (funcs != NULL, NULL);

	retval = lt_mem_alloc_object(sizeof (lt_ext_module_t));
	if (retval) {
		retval->name = strdup(name);
		lt_mem_add_ref(&retval->parent, retval->name,
			       (lt_destroy_func_t)free);
		retval->funcs = funcs;

		lt_debug(LT_MSGCAT_MODULE, "Loading the internal extension handler: %s", name);
	}

	return retval;
}
Exemple #12
0
static lt_ext_module_t *
lt_ext_module_new_with_data(const gchar                 *name,
			    const lt_ext_module_funcs_t *funcs)
{
	lt_ext_module_t *retval;

	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (funcs != NULL, NULL);

	retval = lt_mem_alloc_object(sizeof (lt_ext_module_t));
	if (retval) {
		retval->name = g_strdup(name);
		lt_mem_add_ref(&retval->parent, retval->name,
			       (lt_destroy_func_t)g_free);
		retval->funcs = funcs;

		g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
		      "Loading the internal extension handler: %s",
		      name);
	}

	return retval;
}
Exemple #13
0
lt_ext_module_t *
lt_ext_module_new(const gchar *name)
{
	lt_ext_module_t *retval;

	g_return_val_if_fail (name != NULL, NULL);

	retval = lt_mem_alloc_object(sizeof (lt_ext_module_t));

	if (retval) {
		gchar *filename = g_path_get_basename(name), *module = NULL;
		static const gchar *prefix = "liblangtag-ext-";
		static gsize prefix_len = 0;
		gchar singleton_c;
		gint singleton;

		if (prefix_len == 0)
			prefix_len = strlen(prefix);

		if (strncmp(filename, prefix, prefix_len) == 0) {
			gsize len = strlen(&filename[prefix_len]);
			gsize suffix_len = strlen(G_MODULE_SUFFIX) + 1;

			if (len > suffix_len &&
			    g_strcmp0(&filename[prefix_len + len - suffix_len], "." G_MODULE_SUFFIX) == 0) {
				module = g_strndup(&filename[prefix_len], len - suffix_len);
				module[len - suffix_len] = 0;
			}
		}
		if (!module)
			module = g_strdup(filename);
		retval->name = module;
		lt_mem_add_ref(&retval->parent, retval->name,
			       (lt_destroy_func_t)g_free);

		g_free(filename);

		if (!lt_ext_module_load(retval)) {
			lt_ext_module_unref(retval);
			return NULL;
		}
		singleton_c = lt_ext_module_get_singleton(retval);
		if (singleton_c == ' ' ||
		    singleton_c == '*') {
			g_warning("Not allowed to override the internal handlers for special singleton.");
			lt_ext_module_unref(retval);
			return NULL;
		}
		singleton = lt_ext_module_singleton_char_to_int(singleton_c);
		if (singleton < 0) {
			g_warning("Invalid singleton: `%c' - `%s'",
				  singleton_c, 
				  retval->name);
			lt_ext_module_unref(retval);
			return NULL;
		}
		if (__lt_ext_modules[singleton]) {
			g_warning("Duplicate extension module: %s",
				  retval->name);
			lt_ext_module_unref(retval);
			return NULL;
		}
		__lt_ext_modules[singleton] = retval;
		lt_mem_add_weak_pointer(&retval->parent,
					(gpointer *)&__lt_ext_modules[singleton]);
	}

	return retval;
}
Exemple #14
0
static gboolean
lt_ext_module_load(lt_ext_module_t *module)
{
	gchar *filename = g_strdup_printf("liblangtag-ext-%s." G_MODULE_SUFFIX,
					  module->name);
	gchar **path_list, *s, *path = NULL, *fullname = NULL;
	const gchar *env = g_getenv("LANGTAG_EXT_MODULE_PATH");
	gint i;
	gboolean retval = FALSE;
	gsize len;

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

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

		while (*s && g_ascii_isspace(*s))
			s++;
		len = strlen(s);
		while (len > 0 && g_ascii_isspace(s[len - 1]))
			len--;
		g_free(path);
		path = g_strndup(s, len);
		if (path[0] != 0) {
			g_free(fullname);
			fullname = g_build_filename(path, filename, NULL);
			module->module = g_module_open(fullname,
						       G_MODULE_BIND_LAZY|G_MODULE_BIND_LOCAL);
			if (module->module) {
				gpointer func;

				lt_mem_add_ref(&module->parent, module->module,
					       (lt_destroy_func_t)g_module_close);
				g_module_symbol(module->module,
						"module_get_version",
						&func);
				if (!func) {
					g_warning(g_module_error());
					break;
				}
				if (((lt_ext_module_version_func_t)func)() != LT_EXT_MODULE_VERSION) {
					g_warning("`%s' isn't satisfied the required module version.",
						  filename);
					break;
				}
				g_module_symbol(module->module,
						"module_get_funcs",
						&func);
				if (!func) {
					g_warning(g_module_error());
					break;
				}
				if (!(module->funcs = ((lt_ext_module_get_funcs_func_t)func)())) {
					g_warning("No function table for `%s'",
						  filename);
					break;
				}
				g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
				      "Loading the external extension handler module: %s",
				      fullname);
				retval = TRUE;
			}
		}
	}
	if (!retval)
		g_warning("No such modules: %s", module->name);

	g_free(fullname);
	g_free(path);
	g_free(filename);
	g_strfreev(path_list);

	return retval;
}
static lt_bool_t
lt_ext_module_load(lt_ext_module_t *module)
{
	lt_bool_t retval = FALSE;
	lt_string_t *fullname = lt_string_new(NULL);
	char *filename = lt_strdup_printf("liblangtag-ext-%s." LT_MODULE_SUFFIX,
					  module->name);
	char *path_list, *p, *s, *path;
	const char *env = getenv("LANGTAG_EXT_MODULE_PATH");
	size_t len;

	if (!env) {
		path_list = strdup(
#ifdef GNOME_ENABLE_DEBUG
			BUILDDIR LT_DIR_SEPARATOR_S "extensions" LT_SEARCHPATH_SEPARATOR_S
			BUILDDIR LT_DIR_SEPARATOR_S "extensions" LT_DIR_SEPARATOR_S ".libs" LT_SEARCHPATH_SEPARATOR_S
#endif
			LANGTAG_EXT_MODULE_PATH);
	} else {
		path_list = strdup(env);
	}

	s = path_list;
	do {
		if (!s)
			break;
		p = strchr(s, LT_SEARCHPATH_SEPARATOR);
		if (p == s) {
			s++;
			continue;
		}
		path = s;
		if (p) {
			*p = 0;
			p++;
		}
		s = p;
		while (*path && isspace((int)*path))
			path++;
		len = strlen(path);
		while (len > 0 && isspace((int)path[len - 1]))
			len--;
		path[len] = 0;
		if (path[0] != 0) {
			lt_string_clear(fullname);
			lt_string_append_filename(fullname, path, filename, NULL);
			module->module = dlopen(lt_string_value(fullname),
						RTLD_LAZY|RTLD_LOCAL);
			if (module->module) {
				lt_pointer_t func;

				lt_mem_add_ref(&module->parent, module->module,
					       (lt_destroy_func_t)dlclose);
				func = dlsym(module->module, "module_get_version");
				if (!func) {
					lt_warning(dlerror());
					break;
				}
				if (((lt_ext_module_version_func_t)func)() != LT_EXT_MODULE_VERSION) {
					lt_warning("`%s' isn't satisfied the required module version.",
						   filename);
					break;
				}
				func = dlsym(module->module, "module_get_funcs");
				if (!func) {
					lt_warning(dlerror());
					break;
				}
				if (!(module->funcs = ((lt_ext_module_get_funcs_func_t)func)())) {
					lt_warning("No function table for `%s'",
						   filename);
					break;
				}
				lt_debug(LT_MSGCAT_MODULE,
					 "Loading the external extension handler module: %s",
					 lt_string_value(fullname));
				retval = TRUE;
			}
		}
	} while (1);
	if (!retval)
		lt_warning("No such modules: %s", module->name);

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

	return retval;
}