~ubuntu-branches/ubuntu/trusty/fcitx/trusty-proposed

« back to all changes in this revision

Viewing changes to tools/cli/pyTools.c

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-10 17:03:56 UTC
  • mfrom: (1.3.18) (33.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130210170356-2yuv6xy3ed378kn0
Tags: 1:4.2.7-1
* New upstream release.
* New binary packages:
  - fcitx-libs-gclient: D-Bus client library for Glib
  - fcitx-libs-qt: D-Bus client library for Qt
  - fcitx-module-quickphrase-editor: Quick Phrase editor module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2002~2005 by Yuking                                     *
 
3
 *   yuking_net@sohu.com                                                   *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 
19
 ***************************************************************************/
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#include <stdint.h>
 
25
 
 
26
#include "fcitx-utils/utils.h"
 
27
#include "pyTools.h"
 
28
 
 
29
void LoadPYMB(FILE *fi, struct _PYMB **pPYMB, int isUser)
 
30
{
 
31
 
 
32
    struct _PYMB *PYMB;
 
33
    int i, j, r, n, t;
 
34
 
 
35
    /* Is there a way to avoid reading the whole file twice? */
 
36
 
 
37
    /* First Pass: Determine the size of the PYMB array to be created */
 
38
 
 
39
    n = 0;
 
40
 
 
41
    while (1) {
 
42
        int8_t clen;
 
43
        r = fcitx_utils_read_int32(fi, &t);
 
44
 
 
45
        if (!r)
 
46
            break;
 
47
 
 
48
        ++n;
 
49
 
 
50
        fread(&clen, sizeof(int8_t), 1, fi);
 
51
 
 
52
        fseek(fi, sizeof(char) * clen, SEEK_CUR);
 
53
 
 
54
        fcitx_utils_read_int32(fi, &t);
 
55
 
 
56
        for (i = 0; i < t; ++i) {
 
57
            int iLen;
 
58
            fcitx_utils_read_int32(fi, &iLen);
 
59
            fseek(fi , sizeof(char) * iLen, SEEK_CUR);
 
60
            fcitx_utils_read_int32(fi, &iLen);
 
61
            fseek(fi , sizeof(char) * iLen, SEEK_CUR);
 
62
            fcitx_utils_read_int32(fi, &iLen);
 
63
 
 
64
            if (isUser)
 
65
                fcitx_utils_read_int32(fi, &iLen);
 
66
        }
 
67
    }
 
68
 
 
69
    /* Second Pass: Actually read the data */
 
70
 
 
71
    fseek(fi, 0, SEEK_SET);
 
72
 
 
73
    *pPYMB = PYMB = malloc(sizeof(*PYMB) * (n + 1));
 
74
 
 
75
    for (i = 0; i < n; ++i) {
 
76
        r = fcitx_utils_read_int32(fi, &(PYMB[i].PYFAIndex));
 
77
 
 
78
        int8_t clen;
 
79
        fread(&clen, sizeof(int8_t), 1, fi);
 
80
        fread(PYMB[i].HZ, sizeof(char) * clen, 1, fi);
 
81
        PYMB[i].HZ[clen] = '\0';
 
82
 
 
83
        fcitx_utils_read_int32(fi, &(PYMB[i].UserPhraseCount));
 
84
        PYMB[i].UserPhrase = malloc(sizeof(*(PYMB[i].UserPhrase)) * PYMB[i].UserPhraseCount);
 
85
 
 
86
#define PU(i,j) (PYMB[(i)].UserPhrase[(j)])
 
87
 
 
88
        for (j = 0; j < PYMB[i].UserPhraseCount; ++j) {
 
89
            fcitx_utils_read_int32(fi, &(PU(i, j).Length));
 
90
 
 
91
            PU(i, j).Map = malloc(sizeof(char) * PU(i, j).Length + 1);
 
92
            fread(PU(i, j).Map, sizeof(char) * PU(i, j).Length, 1, fi);
 
93
            PU(i, j).Map[PU(i, j).Length] = '\0';
 
94
 
 
95
            int32_t iLen;
 
96
            fcitx_utils_read_int32(fi, &iLen);
 
97
            PU(i, j).Phrase = malloc(sizeof(char) * iLen + 1);
 
98
            fread(PU(i, j).Phrase, sizeof(char) * iLen, 1, fi);
 
99
            PU(i, j).Phrase[iLen] = '\0';
 
100
 
 
101
            fcitx_utils_read_int32(fi, &(PU(i, j).Index));
 
102
 
 
103
            if (isUser)
 
104
                fcitx_utils_read_int32(fi, &(PU(i, j).Hit));
 
105
            else
 
106
                PU(i, j).Hit = 0;
 
107
        }
 
108
 
 
109
#undef PU
 
110
    }
 
111
 
 
112
    PYMB[n].HZ[0] = '\0';
 
113
 
 
114
    return;
 
115
}
 
116
 
 
117
int LoadPYBase(FILE *fi, struct _HZMap **pHZMap)
 
118
{
 
119
    int32_t i, j, r, PYFACount;
 
120
 
 
121
    struct _HZMap *HZMap;
 
122
 
 
123
    r = fcitx_utils_read_int32(fi, &PYFACount);
 
124
 
 
125
    if (!r)
 
126
        return 0;
 
127
 
 
128
    *pHZMap = HZMap = malloc(sizeof(*HZMap) * (PYFACount + 1));
 
129
 
 
130
    for (i = 0; i < PYFACount; ++i) {
 
131
        fread(HZMap[i].Map, sizeof(char) * 2, 1, fi);
 
132
        HZMap[i].Map[2] = '\0';
 
133
 
 
134
        fcitx_utils_read_int32(fi, &(HZMap[i].BaseCount));
 
135
        HZMap[i].HZ = malloc(sizeof(char *) * HZMap[i].BaseCount);
 
136
        HZMap[i].Index = malloc(sizeof(int) * HZMap[i].BaseCount);
 
137
 
 
138
        for (j = 0; j < HZMap[i].BaseCount; ++j) {
 
139
            int8_t clen;
 
140
            fread(&clen, sizeof(int8_t), 1, fi);
 
141
            HZMap[i].HZ[j] = malloc(sizeof(char) * (clen + 1));
 
142
            fread(HZMap[i].HZ[j], sizeof(char) * clen, 1, fi);
 
143
            HZMap[i].HZ[j][clen] = '\0';
 
144
            fcitx_utils_read_int32(fi, &HZMap[i].Index[j]);
 
145
        }
 
146
    }
 
147
 
 
148
    HZMap[i].Map[0] = '\0';
 
149
 
 
150
    return PYFACount;
 
151
}
 
152
 
 
153
// kate: indent-mode cstyle; space-indent on; indent-width 4;