From e7afac3c2819bd1fea76d619854388899ed93ce0 Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Mon, 8 Apr 2019 23:42:31 +0800 Subject: [PATCH] v0.6.1 change code format. format code using myastyle. removed space after function name. put pointer on variable name side. --- main.c | 314 ++++++++++++------------- meson.build | 2 +- parse-pinyin-test.cpp | 92 ++++---- parse-pinyin.cpp | 14 +- parse-pinyin.h | 2 +- zero-pinyin-service-test.c | 38 ++-- zero-pinyin-service.c | 454 ++++++++++++++++++------------------- zero-pinyin-service.h | 28 +-- 8 files changed, 472 insertions(+), 472 deletions(-) diff --git a/main.c b/main.c index fa6edcd..deb2627 100644 --- a/main.c +++ b/main.c @@ -14,198 +14,198 @@ typedef struct { } AppData; static gboolean -on_handle_get_candidates (ZeroPinyinService *object, - GDBusMethodInvocation *invocation, - const gchar *preedit_str, - guint fetch_size, - AppData *appdata) +on_handle_get_candidates(ZeroPinyinService *object, + GDBusMethodInvocation *invocation, + const gchar *preedit_str, + guint fetch_size, + AppData *appdata) { if (preedit_str == NULL || fetch_size == 0) { - g_dbus_method_invocation_return_dbus_error ( + g_dbus_method_invocation_return_dbus_error( invocation, "org.gtk.GDBus.Failed", "Bad param"); return TRUE; } - g_message ("get_candidates for preedit_str=%s fetch_size=%u", - preedit_str, fetch_size); + g_message("get_candidates for preedit_str=%s fetch_size=%u", + preedit_str, fetch_size); GVariant *result = NULL; GVariantBuilder *candidates_builder = NULL; GVariantBuilder *matched_lengths_builder = NULL; GVariantBuilder *candidates_pinyin_indices = NULL; - /* test data */ + /* test data */ /* get_candidates_test (preedit_str, fetch_size, candidates_builder, matched_lengths_builder); */ - candidates_builder = g_variant_builder_new (G_VARIANT_TYPE ("as")); - matched_lengths_builder = g_variant_builder_new (G_VARIANT_TYPE ("au")); - candidates_pinyin_indices = g_variant_builder_new (G_VARIANT_TYPE ("aa(ii)")); - get_candidates (appdata->db, preedit_str, fetch_size, candidates_builder, matched_lengths_builder, candidates_pinyin_indices); + candidates_builder = g_variant_builder_new(G_VARIANT_TYPE("as")); + matched_lengths_builder = g_variant_builder_new(G_VARIANT_TYPE("au")); + candidates_pinyin_indices = g_variant_builder_new(G_VARIANT_TYPE("aa(ii)")); + get_candidates(appdata->db, preedit_str, fetch_size, candidates_builder, matched_lengths_builder, candidates_pinyin_indices); - result = g_variant_new ("(asauaa(ii))", candidates_builder, matched_lengths_builder, candidates_pinyin_indices); - g_assert_nonnull (result); + result = g_variant_new("(asauaa(ii))", candidates_builder, matched_lengths_builder, candidates_pinyin_indices); + g_assert_nonnull(result); /* result is a GVarient tuple of two dbus arrays */ - g_dbus_method_invocation_return_value (invocation, result); + g_dbus_method_invocation_return_value(invocation, result); - g_variant_builder_unref (candidates_builder); - g_variant_builder_unref (matched_lengths_builder); - g_variant_builder_unref (candidates_pinyin_indices); + g_variant_builder_unref(candidates_builder); + g_variant_builder_unref(matched_lengths_builder); + g_variant_builder_unref(candidates_pinyin_indices); return TRUE; } static gboolean -on_handle_commit_candidate (ZeroPinyinService *object, - GDBusMethodInvocation *invocation, - const gchar *candidate, - GVariant *candidate_pinyin_indices, - AppData *appdata) +on_handle_commit_candidate(ZeroPinyinService *object, + GDBusMethodInvocation *invocation, + const gchar *candidate, + GVariant *candidate_pinyin_indices, + AppData *appdata) { - commit_candidate (appdata->db, candidate, candidate_pinyin_indices); - g_dbus_method_invocation_return_value (invocation, NULL); + commit_candidate(appdata->db, candidate, candidate_pinyin_indices); + g_dbus_method_invocation_return_value(invocation, NULL); return TRUE; } static gboolean -on_handle_delete_candidate (ZeroPinyinService *object, - GDBusMethodInvocation *invocation, - const char *candidate, - AppData *appdata) +on_handle_delete_candidate(ZeroPinyinService *object, + GDBusMethodInvocation *invocation, + const char *candidate, + AppData *appdata) { if (! candidate) { - g_dbus_method_invocation_return_value (invocation, NULL); + g_dbus_method_invocation_return_value(invocation, NULL); return TRUE; } - guint len = g_utf8_strlen (candidate, -1); + guint len = g_utf8_strlen(candidate, -1); if (len == 1) { - g_message ("delete single character %s is a no-op", candidate); - g_dbus_method_invocation_return_value (invocation, NULL); + g_message("delete single character %s is a no-op", candidate); + g_dbus_method_invocation_return_value(invocation, NULL); return TRUE; } - g_message ("delete candidate %s", candidate); + g_message("delete candidate %s", candidate); /* insert phrase to userdb.not_phrase table. */ char *sql = NULL; gboolean rb = FALSE; - sql = sqlite3_mprintf ("INSERT INTO userdb.not_phrase (phrase) VALUES (%Q);", candidate); - rb = sqlite3_exec_simple (appdata->db, sql); + sql = sqlite3_mprintf("INSERT INTO userdb.not_phrase (phrase) VALUES (%Q);", candidate); + rb = sqlite3_exec_simple(appdata->db, sql); if (! rb) { - g_warning ("insert phrase to not_phrase table failed"); + g_warning("insert phrase to not_phrase table failed"); } - sqlite3_free (sql); + sqlite3_free(sql); /* delete phrase from userdb.py_phrase_x table. */ guint table_suffix = len - 1; - sql = sqlite3_mprintf ("DELETE FROM userdb.py_phrase_%u WHERE phrase = %Q;", table_suffix, candidate); - rb = sqlite3_exec_simple (appdata->db, sql); + sql = sqlite3_mprintf("DELETE FROM userdb.py_phrase_%u WHERE phrase = %Q;", table_suffix, candidate); + rb = sqlite3_exec_simple(appdata->db, sql); if (! rb) { - g_warning ("delete phrase from py_phrase_%u table failed", table_suffix); + g_warning("delete phrase from py_phrase_%u table failed", table_suffix); } - sqlite3_free (sql); + sqlite3_free(sql); - g_dbus_method_invocation_return_value (invocation, NULL); + g_dbus_method_invocation_return_value(invocation, NULL); return TRUE; } static gboolean -on_handle_quit (ZeroPinyinService *object, - GDBusMethodInvocation *invocation, - AppData *appdata) +on_handle_quit(ZeroPinyinService *object, + GDBusMethodInvocation *invocation, + AppData *appdata) { - g_application_quit (appdata->app); - g_dbus_method_invocation_return_value (invocation, NULL); + g_application_quit(appdata->app); + g_dbus_method_invocation_return_value(invocation, NULL); return TRUE; } static void -on_bus_acquired (GDBusConnection *connection, - const gchar *name, - gpointer user_data) +on_bus_acquired(GDBusConnection *connection, + const gchar *name, + gpointer user_data) { - AppData *appdata = (AppData*) user_data; + AppData *appdata = (AppData *) user_data; GError *err = NULL; - g_message ("on_bus_acquired() name=%s", name); - - appdata->interface = zero_pinyin_service_skeleton_new (); - g_signal_connect (appdata->interface, - "handle-get-candidates", - G_CALLBACK (on_handle_get_candidates), - appdata); - g_signal_connect (appdata->interface, - "handle-commit-candidate", - G_CALLBACK (on_handle_commit_candidate), - appdata); - g_signal_connect (appdata->interface, - "handle-delete-candidate", - G_CALLBACK (on_handle_delete_candidate), - appdata); - g_signal_connect (appdata->interface, - "handle-quit", - G_CALLBACK (on_handle_quit), - appdata); - g_dbus_interface_skeleton_export ( - G_DBUS_INTERFACE_SKELETON (appdata->interface), + g_message("on_bus_acquired() name=%s", name); + + appdata->interface = zero_pinyin_service_skeleton_new(); + g_signal_connect(appdata->interface, + "handle-get-candidates", + G_CALLBACK(on_handle_get_candidates), + appdata); + g_signal_connect(appdata->interface, + "handle-commit-candidate", + G_CALLBACK(on_handle_commit_candidate), + appdata); + g_signal_connect(appdata->interface, + "handle-delete-candidate", + G_CALLBACK(on_handle_delete_candidate), + appdata); + g_signal_connect(appdata->interface, + "handle-quit", + G_CALLBACK(on_handle_quit), + appdata); + g_dbus_interface_skeleton_export( + G_DBUS_INTERFACE_SKELETON(appdata->interface), connection, ZERO_PINYIN_OBJECT_PATH, &err); if (err) { - g_warning ("export interface at %s failed: %s", - ZERO_PINYIN_OBJECT_PATH, err->message); - g_error_free (err); - g_application_quit (G_APPLICATION (appdata->app)); + g_warning("export interface at %s failed: %s", + ZERO_PINYIN_OBJECT_PATH, err->message); + g_error_free(err); + g_application_quit(G_APPLICATION(appdata->app)); return; } - g_message ("interface exported at %s", ZERO_PINYIN_OBJECT_PATH); + g_message("interface exported at %s", ZERO_PINYIN_OBJECT_PATH); } static void -on_name_acquired (GDBusConnection *connection, - const gchar *name, - gpointer user_data) +on_name_acquired(GDBusConnection *connection, + const gchar *name, + gpointer user_data) { - g_message ("on_name_acquired() name=%s", name); + g_message("on_name_acquired() name=%s", name); } static void -on_name_lost (GDBusConnection *connection, - const gchar *name, - gpointer user_data) +on_name_lost(GDBusConnection *connection, + const gchar *name, + gpointer user_data) { - AppData *appdata = (AppData*) user_data; + AppData *appdata = (AppData *) user_data; /* this won't happen if this is the only app that tries to take the * name, because GApplication already have primary instance * concept. None primary instance will just send 'activate' signal to * primary instance and exit. They will not try to register ibus at * all. */ - g_message ("on_name_lost() name=%s exiting", name); - g_application_quit (G_APPLICATION (appdata->app)); + g_message("on_name_lost() name=%s exiting", name); + g_application_quit(G_APPLICATION(appdata->app)); } static void -config_dbus_service (AppData *appdata) +config_dbus_service(AppData *appdata) { - appdata->owner_id = g_bus_own_name ( - G_BUS_TYPE_SESSION, - ZERO_PINYIN_WELL_KNOWN_NAME, - G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT|G_BUS_NAME_OWNER_FLAGS_REPLACE, - on_bus_acquired, - on_name_acquired, - on_name_lost, - appdata, - NULL); - g_assert_cmpint (appdata->owner_id, >, 0); + appdata->owner_id = g_bus_own_name( + G_BUS_TYPE_SESSION, + ZERO_PINYIN_WELL_KNOWN_NAME, + G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE, + on_bus_acquired, + on_name_acquired, + on_name_lost, + appdata, + NULL); + g_assert_cmpint(appdata->owner_id, >, 0); } /** * handle SIGTERM gracefully. */ static gboolean -on_sigterm_received (gpointer user_data) +on_sigterm_received(gpointer user_data) { - AppData *appdata = (AppData*) user_data; - g_application_quit (appdata->app); + AppData *appdata = (AppData *) user_data; + g_application_quit(appdata->app); return G_SOURCE_REMOVE; } @@ -213,43 +213,43 @@ on_sigterm_received (gpointer user_data) * init appdata->db */ static void -config_db (AppData* appdata) +config_db(AppData *appdata) { gint ri = 0; gboolean rb = FALSE; - static const char* SQLITE3_MEMORY_DB = ":memory:"; - sqlite3* db = NULL; - gchar* sql = NULL; - ri = sqlite3_open (SQLITE3_MEMORY_DB, &db); + static const char *SQLITE3_MEMORY_DB = ":memory:"; + sqlite3 *db = NULL; + gchar *sql = NULL; + ri = sqlite3_open(SQLITE3_MEMORY_DB, &db); if (ri != SQLITE_OK) { - g_warning ("sqlite3_open :memory: db failed, query will not work."); + g_warning("sqlite3_open :memory: db failed, query will not work."); goto db_fail; } - g_assert_nonnull (db); + g_assert_nonnull(db); /* TODO make db path configurable */ /* TODO remove user name in db path */ - sql = sqlite3_mprintf ("ATTACH %Q AS maindb", "/home/sylecn/.cache/ibus/pinyin/main.db"); - rb = sqlite3_exec_simple (db, sql); + sql = sqlite3_mprintf("ATTACH %Q AS maindb", "/home/sylecn/.cache/ibus/pinyin/main.db"); + rb = sqlite3_exec_simple(db, sql); if (! rb) { - g_warning ("attach maindb failed, query will not work."); + g_warning("attach maindb failed, query will not work."); goto attach_fail; } - sqlite3_free (sql); + sqlite3_free(sql); /* TODO make db path configurable */ - sql = sqlite3_mprintf ("ATTACH %Q AS userdb", "/home/sylecn/.cache/ibus/pinyin/user-1.0.db"); - rb = sqlite3_exec_simple (db, sql); + sql = sqlite3_mprintf("ATTACH %Q AS userdb", "/home/sylecn/.cache/ibus/pinyin/user-1.0.db"); + rb = sqlite3_exec_simple(db, sql); if (! rb) { - g_warning ("attach userdb failed, query will not work."); + g_warning("attach userdb failed, query will not work."); goto attach_fail; } - sqlite3_free (sql); + sqlite3_free(sql); sql = "CREATE TABLE IF NOT EXISTS userdb.not_phrase (phrase TEXT UNIQUE);"; - rb = sqlite3_exec_simple (db, sql); + rb = sqlite3_exec_simple(db, sql); if (! rb) { - g_warning ("create userdb.not_phrase table failed, query will not work."); + g_warning("create userdb.not_phrase table failed, query will not work."); sql = NULL; goto attach_fail; } @@ -258,8 +258,8 @@ config_db (AppData* appdata) return; attach_fail: - sqlite3_free (sql); - sqlite3_close (db); + sqlite3_free(sql); + sqlite3_close(db); db_fail: appdata->db = NULL; } @@ -268,49 +268,49 @@ db_fail: * allow graceful shutdown by Ctrl-C and SIGTERM. */ static void -setup_sigint_sigterm_handler (AppData *appdata) +setup_sigint_sigterm_handler(AppData *appdata) { GSource *source = NULL; - source = g_unix_signal_source_new (SIGTERM); - g_source_set_callback (source, on_sigterm_received, appdata, NULL); - g_source_attach (source, NULL); - g_source_unref (source); - - source = g_unix_signal_source_new (SIGINT); - g_source_set_callback (source, on_sigterm_received, appdata, NULL); - g_source_attach (source, NULL); - g_source_unref (source); + source = g_unix_signal_source_new(SIGTERM); + g_source_set_callback(source, on_sigterm_received, appdata, NULL); + g_source_attach(source, NULL); + g_source_unref(source); + + source = g_unix_signal_source_new(SIGINT); + g_source_set_callback(source, on_sigterm_received, appdata, NULL); + g_source_attach(source, NULL); + g_source_unref(source); } static void -on_startup (GApplication* app, - AppData* appdata) +on_startup(GApplication *app, + AppData *appdata) { - g_message ("zero-pinyin-service startup()"); - config_db (appdata); - config_dbus_service (appdata); - setup_sigint_sigterm_handler (appdata); - g_application_hold (app); + g_message("zero-pinyin-service startup()"); + config_db(appdata); + config_dbus_service(appdata); + setup_sigint_sigterm_handler(appdata); + g_application_hold(app); } static void -on_activate (GApplication *app, - AppData *appdata) +on_activate(GApplication *app, + AppData *appdata) { - g_message ("zero-pinyin-service activate()"); + g_message("zero-pinyin-service activate()"); } static void -on_shutdown (GApplication *app, - AppData *appdata) +on_shutdown(GApplication *app, + AppData *appdata) { - g_message ("zero-pinyin-service shutdown()"); + g_message("zero-pinyin-service shutdown()"); if (appdata->owner_id > 0) { - g_bus_unown_name (appdata->owner_id); + g_bus_unown_name(appdata->owner_id); appdata->owner_id = 0; } if (appdata->db != NULL) { - sqlite3_close (appdata->db); + sqlite3_close(appdata->db); appdata->db = NULL; } } @@ -320,24 +320,24 @@ on_shutdown (GApplication *app, * it's a console app (GApplication) based on glib and gio. */ int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { static AppData appdata = {0}; GApplication *app = NULL; int status = 0; - setlocale (LC_ALL, ""); + setlocale(LC_ALL, ""); - app = g_application_new ("com.emacsos.zero.ZeroPinyinServiceApp", - G_APPLICATION_FLAGS_NONE); - g_assert_nonnull (app); + app = g_application_new("com.emacsos.zero.ZeroPinyinServiceApp", + G_APPLICATION_FLAGS_NONE); + g_assert_nonnull(app); appdata.app = app; - g_signal_connect (app, "startup", G_CALLBACK (on_startup), &appdata); - g_signal_connect (app, "activate", G_CALLBACK (on_activate), &appdata); - g_signal_connect (app, "shutdown", G_CALLBACK (on_shutdown), &appdata); + g_signal_connect(app, "startup", G_CALLBACK(on_startup), &appdata); + g_signal_connect(app, "activate", G_CALLBACK(on_activate), &appdata); + g_signal_connect(app, "shutdown", G_CALLBACK(on_shutdown), &appdata); - status = g_application_run (G_APPLICATION (app), argc, argv); - g_object_unref (app); + status = g_application_run(G_APPLICATION(app), argc, argv); + g_object_unref(app); return status; } diff --git a/meson.build b/meson.build index 48087ec..323b85e 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,6 @@ # -*- mode: conf -*- project('zero-pinyin-service', ['c', 'cpp'], - version: '0.6.0', + version: '0.6.1', license: 'GPL', default_options: [ 'warning_level=2', diff --git a/parse-pinyin-test.cpp b/parse-pinyin-test.cpp index 7aa5a8e..204f582 100644 --- a/parse-pinyin-test.cpp +++ b/parse-pinyin-test.cpp @@ -4,79 +4,79 @@ #include static void -test_parse_pinyin () +test_parse_pinyin() { - GList* result = NULL; - Pinyin* thispy = NULL; + GList *result = NULL; + Pinyin *thispy = NULL; - result = parse_pinyin ("liyifeng", 15); - g_assert_cmpint (g_list_length (result), ==, 3); + result = parse_pinyin("liyifeng", 15); + g_assert_cmpint(g_list_length(result), ==, 3); - thispy = (Pinyin*) g_list_nth_data (result, 0); - g_assert_cmpint (thispy->shengmu_i, ==, 10); - g_assert_cmpint (thispy->yunmu_i, ==, 34); - g_assert_cmpint (thispy->length, ==, 2); + thispy = (Pinyin *) g_list_nth_data(result, 0); + g_assert_cmpint(thispy->shengmu_i, ==, 10); + g_assert_cmpint(thispy->yunmu_i, ==, 34); + g_assert_cmpint(thispy->length, ==, 2); - thispy = (Pinyin*) g_list_nth_data (result, 1); - g_assert_cmpint (thispy->shengmu_i, ==, 21); - g_assert_cmpint (thispy->yunmu_i, ==, 34); - g_assert_cmpint (thispy->length, ==, 2); + thispy = (Pinyin *) g_list_nth_data(result, 1); + g_assert_cmpint(thispy->shengmu_i, ==, 21); + g_assert_cmpint(thispy->yunmu_i, ==, 34); + g_assert_cmpint(thispy->length, ==, 2); - thispy = (Pinyin*) g_list_nth_data (result, 2); - g_assert_cmpint (thispy->shengmu_i, ==, 5); - g_assert_cmpint (thispy->yunmu_i, ==, 32); - g_assert_cmpint (thispy->length, ==, 4); + thispy = (Pinyin *) g_list_nth_data(result, 2); + g_assert_cmpint(thispy->shengmu_i, ==, 5); + g_assert_cmpint(thispy->yunmu_i, ==, 32); + g_assert_cmpint(thispy->length, ==, 4); - g_list_free_full (result, g_free); + g_list_free_full(result, g_free); } /** * print GList of Pinyin. */ static void -print_parse_result (GList* result) +print_parse_result(GList *result) { - GList* iter = result; - Pinyin* thispy = NULL; + GList *iter = result; + Pinyin *thispy = NULL; while (iter != NULL) { - thispy = (Pinyin*) iter->data; - g_printf ("shengmu_i=%i yunmu_i=%i length=%u\n", - thispy->shengmu_i, thispy->yunmu_i, thispy->length); + thispy = (Pinyin *) iter->data; + g_printf("shengmu_i=%i yunmu_i=%i length=%u\n", + thispy->shengmu_i, thispy->yunmu_i, thispy->length); iter = iter->next; } } static void -test_parse_pinyin_incomplete_pinyin () +test_parse_pinyin_incomplete_pinyin() { - GList* result = NULL; - Pinyin* thispy = NULL; + GList *result = NULL; + Pinyin *thispy = NULL; - result = parse_pinyin ("zhey", 15); - print_parse_result (result); - g_assert_cmpint (g_list_length (result), ==, 2); + result = parse_pinyin("zhey", 15); + print_parse_result(result); + g_assert_cmpint(g_list_length(result), ==, 2); - thispy = (Pinyin*) g_list_nth_data (result, 0); - g_assert_cmpint (thispy->shengmu_i, ==, 23); - g_assert_cmpint (thispy->yunmu_i, ==, 29); - g_assert_cmpint (thispy->length, ==, 3); + thispy = (Pinyin *) g_list_nth_data(result, 0); + g_assert_cmpint(thispy->shengmu_i, ==, 23); + g_assert_cmpint(thispy->yunmu_i, ==, 29); + g_assert_cmpint(thispy->length, ==, 3); - thispy = (Pinyin*) g_list_nth_data (result, 1); - g_assert_cmpint (thispy->shengmu_i, ==, 21); - g_assert_cmpint (thispy->yunmu_i, ==, 0); - g_assert_cmpint (thispy->length, ==, 1); + thispy = (Pinyin *) g_list_nth_data(result, 1); + g_assert_cmpint(thispy->shengmu_i, ==, 21); + g_assert_cmpint(thispy->yunmu_i, ==, 0); + g_assert_cmpint(thispy->length, ==, 1); - g_list_free_full (result, g_free); + g_list_free_full(result, g_free); } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - g_test_init (&argc, &argv, NULL); - g_test_add_func ("/zero/test_parse_pinyin", - test_parse_pinyin); - g_test_add_func ("/zero/test_parse_pinyin incomplete pinyin", - test_parse_pinyin_incomplete_pinyin); + g_test_init(&argc, &argv, NULL); + g_test_add_func("/zero/test_parse_pinyin", + test_parse_pinyin); + g_test_add_func("/zero/test_parse_pinyin incomplete pinyin", + test_parse_pinyin_incomplete_pinyin); - return g_test_run (); + return g_test_run(); } diff --git a/parse-pinyin.cpp b/parse-pinyin.cpp index 095d3af..5d7e5c3 100644 --- a/parse-pinyin.cpp +++ b/parse-pinyin.cpp @@ -3,20 +3,20 @@ #include "../PinyinParser.h" #include "../Const.h" -GList* -parse_pinyin (const char* preedit_str, const guint max_pinyin) +GList * +parse_pinyin(const char *preedit_str, const guint max_pinyin) { - GList* result = NULL; + GList *result = NULL; PyZy::PinyinArray pyar = {0}; - Pinyin* thispy = NULL; + Pinyin *thispy = NULL; const guint FLAGS = PINYIN_INCOMPLETE_PINYIN | PINYIN_CORRECT_ALL | PINYIN_FUZZY_ALL; - PyZy::PinyinParser::parse (preedit_str, strlen(preedit_str), FLAGS, pyar, max_pinyin); + PyZy::PinyinParser::parse(preedit_str, strlen(preedit_str), FLAGS, pyar, max_pinyin); for (guint i = 0; i < pyar.size(); ++i) { - thispy = g_new (Pinyin, 1); + thispy = g_new(Pinyin, 1); thispy->shengmu_i = (int) pyar[i].pinyin->pinyin_id[0].sheng; thispy->yunmu_i = (int) pyar[i].pinyin->pinyin_id[0].yun; thispy->length = pyar[i].len; - result = g_list_append (result, thispy); + result = g_list_append(result, thispy); } return result; } diff --git a/parse-pinyin.h b/parse-pinyin.h index eb791ce..347de30 100644 --- a/parse-pinyin.h +++ b/parse-pinyin.h @@ -13,7 +13,7 @@ extern "C" * parse preedit_str to groups of pinyin. * caller should free each Pinyin and the GList after use. */ -GList* parse_pinyin (const char* preedit_str, const guint max_pinyin); +GList *parse_pinyin(const char *preedit_str, const guint max_pinyin); #ifdef __cplusplus } diff --git a/zero-pinyin-service-test.c b/zero-pinyin-service-test.c index 8f43cef..bf72c9a 100644 --- a/zero-pinyin-service-test.c +++ b/zero-pinyin-service-test.c @@ -1,35 +1,35 @@ #include "zero-pinyin-service.h" static void -test_GString () +test_GString() { - GString* s = NULL; - s = g_string_new (NULL); - g_string_append_printf (s, "s0=%d ", 1); + GString *s = NULL; + s = g_string_new(NULL); + g_string_append_printf(s, "s0=%d ", 1); - g_assert_cmpstr (s->str, ==, "s0=1 "); + g_assert_cmpstr(s->str, ==, "s0=1 "); - g_string_free (s, TRUE); + g_string_free(s, TRUE); } static void -test_build_s_y_fields () +test_build_s_y_fields() { - gchar* result = NULL; - result = build_s_y_fields (1); - g_assert_cmpstr (result, ==, ", s0, y0 "); - g_free (result); + gchar *result = NULL; + result = build_s_y_fields(1); + g_assert_cmpstr(result, ==, ", s0, y0 "); + g_free(result); - result = build_s_y_fields (2); - g_assert_cmpstr (result, ==, ", s0, y0, s1, y1 "); - g_free (result); + result = build_s_y_fields(2); + g_assert_cmpstr(result, ==, ", s0, y0, s1, y1 "); + g_free(result); } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - g_test_init (&argc, &argv, NULL); - g_test_add_func ("/zero/test_GString", test_GString); - g_test_add_func ("/zero/test_build_s_y_fields", test_build_s_y_fields); - return g_test_run (); + g_test_init(&argc, &argv, NULL); + g_test_add_func("/zero/test_GString", test_GString); + g_test_add_func("/zero/test_build_s_y_fields", test_build_s_y_fields); + return g_test_run(); } diff --git a/zero-pinyin-service.c b/zero-pinyin-service.c index 4ed3aab..034f9a0 100644 --- a/zero-pinyin-service.c +++ b/zero-pinyin-service.c @@ -3,31 +3,31 @@ #include "../sqlite3_util.h" void -get_candidates_test (const char* preedit_str, - const guint fetch_size, - GVariantBuilder *candidates_builder, - GVariantBuilder *matched_lengths_builder) +get_candidates_test(const char *preedit_str, + const guint fetch_size, + GVariantBuilder *candidates_builder, + GVariantBuilder *matched_lengths_builder) { - if (g_str_equal (preedit_str, "liyifeng")) { + if (g_str_equal(preedit_str, "liyifeng")) { const gchar *matches[] = {"李易峰", "利益", "礼仪", "离异", "里", "理", "力"}; guint matched_lengths[] = {8, 4, 4, 4, 2, 2, 2}; - for (guint i = 0; i < G_N_ELEMENTS (matches); ++i) { - g_variant_builder_add (candidates_builder, "s", matches[i]); - g_variant_builder_add (matched_lengths_builder, "u", matched_lengths[i]); + for (guint i = 0; i < G_N_ELEMENTS(matches); ++i) { + g_variant_builder_add(candidates_builder, "s", matches[i]); + g_variant_builder_add(matched_lengths_builder, "u", matched_lengths[i]); } - } else if (g_str_equal (preedit_str, "feng")) { + } else if (g_str_equal(preedit_str, "feng")) { const gchar *matches[] = {"风", "封", "疯", "丰", "凤"}; guint matched_lengths[] = {4, 4, 4, 4, 4, 4}; - for (guint i = 0; i < G_N_ELEMENTS (matches); ++i) { - g_variant_builder_add (candidates_builder, "s", matches[i]); - g_variant_builder_add (matched_lengths_builder, "u", matched_lengths[i]); + for (guint i = 0; i < G_N_ELEMENTS(matches); ++i) { + g_variant_builder_add(candidates_builder, "s", matches[i]); + g_variant_builder_add(matched_lengths_builder, "u", matched_lengths[i]); } - } else if (g_str_equal (preedit_str, "yifeng")) { + } else if (g_str_equal(preedit_str, "yifeng")) { const gchar *matches[] = {"一封", "遗风", "艺", "依", "一", "以"}; guint matched_lengths[] = {6, 6, 2, 2, 2, 2}; - for (guint i = 0; i < G_N_ELEMENTS (matches); ++i) { - g_variant_builder_add (candidates_builder, "s", matches[i]); - g_variant_builder_add (matched_lengths_builder, "u", matched_lengths[i]); + for (guint i = 0; i < G_N_ELEMENTS(matches); ++i) { + g_variant_builder_add(candidates_builder, "s", matches[i]); + g_variant_builder_add(matched_lengths_builder, "u", matched_lengths[i]); } } } @@ -40,38 +40,38 @@ get_candidates_test (const char* preedit_str, * * returns: where_clause, caller should g_free() result after use. */ -static char* -build_where_clause (GList* pylist, - const guint n) +static char * +build_where_clause(GList *pylist, + const guint n) { - GString* s = NULL; - GList* iter = pylist; + GString *s = NULL; + GList *iter = pylist; gboolean first_condition_done = FALSE; - Pinyin* thispy = NULL; - s = g_string_new (NULL); + Pinyin *thispy = NULL; + s = g_string_new(NULL); for (guint i = 0; i < n; ++i) { - g_assert_nonnull (iter); - thispy = (Pinyin*) iter->data; + g_assert_nonnull(iter); + thispy = (Pinyin *) iter->data; if (thispy->shengmu_i) { - if (G_LIKELY (first_condition_done)) { - g_string_append_printf (s, "AND s%u=%d ", i, thispy->shengmu_i); + if (G_LIKELY(first_condition_done)) { + g_string_append_printf(s, "AND s%u=%d ", i, thispy->shengmu_i); } else { - g_string_append_printf (s, "s%u=%d ", i, thispy->shengmu_i); + g_string_append_printf(s, "s%u=%d ", i, thispy->shengmu_i); first_condition_done = TRUE; } } if (thispy->yunmu_i) { - if (G_LIKELY (first_condition_done)) { - g_string_append_printf (s, "AND y%u=%d ", i, thispy->yunmu_i); + if (G_LIKELY(first_condition_done)) { + g_string_append_printf(s, "AND y%u=%d ", i, thispy->yunmu_i); } else { - g_string_append_printf (s, "y%u=%d ", i, thispy->yunmu_i); + g_string_append_printf(s, "y%u=%d ", i, thispy->yunmu_i); first_condition_done = TRUE; } } iter = iter->next; } - gchar* result = s->str; - g_string_free (s, FALSE); + gchar *result = s->str; + g_string_free(s, FALSE); return result; } @@ -80,18 +80,18 @@ build_where_clause (GList* pylist, * * caller should g_free() result after use. */ -char* -build_s_y_fields (const guint n) +char * +build_s_y_fields(const guint n) { GString *s = NULL; - g_assert_cmpint (n, >=, 1); - s = g_string_new (NULL); + g_assert_cmpint(n, >=, 1); + s = g_string_new(NULL); for (guint i = 0; i < n; ++i) { - g_string_append_printf (s, ", s%u, y%u", i, i); + g_string_append_printf(s, ", s%u, y%u", i, i); } - s = g_string_append (s, " "); + s = g_string_append(s, " "); gchar *result = s->str; - g_string_free (s, FALSE); + g_string_free(s, FALSE); return result; } @@ -101,45 +101,45 @@ build_s_y_fields (const guint n) * * caller should free result with g_free() after use. */ -static char* -build_sql_for_n_pinyin (GList* pylist, - const guint n, - const guint limit) +static char * +build_sql_for_n_pinyin(GList *pylist, + const guint n, + const guint limit) { /* always keep one space after current term */ - GString* sql = NULL; - gchar* where_clause = NULL; - sql = g_string_new ("SELECT MAX(user_freq) AS user_freq, " - "phrase, MAX(freq) AS freq"); - gchar* s_y_fields = build_s_y_fields (n); - g_string_append_printf (sql, s_y_fields); - g_string_append_printf (sql, "FROM ("); - g_string_append_printf ( + GString *sql = NULL; + gchar *where_clause = NULL; + sql = g_string_new("SELECT MAX(user_freq) AS user_freq, " + "phrase, MAX(freq) AS freq"); + gchar *s_y_fields = build_s_y_fields(n); + g_string_append_printf(sql, s_y_fields); + g_string_append_printf(sql, "FROM ("); + g_string_append_printf( sql, "SELECT 0 AS user_freq, phrase, freq"); - g_string_append_printf (sql, s_y_fields); - g_string_append_printf ( + g_string_append_printf(sql, s_y_fields); + g_string_append_printf( sql, "FROM maindb.py_phrase_%u WHERE ", n - 1); - where_clause = build_where_clause (pylist, n); - g_assert_nonnull (where_clause); - g_debug ("where_clause=%s", where_clause); - sql = g_string_append (sql, where_clause); - sql = g_string_append (sql, "UNION "); - g_string_append_printf ( + where_clause = build_where_clause(pylist, n); + g_assert_nonnull(where_clause); + g_debug("where_clause=%s", where_clause); + sql = g_string_append(sql, where_clause); + sql = g_string_append(sql, "UNION "); + g_string_append_printf( sql, "SELECT user_freq, phrase, freq"); - g_string_append_printf (sql, s_y_fields); - g_string_append_printf ( + g_string_append_printf(sql, s_y_fields); + g_string_append_printf( sql, "FROM userdb.py_phrase_%u WHERE ", n - 1); - sql = g_string_append (sql, where_clause); - sql = g_string_append ( - sql, ") " - "WHERE phrase NOT IN (SELECT phrase FROM userdb.not_phrase) " - "GROUP BY phrase " - "ORDER BY user_freq DESC, freq DESC "); - g_string_append_printf (sql, "LIMIT %u;", limit); - char* result = sql->str; - g_free (s_y_fields); - g_free (where_clause); - g_string_free (sql, FALSE); + sql = g_string_append(sql, where_clause); + sql = g_string_append( + sql, ") " + "WHERE phrase NOT IN (SELECT phrase FROM userdb.not_phrase) " + "GROUP BY phrase " + "ORDER BY user_freq DESC, freq DESC "); + g_string_append_printf(sql, "LIMIT %u;", limit); + char *result = sql->str; + g_free(s_y_fields); + g_free(where_clause); + g_string_free(sql, FALSE); return result; } @@ -151,14 +151,14 @@ build_sql_for_n_pinyin (GList* pylist, * see param meaning there. */ static guint -get_matched_py_length (const char* preedit_str, - GList* pylist, - const guint group_size) +get_matched_py_length(const char *preedit_str, + GList *pylist, + const guint group_size) { guint matched_py_length = 0; - GList* iter = pylist; + GList *iter = pylist; - g_assert_cmpint (group_size, >=, 1); + g_assert_cmpint(group_size, >=, 1); /* For usual pinyin string, just add up the Pinyin length. But for * pinyin that contains ', when a Pinyin in pylist is used, also take * the ' before and after it. */ @@ -166,7 +166,7 @@ get_matched_py_length (const char* preedit_str, while (preedit_str[matched_py_length] == '\'') { matched_py_length++; } - matched_py_length += ((Pinyin*) iter->data)->length; + matched_py_length += ((Pinyin *) iter->data)->length; while (preedit_str[matched_py_length] == '\'') { matched_py_length++; } @@ -189,167 +189,167 @@ get_matched_py_length (const char* preedit_str, * returns: how many candidates fetched. */ static guint -get_candidates_for_n_pinyin (sqlite3* db, - const char* preedit_str, - GList* pylist, - const guint group_size, - const guint limit, - GList** candidates) +get_candidates_for_n_pinyin(sqlite3 *db, + const char *preedit_str, + GList *pylist, + const guint group_size, + const guint limit, + GList **candidates) { const guint DEFAULT_LIMIT = 50; - GList* result = NULL; /* GList of Candidate */ + GList *result = NULL; /* GList of Candidate */ - g_assert_nonnull (db); - g_assert_cmpint (group_size, >=, 1); - g_assert_cmpint (group_size, <=, g_list_length (pylist)); + g_assert_nonnull(db); + g_assert_cmpint(group_size, >=, 1); + g_assert_cmpint(group_size, <=, g_list_length(pylist)); gint candidates_count = 0; gint r = 0; /* build SQL and run SQL query */ - char* sql = NULL; - sql = build_sql_for_n_pinyin (pylist, group_size, MAX (limit, DEFAULT_LIMIT)); - g_debug ("build_sql_for_n_pinyin result SQL:\n\n%s\n", sql); + char *sql = NULL; + sql = build_sql_for_n_pinyin(pylist, group_size, MAX(limit, DEFAULT_LIMIT)); + g_debug("build_sql_for_n_pinyin result SQL:\n\n%s\n", sql); - guint matched_py_length = get_matched_py_length (preedit_str, pylist, group_size); + guint matched_py_length = get_matched_py_length(preedit_str, pylist, group_size); - sqlite3_stmt* stmt = NULL; - const char* unused; - Candidate* c = NULL; - r = sqlite3_prepare_v2 (db, sql, -1, &stmt, &unused); - g_assert_nonnull (unused); - g_assert_cmpstr (unused, ==, ""); - if (strlen (unused)) { - g_warning ("part of sql is unused \"%s\" length=%zu", - unused, strlen (unused)); + sqlite3_stmt *stmt = NULL; + const char *unused; + Candidate *c = NULL; + r = sqlite3_prepare_v2(db, sql, -1, &stmt, &unused); + g_assert_nonnull(unused); + g_assert_cmpstr(unused, ==, ""); + if (strlen(unused)) { + g_warning("part of sql is unused \"%s\" length=%zu", + unused, strlen(unused)); } - g_free (sql); + g_free(sql); while (TRUE) { - r = sqlite3_step (stmt); + r = sqlite3_step(stmt); if (r == SQLITE_DONE) { break; } else if (r == SQLITE_ROW) { - c = g_new0 (Candidate, 1); + c = g_new0(Candidate, 1); /* sql SELECT should select these columns in order */ - c->user_freq = sqlite3_column_int (stmt, 0); - c->str = g_strdup ((const char*) sqlite3_column_text (stmt, 1)); - c->freq = sqlite3_column_int (stmt, 2); + c->user_freq = sqlite3_column_int(stmt, 0); + c->str = g_strdup((const char *) sqlite3_column_text(stmt, 1)); + c->freq = sqlite3_column_int(stmt, 2); c->matched_py_length = matched_py_length; c->char_len = group_size; - c->py_indices = g_malloc0 (sizeof (Pinyin*) * group_size); + c->py_indices = g_malloc0(sizeof(Pinyin *) * group_size); for (guint i = 0; i < group_size; ++i) { - c->py_indices[i] = g_new0 (Pinyin, 1); - c->py_indices[i]->shengmu_i = sqlite3_column_int (stmt, 3 + i * 2); - c->py_indices[i]->yunmu_i = sqlite3_column_int (stmt, 4 + i * 2); + c->py_indices[i] = g_new0(Pinyin, 1); + c->py_indices[i]->shengmu_i = sqlite3_column_int(stmt, 3 + i * 2); + c->py_indices[i]->yunmu_i = sqlite3_column_int(stmt, 4 + i * 2); /* we don't care about ->length field */ } - if (g_utf8_validate (c->str, -1, NULL)) { - result = g_list_prepend (result, c); + if (g_utf8_validate(c->str, -1, NULL)) { + result = g_list_prepend(result, c); candidates_count++; } else { - g_warning ("ignore non utf8 phrase: %s", c->str); + g_warning("ignore non utf8 phrase: %s", c->str); } } else if (r == SQLITE_BUSY) { - g_warning ("sqlite3_step got SQLITE_BUSY"); + g_warning("sqlite3_step got SQLITE_BUSY"); break; } else { - g_warning ("sqlite3_step error: %d (%s)", - r, sqlite3_errmsg (db)); + g_warning("sqlite3_step error: %d (%s)", + r, sqlite3_errmsg(db)); break; } } - r = sqlite3_finalize (stmt); + r = sqlite3_finalize(stmt); if (r != SQLITE_OK) { - g_debug ("sqlite3_finalize error: %d (%s)", r, sqlite3_errmsg (db)); + g_debug("sqlite3_finalize error: %d (%s)", r, sqlite3_errmsg(db)); } /* store query result in a new GList */ - *candidates = g_list_reverse (result); + *candidates = g_list_reverse(result); return candidates_count; } static void -add_candidate_to_builders (Candidate *c, - GVariantBuilder *candidates_builder, - GVariantBuilder *matched_lengths_builder, - GVariantBuilder *candidates_pinyin_indices) +add_candidate_to_builders(Candidate *c, + GVariantBuilder *candidates_builder, + GVariantBuilder *matched_lengths_builder, + GVariantBuilder *candidates_pinyin_indices) { - g_variant_builder_add (candidates_builder, "s", c->str); - g_variant_builder_add (matched_lengths_builder, "u", c->matched_py_length); + g_variant_builder_add(candidates_builder, "s", c->str); + g_variant_builder_add(matched_lengths_builder, "u", c->matched_py_length); GVariantBuilder *py_indices_builder = NULL; - py_indices_builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ii)")); + py_indices_builder = g_variant_builder_new(G_VARIANT_TYPE("a(ii)")); for (guint i = 0; i < c->char_len; ++i) { - g_variant_builder_add ( + g_variant_builder_add( py_indices_builder, "(ii)", c->py_indices[i]->shengmu_i, c->py_indices[i]->yunmu_i); - g_debug ("adding (ii) %d %d", - c->py_indices[i]->shengmu_i, - c->py_indices[i]->yunmu_i); - g_free (c->py_indices[i]); + g_debug("adding (ii) %d %d", + c->py_indices[i]->shengmu_i, + c->py_indices[i]->yunmu_i); + g_free(c->py_indices[i]); } - g_debug ("adding a(ii) to aa(ii)"); - g_variant_builder_add (candidates_pinyin_indices, "a(ii)", - py_indices_builder); - g_variant_builder_unref (py_indices_builder); - g_free (c->str); - g_free (c->py_indices); + g_debug("adding a(ii) to aa(ii)"); + g_variant_builder_add(candidates_pinyin_indices, "a(ii)", + py_indices_builder); + g_variant_builder_unref(py_indices_builder); + g_free(c->str); + g_free(c->py_indices); } void -get_candidates (sqlite3* db, - const char* preedit_str, - const guint fetch_size, - GVariantBuilder *candidates_builder, - GVariantBuilder *matched_lengths_builder, - GVariantBuilder *candidates_pinyin_indices) +get_candidates(sqlite3 *db, + const char *preedit_str, + const guint fetch_size, + GVariantBuilder *candidates_builder, + GVariantBuilder *matched_lengths_builder, + GVariantBuilder *candidates_pinyin_indices) { if (! db) { - g_warning ("No db connection, can't get candidates."); + g_warning("No db connection, can't get candidates."); return; } - GList* pylist = NULL; + GList *pylist = NULL; guint pylist_len = 0; - pylist = parse_pinyin (preedit_str, 15); - pylist_len = g_list_length (pylist); + pylist = parse_pinyin(preedit_str, 15); + pylist_len = g_list_length(pylist); guint group_size = pylist_len; guint fetched_size = 0; guint r = 0; - GList* candidates = NULL; + GList *candidates = NULL; while (fetched_size < fetch_size && group_size > 0) { - g_message ("phrase length=%u", group_size); - r = get_candidates_for_n_pinyin (db, preedit_str, pylist, group_size, fetch_size - fetched_size, &candidates); + g_message("phrase length=%u", group_size); + r = get_candidates_for_n_pinyin(db, preedit_str, pylist, group_size, fetch_size - fetched_size, &candidates); if (candidates) { - GList* iter = g_list_first (candidates); - Candidate* c = NULL; + GList *iter = g_list_first(candidates); + Candidate *c = NULL; while (iter != NULL) { - c = (Candidate*) iter->data; - add_candidate_to_builders ( + c = (Candidate *) iter->data; + add_candidate_to_builders( c, candidates_builder, matched_lengths_builder, candidates_pinyin_indices); iter = iter->next; } - g_list_free_full (candidates, g_free); + g_list_free_full(candidates, g_free); } - g_message ("%u candidates found", r); + g_message("%u candidates found", r); fetched_size += r; group_size--; } - g_message ("returning %u candidates", fetched_size); - g_list_free_full (pylist, g_free); + g_message("returning %u candidates", fetched_size); + g_list_free_full(pylist, g_free); } /** * sub function for commit_candidate() */ static void -_update_userdb_py_phrase (sqlite3 *db, - const gchar *candidate, - GVariant *candidate_pinyin_indices, - guint len) /* utf8 length of candidate char */ +_update_userdb_py_phrase(sqlite3 *db, + const gchar *candidate, + GVariant *candidate_pinyin_indices, + guint len) /* utf8 length of candidate char */ { GString *sql = NULL; GVariantIter iter = {0}; @@ -360,117 +360,117 @@ _update_userdb_py_phrase (sqlite3 *db, char *s = NULL; gboolean rb = FALSE; - g_assert_nonnull (db); - g_assert_nonnull (candidate); - g_assert_nonnull (candidate_pinyin_indices); + g_assert_nonnull(db); + g_assert_nonnull(candidate); + g_assert_nonnull(candidate_pinyin_indices); /* insert candidate maybe */ - sql = g_string_new (NULL); - g_string_append_printf (sql, "INSERT OR IGNORE INTO userdb.py_phrase_%u (user_freq, phrase, freq", len - 1); - gchar* s_y_fields = build_s_y_fields (len); - sql = g_string_append (sql, s_y_fields); - g_free (s_y_fields); - s = sqlite3_mprintf (") VALUES (0, %Q, 0", candidate); - sql = g_string_append (sql, s); - sqlite3_free (s); + sql = g_string_new(NULL); + g_string_append_printf(sql, "INSERT OR IGNORE INTO userdb.py_phrase_%u (user_freq, phrase, freq", len - 1); + gchar *s_y_fields = build_s_y_fields(len); + sql = g_string_append(sql, s_y_fields); + g_free(s_y_fields); + s = sqlite3_mprintf(") VALUES (0, %Q, 0", candidate); + sql = g_string_append(sql, s); + sqlite3_free(s); /* iter over GVariant "a(ii)" */ - g_variant_iter_init (&iter, candidate_pinyin_indices); + g_variant_iter_init(&iter, candidate_pinyin_indices); count = 0; - while ((child = g_variant_iter_next_value (&iter))) { - g_variant_get (child, "(ii)", &x, &y); - g_string_append_printf (sql, ", %d, %d", x, y); + while ((child = g_variant_iter_next_value(&iter))) { + g_variant_get(child, "(ii)", &x, &y); + g_string_append_printf(sql, ", %d, %d", x, y); count++; } if (count != len) { - g_warning ("candidate length=%u, a(ii) length=%u, mismatch!", - len, count); - g_string_free (sql, TRUE); - g_assert_not_reached (); + g_warning("candidate length=%u, a(ii) length=%u, mismatch!", + len, count); + g_string_free(sql, TRUE); + g_assert_not_reached(); return; } - g_string_append_printf (sql, ");"); - rb = sqlite3_exec_simple (db, sql->str); + g_string_append_printf(sql, ");"); + rb = sqlite3_exec_simple(db, sql->str); if (! rb) { - g_warning ("INSERT candidate to userdb failed"); + g_warning("INSERT candidate to userdb failed"); } else { - if (sqlite3_changes (db) == 1) { - g_message ("candidate %s inserted to userdb", candidate); + if (sqlite3_changes(db) == 1) { + g_message("candidate %s inserted to userdb", candidate); } } - g_string_free (sql, TRUE); + g_string_free(sql, TRUE); /* increment user_freq field for candidate */ - sql = g_string_new (NULL); - g_string_append_printf (sql, "UPDATE userdb.py_phrase_%u " - "SET user_freq = user_freq + 1 ", len - 1); - s = sqlite3_mprintf ("WHERE phrase = %Q ", candidate); - sql = g_string_append (sql, s); - sqlite3_free (s); - g_variant_iter_init (&iter, candidate_pinyin_indices); + sql = g_string_new(NULL); + g_string_append_printf(sql, "UPDATE userdb.py_phrase_%u " + "SET user_freq = user_freq + 1 ", len - 1); + s = sqlite3_mprintf("WHERE phrase = %Q ", candidate); + sql = g_string_append(sql, s); + sqlite3_free(s); + g_variant_iter_init(&iter, candidate_pinyin_indices); count = 0; - while ((child = g_variant_iter_next_value (&iter))) { - g_variant_get (child, "(ii)", &x, &y); - g_string_append_printf (sql, "AND s%d=%d AND y%d=%d ", - count, x, count, y); + while ((child = g_variant_iter_next_value(&iter))) { + g_variant_get(child, "(ii)", &x, &y); + g_string_append_printf(sql, "AND s%d=%d AND y%d=%d ", + count, x, count, y); count++; } - sql = g_string_append (sql, ";"); - rb = sqlite3_exec_simple (db, sql->str); + sql = g_string_append(sql, ";"); + rb = sqlite3_exec_simple(db, sql->str); if (! rb) { - g_warning ("UPDATE candidate user_freq failed"); + g_warning("UPDATE candidate user_freq failed"); } else { - if (sqlite3_changes (db) == 1) { - g_message ("candidate %s user_freq incremented", candidate); + if (sqlite3_changes(db) == 1) { + g_message("candidate %s user_freq incremented", candidate); } else { - g_warning ("UPDATE candidate user_freq failed, no match"); + g_warning("UPDATE candidate user_freq failed, no match"); } } - g_string_free (sql, TRUE); + g_string_free(sql, TRUE); } static void -_update_userdb_not_phrase (sqlite3 *db, - const gchar *candidate) +_update_userdb_not_phrase(sqlite3 *db, + const gchar *candidate) { - g_assert_nonnull (db); - g_assert_nonnull (candidate); + g_assert_nonnull(db); + g_assert_nonnull(candidate); gboolean rb = FALSE; - char *sql = sqlite3_mprintf ("DELETE FROM userdb.not_phrase WHERE phrase = %Q;", candidate); - rb = sqlite3_exec_simple (db, sql); + char *sql = sqlite3_mprintf("DELETE FROM userdb.not_phrase WHERE phrase = %Q;", candidate); + rb = sqlite3_exec_simple(db, sql); if (! rb) { - g_warning ("DELETE candidate from not_phrase failed"); + g_warning("DELETE candidate from not_phrase failed"); } else { - if (sqlite3_changes (db) == 1) { - g_message ("candidate %s removed from not_phrase", candidate); + if (sqlite3_changes(db) == 1) { + g_message("candidate %s removed from not_phrase", candidate); } } - sqlite3_free (sql); + sqlite3_free(sql); } void -commit_candidate (sqlite3 *db, - const gchar *candidate, - GVariant *candidate_pinyin_indices) +commit_candidate(sqlite3 *db, + const gchar *candidate, + GVariant *candidate_pinyin_indices) { if (! db) { - g_warning ("No db connection, can't commit candidates."); + g_warning("No db connection, can't commit candidates."); return; } if (! candidate) { - g_warning ("candidate should not be NULL. won't commit candidate."); + g_warning("candidate should not be NULL. won't commit candidate."); return; } if (! candidate_pinyin_indices) { - g_warning ("candidate_pinyin_indices should not be NULL. won't commit candidate."); + g_warning("candidate_pinyin_indices should not be NULL. won't commit candidate."); return; } - guint len = g_utf8_strlen (candidate, -1); + guint len = g_utf8_strlen(candidate, -1); if (len <= 1) { - g_message ("commit single character %s is a no-op", candidate); + g_message("commit single character %s is a no-op", candidate); return; } - _update_userdb_py_phrase (db, candidate, candidate_pinyin_indices, len); - _update_userdb_not_phrase (db, candidate); + _update_userdb_py_phrase(db, candidate, candidate_pinyin_indices, len); + _update_userdb_not_phrase(db, candidate); } diff --git a/zero-pinyin-service.h b/zero-pinyin-service.h index 13d0d19..2067e52 100644 --- a/zero-pinyin-service.h +++ b/zero-pinyin-service.h @@ -29,10 +29,10 @@ typedef struct { /** * an implementation of get_candidates() with simple test data. */ -void get_candidates_test (const char *preedit_str, - const guint fetch_size, - GVariantBuilder *candidates_builder, - GVariantBuilder *matched_lengths_builder); +void get_candidates_test(const char *preedit_str, + const guint fetch_size, + GVariantBuilder *candidates_builder, + GVariantBuilder *matched_lengths_builder); /** * fetch candidates for preedit_str. @@ -42,12 +42,12 @@ void get_candidates_test (const char *preedit_str, * @candidates_builder candidates will be added to this builder * @matched_lengths_builder matched preedit_str length will be added to this builder */ -void get_candidates (sqlite3 *db, - const char *preedit_str, - const guint fetch_size, - GVariantBuilder *candidates_builder, - GVariantBuilder *matched_lengths_builder, - GVariantBuilder *candidates_pinyin_indices); +void get_candidates(sqlite3 *db, + const char *preedit_str, + const guint fetch_size, + GVariantBuilder *candidates_builder, + GVariantBuilder *matched_lengths_builder, + GVariantBuilder *candidates_pinyin_indices); /** * commit candidate. @@ -55,12 +55,12 @@ void get_candidates (sqlite3 *db, * This will save candidate in user db so it's available in the future. * It also update user_freq for given candidate. */ -void commit_candidate (sqlite3 *db, - const gchar *candidate, - GVariant *candidate_pinyin_indices); +void commit_candidate(sqlite3 *db, + const gchar *candidate, + GVariant *candidate_pinyin_indices); /* for test only */ -char* build_s_y_fields (const guint n); +char *build_s_y_fields(const guint n); G_END_DECLS -- GitLab