tracy/server/TracyFileRead.hpp

59 lines
1017 B
C++
Raw Normal View History

2017-09-30 14:19:50 +00:00
#ifndef __TRACYFILEREAD_HPP__
#define __TRACYFILEREAD_HPP__
2017-09-30 16:43:39 +00:00
#include <algorithm>
2017-09-30 14:19:50 +00:00
#include <stdio.h>
namespace tracy
{
class FileRead
{
public:
static FileRead* Open( const char* fn )
{
auto f = fopen( fn, "rb" );
return f ? new FileRead( f ) : nullptr;
}
~FileRead()
{
fclose( m_file );
}
2017-09-30 16:43:39 +00:00
void Read( void* ptr, size_t size )
2017-09-30 14:19:50 +00:00
{
2017-09-30 16:43:39 +00:00
auto dst = (char*)ptr;
while( size > 0 )
{
if( m_offset == BufSize )
{
fread( m_buf, 1, BufSize, m_file );
m_offset = 0;
}
const auto sz = std::min( size, BufSize - m_offset );
memcpy( dst, m_buf + m_offset, sz );
m_offset += sz;
dst += sz;
size -= sz;
}
2017-09-30 14:19:50 +00:00
}
private:
2017-09-30 16:43:39 +00:00
FileRead( FILE* f )
: m_file( f )
, m_offset( BufSize )
{}
enum { BufSize = 64 * 1024 };
2017-09-30 14:19:50 +00:00
FILE* m_file;
2017-09-30 16:43:39 +00:00
char m_buf[BufSize];
size_t m_offset;
2017-09-30 14:19:50 +00:00
};
}
#endif