-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markup.cpp
5495 lines (5227 loc) · 167 KB
/
Markup.cpp
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Markup.cpp: implementation of the CMarkup class.
//
// Markup Release 11.5
// Copyright (C) 2011 First Objective Software, Inc. All rights reserved
// Go to www.firstobject.com for the latest CMarkup and EDOM documentation
// Use in commercial applications requires written permission
// This software is provided "as is", with no warranty.
//
#include <stdio.h>
#include "Markup.h"
#if defined(MCD_STRERROR) // C error routine
#include <errno.h>
#endif // C error routine
#if defined (MARKUP_ICONV)
#include <iconv.h>
#endif
#define x_ATTRIBQUOTE '\"' // can be double or single quote
#if defined(MARKUP_STL) && ( defined(MARKUP_WINCONV) || (! defined(MCD_STRERROR)))
#include <windows.h> // for MultiByteToWideChar, WideCharToMultiByte, FormatMessage
#endif // need windows.h when STL and (not setlocale or not strerror), MFC afx.h includes it already
#if defined(MARKUP_MBCS) // MBCS/double byte
#pragma message( "Note: MBCS build (not UTF-8)" )
// For UTF-8, remove MBCS from project settings C/C++ preprocessor definitions
#if defined (MARKUP_WINCONV)
#include <mbstring.h> // for VC++ _mbclen
#endif // WINCONV
#endif // MBCS/double byte
#if defined(_DEBUG) && _MSC_VER > 1000 // VC++ DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#if defined(DEBUG_NEW)
#define new DEBUG_NEW
#endif // DEBUG_NEW
#endif // VC++ DEBUG
// Disable "while ( 1 )" warning in VC++ 2002
#if _MSC_VER >= 1300 // VC++ 2002 (7.0)
#pragma warning(disable:4127)
#endif // VC++ 2002 (7.0)
//////////////////////////////////////////////////////////////////////
// Internal static utility functions
//
void x_StrInsertReplace( MCD_STR& str, int nLeft, int nReplace, const MCD_STR& strInsert )
{
// Insert strInsert into str at nLeft replacing nReplace chars
// Reduce reallocs on growing string by reserving string space
// If realloc needed, allow for 1.5 times the new length
//
int nStrLength = MCD_STRLENGTH(str);
int nInsLength = MCD_STRLENGTH(strInsert);
int nNewLength = nInsLength + nStrLength - nReplace;
int nAllocLen = MCD_STRCAPACITY(str);
#if defined(MCD_STRINSERTREPLACE) // STL, replace method
if ( nNewLength > nAllocLen )
MCD_BLDRESERVE( str, (nNewLength + nNewLength/2 + 128) );
MCD_STRINSERTREPLACE( str, nLeft, nReplace, strInsert );
#else // MFC, no replace method
int nBufferLen = nNewLength;
if ( nNewLength > nAllocLen )
nBufferLen += nBufferLen/2 + 128;
MCD_CHAR* pDoc = MCD_GETBUFFER( str, nBufferLen );
if ( nInsLength != nReplace && nLeft+nReplace < nStrLength )
memmove( &pDoc[nLeft+nInsLength], &pDoc[nLeft+nReplace], (nStrLength-nLeft-nReplace)*sizeof(MCD_CHAR) );
if ( nInsLength )
memcpy( &pDoc[nLeft], strInsert, nInsLength*sizeof(MCD_CHAR) );
MCD_RELEASEBUFFER( str, pDoc, nNewLength );
#endif // MFC, no replace method
}
int x_Hash( MCD_PCSZ p, int nSize )
{
unsigned int n=0;
while (*p)
n += (unsigned int)(*p++);
return n % nSize;
}
MCD_STR x_IntToStr( int n )
{
MCD_CHAR sz[25];
MCD_SPRINTF(MCD_SSZ(sz),MCD_T("%d"),n);
MCD_STR s=sz;
return s;
}
int x_StrNCmp( MCD_PCSZ p1, MCD_PCSZ p2, int n, int bIgnoreCase = 0 )
{
// Fast string compare to determine equality
if ( bIgnoreCase )
{
bool bNonAsciiFound = false;
MCD_CHAR c1, c2;
while ( n-- )
{
c1 = *p1++;
c2 = *p2++;
if ( c1 != c2 )
{
if ( bNonAsciiFound )
return c1 - c2;
if ( c1 >= 'a' && c1 <= 'z' )
c1 = (MCD_CHAR)( c1 - ('a'-'A') );
if ( c2 >= 'a' && c2 <= 'z' )
c2 = (MCD_CHAR)( c2 - ('a'-'A') );
if ( c1 != c2 )
return c1 - c2;
}
else if ( (unsigned int)c1 > 127 )
bNonAsciiFound = true;
}
}
else
{
while ( n-- )
{
if ( *p1 != *p2 )
return *p1 - *p2;
p1++;
p2++;
}
}
return 0;
}
enum MarkupResultCode
{
MRC_COUNT = 1,
MRC_TYPE = 2,
MRC_NUMBER = 4,
MRC_ENCODING = 8,
MRC_LENGTH = 16,
MRC_MODIFY = 32,
MRC_MSG = 64
};
void x_AddResult( MCD_STR& strResult, MCD_CSTR pszID, MCD_CSTR pszVal = NULL, int nResultCode = 0, int n = -1, int n2 = -1 )
{
// Call this to append an error result to strResult, discard if accumulating too large
if ( MCD_STRLENGTH(strResult) < 1000 )
{
// Use a temporary CMarkup object but keep strResult in a string to minimize memory footprint
CMarkup mResult( strResult );
if ( nResultCode & MRC_MODIFY )
mResult.FindElem( pszID );
else
mResult.AddElem( pszID, MCD_T(""), CMarkup::MNF_WITHNOLINES );
if ( pszVal.pcsz )
{
if ( nResultCode & MRC_TYPE )
mResult.SetAttrib( MCD_T("type"), pszVal );
else if ( nResultCode & MRC_ENCODING )
mResult.SetAttrib( MCD_T("encoding"), pszVal );
else if ( nResultCode & MRC_MSG )
mResult.SetAttrib( MCD_T("msg"), pszVal );
else
mResult.SetAttrib( MCD_T("tagname"), pszVal );
}
if ( nResultCode & MRC_NUMBER )
mResult.SetAttrib( MCD_T("n"), n );
else if ( nResultCode & MRC_COUNT )
mResult.SetAttrib( MCD_T("count"), n );
else if ( nResultCode & MRC_LENGTH )
mResult.SetAttrib( MCD_T("length"), n );
else if ( n != -1 )
mResult.SetAttrib( MCD_T("offset"), n );
if ( n2 != -1 )
mResult.SetAttrib( MCD_T("offset2"), n2 );
strResult = mResult.GetDoc();
}
}
//////////////////////////////////////////////////////////////////////
// Encoding conversion struct and methods
//
struct TextEncoding
{
TextEncoding( MCD_CSTR pszFromEncoding, const void* pFromBuffer, int nFromBufferLen )
{
m_strFromEncoding = pszFromEncoding;
m_pFrom = pFromBuffer;
m_nFromLen = nFromBufferLen;
m_nFailedChars = 0;
m_nToCount = 0;
};
int PerformConversion( void* pTo, MCD_CSTR pszToEncoding = NULL );
bool FindRaggedEnd( int& nTruncBeforeBytes );
#if defined(MARKUP_ICONV)
static const char* IConvName( char* szEncoding, MCD_CSTR pszEncoding );
int IConv( void* pTo, int nToCharSize, int nFromCharSize );
#endif // ICONV
#if ! defined(MARKUP_WCHAR)
static bool CanConvert( MCD_CSTR pszToEncoding, MCD_CSTR pszFromEncoding );
#endif // WCHAR
MCD_STR m_strToEncoding;
MCD_STR m_strFromEncoding;
const void* m_pFrom;
int m_nFromLen;
int m_nToCount;
int m_nFailedChars;
};
// Encoding names
// This is a precompiled ASCII hash table for speed and minimum memory requirement
// Each entry consists of a 2 digit name length, 5 digit code page, and the encoding name
// Each table slot can have multiple entries, table size 155 was chosen for even distribution
//
MCD_PCSZ EncodingNameTable[155] =
{
MCD_T("0800949ksc_5601"),MCD_T("1920932cseucpkdfmtjapanese0920003x-cp20003"),
MCD_T("1250221_iso-2022-jp0228591l10920004x-cp20004"),
MCD_T("0228592l20920005x-cp20005"),
MCD_T("0228593l30600850ibm8501000858ccsid00858"),
MCD_T("0228594l40600437ibm4370701201ucs-2be0600860ibm860"),
MCD_T("0600852ibm8520501250ms-ee0600861ibm8610228599l50751932cp51932"),
MCD_T("0600862ibm8620620127ibm3670700858cp008581010021x-mac-thai0920261x-cp20261"),
MCD_T("0600737ibm7370500869cp-gr1057003x-iscii-be0600863ibm863"),
MCD_T("0750221ms502210628591ibm8190600855ibm8550600864ibm864"),
MCD_T("0600775ibm7751057002x-iscii-de0300949uhc0228605l91028591iso-ir-1000600865ibm865"),
MCD_T("1028594iso-ir-1101028592iso-ir-1010600866ibm8660500861cp-is0600857ibm857"),
MCD_T("0950227x-cp50227"),
MCD_T("0320866koi1628598csisolatinhebrew1057008x-iscii-ka"),
MCD_T("1000950big5-hkscs1220106x-ia5-german0600869ibm869"),
MCD_T("1057009x-iscii-ma0701200ucs-2le0712001utf32be0920269x-cp20269"),
MCD_T("0800708asmo-7080500437cspc81765000unicode-1-1-utf-70612000utf-320920936x-cp20936"),
MCD_T("1200775ebcdic-cp-be0628598hebrew0701201utf16be1765001unicode-1-1-utf-81765001unicode-2-0-utf-80551932x-euc"),
MCD_T("1028595iso-ir-1441028597iso-ir-1260728605latin-90601200utf-161057011x-iscii-pa"),
MCD_T("1028596iso-ir-1271028593iso-ir-1090751932ms51932"),
MCD_T("0801253ms-greek0600949korean1050225iso2022-kr1128605iso_8859-150920949x-cp20949"),
MCD_T("1200775ebcdic-cp-ch1028598iso-ir-1381057006x-iscii-as1450221iso-2022-jp-ms"),
MCD_T("1057004x-iscii-ta1028599iso-ir-148"),
MCD_T("1000949iso-ir-1490820127us-ascii"),MCD_T(""),
MCD_T("1000936gb_2312-801900850cspc850multilingual0712000utf32le"),
MCD_T("1057005x-iscii-te1300949csksc560119871965000x-unicode-2-0-utf-7"),
MCD_T("0701200utf16le1965001x-unicode-2-0-utf-80928591iso8859-1"),
MCD_T("0928592iso8859-21420002x_chinese-eten0520866koi8r1000932x-ms-cp932"),
MCD_T("1320000x-chinese-cns1138598iso8859-8-i1057010x-iscii-gu0928593iso8859-3"),
MCD_T("0928594iso8859-4"),MCD_T("0928595iso8859-51150221csiso2022jp"),
MCD_T("0928596iso8859-60900154csptcp154"),
MCD_T("0928597iso8859-70900932shift_jis1400154cyrillic-asian"),
MCD_T("0928598iso8859-81057007x-iscii-or1150225csiso2022kr"),
MCD_T("0721866koi8-ru0928599iso8859-9"),MCD_T("0910000macintosh"),MCD_T(""),
MCD_T(""),MCD_T(""),
MCD_T("1210004x-mac-arabic0800936gb2312800628598visual1520108x-ia5-norwegian"),
MCD_T(""),MCD_T("0829001x-europa"),MCD_T(""),MCD_T("1510079x-mac-icelandic"),
MCD_T("0800932sjis-win1128591csisolatin1"),MCD_T("1128592csisolatin2"),
MCD_T("1400949ks_c_5601-19871128593csisolatin3"),MCD_T("1128594csisolatin4"),
MCD_T("0400950big51128595csisolatin51400949ks_c_5601-1989"),
MCD_T("0500775cp5001565000csunicode11utf7"),MCD_T("0501361johab"),
MCD_T("1100932windows-9321100437codepage437"),
MCD_T("1800862cspc862latinhebrew1310081x-mac-turkish"),MCD_T(""),
MCD_T("0701256ms-arab0800775csibm5000500154cp154"),
MCD_T("1100936windows-9360520127ascii"),
MCD_T("1528597csisolatingreek1100874windows-874"),MCD_T("0500850cp850"),
MCD_T("0700720dos-7200500950cp9500500932cp9320500437cp4370500860cp8601650222_iso-2022-jp$sio"),
MCD_T("0500852cp8520500861cp8610700949ksc56010812001utf-32be"),
MCD_T("0528597greek0500862cp8620520127cp3670500853cp853"),
MCD_T("0500737cp7371150220iso-2022-jp0801201utf-16be0500863cp863"),
MCD_T("0500936cp9360528591cp8194520932extended_unix_code_packed_format_for_japanese0500855cp8550500864cp864"),
MCD_T("0500775cp7750500874cp8740800860csibm8600500865cp865"),
MCD_T("0500866cp8660800861csibm8611150225iso-2022-kr0500857cp8571101201unicodefffe"),
MCD_T("0700862dos-8620701255ms-hebr0500858cp858"),
MCD_T("1210005x-mac-hebrew0500949cp9490800863csibm863"),
MCD_T("0500869cp8691600437cspc8codepage4370700874tis-6200800855csibm8550800864csibm864"),
MCD_T("0800950x-x-big50420866koi80800932ms_kanji0700874dos-8740800865csibm865"),
MCD_T("0800866csibm8661210003x-mac-korean0800857csibm8570812000utf-32le"),
MCD_T(""),MCD_T("0500932ms9320801200utf-16le1028591iso-8859-10500154pt154"),
MCD_T("1028592iso-8859-20620866koi8-r0800869csibm869"),
MCD_T("1500936csiso58gb2312800828597elot_9281238598iso-8859-8-i1028593iso-8859-30820127iso-ir-6"),
MCD_T("1028594iso-8859-4"),
MCD_T("0800852cspcp8520500936ms9361028595iso-8859-50621866koi8-u0701252ms-ansi"),
MCD_T("1028596iso-8859-60220127us2400858pc-multilingual-850+euro"),
MCD_T("1028597iso-8859-71028603iso8859-13"),
MCD_T("1320000x-chinese_cns1028598iso-8859-8"),
MCD_T("1828595csisolatincyrillic1028605iso8859-151028599iso-8859-9"),
MCD_T("0465001utf8"),MCD_T("1510017x-mac-ukrainian"),MCD_T(""),
MCD_T("0828595cyrillic"),MCD_T("0900936gb2312-80"),MCD_T(""),
MCD_T("0720866cskoi8r1528591iso_8859-1:1987"),MCD_T("1528592iso_8859-2:1987"),
MCD_T("1354936iso-4873:1986"),MCD_T("0700932sjis-ms1528593iso_8859-3:1988"),
MCD_T("1528594iso_8859-4:19880600936gb23120701251ms-cyrl"),
MCD_T("1528596iso_8859-6:19871528595iso_8859-5:1988"),
MCD_T("1528597iso_8859-7:1987"),
MCD_T("1201250windows-12501300932shifft_jis-ms"),
MCD_T("0810029x-mac-ce1201251windows-12511528598iso_8859-8:19880900949ks_c_56011110000csmacintosh"),
MCD_T("0601200cp12001201252windows-1252"),
MCD_T("1052936hz-gb-23121201253windows-12531400949ks_c_5601_19871528599iso_8859-9:19890601201cp1201"),
MCD_T("1201254windows-1254"),MCD_T("1000936csgb2312801201255windows-1255"),
MCD_T("1201256windows-12561100932windows-31j"),
MCD_T("1201257windows-12570601250cp12500601133cp1133"),
MCD_T("0601251cp12511201258windows-12580601125cp1125"),
MCD_T("0701254ms-turk0601252cp1252"),MCD_T("0601253cp12530601361cp1361"),
MCD_T("0800949ks-c56010601254cp1254"),MCD_T("0651936euc-cn0601255cp1255"),
MCD_T("0601256cp1256"),MCD_T("0601257cp12570600950csbig50800858ibm00858"),
MCD_T("0601258cp1258"),MCD_T("0520105x-ia5"),
MCD_T("0801250x-cp12501110006x-mac-greek0738598logical"),
MCD_T("0801251x-cp1251"),MCD_T(""),
MCD_T("1410001x-mac-japanese1200932cswindows31j"),
MCD_T("0700936chinese0720127csascii0620932euc-jp"),
MCD_T("0851936x-euc-cn0501200ucs-2"),MCD_T("0628597greek8"),
MCD_T("0651949euc-kr"),MCD_T(""),MCD_T("0628591latin1"),
MCD_T("0628592latin21100874iso-8859-11"),
MCD_T("0628593latin31420127ansi_x3.4-19681420127ansi_x3.4-19861028591iso_8859-1"),
MCD_T("0628594latin41028592iso_8859-20701200unicode1128603iso-8859-13"),
MCD_T("1028593iso_8859-30628599latin51410082x-mac-croatian"),
MCD_T("1028594iso_8859-41128605iso-8859-150565000utf-70851932x-euc-jp"),
MCD_T("1300775cspc775baltic1028595iso_8859-50565001utf-80512000utf32"),
MCD_T("1028596iso_8859-61710002x-mac-chinesetrad0601252x-ansi"),
MCD_T("1028597iso_8859-70628605latin90501200utf160700154ptcp1541410010x-mac-romanian"),
MCD_T("0900936iso-ir-581028598iso_8859-8"),MCD_T("1028599iso_8859-9"),
MCD_T("1350221iso2022-jp-ms0400932sjis"),MCD_T("0751949cseuckr"),
MCD_T("1420002x-chinese-eten"),MCD_T("1410007x-mac-cyrillic"),
MCD_T("1000932shifft_jis"),MCD_T("0828596ecma-114"),MCD_T(""),
MCD_T("0900932shift-jis"),MCD_T("0701256cp1256 1320107x-ia5-swedish"),
MCD_T("0828597ecma-118"),
MCD_T("1628596csisolatinarabic1710008x-mac-chinesesimp0600932x-sjis"),MCD_T(""),
MCD_T("0754936gb18030"),MCD_T("1350221windows-502210712000cp12000"),
MCD_T("0628596arabic0500936cn-gb0900932sjis-open0712001cp12001"),MCD_T(""),
MCD_T(""),MCD_T("0700950cn-big50920127iso646-us1001133ibm-cp1133"),MCD_T(""),
MCD_T("0800936csgb23120900949ks-c-56010310000mac"),
MCD_T("1001257winbaltrim0750221cp502211020127iso-ir-6us"),
MCD_T("1000932csshiftjis"),MCD_T("0300936gbk0765001cp65001"),
MCD_T("1620127iso_646.irv:19911351932windows-519320920001x-cp20001")
};
int x_GetEncodingCodePage( MCD_CSTR pszEncoding )
{
// redo for completeness, the iconv set, UTF-32, and uppercase
// Lookup strEncoding in EncodingNameTable and return Windows code page
int nCodePage = -1;
int nEncLen = MCD_PSZLEN( pszEncoding );
if ( ! nEncLen )
nCodePage = MCD_ACP;
else if ( x_StrNCmp(pszEncoding,MCD_T("UTF-32"),6) == 0 )
nCodePage = MCD_UTF32;
else if ( nEncLen < 100 )
{
MCD_CHAR szEncodingLower[100];
for ( int nEncChar=0; nEncChar<nEncLen; ++nEncChar )
{
MCD_CHAR cEncChar = pszEncoding[nEncChar];
szEncodingLower[nEncChar] = (cEncChar>='A' && cEncChar<='Z')? (MCD_CHAR)(cEncChar+('a'-'A')) : cEncChar;
}
szEncodingLower[nEncLen] = '\0';
MCD_PCSZ pEntry = EncodingNameTable[x_Hash(szEncodingLower,sizeof(EncodingNameTable)/sizeof(MCD_PCSZ))];
while ( *pEntry )
{
// e.g. entry: 0565001utf-8 means length 05, code page 65001, encoding name utf-8
int nEntryLen = (*pEntry - '0') * 10;
++pEntry;
nEntryLen += (*pEntry - '0');
++pEntry;
MCD_PCSZ pCodePage = pEntry;
pEntry += 5;
if ( nEntryLen == nEncLen && x_StrNCmp(szEncodingLower,pEntry,nEntryLen) == 0 )
{
// Convert digits to integer up to code name which always starts with alpha
nCodePage = MCD_PSZTOL( pCodePage, NULL, 10 );
break;
}
pEntry += nEntryLen;
}
}
return nCodePage;
}
#if ! defined(MARKUP_WCHAR)
bool TextEncoding::CanConvert( MCD_CSTR pszToEncoding, MCD_CSTR pszFromEncoding )
{
// Return true if MB to MB conversion is possible
#if defined(MARKUP_ICONV)
// iconv_open should fail if either encoding not supported or one is alias for other
char szTo[100], szFrom[100];
iconv_t cd = iconv_open( IConvName(szTo,pszToEncoding), IConvName(szFrom,pszFromEncoding) );
if ( cd == (iconv_t)-1 )
return false;
iconv_close(cd);
#else
int nToCP = x_GetEncodingCodePage( pszToEncoding );
int nFromCP = x_GetEncodingCodePage( pszFromEncoding );
if ( nToCP == -1 || nFromCP == -1 )
return false;
#if defined(MARKUP_WINCONV)
if ( nToCP == MCD_ACP || nFromCP == MCD_ACP ) // either ACP ANSI?
{
int nACP = GetACP();
if ( nToCP == MCD_ACP )
nToCP = nACP;
if ( nFromCP == MCD_ACP )
nFromCP = nACP;
}
#else // no conversion API, but we can do AToUTF8 and UTF8ToA
if ( nToCP != MCD_UTF8 && nFromCP != MCD_UTF8 ) // either UTF-8?
return false;
#endif // no conversion API
if ( nToCP == nFromCP )
return false;
#endif // not ICONV
return true;
}
#endif // not WCHAR
#if defined(MARKUP_ICONV)
const char* TextEncoding::IConvName( char* szEncoding, MCD_CSTR pszEncoding )
{
// Make upper case char-based name from strEncoding which consists only of characters in the ASCII range
int nEncChar = 0;
while ( pszEncoding[nEncChar] )
{
char cEncChar = (char)pszEncoding[nEncChar];
szEncoding[nEncChar] = (cEncChar>='a' && cEncChar<='z')? (cEncChar-('a'-'A')) : cEncChar;
++nEncChar;
}
if ( nEncChar == 6 && x_StrNCmp(szEncoding,"UTF-16",6) == 0 )
{
szEncoding[nEncChar++] = 'B';
szEncoding[nEncChar++] = 'E';
}
szEncoding[nEncChar] = '\0';
return szEncoding;
}
int TextEncoding::IConv( void* pTo, int nToCharSize, int nFromCharSize )
{
// Converts from m_pFrom to pTo
char szTo[100], szFrom[100];
iconv_t cd = iconv_open( IConvName(szTo,m_strToEncoding), IConvName(szFrom,m_strFromEncoding) );
int nToLenBytes = 0;
if ( cd != (iconv_t)-1 )
{
size_t nFromLenRemaining = (size_t)m_nFromLen * nFromCharSize;
size_t nToCountRemaining = (size_t)m_nToCount * nToCharSize;
size_t nToCountRemainingBefore;
char* pToChar = (char*)pTo;
char* pFromChar = (char*)m_pFrom;
char* pToTempBuffer = NULL;
const size_t nTempBufferSize = 2048;
size_t nResult;
if ( ! pTo )
{
pToTempBuffer = new char[nTempBufferSize];
pToChar = pToTempBuffer;
nToCountRemaining = nTempBufferSize;
}
while ( nFromLenRemaining )
{
nToCountRemainingBefore = nToCountRemaining;
nResult = iconv( cd, &pFromChar, &nFromLenRemaining, &pToChar, &nToCountRemaining );
nToLenBytes += (int)(nToCountRemainingBefore - nToCountRemaining);
if ( nResult == (size_t)-1 )
{
int nErrno = errno;
if ( nErrno == EILSEQ )
{
// Bypass bad char, question mark denotes problem in source string
pFromChar += nFromCharSize;
nFromLenRemaining -= nFromCharSize;
if ( nToCharSize == 1 )
*pToChar = '?';
else if ( nToCharSize == 2 )
*((unsigned short*)pToChar) = (unsigned short)'?';
else if ( nToCharSize == 4 )
*((unsigned int*)pToChar) = (unsigned int)'?';
pToChar += nToCharSize;
nToCountRemaining -= nToCharSize;
nToLenBytes += nToCharSize;
size_t nInitFromLen = 0, nInitToCount = 0;
iconv(cd, NULL, &nInitFromLen ,NULL, &nInitToCount );
}
else if ( nErrno == EINVAL )
break; // incomplete character or shift sequence at end of input
else if ( nErrno == E2BIG && !pToTempBuffer )
break; // output buffer full should only happen when using a temp buffer
}
else
m_nFailedChars += nResult;
if ( pToTempBuffer && nToCountRemaining < 10 )
{
nToCountRemaining = nTempBufferSize;
pToChar = pToTempBuffer;
}
}
if ( pToTempBuffer )
delete[] pToTempBuffer;
iconv_close(cd);
}
return nToLenBytes / nToCharSize;
}
#endif
#if defined(MARKUP_WINCONV)
bool x_NoDefaultChar( int nCP )
{
// WideCharToMultiByte fails if lpUsedDefaultChar is non-NULL for these code pages:
return (bool)(nCP == 65000 || nCP == 65001 || nCP == 50220 || nCP == 50221 || nCP == 50222 || nCP == 50225 ||
nCP == 50227 || nCP == 50229 || nCP == 52936 || nCP == 54936 || (nCP >= 57002 && nCP <= 57011) );
}
#endif
int TextEncoding::PerformConversion( void* pTo, MCD_CSTR pszToEncoding/*=NULL*/ )
{
// If pTo is not NULL, it must be large enough to hold result, length of result is returned
// m_nFailedChars will be set to >0 if characters not supported in strToEncoding
int nToLen = 0;
if ( pszToEncoding.pcsz )
m_strToEncoding = pszToEncoding;
int nToCP = x_GetEncodingCodePage( m_strToEncoding );
if ( nToCP == -1 )
nToCP = MCD_ACP;
int nFromCP = x_GetEncodingCodePage( m_strFromEncoding );
if ( nFromCP == -1 )
nFromCP = MCD_ACP;
m_nFailedChars = 0;
#if ! defined(MARKUP_WINCONV) && ! defined(MARKUP_ICONV)
// Only non-Unicode encoding supported is locale charset, must call setlocale
if ( nToCP != MCD_UTF8 && nToCP != MCD_UTF16 && nToCP != MCD_UTF32 )
nToCP = MCD_ACP;
if ( nFromCP != MCD_UTF8 && nFromCP != MCD_UTF16 && nFromCP != MCD_UTF32 )
nFromCP = MCD_ACP;
if ( nFromCP == MCD_ACP )
{
const char* pA = (const char*)m_pFrom;
int nALenRemaining = m_nFromLen;
int nCharLen;
wchar_t wcChar;
char* pU = (char*)pTo;
while ( nALenRemaining )
{
nCharLen = mbtowc( &wcChar, pA, nALenRemaining );
if ( nCharLen < 1 )
{
wcChar = (wchar_t)'?';
nCharLen = 1;
}
pA += nCharLen;
nALenRemaining -= nCharLen;
if ( nToCP == MCD_UTF8 )
CMarkup::EncodeCharUTF8( (int)wcChar, pU, nToLen );
else if ( nToCP == MCD_UTF16 )
CMarkup::EncodeCharUTF16( (int)wcChar, (unsigned short*)pU, nToLen );
else // UTF32
{
if ( pU )
((unsigned int*)pU)[nToLen] = (unsigned int)wcChar;
++nToLen;
}
}
}
else if ( nToCP == MCD_ACP )
{
union pUnicodeUnion { const char* p8; const unsigned short* p16; const unsigned int* p32; } pU;
pU.p8 = (const char*)m_pFrom;
const char* pUEnd = pU.p8 + m_nFromLen;
if ( nFromCP == MCD_UTF16 )
pUEnd = (char*)( pU.p16 + m_nFromLen );
else if ( nFromCP == MCD_UTF32 )
pUEnd = (char*)( pU.p32 + m_nFromLen );
int nCharLen;
char* pA = (char*)pTo;
char szA[8];
int nUChar;
while ( pU.p8 != pUEnd )
{
if ( nFromCP == MCD_UTF8 )
nUChar = CMarkup::DecodeCharUTF8( pU.p8, pUEnd );
else if ( nFromCP == MCD_UTF16 )
nUChar = CMarkup::DecodeCharUTF16( pU.p16, (const unsigned short*)pUEnd );
else // UTF32
nUChar = *(pU.p32)++;
if ( nUChar == -1 )
nCharLen = -2;
else if ( nUChar & ~0xffff )
nCharLen = -1;
else
nCharLen = wctomb( pA?pA:szA, (wchar_t)nUChar );
if ( nCharLen < 0 )
{
if ( nCharLen == -1 )
++m_nFailedChars;
nCharLen = 1;
if ( pA )
*pA = '?';
}
if ( pA )
pA += nCharLen;
nToLen += nCharLen;
}
}
#endif // not WINCONV and not ICONV
if ( nFromCP == MCD_UTF32 )
{
const unsigned int* p32 = (const unsigned int*)m_pFrom;
const unsigned int* p32End = p32 + m_nFromLen;
if ( nToCP == MCD_UTF8 )
{
char* p8 = (char*)pTo;
while ( p32 != p32End )
CMarkup::EncodeCharUTF8( *p32++, p8, nToLen );
}
else if ( nToCP == MCD_UTF16 )
{
unsigned short* p16 = (unsigned short*)pTo;
while ( p32 != p32End )
CMarkup::EncodeCharUTF16( (int)*p32++, p16, nToLen );
}
else // to ANSI
{
// WINCONV not supported for 32To8, since only used for sizeof(wchar_t) == 4
#if defined(MARKUP_ICONV)
nToLen = IConv( pTo, 1, 4 );
#endif // ICONV
}
}
else if ( nFromCP == MCD_UTF16 )
{
// UTF16To8 will be deprecated since weird output buffer size sensitivity not worth implementing here
const unsigned short* p16 = (const unsigned short*)m_pFrom;
const unsigned short* p16End = p16 + m_nFromLen;
int nUChar;
if ( nToCP == MCD_UTF32 )
{
unsigned int* p32 = (unsigned int*)pTo;
while ( p16 != p16End )
{
nUChar = CMarkup::DecodeCharUTF16( p16, p16End );
if ( nUChar == -1 )
nUChar = '?';
if ( p32 )
p32[nToLen] = (unsigned int)nUChar;
++nToLen;
}
}
#if defined(MARKUP_WINCONV)
else // to UTF-8 or other multi-byte
{
nToLen = WideCharToMultiByte(nToCP,0,(const wchar_t*)m_pFrom,m_nFromLen,(char*)pTo,
m_nToCount?m_nToCount+1:0,NULL,x_NoDefaultChar(nToCP)?NULL:&m_nFailedChars);
}
#else // not WINCONV
else if ( nToCP == MCD_UTF8 )
{
char* p8 = (char*)pTo;
while ( p16 != p16End )
{
nUChar = CMarkup::DecodeCharUTF16( p16, p16End );
if ( nUChar == -1 )
nUChar = '?';
CMarkup::EncodeCharUTF8( nUChar, p8, nToLen );
}
}
else // to ANSI
{
#if defined(MARKUP_ICONV)
nToLen = IConv( pTo, 1, 2 );
#endif // ICONV
}
#endif // not WINCONV
}
else if ( nToCP == MCD_UTF16 ) // to UTF-16 from UTF-8/ANSI
{
#if defined(MARKUP_WINCONV)
nToLen = MultiByteToWideChar(nFromCP,0,(const char*)m_pFrom,m_nFromLen,(wchar_t*)pTo,m_nToCount);
#else // not WINCONV
if ( nFromCP == MCD_UTF8 )
{
const char* p8 = (const char*)m_pFrom;
const char* p8End = p8 + m_nFromLen;
int nUChar;
unsigned short* p16 = (unsigned short*)pTo;
while ( p8 != p8End )
{
nUChar = CMarkup::DecodeCharUTF8( p8, p8End );
if ( nUChar == -1 )
nUChar = '?';
if ( p16 )
p16[nToLen] = (unsigned short)nUChar;
++nToLen;
}
}
else // from ANSI
{
#if defined(MARKUP_ICONV)
nToLen = IConv( pTo, 2, 1 );
#endif // ICONV
}
#endif // not WINCONV
}
else if ( nToCP == MCD_UTF32 ) // to UTF-32 from UTF-8/ANSI
{
if ( nFromCP == MCD_UTF8 )
{
const char* p8 = (const char*)m_pFrom;
const char* p8End = p8 + m_nFromLen;
int nUChar;
unsigned int* p32 = (unsigned int*)pTo;
while ( p8 != p8End )
{
nUChar = CMarkup::DecodeCharUTF8( p8, p8End );
if ( nUChar == -1 )
nUChar = '?';
if ( p32 )
p32[nToLen] = (unsigned int)nUChar;
++nToLen;
}
}
else // from ANSI
{
// WINCONV not supported for ATo32, since only used for sizeof(wchar_t) == 4
#if defined(MARKUP_ICONV)
// nToLen = IConv( pTo, 4, 1 );
// Linux: had trouble getting IConv to leave the BOM off of the UTF-32 output stream
// So converting via UTF-16 with native endianness
unsigned short* pwszUTF16 = new unsigned short[m_nFromLen];
MCD_STR strToEncoding = m_strToEncoding;
m_strToEncoding = MCD_T("UTF-16BE");
short nEndianTest = 1;
if ( ((char*)&nEndianTest)[0] ) // Little-endian?
m_strToEncoding = MCD_T("UTF-16LE");
m_nToCount = m_nFromLen;
int nUTF16Len = IConv( pwszUTF16, 2, 1 );
m_strToEncoding = strToEncoding;
const unsigned short* p16 = (const unsigned short*)pwszUTF16;
const unsigned short* p16End = p16 + nUTF16Len;
int nUChar;
unsigned int* p32 = (unsigned int*)pTo;
while ( p16 != p16End )
{
nUChar = CMarkup::DecodeCharUTF16( p16, p16End );
if ( nUChar == -1 )
nUChar = '?';
if ( p32 )
*p32++ = (unsigned int)nUChar;
++nToLen;
}
delete[] pwszUTF16;
#endif // ICONV
}
}
else
{
#if defined(MARKUP_ICONV)
nToLen = IConv( pTo, 1, 1 );
#elif defined(MARKUP_WINCONV)
wchar_t* pwszUTF16 = new wchar_t[m_nFromLen];
int nUTF16Len = MultiByteToWideChar(nFromCP,0,(const char*)m_pFrom,m_nFromLen,pwszUTF16,m_nFromLen);
nToLen = WideCharToMultiByte(nToCP,0,pwszUTF16,nUTF16Len,(char*)pTo,m_nToCount,NULL,
x_NoDefaultChar(nToCP)?NULL:&m_nFailedChars);
delete[] pwszUTF16;
#endif // WINCONV
}
// Store the length in case this is called again after allocating output buffer to fit
m_nToCount = nToLen;
return nToLen;
}
bool TextEncoding::FindRaggedEnd( int& nTruncBeforeBytes )
{
// Check for ragged end UTF-16 or multi-byte according to m_strToEncoding, expects at least 40 bytes to work with
bool bSuccess = true;
nTruncBeforeBytes = 0;
int nCP = x_GetEncodingCodePage( m_strFromEncoding );
if ( nCP == MCD_UTF16 )
{
unsigned short* pUTF16Buffer = (unsigned short*)m_pFrom;
const unsigned short* pUTF16Last = &pUTF16Buffer[m_nFromLen-1];
if ( CMarkup::DecodeCharUTF16(pUTF16Last,&pUTF16Buffer[m_nFromLen]) == -1 )
nTruncBeforeBytes = 2;
}
else // UTF-8, SBCS DBCS
{
if ( nCP == MCD_UTF8 )
{
char* pUTF8Buffer = (char*)m_pFrom;
char* pUTF8End = &pUTF8Buffer[m_nFromLen];
int nLast = m_nFromLen - 1;
const char* pUTF8Last = &pUTF8Buffer[nLast];
while ( nLast > 0 && CMarkup::DecodeCharUTF8(pUTF8Last,pUTF8End) == -1 )
pUTF8Last = &pUTF8Buffer[--nLast];
nTruncBeforeBytes = (int)(pUTF8End - pUTF8Last);
}
else
{
// Do a conversion-based test unless we can determine it is not multi-byte
// If m_strEncoding="" default code page then GetACP can tell us the code page, otherwise just do the test
#if defined(MARKUP_WINCONV)
if ( nCP == 0 )
nCP = GetACP();
#endif
int nMultibyteCharsToTest = 2;
switch ( nCP )
{
case 54936:
nMultibyteCharsToTest = 4;
case 932: case 51932: case 20932: case 50220: case 50221: case 50222: case 10001: // Japanese
case 949: case 51949: case 50225: case 1361: case 10003: case 20949: // Korean
case 874: case 20001: case 20004: case 10021: case 20003: // Taiwan
case 50930: case 50939: case 50931: case 50933: case 20833: case 50935: case 50937: // EBCDIC
case 936: case 51936: case 20936: case 52936: // Chinese
case 950: case 50227: case 10008: case 20000: case 20002: case 10002: // Chinese
nCP = 0;
break;
}
if ( nMultibyteCharsToTest > m_nFromLen )
nMultibyteCharsToTest = m_nFromLen;
if ( nCP == 0 && nMultibyteCharsToTest )
{
/*
1. convert the piece to Unicode with MultiByteToWideChar
2. Identify at least two Unicode code point boundaries at the end of
the converted piece by stepping backwards from the end and re-
converting the final 2 bytes, 3 bytes, 4 bytes etc, comparing the
converted end string to the end of the entire converted piece to find
a valid code point boundary.
3. Upon finding a code point boundary, I still want to make sure it
will convert the same separately on either side of the divide as it
does together, so separately convert the first byte and the remaining
bytes and see if the result together is the same as the whole end, if
not try the first two bytes and the remaining bytes. etc., until I
find a useable dividing point. If none found, go back to step 2 and
get a longer end string to try.
*/
m_strToEncoding = MCD_T("UTF-16");
m_nToCount = m_nFromLen*2;
unsigned short* pUTF16Buffer = new unsigned short[m_nToCount];
int nUTF16Len = PerformConversion( (void*)pUTF16Buffer );
int nOriginalByteLen = m_nFromLen;
// Guaranteed to have at least MARKUP_FILEBLOCKSIZE/2 bytes to work with
const int nMaxBytesToTry = 40;
unsigned short wsz16End[nMaxBytesToTry*2];
unsigned short wsz16EndDivided[nMaxBytesToTry*2];
const char* pszOriginalBytes = (const char*)m_pFrom;
int nBoundariesFound = 0;
bSuccess = false;
while ( nTruncBeforeBytes < nMaxBytesToTry && ! bSuccess )
{
++nTruncBeforeBytes;
m_pFrom = &pszOriginalBytes[nOriginalByteLen-nTruncBeforeBytes];
m_nFromLen = nTruncBeforeBytes;
m_nToCount = nMaxBytesToTry*2;
int nEndUTF16Len = PerformConversion( (void*)wsz16End );
if ( nEndUTF16Len && memcmp(wsz16End,&pUTF16Buffer[nUTF16Len-nEndUTF16Len],nEndUTF16Len*2) == 0 )
{
++nBoundariesFound;
if ( nBoundariesFound > 2 )
{
int nDivideAt = 1;
while ( nDivideAt < nTruncBeforeBytes )
{
m_pFrom = &pszOriginalBytes[nOriginalByteLen-nTruncBeforeBytes];
m_nFromLen = nDivideAt;
m_nToCount = nMaxBytesToTry*2;
int nDividedUTF16Len = PerformConversion( (void*)wsz16EndDivided );
if ( nDividedUTF16Len )
{
m_pFrom = &pszOriginalBytes[nOriginalByteLen-nTruncBeforeBytes+nDivideAt];
m_nFromLen = nTruncBeforeBytes-nDivideAt;
m_nToCount = nMaxBytesToTry*2-nDividedUTF16Len;
nDividedUTF16Len += PerformConversion( (void*)&wsz16EndDivided[nDividedUTF16Len] );
if ( m_nToCount && nEndUTF16Len == nDividedUTF16Len && memcmp(wsz16End,wsz16EndDivided,nEndUTF16Len) == 0 )
{
nTruncBeforeBytes -= nDivideAt;
bSuccess = true;
break;
}
}
++nDivideAt;
}
}
}
}
delete [] pUTF16Buffer;
}
}
}
return bSuccess;
}
bool x_EndianSwapRequired( int nDocFlags )
{
short nWord = 1;
char cFirstByte = ((char*)&nWord)[0];
if ( cFirstByte ) // LE
{
if ( nDocFlags & CMarkup::MDF_UTF16BEFILE )
return true;
}
else if ( nDocFlags & CMarkup::MDF_UTF16LEFILE )
return true;
return false;
}
void x_EndianSwapUTF16( unsigned short* pBuffer, int nCharLen )
{
unsigned short cChar;
while ( nCharLen-- )
{
cChar = pBuffer[nCharLen];
pBuffer[nCharLen] = (unsigned short)((cChar<<8) | (cChar>>8));
}
}
//////////////////////////////////////////////////////////////////////
// Element position indexes
// This is the primary means of storing the layout of the document
//
struct ElemPos
{
ElemPos() {};
ElemPos( const ElemPos& pos ) { *this = pos; };
int StartTagLen() const { return nStartTagLen; };
void SetStartTagLen( int n ) { nStartTagLen = n; };
void AdjustStartTagLen( int n ) { nStartTagLen += n; };
int EndTagLen() const { return nEndTagLen; };
void SetEndTagLen( int n ) { nEndTagLen = n; };
bool IsEmptyElement() { return (StartTagLen()==nLength)?true:false; };
int StartContent() const { return nStart + StartTagLen(); };
int ContentLen() const { return nLength - StartTagLen() - EndTagLen(); };
int StartAfter() const { return nStart + nLength; };
int Level() const { return nFlags & 0xffff; };
void SetLevel( int nLev ) { nFlags = (nFlags & ~0xffff) | nLev; };
void ClearVirtualParent() { memset(this,0,sizeof(ElemPos)); };
void SetEndTagLenUnparsed() { SetEndTagLen(1); };
bool IsUnparsed() { return EndTagLen() == 1; };
// Memory size: 8 32-bit integers == 32 bytes
int nStart;
int nLength;
unsigned int nStartTagLen : 22; // 4MB limit for start tag
unsigned int nEndTagLen : 10; // 1K limit for end tag
int nFlags; // 16 bits flags, 16 bits level 65536 depth limit
int iElemParent;
int iElemChild; // first child
int iElemNext; // next sibling
int iElemPrev; // if this is first, iElemPrev points to last
};
enum MarkupNodeFlagsInternal2
{
MNF_REPLACE = 0x001000,
MNF_QUOTED = 0x008000,
MNF_EMPTY = 0x010000,
MNF_DELETED = 0x020000,
MNF_FIRST = 0x080000,
MNF_PUBLIC = 0x300000,
MNF_ILLFORMED = 0x800000,
MNF_USER = 0xf000000
};
struct ElemPosTree
{
ElemPosTree() { Clear(); };
~ElemPosTree() { Release(); };
enum { PA_SEGBITS = 16, PA_SEGMASK = 0xffff };
void ReleaseElemPosTree() { Release(); Clear(); };
void Release() { for (int n=0;n<SegsUsed();++n) delete[] (char*)m_pSegs[n]; if (m_pSegs) delete[] (char*)m_pSegs; };
void Clear() { m_nSegs=0; m_nSize=0; m_pSegs=NULL; };
int GetSize() const { return m_nSize; };
int SegsUsed() const { return ((m_nSize-1)>>PA_SEGBITS) + 1; };
ElemPos& GetRefElemPosAt(int i) const { return m_pSegs[i>>PA_SEGBITS][i&PA_SEGMASK]; };
void CopyElemPosTree( ElemPosTree* pOtherTree, int n );
void GrowElemPosTree( int nNewSize );
private:
ElemPos** m_pSegs;
int m_nSize;
int m_nSegs;
};
void ElemPosTree::CopyElemPosTree( ElemPosTree* pOtherTree, int n )
{
ReleaseElemPosTree();
m_nSize = n;
if ( m_nSize < 8 )
m_nSize = 8;
m_nSegs = SegsUsed();
if ( m_nSegs )
{
m_pSegs = (ElemPos**)(new char[m_nSegs*sizeof(char*)]);
int nSegSize = 1 << PA_SEGBITS;
for ( int nSeg=0; nSeg < m_nSegs; ++nSeg )
{
if ( nSeg + 1 == m_nSegs )
nSegSize = m_nSize - (nSeg << PA_SEGBITS);
m_pSegs[nSeg] = (ElemPos*)(new char[nSegSize*sizeof(ElemPos)]);
memcpy( m_pSegs[nSeg], pOtherTree->m_pSegs[nSeg], nSegSize*sizeof(ElemPos) );
}
}
}
void ElemPosTree::GrowElemPosTree( int nNewSize )