1
/* vim:set et ts=4 sts=4:
3
* ibus-pinyin - The Chinese PinYin engine for IBus
5
* Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
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 2, or (at your option)
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.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include "PYSpecialPhraseTable.h"
23
#include "PYDynamicSpecialPhrase.h"
24
#include "PYSpecialPhrase.h"
28
SpecialPhraseTable SpecialPhraseTable::m_instance;
30
class StaticSpecialPhrase : public SpecialPhrase {
32
StaticSpecialPhrase (const std::string &text, guint pos) :
33
SpecialPhrase (pos), m_text (text) { }
34
~StaticSpecialPhrase (void) { }
36
std::string text (void) { return m_text; }
42
SpecialPhraseTable::SpecialPhraseTable (void)
44
gchar * path = g_build_filename (g_get_user_config_dir (),
45
"ibus", "pinyin", "phrases.txt", NULL);
47
load ("phrases.txt") ||
49
load (PKGDATADIR G_DIR_SEPARATOR_S "phrases.txt");
54
SpecialPhraseTable::lookup (const std::string &command,
55
std::vector<std::string> &result)
59
std::pair<Map::iterator, Map::iterator> range = m_map.equal_range (command);
60
for (Map::iterator it = range.first; it != range.second; it ++) {
61
result.push_back ((*it).second->text ());
64
return result.size () > 0;
68
SpecialPhraseTable::load (const gchar *file)
72
std::ifstream in (file);
79
if (line.size () == 0 || line[0] == ';')
81
size_t pos = line.find ('=');
85
std::string command = line.substr(0, pos);
86
std::string value = line.substr(pos + 1);
87
if (command.empty () || value.empty ())
90
if (value[0] != '#') {
91
SpecialPhrasePtr phrase (new StaticSpecialPhrase (value, 0));
92
m_map.insert (Map::value_type (command, phrase));
94
else if (value.size () > 1) {
95
SpecialPhrasePtr phrase (new DynamicSpecialPhrase (value.substr (1), 0));
96
m_map.insert (Map::value_type (command, phrase));