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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*************************************************************************
*
* $RCSfile: wtratree.cxx,v $
*
* $Revision: 1.1.1.1 $
*
* last change: $Author: hr $ $Date: 2000-09-18 17:03:26 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#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('')), c_OE(u_char('')), c_UE(u_char('')),
c_ae(u_char('')), c_oe(u_char('')), c_ue(u_char('')),
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("");
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';
}
|