Sequencer: Save file to tmp file before overwriting original.

This commit is contained in:
Jonathan Moore Liles 2020-09-16 18:18:43 -07:00
parent b3ffef17b5
commit 1a07dda5d9
1 changed files with 55 additions and 37 deletions

View File

@ -371,51 +371,69 @@ sequence::load ( const char *name )
void
sequence::save ( const char *name ) const
{
smf f;
/* open for writing */
f.open( name, smf::WRITE );
char *tmp = NULL;
f.write_header( 2 );
DMESSAGE( "saving playlist" );
f.open_track( NULL, -1 );
DMESSAGE( "saving song info" );
f.write_song_info( song.play_mode, phrase::phrases(), pattern::patterns(), this->name(), notes() );
for ( int i = 0; i < _rd->num; ++i )
{
char pat[256];
const char *filename = basename(name);
char *dir = (char*)malloc( (strlen(name) - strlen(filename)) + 1 );
strncpy( dir, name, strlen(name) - strlen(filename) );
phrase *p = phrase::phrase_by_number( _rd->phrases[ i ] );
snprintf( pat, 256, "%d: %s", p->number(), p->name() );
f.write_meta_event( smf::CUEPOINT, pat );
asprintf( &tmp, "%s#%s", dir, filename );
free(dir);
}
f.close_track( 0 );
DMESSAGE( "saving phrases" );
for ( int i = 0; i < phrase::phrases(); i++ )
{
phrase *p = phrase::phrase_by_number( i + 1 );
smf f;
p->dump( &f );
/* open for writing */
f.open( tmp, smf::WRITE );
f.write_header( 2 );
DMESSAGE( "saving playlist" );
f.open_track( NULL, -1 );
DMESSAGE( "saving song info" );
f.write_song_info( song.play_mode, phrase::phrases(), pattern::patterns(), this->name(), notes() );
for ( int i = 0; i < _rd->num; ++i )
{
char pat[256];
phrase *p = phrase::phrase_by_number( _rd->phrases[ i ] );
snprintf( pat, 256, "%d: %s", p->number(), p->name() );
f.write_meta_event( smf::CUEPOINT, pat );
}
f.close_track( 0 );
DMESSAGE( "saving phrases" );
for ( int i = 0; i < phrase::phrases(); i++ )
{
phrase *p = phrase::phrase_by_number( i + 1 );
p->dump( &f );
}
DMESSAGE( "saving patterns" );
for ( int i = 0; i < pattern::patterns(); i++ )
{
pattern *p = pattern::pattern_by_number( i + 1 );
p->dump( &f );
}
}
DMESSAGE( "saving patterns" );
rename(tmp,name);
for ( int i = 0; i < pattern::patterns(); i++ )
{
pattern *p = pattern::pattern_by_number( i + 1 );
p->dump( &f );
}
free(tmp);
}