Created
April 22, 2018 12:20
-
-
Save linnet8989/e806102afd01d62d777165b8344f09ef to your computer and use it in GitHub Desktop.
Read Unknown Size Data For File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example for pfnRead | |
int ReadThunk(int* pData, FILE* pFile) | |
{ | |
fread(pData, sizeof(*pData), 1, pFile); | |
// EOF ? | |
if (*pData == 0) | |
{ | |
return 0; | |
} | |
} | |
template<typename T> | |
int ReadUnknownSizeForFile<T>( | |
T*& pDatas, int& count, | |
int(*pfnRead)(T*, FILE*), FILE* pFile, | |
unsigned int maxCount = -1, unsigned int segSize = 256 | |
) | |
{ | |
int ret = 0; | |
int segCount = 0; | |
pDatas = new T[segSize]; | |
count = 0; | |
while (count >= segSize * segCount++) | |
{ | |
for (; count < segSize * segCount; count++) | |
{ | |
if (count > maxCount) | |
{ | |
// -1: Count Over Range | |
return -1; | |
} | |
ret = pfnRead(&pDatas[count], pFile); | |
if (ret != 1) // 1: pfnRead OK | |
{ | |
// Exception Occurred | |
return ret; | |
} | |
} | |
T* pBuf = pDatas; | |
pDatas = new T[segSize * segCount]; | |
memcpy(pDatas, pBuf, sizeof(T) * segSize * (segCount - 1)); | |
delete[] pBuf; | |
} | |
// 0: OK | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment