Initial import into git.
[galago.git] / cpp / galago / src / DocumentNameReader.cpp
blobe702a1f0710b88dd9192343196de55e7eeb312cf
2 //
3 // DocumentNameReader
4 //
5 // 8 January 2007 -- tds
6 //
8 #include "DocumentNameReader.hpp"
10 void DocumentNameReader::getInSlot( std::string& result, const NameSlot& slot, int footerIndex ) const {
11 char buffer[1024];
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;
19 *start = '-';
20 *end = 0;
21 start++;
23 for( char* current = end-1; current >= start; current-- ) {
24 char digit = (footer % 10) + '0';
25 footer /= 10;
26 *current = digit;
29 result = buffer;
32 std::string DocumentNameReader::operator[] ( int index ) const {
33 return get(index);
36 std::string DocumentNameReader::get( int index ) const {
37 assert( index >= 0 );
38 assert( index < _documentCount );
40 if( index >= _documentCount )
41 return "unknown";
43 if( index < 0 )
44 return "unknown";
46 int big = _slots.size()-1;
47 int small = 0;
49 while( big-small > 1 ) {
50 int middle = small + (big-small)/2;
52 if( _slots[middle].offset >= index )
53 big = middle;
54 else
55 small = middle;
58 const NameSlot& one = _slots[small];
59 const NameSlot& two = _slots[big];
60 std::string result;
62 if (two.offset <= index)
63 getInSlot(result, two, index);
64 else
65 getInSlot(result, one, index);
67 return result;
70 void DocumentNameReader::read( const std::string& filename ) {
71 int offset = 0;
73 // mmap the file
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 );
93 NameSlot slot;
94 slot.footerWidth = footerWidth;
95 slot.offset = offset;
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();
105 delete[] prefixData;
108 _documentCount = offset;
110 delete mapping;
111 inputFile.close();