[tracker/collation] libtracker-common: Added libicu, libunistring and glib based collations



commit 7ed7fa0019ccc3aca6a806c17bc229475e53119b
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Mon Aug 16 12:23:05 2010 +0200

    libtracker-common: Added libicu, libunistring and glib based collations

 src/libtracker-common/Makefile.am         |    6 +-
 src/libtracker-common/tracker-collation.c |  150 +++++++++++++++++++++++++++++
 src/libtracker-common/tracker-collation.h |   38 +++++++
 src/libtracker-common/tracker-common.h    |    1 +
 4 files changed, 193 insertions(+), 2 deletions(-)
---
diff --git a/src/libtracker-common/Makefile.am b/src/libtracker-common/Makefile.am
index e6ebd17..3e36719 100644
--- a/src/libtracker-common/Makefile.am
+++ b/src/libtracker-common/Makefile.am
@@ -50,7 +50,8 @@ libtracker_common_la_SOURCES =	 			\
 	tracker-log.c	 				\
 	tracker-type-utils.c				\
 	tracker-utils.c					\
-	tracker-crc32.c
+	tracker-crc32.c					\
+	tracker-collation.c
 
 noinst_HEADERS =					\
 	$(power_headers)				\
@@ -66,7 +67,8 @@ noinst_HEADERS =					\
 	tracker-ontologies.h				\
 	tracker-type-utils.h				\
 	tracker-utils.h					\
-	tracker-crc32.h
+	tracker-crc32.h					\
+	tracker-collation.h
 
 if HAVE_TRACKER_FTS
 libtracker_common_la_SOURCES +=	 			\
diff --git a/src/libtracker-common/tracker-collation.c b/src/libtracker-common/tracker-collation.c
new file mode 100644
index 0000000..69c7cea
--- /dev/null
+++ b/src/libtracker-common/tracker-collation.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2010, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+#include <glib.h>
+
+#include "tracker-collation.h"
+
+#ifdef HAVE_LIBUNISTRING
+/* libunistring versions prior to 9.1.2 need this hack */
+#define _UNUSED_PARAMETER_
+#include <unistr.h>
+#elif HAVE_LIBICU
+#include <ucol.h>
+#endif
+
+#ifdef HAVE_LIBUNISTRING /* ---- GNU libunistring based collation ---- */
+
+gboolean
+tracker_collation_init (void)
+{
+	/* Nothing to do */
+	return TRUE;
+}
+
+void
+tracker_collation_deinit (void)
+{
+	/* Nothing to do */
+}
+
+gint
+tracker_collation_utf8 (gchar *str1,
+                        gint   len1,
+                        gchar *str2,
+                        gint   len2)
+{
+	return u8_strcoll (str1, str2);
+}
+
+#elif HAVE_LIBICU /* ---- ICU based collation (UTF-16) ----*/
+
+/* Global collator to be re-used every time */
+UCollator *collator;
+
+gboolean
+tracker_collation_init (void)
+{
+	UErrorCode status = U_ZERO_ERROR;
+	const gchar *locale = setlocale (LC_ALL, NULL);
+
+	g_debug ("Initializing collator for locale '%s'", locale);
+	collator = ucol_open (locale, &status);
+	if (!collator) {
+		g_warning ("Collator for locale '%s' cannot be created: %s",
+		           locale, u_errorName (status));
+		/* Try to get UCA collator then... */
+		status = U_ZERO_ERROR;
+		collator = ucol_open ("root", &status);
+		if (!collator) {
+			g_critical ("UCA Collator cannot be created: %s",
+			            u_errorName (status));
+			return FALSE;
+		}
+	}
+	return TRUE;
+}
+
+void
+tracker_collation_deinit (void)
+{
+	if (collator) {
+		ucol_close (collator);
+		collator = NULL;
+	}
+}
+
+gint
+tracker_collation_utf8 (gchar *str1,
+                        gint   len1,
+                        gchar *str2,
+                        gint   len2)
+{
+	UErrorCode status = U_ZERO_ERROR;
+	UCharIterator iter1;
+	UCharIterator iter2;
+	UCollationResult result;
+
+	/* Collator must be created before trying to collate */
+	g_return_val_if_fail (collator, -1);
+
+	/* Setup iterators */
+	uiter_setUTF8 (&iter1, str1, len1);
+	uiter_setUTF8 (&iter2, str2, len2);
+
+	result = ucol_strcollIter (collator,
+	                           &iter1,
+	                           &iter2,
+	                           &status);
+	if (status != U_ZERO_ERROR)
+		g_critical ("Error collating: %s", u_errorName (status));
+
+	if (result == UCOL_GREATER)
+		return 1;
+	if (result == UCOL_LESS)
+		return -1;
+	return 0;
+}
+
+#else /* ---- GLib based collation ---- */
+
+gboolean
+tracker_collation_init (void)
+{
+	/* Nothing to do */
+	return TRUE;
+}
+
+void
+tracker_collation_deinit (void)
+{
+	/* Nothing to do */
+}
+
+gint
+tracker_collation_utf8 (gchar *str1,
+                        gint   len1,
+                        gchar *str2,
+                        gint   len2)
+{
+	return g_utf8_collate (str1, str2);
+}
+
+#endif
diff --git a/src/libtracker-common/tracker-collation.h b/src/libtracker-common/tracker-collation.h
new file mode 100644
index 0000000..5e3472c
--- /dev/null
+++ b/src/libtracker-common/tracker-collation.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2010 Nokia <ivan frade nokia com>
+ *
+ * This library 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.
+ *
+ * This library 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 Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __LIBTRACKER_COMMON_COLLATION_H__
+#define __LIBTRACKER_COMMON_COLLATION_H__
+
+G_BEGIN_DECLS
+
+#if !defined (__LIBTRACKER_COMMON_INSIDE__) && !defined (TRACKER_COMPILATION)
+#error "only <libtracker-common/tracker-common.h> must be included directly."
+#endif
+
+gboolean tracker_collation_init   (void);
+void     tracker_collation_deinit (void);
+gint     tracker_collation_utf8   (gchar *str1,
+                                   gint   len1,
+                                   gchar *str2,
+                                   gint   len2);
+
+G_END_DECLS
+
+#endif /* __LIBTRACKER_COMMON_COLLATION_H__ */
diff --git a/src/libtracker-common/tracker-common.h b/src/libtracker-common/tracker-common.h
index fbd0550..80627ef 100644
--- a/src/libtracker-common/tracker-common.h
+++ b/src/libtracker-common/tracker-common.h
@@ -41,6 +41,7 @@
 #include "tracker-power.h"
 #include "tracker-type-utils.h"
 #include "tracker-utils.h"
+#include "tracker-collation.h"
 
 #undef __LIBTRACKER_COMMON_INSIDE__
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]