summaryrefslogtreecommitdiff
path: root/l10ntools/source/wtratree.cxx
blob: dbf02a6f22be786c3e08d5b6397d6c744e7fe1a3 (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: wtratree.cxx,v $
 * $Revision: 1.5 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_l10ntools.hxx"


#include "wtratree.hxx"



/** @ATTENTION
    For reasons of speed, class WordTransTree works with two simple
    char arrays, sOutput and sInput, instead of secure containers or
    streams. So be extremely careful, when changing this code!!!
**/



// NOT FULLY DECLARED SERVICES
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "wtranode.hxx"


const BRANCH_T  BR_END          = 0;
const BRANCH_T  BR_NONALPHA     = 1;
const BRANCH_T  BR_HOTKEY       = 2;
const BRANCH_T  BR_BACKSLASH    = 3;
const BRANCH_T  BR_ALPHABASE    = 4;    /// @ATTENTION  All branches not valid for words must be smaller than this value!
const BRANCH_T  BR_AE           = 30;
const BRANCH_T  BR_OE           = 31;
const BRANCH_T  BR_UE           = 32;
const BRANCH_T  BR_SZ           = 33;
const BRANCH_T  BR_MAX          = 34;   /// @ATTENTION  Must be updated always!

const BRANCH_T  BR_START        = 0;





WordTransTree::WordTransTree(CharSet  i_nWorkingCharSet)
    :   sInput(0),
        nInputLength(0),
        pInputEnd(0),
        sOutput(0),
        nOutputMaxLength(0),
        dpParsingTreeTop(0),
        pUnknownAlpha(0),
        // cChar2Branch
        c_AE(u_char('\xC4')), c_OE(u_char('\xD6')), c_UE(u_char('\xDC')),
        c_ae(u_char('\xE4')), c_oe(u_char('\xF6')), c_ue(u_char('\xFC')),
        pInputCurTokenStart(0),
        pInputPosition(0),
        pOutputPosition(0),
        pCurParseNode(0),
        eCurResult(OK),
        cCurHotkey(0),
        cCurHotkeySign(u_char('~'))
{
    // Initialize parsing tree:
    pUnknownAlpha = new WTT_Node(BR_ALPHABASE,0,0); // This will be deleted as part of the parsing tree.
    for ( UINT8 i = BR_ALPHABASE; i < C_NR_OF_BRANCHES; i++)
    {
        pUnknownAlpha->SetBranch(i,pUnknownAlpha);
    }  // end for

    dpParsingTreeTop = new WTT_Node(BR_START,0,pUnknownAlpha);

    WTT_Node * dpNonAlpha = new WTT_Node(BR_NONALPHA,0,0);

    dpNonAlpha->SetBranch(BR_NONALPHA,dpNonAlpha);
    dpParsingTreeTop->SetBranch(BR_NONALPHA,dpNonAlpha);

    WTT_Node * dpBackslash = new WTT_Node(BR_BACKSLASH,dpNonAlpha,dpNonAlpha);
    dpBackslash->SetBranch(BR_END,0);

    dpParsingTreeTop->SetBranch(BR_BACKSLASH,dpBackslash);
    dpNonAlpha->SetBranch(BR_BACKSLASH,dpBackslash);


    // Initialize character set:
    SetCharSet(i_nWorkingCharSet);

    if (C_BR_ALPHABASE != BR_ALPHABASE || C_NR_OF_BRANCHES != BR_MAX)
    {
        fprintf(stderr, "Assertion failed: file %s line %d.", __FILE__,  __LINE__);
        exit(1);
    }
}

void
WordTransTree::SetCharSet(CharSet i_nWorkingCharSet)
{
    ByteString sConvert("\xC4\xD6\xDC\xE4\xF6\xFC\xDF");
    const u_char * pConvert = (const u_char * ) ( sConvert.Convert(RTL_TEXTENCODING_MS_1252, i_nWorkingCharSet).GetBuffer() );

    INT16 i = 0;
    for ( ; i < C_NR_OF_POSSIBLE_CHARS; ++i )
    {
        cChar2Branch[i] = BR_NONALPHA;
    }  // end for
    for ( i = 'a'; i <= 'z'; ++i )
    {
        cChar2Branch[i] = BR_ALPHABASE + i - 'a';
    }  // end for
    for ( i = 'A'; i <= 'Z'; ++i )
    {
        cChar2Branch[i] = BR_ALPHABASE + i - 'A';
    }  // end for
    cChar2Branch[pConvert[0]] = BR_AE;
    cChar2Branch[pConvert[1]] = BR_OE;
    cChar2Branch[pConvert[2]] = BR_UE;
    cChar2Branch[pConvert[3]] = BR_AE;
    cChar2Branch[pConvert[4]] = BR_OE;
    cChar2Branch[pConvert[5]] = BR_UE;
    cChar2Branch[pConvert[6]] = BR_SZ;

    cChar2Branch[u_char('~')] = BR_HOTKEY;
    cChar2Branch[u_char('&')] = BR_HOTKEY;


    c_AE = pConvert[0];
    c_OE = pConvert[1];
    c_UE = pConvert[2];
    c_ae = pConvert[3];
    c_oe = pConvert[4];
    c_ue = pConvert[5];
}

WordTransTree::~WordTransTree()
{
    delete dpParsingTreeTop;
    if (sOutput != 0)
        delete [] sOutput;
}

void
WordTransTree::AddWordPair( const ByteString &      i_sOldString,
                            const ByteString &      i_sReplaceString )
{
    if (i_sOldString.Len() == 0)
        return;

    pCurParseNode = dpParsingTreeTop;
    WTT_Node * pBranch = 0;
    char cBranch = 0;

    for ( constr pOld = i_sOldString.GetBuffer();
          *pOld != 0;
          pOld++ )
    {
        cBranch = CalculateBranch(*pOld);
        pBranch = pCurParseNode->GetNextNode(cBranch);
        if (pBranch == 0 || pBranch == pUnknownAlpha)
        {
            pBranch = new WTT_Node(cBranch,0,pUnknownAlpha);
            pCurParseNode->SetBranch(cBranch,pBranch);
        }
        pCurParseNode = pBranch;
    }   // end for
    pCurParseNode->SetAsTokenToReplace(i_sReplaceString);
}

void
WordTransTree::InitTransformation( const char * i_sInput,
                                   UINT32       i_nInputLength,
                                   UINT32       i_nOutputMaxLength )
{
    sInput = (const u_char *)i_sInput;
    nInputLength = i_nInputLength;
    pInputEnd = &sInput[i_nInputLength];

    pInputCurTokenStart = sInput;
    pInputPosition = sInput;

    if (nOutputMaxLength < i_nOutputMaxLength)
    {
        if (sOutput != 0)
            delete [] sOutput;
        sOutput = new unsigned char[i_nOutputMaxLength];
        nOutputMaxLength = i_nOutputMaxLength;
    }
    pOutputPosition = sOutput;
}

/** pInputCurTokenStart and CurParseNode are updated just when
    starting this function. After its end they must not be changed
    till this functon is called again.
    Outside this function pInputPositon and pOutputPosition are both
    on the first not transformed char in their respective array.
**/
WordTransTree::E_Result
WordTransTree::TransformNextToken()
{
    pInputCurTokenStart = pInputPosition;
    pCurParseNode = dpParsingTreeTop;
    cCurHotkey = 0;
    eCurResult = OK;

    WTT_Node * pBranch = 0;
    UINT8 cBranch = 0;

    for ( pCurParseNode = dpParsingTreeTop;
          pInputPosition != pInputEnd;
          ++pInputPosition )
    {
        cBranch = CalculateBranch(*pInputPosition);
        pBranch = pCurParseNode->GetNextNode( cBranch );
        if (pBranch != 0)
        {
            pCurParseNode = pBranch;
        }
        else
        {
            if (cBranch == BR_HOTKEY)   // current letter is '~' or '&'.
            {
                // Logic of the following. There are 9 possible cases -
                // A = alphabetic letter, NA = non alphabetic, TB = token begin,
                // Eot = end of text:
                //   1. A~A          set hotkey to following letter, continue
                //   2. A~NA         token end
                //   3. A~Eot        token end
                //   4. NA~A         token end
                //   5. NA~NA        continue
                //   6. A~Eof        continue
                //   7. TB~A         set hotkey to following letter, continue
                //   8. TB~NA        continue
                //   9. TB~Eot       continue

                // bNext and Prev are true, if there are alphabetic letters:
                BOOL bNext =  pInputPosition + 1 != pInputEnd
                                    ?   CalculateBranch(pInputPosition[1]) >= BR_ALPHABASE
                                    :   FALSE;
                BOOL bPrev = pCurParseNode->Value() >= BR_ALPHABASE;

                if ( bNext && (bPrev || pCurParseNode == dpParsingTreeTop) )
                {   // case 1. and 7.
                    Handle_Hotkey();
                    continue;
                }
                else if  (!bPrev && !bNext)
                {   // case 5.,6.,8.,9.
                    continue;
                }

                // Case 2.,3.,4. :
                //  so this should be handled as an end of a token.
            }
            if (pCurParseNode->TokenType() == WTT_Node::token_to_keep)
            {
                Handle_TokenToKeep();
                return eCurResult;
            }
            else
            {
                Handle_TokenToTransform();
                return eCurResult;
            }   // endif (pCurParseNode->TokenType() == WTT_Node::token_to_keep)
        }   // endif (pBranch == 0) else
    }   // end for

    // If here, the text end is reached
    if (pCurParseNode->TokenType() == WTT_Node::token_to_keep)
    {
        Handle_TokenToKeep();
        return eCurResult;
    }
    else
    {
        Handle_TokenToTransform();
        return eCurResult;
    }
}

ByteString
WordTransTree::CurReplacingString() const
{
    return pCurParseNode->ReplaceString();
}

void
WordTransTree::Handle_Hotkey()
{
    if (cCurHotkey == 0)    // Avoid to replace the first found hotkey by
                            //   a later one - though this shouldn't happen anyway.
    {
        cCurHotkey = (pInputPosition+1) != pInputEnd ? pInputPosition[1] : 0;
        cCurHotkeySign = *pInputPosition;
    }
}

void
WordTransTree::Handle_TokenToKeep()
{
    UINT32 nTokenLength = pInputPosition-pInputCurTokenStart;

    memcpy(pOutputPosition,pInputCurTokenStart,nTokenLength);

    pOutputPosition += nTokenLength;
    *pOutputPosition = '\0';
}

void
WordTransTree::Handle_TokenToTransform()
{
    BOOL bHaveHotkey = CalculateBranch(cCurHotkey) >= BR_ALPHABASE;
    const ByteString & rReplace = pCurParseNode->ReplaceString();

    // Find position of hotkey in replace-string:
    USHORT nHotkeyPos = bHaveHotkey
                            ?   rReplace.Search(char(cCurHotkey))
                            :   STRING_NOTFOUND;
    if (nHotkeyPos == STRING_NOTFOUND && bHaveHotkey)
    {
        if (cCurHotkey < 128)
        {
            if (islower(cCurHotkey))
                nHotkeyPos = rReplace.Search(toupper(char(cCurHotkey)));
            else
                nHotkeyPos = rReplace.Search(tolower(char(cCurHotkey)));
        }
        else    // cCurHotkey >= 128
        {
            if (cCurHotkey == c_ae)
                nHotkeyPos = rReplace.Search(char(c_AE));
            else if (cCurHotkey == c_oe)
                nHotkeyPos = rReplace.Search(char(c_OE));
            else if (cCurHotkey == c_ue)
                nHotkeyPos = rReplace.Search(char(c_UE));
            else if (cCurHotkey == c_AE)
                nHotkeyPos = rReplace.Search(char(c_ae));
            else if (cCurHotkey == c_OE)
                nHotkeyPos = rReplace.Search(char(c_oe));
            else if (cCurHotkey == c_UE)
                nHotkeyPos = rReplace.Search(char(c_ue));
        }   // endif (cCurHotkey < 128) else

        if (nHotkeyPos == STRING_NOTFOUND)
        {
            eCurResult = HOTKEY_LOST;
            bHaveHotkey = FALSE;
        }
    }   // endif (nHotkeyPos == STRING_NOT_FOUND && bHaveHotkey)


    UINT32 nOutputTokenLength = rReplace.Len() + (bHaveHotkey ? 1 : 0);

    if (bHaveHotkey)
    {
        memcpy( pOutputPosition,
                pCurParseNode->ReplaceString().GetBuffer(),
                nHotkeyPos );
        *(pOutputPosition + nHotkeyPos) = cCurHotkeySign;
        memcpy( pOutputPosition + nHotkeyPos + 1,
                pCurParseNode->ReplaceString().GetBuffer() + nHotkeyPos,
                nOutputTokenLength - nHotkeyPos - 1);
    }
    else
    {
        memcpy( pOutputPosition,
                pCurParseNode->ReplaceString().GetBuffer(),
                nOutputTokenLength );
    }

    // Convert first letter into upper if necessary:
    u_char cInStart = CalculateBranch(*pInputCurTokenStart) == BR_HOTKEY
                            ?   pInputCurTokenStart[1]
                            :   pInputCurTokenStart[0] ;
    u_char * pOutStart = nHotkeyPos == 0
                            ?   pOutputPosition + 1
                            :   pOutputPosition ;
    if (isupper(cInStart) || cInStart > 127)
    {   // Possibly cInStart is upper character:
        if (isupper(cInStart) || cInStart == c_AE || cInStart == c_OE || cInStart == c_UE)
        {   // Surely cInStart is upper character:
            u_char cOutStart = *pOutStart;
            if (cOutStart < 128)
                *pOutStart = toupper(cOutStart);
            else if (cOutStart == c_ae)
                *pOutStart = c_AE;
            else if (cOutStart == c_oe)
                *pOutStart = c_OE;
            else if (cOutStart == c_ue)
                *pOutStart = c_UE;
        }
    }   // endif (isupper(cInStart) || cInStart > 127)

    pOutputPosition += nOutputTokenLength;
    *pOutputPosition = '\0';
}