1 /* 2 * libpinyin 3 * Library to deal with pinyin. 4 * 5 * Copyright (C) 2011 Peng Wu <alexepico@gmail.com> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #ifndef CHEWING_KEY_H 22 #define CHEWING_KEY_H 23 24 #include <glib.h> 25 #include "chewing_enum.h" 26 27 using namespace pinyin; 28 29 G_BEGIN_DECLS 30 31 /** @file chewing_key.h 32 * @brief the definitions of chewing key related classes and structs. 33 */ 34 35 36 /** Note: The parsed pinyins are stored in the following two 37 * GArrays to speed up chewing table lookup. 38 * As the chewing large table only contains information of struct ChewingKey. 39 */ 40 41 struct _ChewingKey 42 { 43 guint16 m_initial : 5; 44 guint16 m_middle : 2; 45 guint16 m_final : 5; 46 guint16 m_tone : 3; 47 guint16 m_zero_padding : 1; 48 _ChewingKey_ChewingKey49 _ChewingKey() { 50 m_initial = CHEWING_ZERO_INITIAL; 51 m_middle = CHEWING_ZERO_MIDDLE; 52 m_final = CHEWING_ZERO_FINAL; 53 m_tone = CHEWING_ZERO_TONE; 54 m_zero_padding = 0; 55 } 56 _ChewingKey_ChewingKey57 _ChewingKey(ChewingInitial initial, ChewingMiddle middle, 58 ChewingFinal final) { 59 m_initial = initial; 60 m_middle = middle; 61 m_final = final; 62 m_tone = CHEWING_ZERO_TONE; 63 m_zero_padding = 0; 64 } 65 66 public: 67 gint get_table_index(); 68 bool is_valid_zhuyin(); 69 70 /* Note: the return value should be freed by g_free. */ 71 gchar * get_pinyin_string(); 72 gchar * get_shengmu_string(); 73 gchar * get_yunmu_string(); 74 gchar * get_zhuyin_string(); 75 gchar * get_luoma_pinyin_string(); 76 gchar * get_secondary_zhuyin_string(); 77 }; 78 79 typedef struct _ChewingKey ChewingKey; 80 81 static inline bool operator == (ChewingKey lhs, ChewingKey rhs) { 82 if (lhs.m_initial != rhs.m_initial) 83 return false; 84 if (lhs.m_middle != rhs.m_middle) 85 return false; 86 if (lhs.m_final != rhs.m_final) 87 return false; 88 if (lhs.m_tone != rhs.m_tone) 89 return false; 90 return true; 91 } 92 93 static inline bool operator != (ChewingKey lhs, ChewingKey rhs) { 94 return !(lhs == rhs); 95 } 96 97 struct _ChewingKeyRest 98 { 99 /* Note: the table index is removed, 100 * Please use get_table_index in ChewingKey. 101 */ 102 guint16 m_raw_begin; /* the begin of the raw input. */ 103 guint16 m_raw_end; /* the end of the raw input. */ 104 _ChewingKeyRest_ChewingKeyRest105 _ChewingKeyRest() { 106 /* the 0th item in pinyin parser table is reserved for invalid. */ 107 m_raw_begin = 0; 108 m_raw_end = 0; 109 } 110 length_ChewingKeyRest111 guint16 length() { 112 return m_raw_end - m_raw_begin; 113 } 114 }; 115 116 typedef struct _ChewingKeyRest ChewingKeyRest; 117 118 G_END_DECLS 119 120 #endif 121