From b64e42f4000785e38a83322a381fe6dd98d347cd Mon Sep 17 00:00:00 2001 From: Peng Huang Date: Wed, 14 Apr 2010 22:29:31 +0800 Subject: [PATCH] Load phrases.txt from home dir & pkgdata dir --- src/SpecialTable.cc | 198 ++++++++++++++++++++++++++++++++++------------------ src/phrases.txt | 71 +++++++++++++++++++ src/special_phrases | 68 ------------------ 3 files changed, 200 insertions(+), 137 deletions(-) create mode 100644 src/phrases.txt delete mode 100644 src/special_phrases diff --git a/src/SpecialTable.cc b/src/SpecialTable.cc index 4238573..87c4105 100644 --- a/src/SpecialTable.cc +++ b/src/SpecialTable.cc @@ -73,56 +73,108 @@ public: std::snprintf (string, sizeof (string), fmt, d); return string; } -#if 0 -; $(year) 年(4位) 2006、2008 -; $(year_yy) 年(2位) 06、08 -; $(month) 月 12、8、3 -; $(month_mm) 月 12、08、03 -; $(day) 日 3、13、22 -; $(day_dd) 日 03、13、22 -; $(weekday) 星期 0、1、2、5、6 -; $(fullhour) 时(24小时制) 02、08、13、23 -; $(halfhour) 时(12小时制) 02、08、01、11 -; $(ampm) AM、PM(英) AM、PM(大写) -; $(minute) 分 02、08、15、28 -; $(second) 秒 02、08、15、28 -; $(year_cn) 年(中文4位) 二〇〇六 -; $(year_yy_cn) 年(中文2位) 〇六 -; $(month_cn) 月(中文) 十二、八、三 -; $(day_cn) 日(中文) 三、十三、二十二 -; $(weekday_cn) 星期(中文) 日、一、二、五、六 -; $(fullhour_cn) 月(中文24时制) 二、八、十三、二十三 -; $(halfhour_cn) 时(中文12时制) 二、八、一、十一 -; $(ampm_cn) 上午下午(中文) 上午、下午 -; $(minute_cn) 分(中文) 零二、零八、十五、二十八 -; $(second_cn) 秒(中文) 零二、零八、十五、二十八 -#endif + + const std::string year_cn (gboolean yy = FALSE) { + static const gchar * digits[] = { + "〇", "一", "二", "三", "四", + "五", "六", "七", "八", "九" + }; + + gint year = localtime (&m_time)->tm_year + 1900; + gint bit = 0; + if (yy) { + year %= 100; + bit = 2; + } + + std::string result; + while (year != 0 || bit > 0) { + result.insert(0, digits[year % 10]); + year /= 10; + bit -= 1; + } + return result; + } + + const std::string month_cn (void) { + static const gchar *month_num[] = { + "一", "二", "三", "四", "五", "六", "七", "八", + "九", "十", "十一", "十二" + }; + return month_num[localtime (&m_time)->tm_mon]; + } + + const std::string weekday_cn (void) { + static const gchar *week_num[] = { + "一", "二", "三", "四", "五", "六", "日", + }; + return week_num[localtime (&m_time)->tm_wday]; + } + + const std::string hour_cn (guint i) { + static const gchar *hour_num[] = { + "一", "二", "三", "四", "五", + "六", "七", "八", "九", "十", + "十一", "十二", "十三", "十四", "十五", + "十六", "十七", "十八", "十九", "二十", + "二十一", "二十二", "二十三", "零" + }; + return hour_num[i]; + } + + const std::string fullhour_cn (void) { + return hour_cn (localtime (&m_time)->tm_hour); + } + + const std::string halfhour_cn (void) { + return hour_cn (localtime (&m_time)->tm_hour % 12); + } + + const std::string day_cn (void) { + static const gchar *day_num[] = { + "", "一", "二", "三", "四", + "五", "六", "七", "八", "九", + "", "十","二十", "三十" + }; + guint day = localtime (&m_time)->tm_mday + 1; + return std::string () + day_num[day / 10 + 10] + day_num[day % 10]; + } + + const std::string minsec_cn (guint i) { + static const gchar *num[] = { + "", "一", "二", "三", "四", + "五", "六", "七", "八", "九", + "零", "十","二十", "三十", "四十" + "五十", "六十" + }; + return std::string () + num[i / 10 + 10] + num[i % 10]; + } + const std::string variable (const std::string &name) { - if (name == "year") - return dec (localtime (&m_time)->tm_year + 1900); - if (name == "year_yy") - return dec ((localtime (&m_time)->tm_year + 1900) % 100, "%02d"); - if (name == "month") - return dec (localtime (&m_time)->tm_mon + 1); - if (name == "month_mm") - return dec (localtime (&m_time)->tm_mon + 1, "%02d"); - if (name == "day") - return dec (localtime (&m_time)->tm_mday + 1); - if (name == "day_dd") - return dec (localtime (&m_time)->tm_mday + 1, "%02d"); - if (name == "week") - return dec (localtime (&m_time)->tm_wday + 1); - if (name == "fullhour") - return dec (localtime (&m_time)->tm_hour + 1, "%02d"); - if (name == "falfhour") - return dec ((localtime (&m_time)->tm_hour + 1) % 12, "%02d"); - if (name == "ampm") - return localtime (&m_time)->tm_hour < 12 ? "AM" : "PM"; - if (name == "minute") - return dec (localtime (&m_time)->tm_min + 1, "%02d"); - if (name == "second") - return dec (localtime (&m_time)->tm_sec + 1, "%02d"); - return name; + if (name == "year") return dec (localtime (&m_time)->tm_year + 1900); + if (name == "year_yy") return dec ((localtime (&m_time)->tm_year + 1900) % 100, "%02d"); + if (name == "month") return dec (localtime (&m_time)->tm_mon + 1); + if (name == "month_mm") return dec (localtime (&m_time)->tm_mon + 1, "%02d"); + if (name == "day") return dec (localtime (&m_time)->tm_mday + 1); + if (name == "day_dd") return dec (localtime (&m_time)->tm_mday + 1, "%02d"); + if (name == "weekday") return dec (localtime (&m_time)->tm_wday + 1); + if (name == "fullhour") return dec (localtime (&m_time)->tm_hour + 1, "%02d"); + if (name == "falfhour") return dec ((localtime (&m_time)->tm_hour + 1) % 12, "%02d"); + if (name == "ampm") return localtime (&m_time)->tm_hour < 12 ? "AM" : "PM"; + if (name == "minute") return dec (localtime (&m_time)->tm_min + 1, "%02d"); + if (name == "second") return dec (localtime (&m_time)->tm_sec + 1, "%02d"); + if (name == "year_cn") return year_cn (); + if (name == "year_yy_cn") return year_cn (TRUE); + if (name == "month_cn") return month_cn (); + if (name == "day_cn") return day_cn (); + if (name == "weekday_cn") return weekday_cn (); + if (name == "fullhour_cn") return fullhour_cn (); + if (name == "halfhour_cn") return halfhour_cn (); + if (name == "ampm_cn") return localtime (&m_time)->tm_hour < 12 ? "上午" : "下午"; + if (name == "minute_cn") return minsec_cn (localtime (&m_time)->tm_min + 1); + if (name == "second_cn") return minsec_cn (localtime (&m_time)->tm_sec + 1); + + return "${" + name + "}"; } private: @@ -144,33 +196,41 @@ SpecialTable::SpecialTable (void) insert ("sj", new DynamicPhrase ("%(hour_24)时%(minute)分%(second)秒", 0)); insert ("sj", new DynamicPhrase ("%(hour_24):%(minute):%(second)", 1)); #endif - load ("special_phrases"); + gchar * path = g_build_filename (g_get_user_config_dir (), + "ibus", "ibus-pinyin", "phrases.txt", NULL); + load (path) || + load (PKGDATADIR G_DIR_SEPARATOR_S "phrases.txt") || + load ("phrases.txt"); + g_free (path); } gboolean SpecialTable::load (const gchar *file) { - try { - std::ifstream in (file); - std::string line; - - while (!in.eof ()) { - getline (in, line); - if (line.size () == 0 || line[0] == ';') - continue; - size_t pos = line.find ('='); - if (pos == line.npos) - continue; - - std::string command = line.substr(0, pos); - std::string phrase = line.substr(pos + 1); - insert (command, new DynamicPhrase (phrase, 0)); - } + std::ifstream in (file); + if (in.fail ()) + return FALSE; + std::string line; + while (!in.eof ()) { + getline (in, line); + if (line.size () == 0 || line[0] == ';') + continue; + size_t pos = line.find ('='); + if (pos == line.npos) + continue; - } catch (...) { - std::cerr << "error" << std::endl; - return FALSE; + std::string command = line.substr(0, pos); + std::string phrase = line.substr(pos + 1); + if (command.empty () || phrase.empty ()) + continue; + + if (phrase[0] != '#') { + insert (command, new StaticPhrase (phrase, 0)); + } + else if (phrase.size () > 1) { + insert (command, new DynamicPhrase (phrase.substr (1), 0)); + } } return TRUE; } diff --git a/src/phrases.txt b/src/phrases.txt new file mode 100644 index 0000000..c938555 --- /dev/null +++ b/src/phrases.txt @@ -0,0 +1,71 @@ +; ibus 音输入法--自定义短语配置文件 +; +; 说明: +; 格式: +; 英文字符串=短语 +; 英文字符串=#动态短语 +; 动态短语: +; 函数 含义 举例 +; ${year} 年(4位) 2006、2008 +; ${year_yy} 年(2位) 06、08 +; ${month} 月 12、8、3 +; ${month_mm} 月 12、08、03 +; ${day} 日 3、13、22 +; ${day_dd} 日 03、13、22 +; ${weekday} 星期 0、1、2、5、6 +; ${fullhour} 时(24小时制) 02、08、13、23 +; ${halfhour} 时(12小时制) 02、08、01、11 +; ${ampm} AM、PM(英) AM、PM(大写) +; ${minute} 分 02、08、15、28 +; ${second} 秒 02、08、15、28 +; ${year_cn} 年(中文4位) 二〇〇六 +; ${year_yy_cn} 年(中文2位) 〇六 +; ${month_cn} 月(中文) 十二、八、三 +; ${day_cn} 日(中文) 三、十三、二十二 +; ${weekday_cn} 星期(中文) 日、一、二、五、六 +; ${fullhour_cn} 月(中文24时制) 二、八、十三、二十三 +; ${halfhour_cn} 时(中文12时制) 二、八、一、十一 +; ${ampm_cn} 上午下午(中文) 上午、下午 +; ${minute_cn} 分(中文) 零二、零八、十五、二十八 +; ${second_cn} 秒(中文) 零二、零八、十五、二十八 + +aazhi=AA制 +agu=A股 +bchao=B超 +bgu=B股 +bichao=B超 +bsn=╭∩╮(︶︿︶)╭∩╮鄙视你! +ceo=首席执行官 +cpan=C盘 +cyuyan=C语言 +dna=脱氧核糖核酸 +dpan=D盘 +gps=全球定位系统 +haha=^_^ +haha=o(∩∩)o...哈哈 +hehe=:-) +hgu=H股 +icka=IC卡 +ipdianhua=IP电话 +ipdizhi=IP地址 +ipka=IP卡 +kalaok=卡拉OK +pcji=PC机 +qiruiqq=奇瑞QQ +qqhao=QQ号 +simka=SIM卡 +tixu=T恤 +tixushan=T恤衫 +txingtai=T型台 +txu=T恤 +upan=U盘 +xixi=(*^__^*) 嘻嘻…… +xshexian=X射线 + +rq=#${year}年${month}月${day}日 +rq=#${year_cn}年${month_cn}月${day_cn}日 +rq=#${year}-${month}-${day} +sj=#${fullhour}时${minute}分${second}秒 +sj=#${fullhour}:${minute}:${second} +xq=#星期${weekday_cn} +nl=#农历${lunardate} diff --git a/src/special_phrases b/src/special_phrases deleted file mode 100644 index c2c367d..0000000 --- a/src/special_phrases +++ /dev/null @@ -1,68 +0,0 @@ -; ibus 音输入法--自定义短语配置文件 -; -; 说明: -; 格式: -; 英文字符串=短语 -; 英文字符串=#动态短语 -; 动态短语: -; 函数 含义 举例 -; $(year) 年(4位) 2006、2008 -; $(year_yy) 年(2位) 06、08 -; $(month) 月 12、8、3 -; $(month_mm) 月 12、08、03 -; $(day) 日 3、13、22 -; $(day_dd) 日 03、13、22 -; $(weekday) 星期 0、1、2、5、6 -; $(fullhour) 时(24小时制) 02、08、13、23 -; $(halfhour) 时(12小时制) 02、08、01、11 -; $(ampm) AM、PM(英) AM、PM(大写) -; $(minute) 分 02、08、15、28 -; $(second) 秒 02、08、15、28 -; $(year_cn) 年(中文4位) 二〇〇六 -; $(year_yy_cn) 年(中文2位) 〇六 -; $(month_cn) 月(中文) 十二、八、三 -; $(day_cn) 日(中文) 三、十三、二十二 -; $(weekday_cn) 星期(中文) 日、一、二、五、六 -; $(fullhour_cn) 月(中文24时制) 二、八、十三、二十三 -; $(halfhour_cn) 时(中文12时制) 二、八、一、十一 -; $(ampm_cn) 上午下午(中文) 上午、下午 -; $(minute_cn) 分(中文) 零二、零八、十五、二十八 -; $(second_cn) 秒(中文) 零二、零八、十五、二十八 - -aazhi=AA制 -agu=A股 -bchao=B超 -bgu=B股 -bichao=B超 -bsn=╭∩╮(︶︿︶)╭∩╮鄙视你! -ceo=首席执行官 -cpan=C盘 -cyuyan=C语言 -dna=脱氧核糖核酸 -dpan=D盘 -gps=全球定位系统 -haha=^_^ -haha=o(∩∩)o...哈哈 -hehe=:-) -hgu=H股 -icka=IC卡 -ipdianhua=IP电话 -ipdizhi=IP地址 -ipka=IP卡 -kalaok=卡拉OK -pcji=PC机 -qiruiqq=奇瑞QQ -qqhao=QQ号 -rq=${year}年${month}月${day}日 -rq=${year}-${month}-${day} -simka=SIM卡 -sj=${hour_24}时${minute}分${second}秒 -sj=${hour_24}:${minute}:${second} -tixu=T恤 -tixushan=T恤衫 -txingtai=T型台 -txu=T恤 -upan=U盘 -xixi=(*^__^*) 嘻嘻…… -xshexian=X射线 - -- 2.7.4