summaryrefslogtreecommitdiffstats
path: root/src/ZYSymbolLookup.cc
blob: c4da7b3bde58782dc2e6a09a3d8ae07fa5b9e8e5 (plain)
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
/* vim:set et ts=4 sts=4:
 *
 * ibus-libzhuyin - New Zhuyin engine based on libzhuyin for IBus
 *
 * Copyright (c) 2014 Peng Wu <alexepico@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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ZYSymbolLookup.h"
#include <stdio.h>
#include <string.h>

namespace ZY {

gboolean
SymbolLookup::loadFromFile(const char * filename)
{
    FILE * symbolfile = fopen (filename, "r");
    if (NULL == symbolfile)
        return FALSE;

    char* linebuf = NULL; size_t size = 0; ssize_t read;
    while ((read = getline (&linebuf, &size, symbolfile)) != -1) {
        if (0 == strlen (linebuf))
            continue;

        if ( '\n' == linebuf[strlen (linebuf) - 1] ) {
            linebuf[strlen (linebuf) - 1] = '\0';
        }

        gchar ** items = g_strsplit_set (linebuf, " =", 2);
        guint len = g_strv_length (items);

        if (0 == len)
            continue;

        String index = items[0];
        /* for symbols.dat, copy display label from symbol. */
        String symbol = index;
        if (2 == len)
            symbol = items[1];

        /* just append. */
        m_indexes.push_back (index);
        m_symbols.push_back (symbol);

        g_strfreev (items);
    }

    return TRUE;
}

String
SymbolLookup::find (const String index)
{
    gboolean found = FALSE;

    size_t i;
    for(i = 0; i < m_indexes.size (); ++i) {
        if (index == m_indexes[i]) {
            found = TRUE;
            break;
        }
    }

    if (found)
        return m_symbols[i];

    return "";
}

void
SymbolLookup::dumpInfo ()
{
    size_t i;
    for (i = 0; i < m_indexes.size (); ++i)
        printf ("%s %s\n", m_indexes[i].c_str (), m_symbols[i].c_str ());
}

};