[go: up one dir, main page]

File: win2mb.cpp

package info (click to toggle)
zhcon 1%3A0.2.3-8.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,876 kB
  • ctags: 1,744
  • sloc: cpp: 11,420; ansic: 5,017; sh: 3,433; makefile: 333; yacc: 318
file content (209 lines) | stat: -rw-r--r-- 5,470 bytes parent folder | download | duplicates (11)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***************************************************************************
                          winime2mb.cpp  -  description
                             -------------------
    begin                : Tue Apr 3 2001
    copyright            : (C) 2001 by ejoy
    email                : ejoy@peoplemail.com.cn
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <string>
#include <deque>
#include <algorithm>
#include <cassert>
#include <fstream>

using namespace std;

struct WinImeHead {
char mName[12];
int mMaxCodes;
char mCodeSet[47];
char mWildChar;
//unsigned int mIndex1,mIndex2,mText;
};

class Word{
public:
    Word(){mpOffset = NULL;}
    Word(const Word& w);
    ~Word(){};

  string mText;
  string mCode;
  char* mpOffset;
};

Word::Word(const Word& w) {
    mText = w.mText;
    mCode = w.mCode;
    mpOffset = w.mpOffset;
}

bool operator<(const Word& w1,const Word& w2) {
        return w1.mCode < w2.mCode;
}

bool operator==(const Word& w1,const Word& w2) {
    return w1.mCode == w2.mCode && w1.mText == w2.mText
        && w1.mpOffset == w2.mpOffset;
}

const int SIZE = 81;
char buf[SIZE];
char o[SIZE],v[SIZE];
char** I1;
char** I2;
WinImeHead h;
typedef deque<class Word> LIST;
LIST list;

bool InCodeSet(char c) {
    int len = strlen(h.mCodeSet);
    char* p = find(h.mCodeSet,h.mCodeSet + len,c);
    return p != h.mCodeSet + len;
}    
int Index(char c) {
    int len = strlen(h.mCodeSet);
    char* p = find(h.mCodeSet,h.mCodeSet + len,c);
    return p - h.mCodeSet;
}
void GetOption(char buf[],char o[],char v[]) {
    char *p = buf;
    int i = 0;
    while (*p != ' ' && *p != '=')
        o[i++] = *p++;
    o[i] = '\0';
    while (*p == ' ' || *p == '=')
        p++;
    i = 0;
    while (*p && *p != ' ')
        v[i++] = *p++;
    v[i] = '\0';
}

void ParseHead(istream& in) {
    in.getline(buf,SIZE);
     while ( in.getline(buf,SIZE) && buf[0] != '[') {
         GetOption(buf,o,v);
      if (strcmp("Name",o) == 0)
           strcpy(h.mName,v);
      else if (strcmp("MaxCodes",o) == 0)
              h.mMaxCodes = atoi(v);
      else if (strcmp("UsedCodes",o) == 0) {
        if (strlen(o) > 46) {
            cerr<<"UsedCodes exceed 46 chars!\n"<<endl;
            exit(-1);
        }
        strcpy(h.mCodeSet,v);
    }
      else if (strcmp("WildChar",o) == 0)
        h.mWildChar = v[0];
     }    //while
     //skip [Rule] if it exist
     if (buf[1] == 'R')
         while ( in.getline(buf,SIZE) && buf[0] != '['); 
}
    
void ParseText(istream& in) {
     Word w;
     skip:
     while (in.getline(buf,SIZE) && strlen(buf) > 0) {
        char* p = buf;
       int i = 0;
        while (!InCodeSet(*p)) {
            o[i++] = *p++;
            o[i++] = *p++;
        }
       o[i] = '\0';
       w.mText = o;

//      while (*p) {
     i = 0;
     while (*p && *p != ' ')
       v[i++] = *p++;
     v[i] = '\0';

       w.mCode = v;
       list.push_back(w);
       //skip blanks
//       while(*p && *p == ' ')
//           p++;
      }
//     }
}

void BuildIndex() {
     //build index
     int len = strlen(h.mCodeSet);

    I1 = new char*[len];
    I2 = new char*[len * len];

    // h.mIndex1 = sizeof(h);
     //h.mIndex2 = h.mIndex1 + len * sizeof(char *);
     //h.mText = h.mIndex2 + len * len * sizeof(char *);

     fill(I1,I1 + len,(char*)0xffffffff);
     fill(I2,I2 + len * len,(char*)0xffffffff);
    
    LIST::iterator w;
    char* p = 0;
     for(w = list.begin();w != list.end();w++) {
         w->mpOffset = p;
         p+= w->mCode.size() + w->mText.size();
     }

    //build index
     for(w = list.begin();w != list.end();w++) {
         string s = w->mCode;
//         cerr<<w->mCode<<"  :"<<w->mCode.size()<<endl;
//         cerr<<w->mText<<"  :"<<w->mText.size()<<endl<<flush;
        int i;
         if( s.size() > 0) {
             i = Index(s[0]);
             if (I1[i] == (char*)0xffffffff)
                 I1[i] = w->mpOffset;
            if (s.size() > 1) {
                 i = i * len + Index(s[1]);
                 if (I2[i] == (char*)0xffffffff)
                     I2[i] = w->mpOffset;
             }
         }
     }
}

void Output(ostream& out) {
    out.write((char*)&h,sizeof(h));
    int len = strlen(h.mCodeSet);
    out.write((char*)I1,len * sizeof(char*));
    out.write((char*)I2,len * len * sizeof(char*));
     for(LIST::iterator w = list.begin();w < list.end();w++) {
         out<<w->mCode<<w->mText;
         //cerr<<w->mCode<<":"<<(int)w->mpOffset<<"\t";
         //cerr<<w->mText<<":"<<(int)w->mpOffset<<endl;
     }
    char* p = 0; //end tag
     out.write((char*)&p,sizeof(char*));
}

int main(int argc,char** argv)
{
    
    ParseHead(cin);
    ParseText(cin);
    BuildIndex();
    Output(cout);
     return 0;
}