MCPcopy Create free account
hub / github.com/diasurgical/devilution / Block4Stream_Open

Function Block4Stream_Open

3rdParty/StormLib/src/FileStream.cpp:2224–2336  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2222}
2223
2224static TFileStream * Block4Stream_Open(const TCHAR * szFileName, DWORD dwStreamFlags)
2225{
2226 TBaseProviderData * NewBaseArray = NULL;
2227 ULONGLONG RemainderBlock;
2228 ULONGLONG BlockCount;
2229 ULONGLONG FileSize;
2230 TBlockStream * pStream;
2231 TCHAR * szNameBuff;
2232 size_t nNameLength;
2233 DWORD dwBaseFiles = 0;
2234 DWORD dwBaseFlags;
2235
2236 // Create new empty stream
2237 pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
2238 if(pStream == NULL)
2239 return NULL;
2240
2241 // Sanity check
2242 assert(pStream->BaseOpen != NULL);
2243
2244 // Get the length of the file name without numeric suffix
2245 nNameLength = _tcslen(pStream->szFileName);
2246 if(pStream->szFileName[nNameLength - 2] == '.' && pStream->szFileName[nNameLength - 1] == '0')
2247 nNameLength -= 2;
2248 pStream->szFileName[nNameLength] = 0;
2249
2250 // Supply the stream functions
2251 pStream->StreamRead = (STREAM_READ)BlockStream_Read;
2252 pStream->StreamGetSize = BlockStream_GetSize;
2253 pStream->StreamGetPos = BlockStream_GetPos;
2254 pStream->StreamClose = (STREAM_CLOSE)Block4Stream_Close;
2255 pStream->BlockRead = (BLOCK_READ)Block4Stream_BlockRead;
2256
2257 // Allocate work space for numeric names
2258 szNameBuff = STORM_ALLOC(TCHAR, nNameLength + 4);
2259 if(szNameBuff != NULL)
2260 {
2261 // Set the base flags
2262 dwBaseFlags = (dwStreamFlags & STREAM_PROVIDERS_MASK) | STREAM_FLAG_READ_ONLY;
2263
2264 // Go all suffixes from 0 to 30
2265 for(int nSuffix = 0; nSuffix < 30; nSuffix++)
2266 {
2267 // Open the n-th file
2268 _stprintf(szNameBuff, _T("%s.%u"), pStream->szFileName, nSuffix);
2269 if(!pStream->BaseOpen(pStream, szNameBuff, dwBaseFlags))
2270 break;
2271
2272 // If the open succeeded, we re-allocate the base provider array
2273 NewBaseArray = STORM_ALLOC(TBaseProviderData, dwBaseFiles + 1);
2274 if(NewBaseArray == NULL)
2275 {
2276 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2277 return NULL;
2278 }
2279
2280 // Copy the old base data array to the new base data array
2281 if(pStream->FileBitmap != NULL)

Callers 1

FileStream_OpenFileFunction · 0.85

Calls 3

AllocateFileStreamFunction · 0.85
SetLastErrorFunction · 0.85
FileStream_CloseFunction · 0.85

Tested by

no test coverage detected