Save/load source substitutions.

This commit is contained in:
Bartosz Taudul
2020-04-18 14:25:04 +02:00
parent ff4b4fd9d9
commit 91ad77d86a
5 changed files with 103 additions and 8 deletions

View File

@@ -18,10 +18,12 @@ constexpr auto FileDescription = "description";
constexpr auto FileTimeline = "timeline";
constexpr auto FileOptions = "options";
constexpr auto FileAnnotations = "annotations";
constexpr auto FileSourceSubstitutions = "srcsub";
enum : uint32_t { VersionTimeline = 0 };
enum : uint32_t { VersionOptions = 5 };
enum : uint32_t { VersionAnnotations = 0 };
enum : uint32_t { VersionSourceSubstitutions = 0 };
UserData::UserData()
: m_preserveState( false )
@@ -225,6 +227,92 @@ void UserData::SaveAnnotations( const std::vector<std::unique_ptr<Annotation>>&
}
}
bool UserData::LoadSourceSubstitutions( std::vector<SourceRegex>& data )
{
assert( Valid() );
bool regexValid = true;
FILE* f = OpenFile( FileSourceSubstitutions, false );
if( f )
{
uint32_t ver;
fread( &ver, 1, sizeof( ver ), f );
if( ver == VersionSourceSubstitutions )
{
uint32_t sz;
fread( &sz, 1, sizeof( sz ), f );
for( uint32_t i=0; i<sz; i++ )
{
std::string pattern, target;
uint32_t tsz;
fread( &tsz, 1, sizeof( tsz ), f );
if( tsz != 0 )
{
char buf[1024];
assert( tsz < 1024 );
fread( buf, 1, tsz, f );
pattern.assign( buf, tsz );
}
fread( &tsz, 1, sizeof( tsz ), f );
if( tsz != 0 )
{
char buf[1024];
assert( tsz < 1024 );
fread( buf, 1, tsz, f );
target.assign( buf, tsz );
}
std::regex regex;
try
{
regex.assign( pattern );
}
catch( std::regex_error& err )
{
regexValid = false;
}
data.emplace_back( SourceRegex { std::move( pattern ), std::move( target ), std::move( regex ) } );
}
}
fclose( f );
}
return regexValid;
}
void UserData::SaveSourceSubstitutions( const std::vector<SourceRegex>& data )
{
if( !m_preserveState ) return;
if( data.empty() )
{
Remove( FileSourceSubstitutions );
return;
}
assert( Valid() );
FILE* f = OpenFile( FileSourceSubstitutions, true );
if( f )
{
uint32_t ver = VersionSourceSubstitutions;
fwrite( &ver, 1, sizeof( ver ), f );
uint32_t sz = uint32_t( data.size() );
fwrite( &sz, 1, sizeof( sz ), f );
for( auto& v : data )
{
sz = uint32_t( v.pattern.size() );
fwrite( &sz, 1, sizeof( sz ), f );
if( sz != 0 )
{
fwrite( v.pattern.c_str(), 1, sz, f );
}
sz = uint32_t( v.target.size() );
fwrite( &sz, 1, sizeof( sz ), f );
if( sz != 0 )
{
fwrite( v.target.c_str(), 1, sz, f );
}
}
fclose( f );
}
}
FILE* UserData::OpenFile( const char* filename, bool write )
{
const auto path = GetSavePath( m_program.c_str(), m_time, filename, write );