Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* vim:set et ts=4 sts=4:
*
* ibus-pinyin - The Chinese PinYin engine for IBus
*
* Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "PyZySpecialPhraseTable.h"
#include <fstream>
#include "PyZyDynamicSpecialPhrase.h"
#include "PyZySpecialPhrase.h"
namespace PyZy {
SpecialPhraseTable SpecialPhraseTable::m_instance;
class StaticSpecialPhrase : public SpecialPhrase {
public:
StaticSpecialPhrase (const std::string &text, guint pos) :
SpecialPhrase (pos), m_text (text) { }
~StaticSpecialPhrase (void) { }
std::string text (void) { return m_text; }
private:
std::string m_text;
};
SpecialPhraseTable::SpecialPhraseTable (void)
{
gchar * path = g_build_filename (g_get_user_config_dir (),
"ibus", "pinyin", "phrases.txt", NULL);
load ("phrases.txt") ||
load (path) ||
load (PKGDATADIR G_DIR_SEPARATOR_S "phrases.txt");
g_free (path);
}
gboolean
SpecialPhraseTable::lookup (const std::string &command,
std::vector<std::string> &result)
{
result.clear ();
std::pair<Map::iterator, Map::iterator> range = m_map.equal_range (command);
for (Map::iterator it = range.first; it != range.second; it ++) {
result.push_back ((*it).second->text ());
}
return result.size () > 0;
}
gboolean
SpecialPhraseTable::load (const gchar *file)
{
m_map.clear ();
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;
std::string command = line.substr(0, pos);
std::string value = line.substr(pos + 1);
if (command.empty () || value.empty ())
continue;
if (value[0] != '#') {
SpecialPhrasePtr phrase (new StaticSpecialPhrase (value, 0));
m_map.insert (Map::value_type (command, phrase));
}
else if (value.size () > 1) {
SpecialPhrasePtr phrase (new DynamicSpecialPhrase (value.substr (1), 0));
m_map.insert (Map::value_type (command, phrase));
}
}
return TRUE;
}
}; // namespace PyZy