[evolution-data-server/openismus-work-master] Added ETransliterator wrapper object.



commit 12caa799286e894ead4d2038b81a1cbc8b5115e9
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Thu Oct 24 22:55:10 2013 +0200

    Added ETransliterator wrapper object.
    
    Exposes a C api to the transliterator service separately from
    the ECollator API.

 libedataserver/Makefile.am                  |    2 +
 libedataserver/e-collator.c                 |    2 +-
 libedataserver/e-transliterator-private.cpp |   18 ++--
 libedataserver/e-transliterator-private.h   |   12 +-
 libedataserver/e-transliterator.c           |  155 +++++++++++++++++++++++++++
 libedataserver/e-transliterator.h           |   55 ++++++++++
 libedataserver/libedataserver.h             |    1 +
 7 files changed, 229 insertions(+), 16 deletions(-)
---
diff --git a/libedataserver/Makefile.am b/libedataserver/Makefile.am
index a016e6c..2f186aa 100644
--- a/libedataserver/Makefile.am
+++ b/libedataserver/Makefile.am
@@ -90,6 +90,7 @@ libedataserver_1_2_la_SOURCES =               \
        e-source-webdav.c               \
        e-debug-log.c                   \
        e-time-utils.c                  \
+       e-transliterator.c              \
        e-transliterator-private.h      \
        e-uid.c                         \
        e-url.c                         \
@@ -168,6 +169,7 @@ libedataserverinclude_HEADERS =             \
        e-source-webdav.h               \
        e-debug-log.h                   \
        e-time-utils.h                  \
+       e-transliterator.h              \
        e-uid.h                         \
        e-url.h                         \
        e-data-server-util.h            \
diff --git a/libedataserver/e-collator.c b/libedataserver/e-collator.c
index 6885e5b..80bb3f6 100644
--- a/libedataserver/e-collator.c
+++ b/libedataserver/e-collator.c
@@ -72,7 +72,7 @@ struct _ECollator
        gint             inflow;
        gint             overflow;
 
-       ETransliterator *transliterator;
+       ECxxTransliterator *transliterator;
 };
 
 /*****************************************************
diff --git a/libedataserver/e-transliterator-private.cpp b/libedataserver/e-transliterator-private.cpp
index 9e6b4a4..959e364 100644
--- a/libedataserver/e-transliterator-private.cpp
+++ b/libedataserver/e-transliterator-private.cpp
@@ -38,43 +38,43 @@
 
 using icu::Transliterator;
 
-struct _ETransliterator {
+struct _ECxxTransliterator {
        Transliterator *priv;
 };
 
 /* Create an Transliterator for the source and target
  * language stripts
  */
-ETransliterator *
+ECxxTransliterator *
 _e_transliterator_cxx_new (const gchar *transliterator_id)
 {
        UErrorCode status = U_ZERO_ERROR;
-       ETransliterator *transliterator;
+       ECxxTransliterator *transliterator;
 
        g_return_val_if_fail (transliterator_id != NULL, NULL);
 
-       transliterator = g_slice_new (ETransliterator);
+       transliterator = g_slice_new (ECxxTransliterator);
        transliterator->priv = Transliterator::createInstance (transliterator_id, UTRANS_FORWARD, status); 
 
        return transliterator;
 }
 
-/* Frees an ETransliterator and it's associated resources
+/* Frees an ECxxTransliterator and it's associated resources
  */
 void
-_e_transliterator_cxx_free (ETransliterator *transliterator)
+_e_transliterator_cxx_free (ECxxTransliterator *transliterator)
 {
        if (transliterator) {
                delete transliterator->priv;
-               g_slice_free (ETransliterator, transliterator);
+               g_slice_free (ECxxTransliterator, transliterator);
        }
 }
 
 /* Transliterates 'str' and returns the new allocated result
  */
 gchar *
-_e_transliterator_cxx_transliterate (ETransliterator  *transliterator,
-                                    const gchar      *str)
+_e_transliterator_cxx_transliterate (ECxxTransliterator  *transliterator,
+                                    const gchar         *str)
 {
        UnicodeString transform;
        std::string sourceUTF8;
diff --git a/libedataserver/e-transliterator-private.h b/libedataserver/e-transliterator-private.h
index 491f83c..6e35ba7 100644
--- a/libedataserver/e-transliterator-private.h
+++ b/libedataserver/e-transliterator-private.h
@@ -37,19 +37,19 @@ G_BEGIN_DECLS
 #endif
 
 /**
- * ETransliterator:
+ * ECxxTransliterator:
  *
  * A private opaque type describing an alphabetic index
  *
  * Since: 3.12
  **/
-typedef struct _ETransliterator ETransliterator;
+typedef struct _ECxxTransliterator ECxxTransliterator;
 
 /* defined in e-transliterator-private.cpp, and used by by e-collator.c */
-E_TRANSLITERATOR_LOCAL ETransliterator *_e_transliterator_cxx_new             (const gchar      
*transliterator_id);
-E_TRANSLITERATOR_LOCAL void             _e_transliterator_cxx_free            (ETransliterator  
*transliterator);
-E_TRANSLITERATOR_LOCAL gchar           *_e_transliterator_cxx_transliterate   (ETransliterator  
*transliterator,
-                                                                              const gchar      *str);
+E_TRANSLITERATOR_LOCAL ECxxTransliterator *_e_transliterator_cxx_new             (const gchar        
*transliterator_id);
+E_TRANSLITERATOR_LOCAL void                _e_transliterator_cxx_free            (ECxxTransliterator 
*transliterator);
+E_TRANSLITERATOR_LOCAL gchar              *_e_transliterator_cxx_transliterate   (ECxxTransliterator 
*transliterator,
+                                                                                 const gchar        *str);
 
 G_END_DECLS
 
diff --git a/libedataserver/e-transliterator.c b/libedataserver/e-transliterator.c
new file mode 100644
index 0000000..4129b75
--- /dev/null
+++ b/libedataserver/e-transliterator.c
@@ -0,0 +1,155 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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 Lesser General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Author: Tristan Van Berkom <tristanvb openismus com>
+ */
+
+/**
+ * SECTION: e-transliterator
+ * @include: libedataserver/libedataserver.h
+ * @short_description: Collation services for locale sensitive sorting
+ *
+ * The #ETransliterator is a wrapper object around ICU collation services and
+ * provides features to sort words in locale specific ways. The transliterator
+ * also provides some API for determining features of the active alphabet
+ * in the user's locale, and which words should be sorted under which
+ * letter in the user's alphabet.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+
+#include "e-transliterator.h"
+#include "e-transliterator-private.h"
+
+G_DEFINE_BOXED_TYPE (ETransliterator,
+                    e_transliterator,
+                    e_transliterator_ref, 
+                    e_transliterator_unref)
+
+struct _ETransliterator
+{
+       ECxxTransliterator *transliterator;
+
+       volatile gint       ref_count;
+};
+
+/*****************************************************
+ *                        API                        *
+ *****************************************************/
+
+/**
+ * e_transliterator_new:
+ * @id: The id of the transliterator to create.
+ *
+ * Creates a new #ETransliterator for the given @id,
+ * IDs are defined by ICU libraries and can be of the
+ * form "Any-Latin", "Han-Latin" etc.
+ *
+ * Transliterator services exist for all script types
+ * to convert into Latin, however the same is not true
+ * for other scripts (i.e. you cannot transliterate
+ * Greek into Chinese or Japanese).
+ *
+ * For more details on ICU transliteration services,
+ * visit this link:
+ *     http://userguide.icu-project.org/transforms/general
+ *
+ * Returns: (transfer full): A newly created #ETransliterator.
+ *
+ * Since: 3.12
+ */
+ETransliterator *
+e_transliterator_new (const gchar     *id)
+{
+       ETransliterator *transliterator;
+
+       transliterator = g_slice_new0 (ETransliterator);
+       transliterator->transliterator = _e_transliterator_cxx_new (id);
+       transliterator->ref_count = 1;
+
+       return transliterator;
+}
+
+/**
+ * e_transliterator_ref:
+ * @transliterator: An #ETransliterator
+ *
+ * Increases the reference count of @transliterator.
+ *
+ * Returns: (transfer full): @transliterator
+ *
+ * Since: 3.12
+ */
+ETransliterator *
+e_transliterator_ref (ETransliterator *transliterator)
+{
+       g_return_val_if_fail (transliterator != NULL, NULL);
+
+       g_atomic_int_inc (&transliterator->ref_count);
+
+       return transliterator;
+}
+
+/**
+ * e_transliterator_unref:
+ * @transliterator: An #ETransliterator
+ *
+ * Decreases the reference count of @transliterator.
+ * If the reference count reaches 0 then the transliterator is freed
+ *
+ * Since: 3.12
+ */
+void
+e_transliterator_unref (ETransliterator *transliterator)
+{
+       g_return_if_fail (transliterator != NULL);
+
+       if (g_atomic_int_dec_and_test (&transliterator->ref_count)) {
+
+               if (transliterator->transliterator)
+                       _e_transliterator_cxx_free (transliterator->transliterator);
+
+               g_slice_free (ETransliterator, transliterator);
+       }
+}
+
+/**
+ * e_transliterator_transliterate:
+ * @transliterator: An #ETransliterator
+ * @str: The string to transliterate
+ *
+ * Transliterates @str according to the transliteration service
+ * chosen and passed to e_transliterator_new().
+ *
+ * Returns: (transfer full): The newly created transliteration of @str
+ *
+ * Since: 3.12
+ */
+gchar *
+e_transliterator_transliterate (ETransliterator *transliterator,
+                               const gchar     *str)
+{
+       g_return_val_if_fail (transliterator != NULL, NULL);
+
+       return _e_transliterator_cxx_transliterate (transliterator->transliterator, str);
+}
diff --git a/libedataserver/e-transliterator.h b/libedataserver/e-transliterator.h
new file mode 100644
index 0000000..9ed33fe
--- /dev/null
+++ b/libedataserver/e-transliterator.h
@@ -0,0 +1,55 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2013 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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 Lesser General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Author: Tristan Van Berkom <tristanvb openismus com>
+ */
+#if !defined (__LIBEDATASERVER_H_INSIDE__) && !defined (LIBEDATASERVER_COMPILATION)
+#error "Only <libedataserver/libedataserver.h> should be included directly."
+#endif
+
+#include <glib.h>
+#include <libedataserver/e-source-enumtypes.h>
+#include <libedataserver/e-data-server-util.h>
+
+#ifndef E_TRANSLITERATOR_H
+#define E_TRANSLITERATOR_H
+
+#define E_TYPE_TRANSLITERATOR (e_transliterator_get_type ())
+
+G_BEGIN_DECLS
+
+/**
+ * ETransliterator:
+ *
+ * An opaque object used for string transliterations.
+ *
+ * Since: 3.12
+ */
+typedef struct _ETransliterator ETransliterator;
+
+GType                e_transliterator_get_type         (void);
+ETransliterator     *e_transliterator_new              (const gchar     *id);
+ETransliterator     *e_transliterator_ref              (ETransliterator *transliterator);
+void                 e_transliterator_unref            (ETransliterator *transliterator);
+gchar               *e_transliterator_transliterate    (ETransliterator *transliterator,
+                                                       const gchar     *str);
+
+
+G_END_DECLS
+
+#endif /* E_TRANSLITERATOR_H */
diff --git a/libedataserver/libedataserver.h b/libedataserver/libedataserver.h
index a4a6b08..44437da 100644
--- a/libedataserver/libedataserver.h
+++ b/libedataserver/libedataserver.h
@@ -69,6 +69,7 @@
 #include <libedataserver/e-source-webdav.h>
 #include <libedataserver/e-source.h>
 #include <libedataserver/e-time-utils.h>
+#include <libedataserver/e-transliterator.h>
 #include <libedataserver/e-uid.h>
 #include <libedataserver/e-url.h>
 #include <libedataserver/e-xml-hash-utils.h>


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