5 // 8 January 2007 -- tds
8 #include "DocumentNameReader.hpp"
10 void DocumentNameReader::getInSlot( std::string
& result
, const NameSlot
& slot
, int footerIndex
) const {
12 int footer
= slot
.footers
[footerIndex
-slot
.offset
];
14 strncpy( buffer
, slot
.prefix
.c_str(), sizeof buffer
);
16 char* start
= buffer
+ slot
.prefix
.size();
17 char* end
= start
+ slot
.footerWidth
+ 1;
23 for( char* current
= end
-1; current
>= start
; current
-- ) {
24 char digit
= (footer
% 10) + '0';
32 std::string
DocumentNameReader::operator[] ( int index
) const {
36 std::string
DocumentNameReader::get( int index
) const {
38 assert( index
< _documentCount
);
40 if( index
>= _documentCount
)
46 int big
= _slots
.size()-1;
49 while( big
-small
> 1 ) {
50 int middle
= small
+ (big
-small
)/2;
52 if( _slots
[middle
].offset
>= index
)
58 const NameSlot
& one
= _slots
[small
];
59 const NameSlot
& two
= _slots
[big
];
62 if (two
.offset
<= index
)
63 getInSlot(result
, two
, index
);
65 getInSlot(result
, one
, index
);
70 void DocumentNameReader::read( const std::string
& filename
) {
74 indri::file::File inputFile
;
76 inputFile
.openRead( filename
);
77 UINT64 length
= inputFile
.size();
78 indri::file::FileMapping
* mapping
= inputFile
.mapFileRead();
79 const UINT8
* region
= (const UINT8
*) mapping
->region
;
80 const UINT8
* end
= region
+ length
;
82 while( region
< end
) {
83 UINT32 prefixLength
= UncompressedRead::read_u32( region
);
85 char* prefixData
= new char[prefixLength
+1];
86 memcpy( prefixData
, region
, prefixLength
);
87 prefixData
[prefixLength
] = 0;
88 region
+= prefixLength
;
90 UINT32 footerWidth
= UncompressedRead::read_u32( region
);
91 UINT32 footerCount
= UncompressedRead::read_u32( region
);
94 slot
.footerWidth
= footerWidth
;
96 slot
.prefix
= prefixData
;
98 for( int i
=0; i
<footerCount
; i
++ ) {
99 UINT32 footer
= UncompressedRead::read_u32( region
);
100 slot
.footers
.push_back( footer
);
103 _slots
.push_back(slot
);
104 offset
+= slot
.footers
.size();
108 _documentCount
= offset
;