Skip to content

Instantly share code, notes, and snippets.

@jkoenen
Created August 10, 2016 09:35
Show Gist options
  • Save jkoenen/5840a50292b05962c4e209d5b2c341de to your computer and use it in GitHub Desktop.
Save jkoenen/5840a50292b05962c4e209d5b2c341de to your computer and use it in GitHub Desktop.
AsyncOp<void*> async_read_compressed_file( const char* pFileName )
{
// async file open:
AsyncOp<FileHandle> openOp = async_open( pFileName );
// when the open is done: read the header:
openOp.wait(); // ????
FileHandle handle = openOp.result;
FileHeader header; // automatic storage ??
AsyncOp<size_t> readOp = async_read( handle, 0u, sizeof( header ) );
readOp.wait(); // ??
//allocate some memory:
uint8* pResult = (uint8*)malloc( header.uncompressedSize )
// until done: read block+decompress
uint64 fileOffset = sizeof( header );
size_t blockSize = ...; // details..
while( fileOffset < sizeof( header ) + header.compressedSize )
{
AsyncOp<size_t> readBlockOp = async_read( handle, pTarget, fileOffset, blockSize );
readBlockOp.wait();
AsyncOp<size_t> decompressBlockOp = async_decompress_inplace( pTarget, blockSize );
decompressBlockOp.wait();
size_t decompressedSize = decompressBlockOp.result;
fileOffset += blockSize;
pTarget += decompressedSize;
}
return createAsyncOp<void*>( pResult );
}
int main()
{
....
AsyncOp<void*> readFile0 = async_read_compressed_file( "Test1.dat" );
AsyncOp<void*> readFile1 = async_read_compressed_file( "Test2.dat" );
uint doneCount = 0u;
while( doneCount < 2u )
{
if( readFile0.isDone() )
{
// do something with the result..
delete readFile0.result;
doneCount++;
}
if( readFile1.isDone() )
{
// do something with the result..
delete readFile1.result;
doneCount++;
}
}
// query result:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment