3 RTIReader::RTIReader(string Filename)
6 m_pFile = fopen(Filename.c_str(), "rb");
8 printf("could not open file %s\n", Filename.c_str());
11 string sIndex = Filename.append(".index").c_str();
12 FILE *pFileIndex = fopen(sIndex.c_str(), "rb");
13 if(pFileIndex == NULL) {
14 printf("could not open index file %s\n", sIndex.c_str());
19 // Load the index file
20 unsigned int nIndexFileLen = GetFileLen(pFileIndex);
21 unsigned int nFileLen = GetFileLen(m_pFile);
22 unsigned int nTotalChainCount = nFileLen / 8;
23 if (nFileLen % 8 != 0)
24 printf("file length mismatch (%u bytes)\n", nFileLen);
28 if (nIndexFileLen % 11 != 0)
29 printf("index file length mismatch (%u bytes)\n", nIndexFileLen);
32 m_pIndex = new IndexChain[nIndexFileLen / 11];
33 memset(m_pIndex, 0x00, sizeof(IndexChain) * (nIndexFileLen / 11));
34 fseek(pFileIndex, 0, SEEK_SET);
37 for(nRows = 0; (nRows * 11) < nIndexFileLen; nRows++)
39 if(fread(&m_pIndex[nRows].nPrefix, 5, 1, pFileIndex) != 1) break;
40 if(fread(&m_pIndex[nRows].nFirstChain, 4, 1, pFileIndex) != 1) break;
41 if(fread(&m_pIndex[nRows].nChainCount, 2, 1, pFileIndex) != 1) break;
42 // Index checking part
43 if(nRows != 0 && m_pIndex[nRows].nFirstChain < m_pIndex[nRows-1].nFirstChain)
45 printf("Corrupted index detected (FirstChain is lower than previous)\n");
48 else if(nRows != 0 && m_pIndex[nRows].nFirstChain != m_pIndex[nRows-1].nFirstChain + m_pIndex[nRows-1].nChainCount)
50 printf("Corrupted index detected (LastChain + nChainCount != FirstChain)\n");
56 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount + 1 <= nTotalChainCount) // +1 is needed.
58 printf("Corrupted index detected: Not covering the entire file\n");
61 if(m_pIndex[m_nIndexSize - 1].nFirstChain + m_pIndex[m_nIndexSize - 1].nChainCount > nTotalChainCount) // +1 is not needed here
63 printf("Corrupted index detected: The index is covering more than the file\n");
67 /* if(nIndexSize != pIndex[i].nFirstChain + pIndex[i].nChainCount)
69 printf("Index is not covering the entire tables\n");
72 // printf("debug: Index loaded successfully (%u entries)\n", nIndexSize);
79 int RTIReader::ReadChains(unsigned int &numChains, RainbowChainCP *pData)
81 // We HAVE to reset the data to 0x00's or we will get in trouble
82 memset(pData, 0x00, sizeof(RainbowChainCP) * numChains);
83 unsigned int readChains = 0;
84 unsigned int chainsleft = GetChainsLeft();
85 for(int i = 0; i < m_nIndexSize; i++)
87 if(m_chainPosition + readChains > m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount) // We found the matching index
89 while(m_chainPosition + readChains < m_pIndex[i].nFirstChain + m_pIndex[i].nChainCount)
91 pData[readChains].nIndexE = m_pIndex[i].nPrefix << 16;
92 int endpoint = 0; // We have to set it to 0
93 fread(&pData[readChains].nIndexS, 6, 1, m_pFile);
94 fread(&endpoint, 2, 1, m_pFile);
95 pData[readChains].nIndexE += endpoint;
97 if(readChains == numChains || readChains == chainsleft) break;
99 if(readChains == numChains) break;
101 if(readChains != numChains) numChains = readChains; // Update how many chains we read
102 m_chainPosition += readChains;
106 unsigned int RTIReader::GetChainsLeft()
108 int len = GetFileLen(m_pFile) / 8 - m_chainPosition;
112 RTIReader::~RTIReader(void)