~ubuntu-branches/ubuntu/saucy/ibus-pinyin/saucy-proposed

« back to all changes in this revision

Viewing changes to src/PYSpecialPhraseTable.cc

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing, Asias He
  • Date: 2010-09-08 21:38:54 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100908213854-q4wlx8zlcyqxvelz
Tags: 1.3.11-1
[ Asias He ]
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:set et ts=4 sts=4:
 
2
 *
 
3
 * ibus-pinyin - The Chinese PinYin engine for IBus
 
4
 *
 
5
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@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 2, or (at your option)
 
10
 * 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, write to the Free Software
 
19
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
#include "PYSpecialPhraseTable.h"
 
22
#include <fstream>
 
23
#include "PYDynamicSpecialPhrase.h"
 
24
#include "PYSpecialPhrase.h"
 
25
 
 
26
namespace PY {
 
27
 
 
28
SpecialPhraseTable SpecialPhraseTable::m_instance;
 
29
 
 
30
class StaticSpecialPhrase : public SpecialPhrase {
 
31
public:
 
32
    StaticSpecialPhrase (const std::string &text, guint pos) :
 
33
        SpecialPhrase (pos), m_text (text) { }
 
34
    ~StaticSpecialPhrase (void) { }
 
35
 
 
36
    std::string text (void) { return m_text; }
 
37
 
 
38
private:
 
39
    std::string m_text;
 
40
};
 
41
 
 
42
SpecialPhraseTable::SpecialPhraseTable (void)
 
43
{
 
44
    gchar * path = g_build_filename (g_get_user_config_dir (),
 
45
                        "ibus", "pinyin", "phrases.txt", NULL);
 
46
 
 
47
    load ("phrases.txt") ||
 
48
    load (path) ||
 
49
    load (PKGDATADIR G_DIR_SEPARATOR_S "phrases.txt");
 
50
    g_free (path);
 
51
}
 
52
 
 
53
gboolean
 
54
SpecialPhraseTable::lookup (const std::string         &command,
 
55
                            std::vector<std::string>  &result)
 
56
{
 
57
    result.clear ();
 
58
 
 
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 ());
 
62
    }
 
63
 
 
64
    return result.size () > 0;
 
65
}
 
66
 
 
67
gboolean
 
68
SpecialPhraseTable::load (const gchar *file)
 
69
{
 
70
    m_map.clear ();
 
71
 
 
72
    std::ifstream in (file);
 
73
    if (in.fail ())
 
74
        return FALSE;
 
75
 
 
76
    std::string line;
 
77
    while (!in.eof ()) {
 
78
        getline (in, line);
 
79
        if (line.size () == 0 || line[0] == ';')
 
80
            continue;
 
81
        size_t pos = line.find ('=');
 
82
        if (pos == line.npos)
 
83
            continue;
 
84
 
 
85
        std::string command = line.substr(0, pos);
 
86
        std::string value = line.substr(pos + 1);
 
87
        if (command.empty () || value.empty ())
 
88
            continue;
 
89
 
 
90
        if (value[0] != '#') {
 
91
            SpecialPhrasePtr phrase (new StaticSpecialPhrase (value, 0));
 
92
            m_map.insert (Map::value_type (command, phrase));
 
93
        }
 
94
        else if (value.size () > 1) {
 
95
            SpecialPhrasePtr phrase (new DynamicSpecialPhrase (value.substr (1), 0));
 
96
            m_map.insert (Map::value_type (command, phrase));
 
97
        }
 
98
    }
 
99
    return TRUE;
 
100
}
 
101
 
 
102
};
 
103