summaryrefslogtreecommitdiff
path: root/svl/source/items/poolio.cxx
blob: caad85d65435742f4e2fb9545f3059e475ab1c52 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */


#include <string.h>
#include <stdio.h>

#include <sal/log.hxx>
#include <tools/solar.h>
#include <svl/itempool.hxx>
#include "whassert.hxx"
#include <svl/brdcst.hxx>
#include <svl/filerec.hxx>
#include "poolio.hxx"
#include <boost/scoped_array.hpp>

const SfxItemPool* SfxItemPool::GetStoringPool()

/*  [Beschreibung]

    Diese Methode liefert den <SfxItemPool>, der gerade gespeichert wird.
    Sie sollte nur in Notf"allen verwendet werden, um z.B. File-Format-
    Kompatibilit"at zu gew"ahrleisten o."o. - z.B. in der "uberladung eines
    <SfxPoolItem::Store()> zus"atzliche Daten aus dem dazuge"horigen
    Pool zu besorgen.
*/

{
    return pStoringPool_;
}


SvStream &SfxItemPool::Store(SvStream &rStream) const

/*  [Beschreibung]

    Der SfxItemPool wird inklusive aller seiner Sekund"arpools mit
    Pool-Defaults und gepoolten Items in dem angegebenen Stream gespeichert.
    Die statischen Defaults werden nicht gespeichert.


    [Fileformat]

    ;zun"achst ein Kompatiblit"ats-Header-Block
    Start:      0x1111  SFX_ITEMPOOL_TAG_STARTPOOLS(_4/_5)
                sal_uInt8   MAJOR_VER                   ;SfxItemPool-Version
                sal_uInt8   MINOR_VER                   ;"
                0xFFFF  SFX_ITEMPOOL_TAG_TRICK4OLD  ;ex. GetVersion()
                sal_uInt16  0x0000                      ;Pseudo-StyleSheetPool
                sal_uInt16  0x0000                      ;Pseudo-StyleSheetPool

    ;den ganzen Pool in einen Record
                record  SfxMiniRecod(SFX_ITEMPOOL_REC)

    ;je ein Header vorweg
    Header:     record      SfxMiniRecord(SFX_ITEMPOOL_REC_HEADER)
                sal_uInt16          GetVersion()            ;Which-Ranges etc.
                String          GetName()               ;Pool-Name

    ;die Versions-Map, um WhichIds neuer File-Versionen mappen zu k"onnen
    Versions:   record      SfxMultiRecord(SFX_ITEMPOOL_REC_VERSIONS, 0)
                sal_uInt16          OldVersion
                sal_uInt16          OldStartWhich
                sal_uInt16          OldEndWhich
                sal_uInt16[]        NewWhich (OldEndWhich-OldStartWhich+1)

    ;jetzt die gepoolten Items (zuerst nicht-SfxSetItems)
    Items:      record      SfxMultiRecord(SFX_ITEMPOOL_REC_WHICHIDS, 0)
                content         SlotId, 0
                sal_uInt16          WhichId
                sal_uInt16          pItem->GetVersion()
                sal_uInt16          Array-Size
                record          SfxMultiRecord(SFX_, 0)
                content             Surrogate
                sal_uInt16              RefCount
                unknown             pItem->Store()

    ;jetzt die gesetzten Pool-Defaults
    Defaults:   record      SfxMultiRecord(SFX_ITEMPOOL_REC_DEFAULTS, 0)
                content         SlotId, 0
                sal_uInt16          WhichId
                sal_uInt16          pPoolDef->GetVersion()
                unknown         pPoolDef->Store();

    ;dahinter folgt ggf. der Secondary ohne Kompatiblit"ats-Header-Block
*/

{
    // Store-Master finden
    SfxItemPool *pStoreMaster = pImp->mpMaster != this ? pImp->mpMaster : 0;
    while ( pStoreMaster && !pStoreMaster->pImp->bStreaming )
        pStoreMaster = pStoreMaster->pImp->mpSecondary;

    // Alter-Header (Version des Pools an sich und Inhalts-Version 0xffff)
    pImp->bStreaming = true;
    if ( !pStoreMaster )
    {
        rStream.WriteUInt16(  rStream.GetVersion() >= SOFFICE_FILEFORMAT_50
                ? SFX_ITEMPOOL_TAG_STARTPOOL_5
                : SFX_ITEMPOOL_TAG_STARTPOOL_4  );
        rStream.WriteUInt8( SFX_ITEMPOOL_VER_MAJOR ).WriteUInt8( SFX_ITEMPOOL_VER_MINOR );
        rStream.WriteUInt16( SFX_ITEMPOOL_TAG_TRICK4OLD );

        // SfxStyleSheet-Bug umgehen
        rStream.WriteUInt16( sal_uInt16(0) ); // Version
        rStream.WriteUInt16( sal_uInt16(0) ); // Count (2. Schleife f"allt sonst auf die Fresse)
    }

    // jeder Pool ist als ganzes ein Record
    SfxMiniRecordWriter aPoolRec( &rStream, SFX_ITEMPOOL_REC );
    pStoringPool_ = this;

    // Einzel-Header (Version des Inhalts und Name)
    {
        SfxMiniRecordWriter aPoolHeaderRec( &rStream, SFX_ITEMPOOL_REC_HEADER);
        rStream.WriteUInt16( pImp->nVersion );
        SfxPoolItem::writeByteString(rStream, pImp->aName);
    }

    // Version-Maps
    {
        SfxMultiVarRecordWriter aVerRec( &rStream, SFX_ITEMPOOL_REC_VERSIONMAP, 0 );
        for ( size_t nVerNo = 0; nVerNo < pImp->aVersions.size(); ++nVerNo )
        {
            aVerRec.NewContent();
            SfxPoolVersion_ImplPtr pVer = pImp->aVersions[nVerNo];
            rStream.WriteUInt16( pVer->_nVer ).WriteUInt16( pVer->_nStart ).WriteUInt16( pVer->_nEnd );
            sal_uInt16 nCount = pVer->_nEnd - pVer->_nStart + 1;
            sal_uInt16 nNewWhich = 0;
            for ( sal_uInt16 n = 0; n < nCount; ++n )
            {
                nNewWhich = pVer->_pMap[n];
                rStream.WriteUInt16( nNewWhich );
            }

            // Workaround gegen Bug in SetVersionMap der 312
            if ( SOFFICE_FILEFORMAT_31 == pImp->mnFileFormatVersion )
                rStream.WriteUInt16( sal_uInt16(nNewWhich+1) );
        }
    }

    // gepoolte Items
    {
        SfxMultiMixRecordWriter aWhichIdsRec( &rStream, SFX_ITEMPOOL_REC_WHICHIDS, 0 );

        // erst Atomaren-Items und dann die Sets schreiben (wichtig beim Laden)
        for (int ft = 0 ; ft < 2 && !rStream.GetError(); ft++)
        {
            pImp->bInSetItem = ft != 0;

            std::vector<SfxPoolItemArray_Impl*>::iterator itrArr = pImp->maPoolItems.begin();
            SfxPoolItem **ppDefItem = pImp->ppStaticDefaults;
            const sal_uInt16 nSize = GetSize_Impl();
            for ( size_t i = 0; i < nSize && !rStream.GetError(); ++i, ++itrArr, ++ppDefItem )
            {
                // Version des Items feststellen
                sal_uInt16 nItemVersion = (*ppDefItem)->GetVersion( pImp->mnFileFormatVersion );
                if ( USHRT_MAX == nItemVersion )
                    // => kam in zu exportierender Version gar nicht vor
                    continue;

                // !poolable wird gar nicht im Pool gespeichert
                // und itemsets/plain-items je nach Runde
                if ( *itrArr && IsItemFlag(**ppDefItem, SFX_ITEM_POOLABLE) &&
                     pImp->bInSetItem == (bool) (*ppDefItem)->ISA(SfxSetItem) )
                {
                    // eigene Kennung, globale Which-Id und Item-Version
                    sal_uInt16 nSlotId = GetSlotId( (*ppDefItem)->Which(), false );
                    aWhichIdsRec.NewContent(nSlotId, 0);
                    rStream.WriteUInt16( (*ppDefItem)->Which() );
                    rStream.WriteUInt16( nItemVersion );
                    const sal_uInt32 nCount = ::std::min<size_t>( (*itrArr)->size(), SAL_MAX_UINT32 );
                    DBG_ASSERT(nCount, "ItemArr is empty");
                    rStream.WriteUInt32( nCount );

                    // Items an sich schreiben
                    SfxMultiMixRecordWriter aItemsRec( &rStream, SFX_ITEMPOOL_REC_ITEMS, 0 );
                    for ( size_t j = 0; j < nCount; ++j )
                    {
                        // Item selbst besorgen
                        const SfxPoolItem *pItem = (*itrArr)->operator[](j);
                        if ( pItem && pItem->GetRefCount() ) //! siehe anderes MI-REF
                        {
                            aItemsRec.NewContent((sal_uInt16)j, 'X' );

                            if ( pItem->GetRefCount() == SFX_ITEMS_SPECIAL )
                                rStream.WriteUInt16( (sal_uInt16) pItem->GetKind() );
                            else
                            {
                                rStream.WriteUInt16( (sal_uInt16) pItem->GetRefCount() );
                                if( pItem->GetRefCount() > SFX_ITEMS_OLD_MAXREF )
                                    rStream.SetError( ERRCODE_IO_NOTSTORABLEINBINARYFORMAT );
                            }

                            if ( !rStream.GetError() )
                                pItem->Store(rStream, nItemVersion);
                            else
                                break;
#ifdef DBG_UTIL_MI
                            if ( !pItem->ISA(SfxSetItem) )
                            {
                                sal_uLong nMark = rStream.Tell();
                                rStream.Seek( nItemStartPos + sizeof(sal_uInt16) );
                                SfxPoolItem *pClone = pItem->Create(rStream, nItemVersion );
                                sal_uInt16 nWh = pItem->Which();
                                SFX_ASSERT( rStream.Tell() == nMark, nWh,"asymmetric store/create" );
                                SFX_ASSERT( *pClone == *pItem, nWh, "unequal after store/create" );
                                delete pClone;
                            }
#endif
                        }
                    }
                }
            }
        }

        pImp->bInSetItem = false;
    }

    // die gesetzten Defaults speichern (Pool-Defaults)
    if ( !rStream.GetError() )
    {
        SfxMultiMixRecordWriter aDefsRec( &rStream, SFX_ITEMPOOL_REC_DEFAULTS, 0 );
        sal_uInt16 nCount = GetSize_Impl();
        for ( sal_uInt16 n = 0; n < nCount; ++n )
        {
            const SfxPoolItem* pDefaultItem = pImp->ppPoolDefaults[n];
            if ( pDefaultItem )
            {
                // Version ermitteln
                sal_uInt16 nItemVersion = pDefaultItem->GetVersion( pImp->mnFileFormatVersion );
                if ( USHRT_MAX == nItemVersion )
                    // => gab es in der Version noch nicht
                    continue;

                // eigene Kennung, globale Kennung, Version
                sal_uInt16 nSlotId = GetSlotId( pDefaultItem->Which(), false );
                aDefsRec.NewContent( nSlotId, 0 );
                rStream.WriteUInt16( pDefaultItem->Which() );
                rStream.WriteUInt16( nItemVersion );

                // Item an sich
                pDefaultItem->Store( rStream, nItemVersion );
            }
        }
    }

    // weitere Pools rausschreiben
    pStoringPool_ = 0;
    aPoolRec.Close();
    if ( !rStream.GetError() && pImp->mpSecondary )
        pImp->mpSecondary->Store( rStream );

    pImp->bStreaming = false;
    return rStream;
}

bool SfxItemPool::HasPersistentRefCounts() const
{
    return pImp->mbPersistentRefCounts;
}

void SfxItemPool::LoadCompleted()

/*  [Beschreibung]

    Wurde der SfxItemPool mit 'bRefCounts' == sal_False geladen, mu\s das
    Laden der Dokumentinhalte mit einem Aufruf dieser Methode beendet
    werden. Ansonsten hat der Aufruf dieser Methode keine Funktion.


    [Anmerkung]

    Beim Laden ohne Ref-Counts werden diese tats"achlich auf 1 gesetzt,
    damit nicht w"ahrend des Ladevorgangs SfxPoolItems gel"oscht werden,
    die danach, aber auch noch beim Ladevorgang, ben"otigt werden. Diese
    Methode setzt den Ref-Count wieder zur"uck und entfernt dabei
    gleichzeitig alle nicht mehr ben"otigten Items.


    [Querverweise]

    <SfxItemPool::Load()>
*/

{
    // wurden keine Ref-Counts mitgeladen?
    if ( pImp->nInitRefCount > 1 )
    {
        // "uber alle Which-Werte iterieren
        std::vector<SfxPoolItemArray_Impl*>::iterator itrItemArr = pImp->maPoolItems.begin();
        for( sal_uInt16 nArrCnt = GetSize_Impl(); nArrCnt; --nArrCnt, ++itrItemArr )
        {
            // ist "uberhaupt ein Item mit dem Which-Wert da?
            if ( *itrItemArr )
            {
                // "uber alle Items mit dieser Which-Id iterieren
                SfxPoolItemArrayBase_Impl::iterator ppHtArr = (*itrItemArr)->begin();
                for( size_t n = (*itrItemArr)->size(); n; --n, ++ppHtArr )
                {
                    if (*ppHtArr)
                    {
                        #ifdef DBG_UTIL
                        const SfxPoolItem &rItem = **ppHtArr;
                        DBG_ASSERT( !rItem.ISA(SfxSetItem) ||
                                    0 != &((const SfxSetItem&)rItem).GetItemSet(),
                                    "SetItem without ItemSet" );
                        #endif

                        if ( !ReleaseRef( **ppHtArr, 1 ) )
                            DELETEZ( *ppHtArr );
                    }
                }
                (*itrItemArr)->ReHash();
            }
        }

        // from now on normal initial ref count
        pImp->nInitRefCount = 1;
    }

    // notify secondary pool
    if ( pImp->mpSecondary )
        pImp->mpSecondary->LoadCompleted();
}

sal_uInt16 SfxItemPool::GetFirstWhich() const
{
    return pImp->mnStart;
}

sal_uInt16 SfxItemPool::GetLastWhich() const
{
    return pImp->mnEnd;
}

bool SfxItemPool::IsInRange( sal_uInt16 nWhich ) const
{
    return nWhich >= pImp->mnStart && nWhich <= pImp->mnEnd;
}

// This had to be moved to a method of its own to keep Solaris GCC happy:
void SfxItemPool_Impl::readTheItems (
    SvStream & rStream, sal_uInt32 nItemCount, sal_uInt16 nVer,
    SfxPoolItem * pDefItem, SfxPoolItemArray_Impl ** ppArr)
{
    SfxMultiRecordReader aItemsRec( &rStream, SFX_ITEMPOOL_REC_ITEMS );

    SfxPoolItemArray_Impl *pNewArr = new SfxPoolItemArray_Impl();
    SfxPoolItem *pItem = 0;

    sal_uLong n, nLastSurrogate = sal_uLong(-1);
    while (aItemsRec.GetContent())
    {
        // n"achstes Surrogat holen
        sal_uInt16 nSurrogate = aItemsRec.GetContentTag();
        DBG_ASSERT( aItemsRec.GetContentVersion() == 'X',
                    "not an item content" );

        // fehlende auff"ullen
        for ( pItem = 0, n = nLastSurrogate+1; n < nSurrogate; ++n )
            pNewArr->push_back( (SfxPoolItem*) pItem );
        nLastSurrogate = nSurrogate;

        // Ref-Count und Item laden
        sal_uInt16 nRef(0);
        rStream.ReadUInt16( nRef );

        pItem = pDefItem->Create(rStream, nVer);
        pNewArr->push_back( (SfxPoolItem*) pItem );

        if ( !mbPersistentRefCounts )
            // bis <SfxItemPool::LoadCompleted()> festhalten
            SfxItemPool::AddRef(*pItem, 1);
        else
        {
            if ( nRef > SFX_ITEMS_OLD_MAXREF )
                SfxItemPool::SetKind(*pItem, nRef);
            else
                SfxItemPool::AddRef(*pItem, nRef);
        }
    }

    // fehlende auff"ullen
    for ( pItem = 0, n = nLastSurrogate+1; n < nItemCount; ++n )
        pNewArr->push_back( (SfxPoolItem*) pItem );

    SfxPoolItemArray_Impl *pOldArr = *ppArr;
    *ppArr = pNewArr;

    // die Items merken, die schon im Pool sind
    bool bEmpty = true;
    if ( 0 != pOldArr )
        for ( n = 0; bEmpty && n < pOldArr->size(); ++n )
            bEmpty = pOldArr->operator[](n) == 0;
    DBG_ASSERTWARNING( bEmpty, "loading non-empty pool" );
    if ( !bEmpty )
    {
        // f"ur alle alten suchen, ob ein gleiches neues existiert
        for ( size_t nOld = 0; nOld < pOldArr->size(); ++nOld )
        {
            SfxPoolItem *pOldItem = (*pOldArr)[nOld];
            if ( pOldItem )
            {
                sal_uInt32 nFree = SAL_MAX_UINT32;
                bool bFound = false;
                for ( size_t nNew = (*ppArr)->size(); nNew--; )
                {
                    // geladenes Item
                    SfxPoolItem *&rpNewItem =
                        (SfxPoolItem*&)(*ppArr)->operator[](nNew);

                    // surrogat unbenutzt?
                    if ( !rpNewItem )
                        nFree = nNew;

                    // gefunden?
                    else if ( *rpNewItem == *pOldItem )
                    {
                        // wiederverwenden
                        SfxItemPool::AddRef( *pOldItem, rpNewItem->GetRefCount() );
                        SfxItemPool::SetRefCount( *rpNewItem, 0 );
                        delete rpNewItem;
                        rpNewItem = pOldItem;
                        bFound = true;
                        break;
                    }
                }

                // vorhervorhandene, nicht geladene uebernehmen
                if ( !bFound )
                {
                    if ( nFree != SAL_MAX_UINT32 )
                        (SfxPoolItem*&)(*ppArr)->operator[](nFree) = pOldItem;
                    else
                        (*ppArr)->push_back( (SfxPoolItem*) pOldItem );
                }
            }
        }
    }
    delete pOldArr;

    (*ppArr)->ReHash(); // paranoid
}

SvStream &SfxItemPool::Load(SvStream &rStream)
{
    DBG_ASSERT(pImp->ppStaticDefaults, "kein DefaultArray");

    // protect items by increasing ref count
    if ( !pImp->mbPersistentRefCounts )
    {

        // "uber alle Which-Werte iterieren
        std::vector<SfxPoolItemArray_Impl*>::iterator itrItemArr = pImp->maPoolItems.begin();
        for( size_t nArrCnt = GetSize_Impl(); nArrCnt; --nArrCnt, ++itrItemArr )
        {
            // ist "uberhaupt ein Item mit dem Which-Wert da?
            if ( *itrItemArr )
            {
                // "uber alle Items mit dieser Which-Id iterieren
                SfxPoolItemArrayBase_Impl::iterator ppHtArr = (*itrItemArr)->begin();
                for( size_t n = (*itrItemArr)->size(); n; --n, ++ppHtArr )
                    if (*ppHtArr)
                    {
                        #ifdef DBG_UTIL
                        const SfxPoolItem &rItem = **ppHtArr;
                        DBG_ASSERT( !rItem.ISA(SfxSetItem) ||
                                    0 != &((const SfxSetItem&)rItem).GetItemSet(),
                                    "SetItem without ItemSet" );
                        DBG_WARNING( "loading non-empty ItemPool" );
                        #endif

                        AddRef( **ppHtArr, 1 );
                    }
            }
        }

        // during loading (until LoadCompleted()) protect all items
        pImp->nInitRefCount = 2;
    }

    // Load-Master finden
    SfxItemPool *pLoadMaster = pImp->mpMaster != this ? pImp->mpMaster : 0;
    while ( pLoadMaster && !pLoadMaster->pImp->bStreaming )
        pLoadMaster = pLoadMaster->pImp->mpSecondary;

    // Gesamt Header einlesen
    pImp->bStreaming = true;
    if ( !pLoadMaster )
    {
        // Format-Version laden
        CHECK_FILEFORMAT2( rStream,
                SFX_ITEMPOOL_TAG_STARTPOOL_5, SFX_ITEMPOOL_TAG_STARTPOOL_4 );
        rStream.ReadUChar( pImp->nMajorVer ).ReadUChar( pImp->nMinorVer );

        // Format-Version in Master-Pool "ubertragen
        pImp->mpMaster->pImp->nMajorVer = pImp->nMajorVer;
        pImp->mpMaster->pImp->nMinorVer = pImp->nMinorVer;

        // altes Format?
        if ( pImp->nMajorVer < 2 )
            // pImp->bStreaming wird von Load1_Impl() zur"uckgesetzt
            return Load1_Impl( rStream );

        // zu neues Format?
        if ( pImp->nMajorVer > SFX_ITEMPOOL_VER_MAJOR )
        {
            rStream.SetError(SVSTREAM_FILEFORMAT_ERROR);
            pImp->bStreaming = false;
            return rStream;
        }

        // Version 1.2-Trick-Daten "uberspringen
        CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_TRICK4OLD );
        rStream.SeekRel( 4 ); // Hack-Daten wegen SfxStyleSheetPool-Bug  skippen
    }

    // neues Record-orientiertes Format
    SfxMiniRecordReader aPoolRec( &rStream, SFX_ITEMPOOL_REC );
    if ( rStream.GetError() )
    {
        pImp->bStreaming = false;
        return rStream;
    }

    // Einzel-Header
    OUString aExternName;
    {
        // Header-Record suchen
        SfxMiniRecordReader aPoolHeaderRec( &rStream, SFX_ITEMPOOL_REC_HEADER );
        if ( rStream.GetError() )
        {
            pImp->bStreaming = false;
            return rStream;
        }

        // Header-lesen
        rStream.ReadUInt16( pImp->nLoadingVersion );
        aExternName = SfxPoolItem::readByteString(rStream);
        bool bOwnPool = aExternName == pImp->aName;

        //! solange wir keine fremden Pools laden k"onnen
        if ( !bOwnPool )
        {
            rStream.SetError(SVSTREAM_FILEFORMAT_ERROR);
            aPoolRec.Skip();
            pImp->bStreaming = false;
            return rStream;
        }
    }

    // Version-Maps
    {
        SfxMultiRecordReader aVerRec( &rStream, SFX_ITEMPOOL_REC_VERSIONMAP );
        if ( rStream.GetError() )
        {
            pImp->bStreaming = false;
            return rStream;
        }

        // Versions-Maps einlesen
        sal_uInt16 nOwnVersion = pImp->nVersion;
        for ( sal_uInt16 nVerNo = 0; aVerRec.GetContent(); ++nVerNo )
        {
            // Header f"ur einzelne Version einlesen
            sal_uInt16 nVersion(0), nHStart(0), nHEnd(0);
            rStream.ReadUInt16( nVersion ).ReadUInt16( nHStart ).ReadUInt16( nHEnd );
            sal_uInt16 nCount = nHEnd - nHStart + 1;

            // Is new version is known?
            if ( nVerNo >= pImp->aVersions.size() )
            {
                // Add new Version
                sal_uInt16 *pMap = new sal_uInt16[nCount];
                memset(pMap, 0, nCount * sizeof(sal_uInt16));
                for ( sal_uInt16 n = 0; n < nCount; ++n )
                    rStream.ReadUInt16( pMap[n] );
                SetVersionMap( nVersion, nHStart, nHEnd, pMap );
            }
        }
        pImp->nVersion = nOwnVersion;
    }

    // Items laden
    bool bSecondaryLoaded = false;
    long nSecondaryEnd = 0;
    {
        SfxMultiRecordReader aWhichIdsRec( &rStream, SFX_ITEMPOOL_REC_WHICHIDS);
        while ( aWhichIdsRec.GetContent() )
        {
            // SlotId, Which-Id und Item-Version besorgen
            sal_uInt32 nCount(0);
            sal_uInt16 nVersion(0), nWhich(0);
            //!sal_uInt16 nSlotId = aWhichIdsRec.GetContentTag();
            rStream.ReadUInt16( nWhich );
            if ( pImp->nLoadingVersion != pImp->nVersion )
                // Which-Id aus File-Version in Pool-Version verschieben
                nWhich = GetNewWhich( nWhich );

            // unbekanntes Item aus neuerer Version
            if ( !IsInRange(nWhich) )
                continue;

            rStream.ReadUInt16( nVersion );
            rStream.ReadUInt32( nCount );
            //!SFX_ASSERTWARNING( !nSlotId || !HasMap() ||
            //!         ( nSlotId == GetSlotId( nWhich, sal_False ) ) ||
            //!         !GetSlotId( nWhich, sal_False ),
            //!         nWhich, "Slot/Which mismatch" );

            sal_uInt16 nIndex = GetIndex_Impl(nWhich);
            SfxPoolItemArray_Impl **ppArr = &pImp->maPoolItems[0] + nIndex;

            // SfxSetItems k"onnten Items aus Sekund"arpools beinhalten
            SfxPoolItem *pDefItem = *(pImp->ppStaticDefaults + nIndex);
            pImp->bInSetItem = pDefItem->ISA(SfxSetItem);
            if ( !bSecondaryLoaded && pImp->mpSecondary && pImp->bInSetItem )
            {
                // an das Ende des eigenen Pools seeken
                sal_uLong nLastPos = rStream.Tell();
                aPoolRec.Skip();

                // Sekund"arpool einlesen
                pImp->mpSecondary->Load( rStream );
                bSecondaryLoaded = true;
                nSecondaryEnd = rStream.Tell();

                // zur"uck zu unseren eigenen Items
                rStream.Seek(nLastPos);
            }

            // Items an sich lesen
            pImp->readTheItems(rStream, nCount, nVersion, pDefItem, ppArr);

            pImp->bInSetItem = false;
        }
    }

    // Pool-Defaults lesen
    {
        SfxMultiRecordReader aDefsRec( &rStream, SFX_ITEMPOOL_REC_DEFAULTS );

        while ( aDefsRec.GetContent() )
        {
            // SlotId, Which-Id und Item-Version besorgen
            sal_uInt16 nVersion(0), nWhich(0);
            //!sal_uInt16 nSlotId = aDefsRec.GetContentTag();
            rStream.ReadUInt16( nWhich );
            if ( pImp->nLoadingVersion != pImp->nVersion )
                // Which-Id aus File-Version in Pool-Version verschieben
                nWhich = GetNewWhich( nWhich );

            // unbekanntes Item aus neuerer Version
            if ( !IsInRange(nWhich) )
                continue;

            rStream.ReadUInt16( nVersion );
            //!SFX_ASSERTWARNING( !HasMap() || ( nSlotId == GetSlotId( nWhich, sal_False ) ),
            //!         nWhich, "Slot/Which mismatch" );

            // Pool-Default-Item selbst laden
            SfxPoolItem *pItem =
                    ( *( pImp->ppStaticDefaults + GetIndex_Impl(nWhich) ) )
                    ->Create( rStream, nVersion );
            pItem->SetKind( SFX_ITEMS_POOLDEFAULT );
            *( pImp->ppPoolDefaults + GetIndex_Impl(nWhich) ) = pItem;
        }
    }

    // ggf. Secondary-Pool laden
    aPoolRec.Skip();
    if ( pImp->mpSecondary )
    {
        if ( !bSecondaryLoaded )
            pImp->mpSecondary->Load( rStream );
        else
            rStream.Seek( nSecondaryEnd );
    }

    // wenn nicht own-Pool, dann kein Name
    if ( aExternName != pImp->aName )
        pImp->aName = OUString();

    pImp->bStreaming = false;
    return rStream;
};

sal_uInt16 SfxItemPool::GetIndex_Impl(sal_uInt16 nWhich) const
{
    DBG_ASSERT(nWhich >= pImp->mnStart && nWhich <= pImp->mnEnd, "Which-Id nicht im Pool-Bereich");
    return nWhich - pImp->mnStart;
}

sal_uInt16 SfxItemPool::GetSize_Impl() const
{
    return pImp->mnEnd - pImp->mnStart + 1;
}

SvStream &SfxItemPool::Load1_Impl(SvStream &rStream)
{
    // beim Master ist der Header schon von <Load()> geladen worden
    if ( !pImp->bStreaming )
    {
        // Header des Secondary lesen
        CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_STARTPOOL_4 );
        rStream.ReadUChar( pImp->nMajorVer ).ReadUChar( pImp->nMinorVer );
    }
    sal_uInt32 nAttribSize(0);
    OUString aExternName;
    if ( pImp->nMajorVer > 1 || pImp->nMinorVer >= 2 )
        rStream.ReadUInt16( pImp->nLoadingVersion );
    aExternName = SfxPoolItem::readByteString(rStream);
    bool bOwnPool = aExternName == pImp->aName;
    pImp->bStreaming = true;

    //! solange wir keine fremden laden k"onnen
    if ( !bOwnPool )
    {
        rStream.SetError(SVSTREAM_FILEFORMAT_ERROR);
        pImp->bStreaming = false;
        return rStream;
    }

    // Versionen bis 1.3 k"onnen noch keine Which-Verschiebungen lesen
    if ( pImp->nMajorVer == 1 && pImp->nMinorVer <= 2 &&
         pImp->nVersion < pImp->nLoadingVersion )
    {
        rStream.SetError(ERRCODE_IO_WRONGVERSION);
        pImp->bStreaming = false;
        return rStream;
    }

    // Size-Table liegt hinter den eigentlichen Attributen
    rStream.ReadUInt32( nAttribSize );

    // Size-Table einlesen
    sal_uLong nStartPos = rStream.Tell();
    rStream.SeekRel( nAttribSize );
    CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_SIZES );
    sal_uInt32 nSizeTableLen(0);
    rStream.ReadUInt32( nSizeTableLen );
    boost::scoped_array<sal_Char> pBuf(new sal_Char[nSizeTableLen]);
    rStream.Read( pBuf.get(), nSizeTableLen );
    sal_uLong nEndOfSizes = rStream.Tell();
    SvMemoryStream aSizeTable( pBuf.get(), nSizeTableLen, STREAM_READ );

    // ab Version 1.3 steht in der Size-Table eine Versions-Map
    if ( pImp->nMajorVer > 1 || pImp->nMinorVer >= 3 )
    {
        // Version-Map finden (letztes sal_uLong der Size-Table gibt Pos an)
        rStream.Seek( nEndOfSizes - sizeof(sal_uInt32) );
        sal_uInt32 nVersionMapPos(0);
        rStream.ReadUInt32( nVersionMapPos );
        rStream.Seek( nVersionMapPos );

        // Versions-Maps einlesen
        CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_VERSIONMAP );
        sal_uInt16 nVerCount(0);
        rStream.ReadUInt16( nVerCount );
        for ( sal_uInt16 nVerNo = 0; nVerNo < nVerCount; ++nVerNo )
        {
            // Header f"ur einzelne Version einlesen
            sal_uInt16 nVersion(0), nHStart(0), nHEnd(0);
            rStream.ReadUInt16( nVersion ).ReadUInt16( nHStart ).ReadUInt16( nHEnd );
            sal_uInt16 nCount = nHEnd - nHStart + 1;
            sal_uInt16 nBytes = (nCount)*sizeof(sal_uInt16);

            // Is new version is known?
            if ( nVerNo >= pImp->aVersions.size() )
            {
                // Add new Version
                sal_uInt16 *pMap = new sal_uInt16[nCount];
                memset(pMap, 0, nCount * sizeof(sal_uInt16));
                for ( sal_uInt16 n = 0; n < nCount; ++n )
                    rStream.ReadUInt16( pMap[n] );
                SetVersionMap( nVersion, nHStart, nHEnd, pMap );
            }
            else
                // Version schon bekannt => "uberspringen
                rStream.SeekRel( nBytes );
        }
    }

    // Items laden
    rStream.Seek( nStartPos );
    CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_ITEMS );
    bool bSecondaryLoaded = false;
    long nSecondaryEnd = 0;
    sal_uInt16 nWhich(0), nSlot(0);
    while ( rStream.ReadUInt16( nWhich ), nWhich )
    {
        // ggf. Which-Id aus alter Version verschieben?
        if ( pImp->nLoadingVersion != pImp->nVersion )
            nWhich = GetNewWhich( nWhich );

        rStream.ReadUInt16( nSlot );

        sal_uInt16 nRef(0), nCount(0), nVersion(0);
        sal_uInt32 nAttrSize(0);
        rStream.ReadUInt16( nVersion ).ReadUInt16( nCount );

        //!SFX_ASSERTWARNING( !nSlot || !HasMap() ||
        //!         ( nSlot == GetSlotId( nWhich, sal_False ) ) ||
        //!         !GetSlotId( nWhich, sal_False ),
        //!         nWhich, "Slot/Which mismatch" );

        sal_uInt16 nIndex = GetIndex_Impl(nWhich);
        std::vector<SfxPoolItemArray_Impl*>::iterator ppArr = pImp->maPoolItems.begin();
        std::advance(ppArr, nIndex);
        SfxPoolItemArray_Impl *pNewArr = new SfxPoolItemArray_Impl();
        SfxPoolItem *pDefItem = *(pImp->ppStaticDefaults + nIndex);

        // Position vor ersten Item merken
        sal_uLong nLastPos = rStream.Tell();

        // SfxSetItems k"onnten Items aus Sekund"arpools beinhalten
        if ( !bSecondaryLoaded && pImp->mpSecondary && pDefItem->ISA(SfxSetItem) )
        {
            // an das Ende des eigenen Pools seeken
            rStream.Seek(nEndOfSizes);
            CHECK_FILEFORMAT_RELEASE( rStream, SFX_ITEMPOOL_TAG_ENDPOOL, pNewArr );
            CHECK_FILEFORMAT_RELEASE( rStream, SFX_ITEMPOOL_TAG_ENDPOOL, pNewArr );

            // Sekund"arpool einlesen
            pImp->mpSecondary->Load1_Impl( rStream );
            bSecondaryLoaded = true;
            nSecondaryEnd = rStream.Tell();

            // zur"uck zu unseren eigenen Items
            rStream.Seek(nLastPos);
        }

        // Items an sich lesen
        for ( sal_uInt16 j = 0; j < nCount; ++j )
        {
            sal_uLong nPos = nLastPos;
            rStream.ReadUInt16( nRef );

            SfxPoolItem *pItem = 0;
            if ( nRef )
            {
                pItem = pDefItem->Create(rStream, nVersion);

                if ( !pImp->mbPersistentRefCounts )
                    // bis <SfxItemPool::LoadCompleted()> festhalten
                    AddRef(*pItem, 1);
                else
                {
                    if ( nRef > SFX_ITEMS_OLD_MAXREF )
                        pItem->SetKind( nRef );
                    else
                        AddRef(*pItem, nRef);
                }
            }
            //pNewArr->insert( pItem, j );
            pNewArr->push_back( (SfxPoolItem*) pItem );

            // restliche gespeicherte Laenge skippen (neueres Format)
            nLastPos = rStream.Tell();

            aSizeTable.ReadUInt32( nAttrSize );
            SFX_ASSERT( ( nPos + nAttrSize) >= nLastPos,
                        nPos,
                        "too many bytes read - version mismatch?" );

            if (nLastPos < (nPos + nAttrSize))
            {
                nLastPos = nPos + nAttrSize;
                rStream.Seek( nLastPos );
            }
        }

        SfxPoolItemArray_Impl *pOldArr = *ppArr;
        *ppArr = pNewArr;

        // die Items merken, die schon im Pool sind
        bool bEmpty = true;
        if ( 0 != pOldArr )
            for ( size_t n = 0; bEmpty && n < pOldArr->size(); ++n )
                bEmpty = pOldArr->operator[](n) == 0;
        DBG_ASSERTWARNING( bEmpty, "loading non-empty pool" );
        if ( !bEmpty )
        {
            // f"ur alle alten suchen, ob ein gleiches neues existiert
            for ( size_t nOld = 0; nOld < pOldArr->size(); ++nOld )
            {
                SfxPoolItem *pOldItem = (*pOldArr)[nOld];
                if ( pOldItem )
                {
                    bool bFound = false;
                    for ( size_t nNew = 0;
                          nNew < (*ppArr)->size();  ++nNew )
                    {
                        SfxPoolItem *&rpNewItem =
                            (SfxPoolItem*&)(*ppArr)->operator[](nNew);

                        if ( rpNewItem && *rpNewItem == *pOldItem )
                        {
                            AddRef( *pOldItem, rpNewItem->GetRefCount() );
                            SetRefCount( *rpNewItem, 0 );
                            delete rpNewItem;
                            rpNewItem = pOldItem;
                            bFound = true;
                            SAL_INFO("svl", "reusing item" << pOldItem);
                            break;
                        }
                    }
                    SAL_INFO_IF(
                        !bFound, "svl", "item not found: " << pOldItem);
                }
            }
        }
        delete pOldArr; /* @@@ */
    }

    // Pool-Defaults lesen
    if ( pImp->nMajorVer > 1 || pImp->nMinorVer > 0 )
        CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_DEFAULTS );

    sal_uLong nLastPos = rStream.Tell();
    while ( rStream.ReadUInt16( nWhich ), nWhich )
    {
        // ggf. Which-Id aus alter Version verschieben?
        if ( pImp->nLoadingVersion != pImp->nVersion )
            nWhich = GetNewWhich( nWhich );

        rStream.ReadUInt16( nSlot );

        sal_uLong nPos = nLastPos;
        sal_uInt32 nSize(0);
        sal_uInt16 nVersion(0);
        rStream.ReadUInt16( nVersion );

        SfxPoolItem *pItem =
            ( *( pImp->ppStaticDefaults + GetIndex_Impl(nWhich) ) )
            ->Create( rStream, nVersion );
        pItem->SetKind( SFX_ITEMS_POOLDEFAULT );
        *( pImp->ppPoolDefaults + GetIndex_Impl(nWhich) ) = pItem;

        nLastPos = rStream.Tell();
        aSizeTable.ReadUInt32( nSize );
        SFX_ASSERT( ( nPos + nSize) >= nLastPos, nPos,
                    "too many bytes read - version mismatch?" );
        if ( nLastPos < (nPos + nSize) )
            rStream.Seek( nPos + nSize );
    }

    pBuf.reset();
    rStream.Seek(nEndOfSizes);
    CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_ENDPOOL );
    CHECK_FILEFORMAT( rStream, SFX_ITEMPOOL_TAG_ENDPOOL );

    if ( pImp->mpSecondary )
    {
        if ( !bSecondaryLoaded )
            pImp->mpSecondary->Load1_Impl( rStream );
        else
            rStream.Seek( nSecondaryEnd );
    }

    if ( aExternName != pImp->aName )
        pImp->aName = OUString();

    pImp->bStreaming = false;
    return rStream;
}


const SfxPoolItem* SfxItemPool::LoadSurrogate
(
    SvStream&           rStream,    // vor einem Surrogat positionierter Stream
    sal_uInt16&             rWhich,     // Which-Id des zu ladenden <SfxPoolItem>s
    sal_uInt16              nSlotId,    // Slot-Id des zu ladenden <SfxPoolItem>s
    const SfxItemPool*  pRefPool    // <SfxItemPool> in dem das Surrogat gilt
)

/*  [Beschreibung]

    L"adt Surrogat aus 'rStream' und liefert das dadurch in 'rRefPool'
    repr"asentierte SfxPoolItem zu"ruck. Ist das im Stream befindliche
    Surrogat == SFX_ITEMS_DIRECT (!SFX_ITEM_POOLABLE) wird 0 zur"uckgegeben,
    das Item ist direkt aus dem Stream zu laden. Bei 0xfffffff0 (SFX_ITEMS_NULL)
    wird auch 0 zurueckgegeben und rWhich auf 0 gesetzt, das Item ist nicht
    verfuegbar.

    Ansonsten wird ber"ucksichtigt, ob der betroffene Pool ohne Ref-Counts
    geladen wird, ob aus einem neuen Pool nachgeladen wird (&rRefPool != this)
    oder ob aus einem g"anzlich anders aufgebauten Pool geladen wird.

    Wird aus einem anders aufgebauten Pool geladen und die 'nSlotId' kann
    nicht in eine Which-Id dieses Pools gemappt werden, wird ebenfalls 0
    zur"uckgeliefert.

    Preconditions:  - Pool mu\s geladen sein
                    - LoadCompleted darf noch nicht gerufen worden sein
                    - 'rStream' steht genau an der Position, an der ein
                      Surrogat f"ur ein Item mit der SlotId 'nSlotId' und
                      der WhichId 'rWhichId' mit StoreSurrogate gepeichert
                      wurde

    Postconditions: - 'rStream' ist so positioniert, wie auch StoreSurrogate
                      sein speichern beendet hatte
                    - konnte ein Item geladen werden, befindet es sich
                      in diesem SfxItemPool
                    - 'rWhichId' enth"alt die ggf. gemappte Which-Id
    Laufzeit:       Tiefe des Ziel Sekund"arpools * 10 + 10

    [Querverweise]

    <SfxItemPool::StoreSurrogate(SvStream&,const SfxPoolItem &)const>
*/

{
    // Read the first surrogate
    sal_uInt32 nSurrogat(0);
    rStream.ReadUInt32( nSurrogat );

    // Is item stored directly?
    if ( SFX_ITEMS_DIRECT == nSurrogat )
        return 0;

    // Item does not exist?
    if ( SFX_ITEMS_NULL == nSurrogat )
    {
        rWhich = 0;
        return 0;
    }

    // Bei einem identisch aufgebauten Pool (im Stream) kann das Surrogat
    // auf jeden Fall aufgel"ost werden.
    if ( !pRefPool )
        pRefPool = this;
    bool bResolvable = !pRefPool->GetName().isEmpty();
    if ( !bResolvable )
    {
        // Bei einem anders aufgebauten Pool im Stream, mu\s die SlotId
        // aus dem Stream in eine Which-Id gemappt werden k"onnen.
        sal_uInt16 nMappedWhich = nSlotId ? GetWhich(nSlotId, true) : 0;
        if ( IsWhich(nMappedWhich) )
        {
            // gemappte SlotId kann "ubernommen werden
            rWhich = nMappedWhich;
            bResolvable = true;
        }
    }

    // kann Surrogat aufgel"ost werden?
    if ( bResolvable )
    {
        const SfxPoolItem *pItem = 0;
        for ( SfxItemPool *pTarget = this; pTarget; pTarget = pTarget->pImp->mpSecondary )
        {
            // richtigen (Folge-) Pool gefunden?
            if ( pTarget->IsInRange(rWhich) )
            {
                // default attribute?
                if ( SFX_ITEMS_DEFAULT == nSurrogat )
                    return *(pTarget->pImp->ppStaticDefaults +
                            pTarget->GetIndex_Impl(rWhich));

                SfxPoolItemArray_Impl* pItemArr =
                    pTarget->pImp->maPoolItems[pTarget->GetIndex_Impl(rWhich)];
                pItem = pItemArr && nSurrogat < pItemArr->size()
                            ? (*pItemArr)[nSurrogat]
                            : 0;
                if ( !pItem )
                {
                    OSL_FAIL( "can't resolve surrogate" );
                    rWhich = 0; // nur zur Sicherheit fuer richtige Stream-Pos
                    return 0;
                }

                // Nachladen aus Ref-Pool?
                if ( pRefPool != pImp->mpMaster )
                    return &pTarget->Put( *pItem );

                // Referenzen sind NICHT schon mit Pool geladen worden?
                if ( !pTarget->HasPersistentRefCounts() )
                    AddRef( *pItem, 1 );
                else
                    return pItem;

                return pItem;
            }
        }

        SFX_ASSERT( false, rWhich, "can't resolve Which-Id in LoadSurrogate" );
    }

    return 0;
}



bool SfxItemPool::StoreSurrogate
(
    SvStream&           rStream,
    const SfxPoolItem*  pItem
)   const

/*  [Beschreibung]

    Speichert ein Surrogat f"ur '*pItem' in 'rStream'.


    [R"uckgabewert]

    bool                    TRUE
                            es wurde ein echtes Surrogat gespeichert, auch
                            SFX_ITEMS_NULL bei 'pItem==0',
                            SFX_ITEMS_STATICDEFAULT und SFX_ITEMS_POOLDEFAULT
                            gelten als 'echte' Surrogate

                            sal_False
                            es wurde ein Dummy-Surrogat (SFX_ITEMS_DIRECT)
                            gespeichert, das eigentliche Item mu\s direkt
                            hinterher selbst gespeichert werden
*/

{
    if ( pItem )
    {
        bool bRealSurrogate = IsItemFlag(*pItem, SFX_ITEM_POOLABLE);
        rStream.WriteUInt32(  bRealSurrogate
                        ? GetSurrogate( pItem )
                        : SFX_ITEMS_DIRECT  );
        return bRealSurrogate;
    }

    rStream.WriteUInt32( SFX_ITEMS_NULL );
    return true;
}



sal_uInt32 SfxItemPool::GetSurrogate(const SfxPoolItem *pItem) const
{
    DBG_ASSERT( pItem, "no 0-Pointer Surrogate" );
    DBG_ASSERT( !IsInvalidItem(pItem), "no Invalid-Item Surrogate" );
    DBG_ASSERT( !IsPoolDefaultItem(pItem), "no Pool-Default-Item Surrogate" );

    if ( !IsInRange(pItem->Which()) )
    {
        if ( pImp->mpSecondary )
            return pImp->mpSecondary->GetSurrogate( pItem );
        SFX_ASSERT( false, pItem->Which(), "unknown Which-Id - dont ask me for surrogates" );
    }

    // pointer on static or pool-default attribute?
    if( IsStaticDefaultItem(pItem) || IsPoolDefaultItem(pItem) )
        return SFX_ITEMS_DEFAULT;

    SfxPoolItemArray_Impl* pItemArr = pImp->maPoolItems[GetIndex_Impl(pItem->Which())];
    DBG_ASSERT(pItemArr, "ItemArr is not available");

    for ( size_t i = 0; i < pItemArr->size(); ++i )
    {
        const SfxPoolItem *p = (*pItemArr)[i];
        if ( p == pItem )
            return i;
    }
    SFX_ASSERT( false, pItem->Which(), "Item not in the pool");
    return SFX_ITEMS_NULL;
}


bool SfxItemPool::IsInStoringRange( sal_uInt16 nWhich ) const
{
    return nWhich >= pImp->nStoringStart &&
           nWhich <= pImp->nStoringEnd;
}


void SfxItemPool::SetStoringRange( sal_uInt16 nFrom, sal_uInt16 nTo )

/*  [Beschreibung]

    Mit dieser Methode kann der Which-Bereich eingeengt werden, der
    von ItemSets dieses Pool (und dem Pool selbst) gespeichert wird.
    Die Methode muss dazu vor <SfxItemPool::Store()> gerufen werden
    und die Werte muessen auch noch gesetzt sein, wenn das eigentliche
    Dokument (also die ItemSets gespeicher werden).

    Ein Zuruecksetzen ist dann nicht noetig, wenn dieser Range vor
    JEDEM Speichern richtig gesetzt wird, da er nur beim Speichern
    beruecksichtigt wird.

    Dieses muss fuer das 3.1-Format gemacht werden, da dort eine
    Bug in der Pool-Lade-Methode vorliegt.
*/

{
    pImp->nStoringStart = nFrom;
    pImp->nStoringEnd = nTo;
}



void SfxItemPool::SetVersionMap
(
    sal_uInt16  nVer,               /*  neue Versionsnummer */
    sal_uInt16  nOldStart,          /*  alte erste Which-Id */
    sal_uInt16  nOldEnd,            /*  alte letzte Which-Id */
    const sal_uInt16* pOldWhichIdTab /*  Array mit genau dem Aufbau der Which-Ids
                                    der vorhergehenden Version, in denen
                                    die jeweils neue Which-Id steht. */
)

/*  [Beschreibung]

    Mit dieser Methode k"onnen neue, inkompatible Which-Id-Folgen oder
    Verteilungen realisiert werden. Pools, die noch mit alten Versionen
    gespeichert wurden, werden dann "uber die angegebene Tabelle solange
    gemappt, bis die aktuelle Version erreicht ist. Neuere Pools k"onnen
    unter Verlust neuer Attribute geladen werden, da die Map mit dem Pool
    gespeichert wird.

    Precondition:   Pool darf noch nicht geladen sein
    Postcondition:  Which-Ids aus fr"uheren Versionen k"onnen bei Laden auf
                    Version 'nVer' gemappt werden
    Laufzeit:       1.5 * new + 10

    [Anmerkung]

    F"ur neue Which-Ranges (nStart,nEnd) m"ssen im Vergleich zur Vorg"anger-
    Version (nOldStart,nOldEnd) immer gelten, da\s (nOldStart,nOldEnd)
    vollst"andig in (nStart,nEnd) enthalten ist. Es ist also zul"assig, den
    Which-Range in beide Richtungen zu erweitern, auch durch Einf"ugung
    von Which-Ids, nicht aber ihn zu beschneiden.

    Diese Methode sollte nur im oder direkt nach Aufruf des Konstruktors
    gerufen werden.

    Das Array mu\s statisch sein, da es nicht kopiert wird und au\serdem
    im Copy-Ctor des SfxItemPool wiederverwendet wird.


    [Beispiel]

    Urspr"unglich (Version 0) hatte der Pool folgende Which-Ids:

        1:A, 2:B, 3:C, 4:D

    Nun soll eine neue Version (Version 1) zwei zus"atzliche Ids X und Y
    zwischen B und C erhalten, also wie folgt aussehen:

        1:A, 2:B, 3:X, 4:Y, 5:C, 6:D

    Dabei haben sich also die Ids 3 und 4 ge"andert. F"ur die neue Version
    m"u\ste am Pool folgendes gesetzt werden:

        static sal_uInt16 nVersion1Map = { 1, 2, 5, 6 };
        pPool->SetVersionMap( 1, 1, 4, &nVersion1Map );


    [Querverweise]

    <SfxItemPool::IsLoadingVersionCurrent()const>
    <SfxItemPool::GetNewWhich(sal_uInt16)>
    <SfxItemPool::GetVersion()const>
*/

{
    // create new map entry to insert
    const SfxPoolVersion_ImplPtr pVerMap = SfxPoolVersion_ImplPtr( new SfxPoolVersion_Impl(
                nVer, nOldStart, nOldEnd, pOldWhichIdTab ) );
    pImp->aVersions.push_back( pVerMap );

    DBG_ASSERT( nVer > pImp->nVersion, "Versions not sorted" );
    pImp->nVersion = nVer;

    // Versions-Range anpassen
    for ( sal_uInt16 n = 0; n < nOldEnd-nOldStart+1; ++n )
    {
        sal_uInt16 nWhich = pOldWhichIdTab[n];
        if ( nWhich < pImp->nVerStart )
        {
            if ( !nWhich )
                nWhich = 0;
            pImp->nVerStart = nWhich;
        }
        else if ( nWhich > pImp->nVerEnd )
            pImp->nVerEnd = nWhich;
    }
}



sal_uInt16 SfxItemPool::GetNewWhich
(
    sal_uInt16  nFileWhich      // die aus dem Stream geladene Which-Id
)   const

/*  [Beschreibung]

    Diese Methoden rechnet Which-Ids aus einem File-Format in die der
    aktuellen Pool-Version um. Ist das File-Format "alter, werden die vom
    Pool-Entwickler mit SetVersion() gesetzten Tabellen verwendet,
    ist das File-Format neuer, dann die aus dem File geladenen Tabellen.
    Im letzteren Fall kann ggf. nicht jede Which-Id gemappt werden,
    so da\s 0 zur"uckgeliefert wird.

    Die Berechnung ist nur f"ur Which-Ids definiert, die in der betreffenden
    File-Version unterst"utzt wurden. Dies ist per Assertion abgesichert.

    Precondition:   Pool mu\s geladen sein
    Postcondition:  unver"andert
    Laufzeit:       linear(Anzahl der Sekund"arpools) +
                    linear(Differenz zwischen alter und neuer Version)


    [Querverweise]

    <SfxItemPool::IsLoadingVersionCurrent()const>
    <SfxItemPool::SetVersionMap(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16*)>
    <SfxItemPool::GetVersion()const>
*/

{
    // (Sekund"ar-) Pool bestimmen
    if ( !IsInVersionsRange(nFileWhich) )
    {
        if ( pImp->mpSecondary )
            return pImp->mpSecondary->GetNewWhich( nFileWhich );
        SFX_ASSERT( false, nFileWhich, "unknown which in GetNewWhich()" );
    }

    // Version neuer/gleich/"alter?
    short nDiff = (short)pImp->nLoadingVersion - (short)pImp->nVersion;

    // Which-Id einer neueren Version?
    if ( nDiff > 0 )
    {
        // von der Top-Version bis runter zur File-Version stufenweise mappen
        for ( size_t nMap = pImp->aVersions.size(); nMap > 0; --nMap )
        {
            SfxPoolVersion_ImplPtr pVerInfo = pImp->aVersions[nMap-1];
            if ( pVerInfo->_nVer > pImp->nVersion )
            {   sal_uInt16 nOfs;
                sal_uInt16 nCount = pVerInfo->_nEnd - pVerInfo->_nStart + 1;
                for ( nOfs = 0;
                      nOfs <= nCount &&
                        pVerInfo->_pMap[nOfs] != nFileWhich;
                      ++nOfs )
                    continue;

                if ( pVerInfo->_pMap[nOfs] == nFileWhich )
                    nFileWhich = pVerInfo->_nStart + nOfs;
                else
                    return 0;
            }
            else
                break;
        }
    }

    // Which-Id einer neueren Version?
    else if ( nDiff < 0 )
    {
        // von der File-Version bis zur aktuellen Version stufenweise mappen
        for ( size_t nMap = 0; nMap < pImp->aVersions.size(); ++nMap )
        {
            SfxPoolVersion_ImplPtr pVerInfo = pImp->aVersions[nMap];
            if ( pVerInfo->_nVer > pImp->nLoadingVersion )
            {
                DBG_ASSERT( nFileWhich >= pVerInfo->_nStart &&
                            nFileWhich <= pVerInfo->_nEnd,
                            "which-id unknown in version" );
                nFileWhich = pVerInfo->_pMap[nFileWhich - pVerInfo->_nStart];
            }
        }
    }

    // originale (nDiff==0) bzw. gemappte (nDiff!=0) Id zur"uckliefern
    return nFileWhich;
}




bool SfxItemPool::IsInVersionsRange( sal_uInt16 nWhich ) const
{
    return nWhich >= pImp->nVerStart && nWhich <= pImp->nVerEnd;
}



bool SfxItemPool::IsCurrentVersionLoading() const

/*  [Beschreibung]

    Mit dieser Methode kann festgestellt werden, ob die geladene Pool-Version
    dem aktuellen Pool-Aufbau entspricht.

    Precondition:   Pool mu\s geladen sein
    Postcondition:  unver"andert
    Laufzeit:       linear(Anzahl der Sekund"arpools)


    [Querverweise]

    <SfxItemPool::SetVersionMap(sal_uInt16,sal_uInt16,sal_uInt16,sal_uInt16*)>
    <SfxItemPool::GetNewWhich(sal_uInt16)const>
    <SfxItemPool::GetVersion()const>
*/

{
    return ( pImp->nVersion == pImp->nLoadingVersion ) &&
           ( !pImp->mpSecondary || pImp->mpSecondary->IsCurrentVersionLoading() );
}



bool SfxItemPool::StoreItem( SvStream &rStream, const SfxPoolItem &rItem,
                                 bool bDirect ) const

/*  [Beschreibung]

    Speichert das <SfxPoolItem> 'rItem' in den <SvStream> 'rStream'
    entweder als Surrogat ('bDirect == sal_False') oder direkt mit 'rItem.Store()'.
    Nicht poolable Items werden immer direkt gespeichert. Items ohne Which-Id,
    also SID-Items, werden nicht gespeichert, ebenso wenn Items, die in der
    File-Format-Version noch nicht vorhanden waren (return sal_False).

    Das Item wird im Stream wie folgt abgelegt:

    sal_uInt16  rItem.Which()
    sal_uInt16  GetSlotId( rItem.Which() ) bzw. 0 falls nicht verf"urbar
    sal_uInt16  GetSurrogate( &rItem ) bzw. SFX_ITEM_DIRECT bei '!SFX_ITEM_POOLBLE'

    optional (falls 'bDirect == sal_True' oder '!rItem.IsPoolable()':

    sal_uInt16  rItem.GetVersion()
    sal_uLong   Size
    Size    rItem.Store()


    [Querverweise]

    <SfxItemPool::LoadItem(SvStream&,bool)const>
*/

{
    DBG_ASSERT( !IsInvalidItem(&rItem), "cannot store invalid items" );

    if ( IsSlot( rItem.Which() ) )
        return false;
    const SfxItemPool *pPool = this;
    while ( !pPool->IsInStoringRange(rItem.Which()) )
        if ( 0 == ( pPool = pPool->pImp->mpSecondary ) )
            return false;

    DBG_ASSERT( !pImp->bInSetItem || !rItem.ISA(SfxSetItem),
                "SetItem contains ItemSet with SetItem" );

    sal_uInt16 nSlotId = pPool->GetSlotId( rItem.Which(), true );
    sal_uInt16 nItemVersion = rItem.GetVersion(pImp->mnFileFormatVersion);
    if ( USHRT_MAX == nItemVersion )
        return false;

    rStream.WriteUInt16( rItem.Which() ).WriteUInt16( nSlotId );
    if ( bDirect || !pPool->StoreSurrogate( rStream, &rItem ) )
    {
        rStream.WriteUInt16( nItemVersion );
        rStream.WriteUInt32( (sal_uInt32) 0L );           // Platz fuer Laenge in Bytes
        sal_uLong nIStart = rStream.Tell();
        rItem.Store(rStream, nItemVersion);
        sal_uLong nIEnd = rStream.Tell();
        rStream.Seek( nIStart-4 );
        rStream.WriteInt32( (sal_Int32) ( nIEnd-nIStart ) );
        rStream.Seek( nIEnd );
    }

    return true;
}



const SfxPoolItem* SfxItemPool::LoadItem( SvStream &rStream, bool bDirect,
                                          const SfxItemPool *pRefPool )

// pRefPool==-1 => nicht putten!

{
    sal_uInt16 nWhich(0), nSlot(0); // nSurrogate;
    rStream.ReadUInt16( nWhich ).ReadUInt16( nSlot );

    bool bDontPut = (SfxItemPool*)-1 == pRefPool;
    if ( bDontPut || !pRefPool )
        pRefPool = this;

    // richtigen Sekund"ar-Pool finden
    while ( !pRefPool->IsInVersionsRange(nWhich) )
    {
        if ( pRefPool->pImp->mpSecondary )
            pRefPool = pRefPool->pImp->mpSecondary;
        else
        {
            // WID in der Version nicht vorhanden => ueberspringen
            sal_uInt32 nSurro(0);
            sal_uInt16 nVersion(0), nLen(0);
            rStream.ReadUInt32( nSurro );
            if ( SFX_ITEMS_DIRECT == nSurro )
            {
                rStream.ReadUInt16( nVersion ).ReadUInt16( nLen );
                rStream.SeekRel( nLen );
            }
            return 0;
        }
    }

    // wird eine andere Version geladen?
    bool bCurVersion = pRefPool->IsCurrentVersionLoading();
    if ( !bCurVersion )
        // Which-Id auf neue Version mappen
        nWhich = pRefPool->GetNewWhich( nWhich );

    DBG_ASSERT( !nWhich || !pImp->bInSetItem ||
                !pRefPool->pImp->ppStaticDefaults[pRefPool->GetIndex_Impl(nWhich)]->ISA(SfxSetItem),
                "loading SetItem in ItemSet of SetItem" );

    // soll "uber Surrogat geladen werden?
    const SfxPoolItem *pItem = 0;
    if ( !bDirect )
    {
        // Which-Id in dieser Version bekannt?
        if ( nWhich )
            // Surrogat laden, reagieren falls keins vorhanden
            pItem = LoadSurrogate( rStream, nWhich, nSlot, pRefPool );
        else
            // sonst "uberspringen
            rStream.SeekRel( sizeof(sal_uInt16) );
    }

    // wird direkt, also nicht "uber Surrogat geladen?
    if ( bDirect || ( nWhich && !pItem ) )
    {
        // bDirekt bzw. nicht IsPoolable() => Item direkt laden
        sal_uInt16 nVersion(0);
        sal_uInt32 nLen(0);
        rStream.ReadUInt16( nVersion ).ReadUInt32( nLen );
        sal_uLong nIStart = rStream.Tell();

        // Which-Id in dieser Version bekannt?
        if ( nWhich )
        {
            // Item direkt laden
            SfxPoolItem *pNewItem =
                    pRefPool->GetDefaultItem(nWhich).Create(rStream, nVersion);
            if ( bDontPut )
                pItem = pNewItem;
            else
                if ( pNewItem )
                {
                    pItem = &Put(*pNewItem);
                    delete pNewItem;
                }
                else
                    pItem = 0;
            sal_uLong nIEnd = rStream.Tell();
            DBG_ASSERT( nIEnd <= (nIStart+nLen), "read past end of item" );
            if ( (nIStart+nLen) != nIEnd )
                rStream.Seek( nIStart+nLen );
        }
        else
            // Item "uberspringen
            rStream.Seek( nIStart+nLen );
    }

    return pItem;
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */