Save/load annotations.

This commit is contained in:
Bartosz Taudul 2019-10-13 16:29:24 +02:00
parent c2f38d0db7
commit e462335f83
3 changed files with 78 additions and 0 deletions

View File

@ -11,9 +11,11 @@ namespace tracy
constexpr auto FileDescription = "description";
constexpr auto FileTimeline = "timeline";
constexpr auto FileOptions = "options";
constexpr auto FileAnnotations = "annotations";
enum : uint32_t { VersionTimeline = 0 };
enum : uint32_t { VersionOptions = 2 };
enum : uint32_t { VersionAnnotations = 0 };
UserData::UserData()
: m_preserveState( false )
@ -143,6 +145,74 @@ void UserData::StateShouldBePreserved()
m_preserveState = true;
}
void UserData::LoadAnnotations( std::vector<std::unique_ptr<Annotation>>& data )
{
assert( Valid() );
FILE* f = OpenFile( FileAnnotations, false );
if( f )
{
uint32_t ver;
fread( &ver, 1, sizeof( ver ), f );
if( ver == VersionAnnotations )
{
uint32_t sz;
fread( &sz, 1, sizeof( sz ), f );
for( uint32_t i=0; i<sz; i++ )
{
auto ann = std::make_unique<Annotation>();
uint32_t tsz;
fread( &tsz, 1, sizeof( tsz ), f );
if( tsz != 0 )
{
char buf[1024];
assert( tsz < 1024 );
fread( buf, 1, tsz, f );
ann->text.assign( buf, tsz );
}
fread( &ann->start, 1, sizeof( ann->start ), f );
fread( &ann->end, 1, sizeof( ann->end ), f );
fread( &ann->color, 1, sizeof( ann->color ), f );
data.emplace_back( std::move( ann ) );
}
}
fclose( f );
}
}
void UserData::SaveAnnotations( const std::vector<std::unique_ptr<Annotation>>& data )
{
if( !m_preserveState ) return;
if( data.empty() )
{
Remove( FileAnnotations );
return;
}
assert( Valid() );
FILE* f = OpenFile( FileAnnotations, true );
if( f )
{
uint32_t ver = VersionAnnotations;
fwrite( &ver, 1, sizeof( ver ), f );
uint32_t sz = uint32_t( data.size() );
fwrite( &sz, 1, sizeof( sz ), f );
for( auto& ann : data )
{
sz = uint32_t( ann->text.size() );
fwrite( &sz, 1, sizeof( sz ), f );
if( sz != 0 )
{
fwrite( ann->text.c_str(), 1, sz, f );
}
fwrite( &ann->start, 1, sizeof( ann->start ), f );
fwrite( &ann->end, 1, sizeof( ann->end ), f );
fwrite( &ann->color, 1, sizeof( ann->color ), f );
}
fclose( f );
}
}
FILE* UserData::OpenFile( const char* filename, bool write )
{
const auto path = GetSavePath( m_program.c_str(), m_time, filename, write );

View File

@ -1,13 +1,16 @@
#ifndef __TRACYUSERDATA_HPP__
#define __TRACYUSERDATA_HPP__
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <vector>
namespace tracy
{
struct Annotation;
struct ViewData;
class UserData
@ -26,6 +29,9 @@ public:
void SaveState( const ViewData& data );
void StateShouldBePreserved();
void LoadAnnotations( std::vector<std::unique_ptr<Annotation>>& data );
void SaveAnnotations( const std::vector<std::unique_ptr<Annotation>>& data );
private:
FILE* OpenFile( const char* filename, bool write );
void Remove( const char* filename );

View File

@ -151,12 +151,14 @@ View::View( FileRead& f, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont,
SetViewToLastFrames();
m_userData.StateShouldBePreserved();
m_userData.LoadState( m_vd );
m_userData.LoadAnnotations( m_annotations );
}
View::~View()
{
m_worker.Shutdown();
m_userData.SaveState( m_vd );
m_userData.SaveAnnotations( m_annotations );
if( m_compare.loadThread.joinable() ) m_compare.loadThread.join();
if( m_saveThread.joinable() ) m_saveThread.join();