1 #include "RTI2Reader.h"
4 RTI2Header *RTI2Reader::m_pHeader = NULL;
5 RTI2Reader::RTI2Reader(string Filename)
7 //m_pIndexPos = NULL, m_pChainPos = NULL;;
9 m_pFile = fopen(Filename.c_str(), "rb");
12 printf("Unable to open file %s", Filename.c_str());
15 FILE *pFileIndex = fopen(Filename.append(".index").c_str(), "rb");
16 if(pFileIndex == NULL)
18 printf("Unable to open file %s", Filename.append(".index").c_str());
23 unsigned int len = GetFileLen(pFileIndex);
24 fseek(pFileIndex, 0, SEEK_SET);
26 m_pIndex = new unsigned char[len];
27 if(fread(m_pIndex, 1, len, pFileIndex) != len)
29 printf("Error while reading index file");
33 m_pHeader = new RTI2Header();
34 memcpy(m_pHeader, m_pIndex, sizeof(RTI2Header));
35 m_pHeader->m_cppos = (unsigned int*)(m_pIndex + 8);
36 m_pHeader->prefixstart = *(uint64*)(m_pIndex + 8 + (m_pHeader->rti_cplength * 4));
37 m_chainsizebytes = ceil((float)(m_pHeader->rti_startptlength + m_pHeader->rti_endptlength + m_pHeader->rti_cplength) / 8); // Get the size of each chain in bytes
38 m_indexrowsizebytes = ceil((float)m_pHeader->rti_index_numchainslength / 8);
40 fseek(m_pFile, 0, SEEK_END);
42 fseek(m_pFile, 0, SEEK_SET);
43 if(len % m_chainsizebytes > 0)
45 printf("Invalid filesize %u\n", len);
52 RTI2Reader::~RTI2Reader(void)
54 if(m_pIndex != NULL) delete m_pIndex;
55 if(m_pFile != NULL) fclose(m_pFile);
59 unsigned int RTI2Reader::GetChainsLeft()
61 int len = GetFileLen(m_pFile);
62 return len / m_chainsizebytes - m_chainPosition;
65 int RTI2Reader::ReadChains(unsigned int &numChains, RainbowChainO *pData)
67 if(strncmp(m_pHeader->header, "RTI2", 4) != 0)
72 unsigned char *pNumChains = m_pIndex + (m_pHeader->rti_cplength * 4) + 16; // Pointer into the index containing info about how many numbers are in the first chain prefix
74 unsigned int indexRow = 0; // Current offset into the index
75 unsigned int curRowPosition = 0;
77 while(true) // Fast forward to current position
79 // ALERT: Possible problem here if m_indexrowsizebytes > 1 as pNumChains is a unsigned char.
80 unsigned int NumChainsInRow = (unsigned int)*(pNumChains + indexRow * m_indexrowsizebytes);
81 if(m_indexrowsizebytes > 1) { printf("Have to find a solution to this problem"); exit(2);}
82 if(i + NumChainsInRow > m_chainPosition)
84 curRowPosition = m_chainPosition - i;
85 break; // The current position is somewhere within this prefix
91 uint64 chainrow = 0; // Buffer to store a single read chain
92 unsigned int chainsProcessed = 0; // Number of chains processed
94 // ALERT: same problem with unsigned char here.
95 unsigned int NumChainsInRow = *(pNumChains + indexRow);
96 while(chainsProcessed < numChains && fread(&chainrow, 1, m_chainsizebytes, m_pFile) == m_chainsizebytes)
98 if(curRowPosition >= NumChainsInRow)
99 { // Skip to next index row position
102 NumChainsInRow = *(pNumChains + indexRow);
104 while(NumChainsInRow == 0) // We skip forward until we hit a index with > 0 chains
107 NumChainsInRow = *(pNumChains + indexRow);
110 // Load the starting point from the data
111 pData[chainsProcessed].nIndexS = chainrow << 64 - m_pHeader->rti_startptlength;
112 pData[chainsProcessed].nIndexS = pData[chainsProcessed].nIndexS >> 64 - m_pHeader->rti_startptlength;
114 // Load the ending point prefix
115 pData[chainsProcessed].nIndexE = m_pHeader->prefixstart + indexRow << m_pHeader->rti_endptlength;
116 // Append the ending point suffix
117 pData[chainsProcessed].nIndexE |= (chainrow & (0xFFFFFFFFFFFFFFFF >> m_pHeader->rti_cplength)) >> m_pHeader->rti_startptlength;
118 //pData[chainsProcessed].nCheckPoint = (chainrow >> m_pHeader->rti_startptlength + m_pHeader->rti_endptlength);
122 numChains = chainsProcessed;
123 m_chainPosition += numChains;