3 RTIReader::RTIReader(string Filename)
6 m_pFile = fopen(Filename.c_str(), "rb");
7 FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb");
10 // Load the index file
11 unsigned int nIndexFileLen = GetFileLen(pFileIndex);
12 unsigned int nFileLen = GetFileLen(m_pFile);
13 unsigned int nTotalChainCount = nFileLen / 8;
14 if (nFileLen % 8 != 0)
15 printf("file length mismatch (%u bytes)\n", nFileLen);
19 if (nIndexFileLen % 11 != 0)
20 printf("index file length mismatch (%u bytes)\n", nIndexFileLen);
23 m_pIndex = new IndexChain[nIndexFileLen / 11];
24 memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11));
25 fseek(pFileIndex, 0, SEEK_SET);
28 for(nRows = 0; (nRows * 11) < nIndexFileLen; nRows++)
30 if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break;
31 if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break;
32 if(fread(&m_pIndex[nRows].nChainCount, 2, 1, pFileIndex) != 1) break;
33 // Index checking part
34 if(nRows != 0 && m_pIndex[nRows].nFirstChain < m_pIndex[nRows-1].nFirstChain)
36 printf("Corrupted index detected (FirstChain is lower than previous)\n");
39 else if(nRows != 0 && m_pIndex[nRows].nFirstChain != m_pIndex[nRows-1].nFirstChain + m_pIndex[nRows-1].nChainCount)
41 printf("Corrupted index detected (LastChain + nChainCount != FirstChain)\n");
47 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount + 1 <= nTotalChainCount) // +1 is needed.
49 printf("Corrupted index detected: Not covering the entire file\n");
52 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here
54 printf("Corrupted index detected: The index is covering more than the file\n");
58 /* if(nIndexSize != pIndex[i].nFirstChain + pIndex[i].nChainCount)
60 printf("Index is not covering the entire tables\n");
63 // printf("debug: Index loaded successfully (%u entries)\n", nIndexSize);
70 int RTIReader::ReadChains(unsigned int &numChains, RainbowChainCP *pData)
72 // We HAVE to reset the data to 0x00's or we will get in trouble
73 memset(pData, 0x00, sizeof(RainbowChainCP) * numChains);
74 unsigned int readChains = 0;
75 unsigned int chainsleft = GetChainsLeft();
76 for(int i = 0; i < m_nIndexSize; i++)
78 if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index
80 while(m_chainPosition + readChains < m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount)
82 pData[readChains].nIndexE = m_pIndex[i].nPrefix << 16;
83 int endpoint = 0; // We have to set it to 0
84 fread(&pData[readChains].nIndexS, 6, 1, m_pFile);
85 fread(&endpoint, 2, 1, m_pFile);
86 pData[readChains].nIndexE += endpoint;
88 if(readChains == numChains || readChains == chainsleft) break;
90 if(readChains == numChains) break;
92 if(readChains != numChains) numChains = readChains; // Update how many chains we read
93 m_chainPosition += readChains;
97 unsigned int RTIReader::GetChainsLeft()
99 int len = GetFileLen(m_pFile) / 8 - m_chainPosition;
103 RTIReader::~RTIReader(void)