diff options
Diffstat (limited to '')
-rw-r--r-- | src/StateFile.cxx (renamed from src/state_file.c) | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/src/state_file.c b/src/StateFile.cxx index de0e70538..1d77b77de 100644 --- a/src/state_file.c +++ b/src/StateFile.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * Copyright (C) 2003-2013 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -18,12 +18,15 @@ */ #include "config.h" -#include "state_file.h" -#include "output_state.h" -#include "playlist.h" -#include "playlist_state.h" +#include "StateFile.hxx" +#include "OutputState.hxx" +#include "PlaylistState.hxx" +#include "TextFile.hxx" +#include "Partition.hxx" + +extern "C" { #include "volume.h" -#include "text_file.h" +} #include <glib.h> #include <assert.h> @@ -46,7 +49,7 @@ static unsigned prev_volume_version, prev_output_version, prev_playlist_version; static void -state_file_write(struct player_control *pc) +state_file_write(Partition &partition) { FILE *fp; @@ -63,51 +66,46 @@ state_file_write(struct player_control *pc) save_sw_volume_state(fp); audio_output_state_save(fp); - playlist_state_save(fp, &g_playlist, pc); + playlist_state_save(fp, &partition.playlist, &partition.pc); fclose(fp); prev_volume_version = sw_volume_state_get_hash(); prev_output_version = audio_output_state_get_version(); - prev_playlist_version = playlist_state_get_hash(&g_playlist, pc); + prev_playlist_version = playlist_state_get_hash(&partition.playlist, + &partition.pc); } static void -state_file_read(struct player_control *pc) +state_file_read(Partition &partition) { - FILE *fp; bool success; assert(state_file_path != NULL); g_debug("Loading state file %s", state_file_path); - fp = fopen(state_file_path, "r"); - if (G_UNLIKELY(!fp)) { + TextFile file(state_file_path); + if (file.HasFailed()) { g_warning("failed to open %s: %s", state_file_path, g_strerror(errno)); return; } - GString *buffer = g_string_sized_new(1024); const char *line; - while ((line = read_text_line(fp, buffer)) != NULL) { + while ((line = file.ReadLine()) != NULL) { success = read_sw_volume_state(line) || audio_output_state_read(line) || - playlist_state_restore(line, fp, buffer, - &g_playlist, pc); + playlist_state_restore(line, file, &partition.playlist, + &partition.pc); if (!success) g_warning("Unrecognized line in state file: %s", line); } - fclose(fp); - prev_volume_version = sw_volume_state_get_hash(); prev_output_version = audio_output_state_get_version(); - prev_playlist_version = playlist_state_get_hash(&g_playlist, pc); - - - g_string_free(buffer, true); + prev_playlist_version = playlist_state_get_hash(&partition.playlist, + &partition.pc); } /** @@ -117,21 +115,22 @@ state_file_read(struct player_control *pc) static gboolean timer_save_state_file(gpointer data) { - struct player_control *pc = data; + Partition &partition = *(Partition *)data; if (prev_volume_version == sw_volume_state_get_hash() && prev_output_version == audio_output_state_get_version() && - prev_playlist_version == playlist_state_get_hash(&g_playlist, pc)) + prev_playlist_version == playlist_state_get_hash(&partition.playlist, + &partition.pc)) /* nothing has changed - don't save the state file, don't spin up the hard disk */ return true; - state_file_write(pc); + state_file_write(partition); return true; } void -state_file_init(const char *path, struct player_control *pc) +state_file_init(const char *path, Partition &partition) { assert(state_file_path == NULL); @@ -139,15 +138,15 @@ state_file_init(const char *path, struct player_control *pc) return; state_file_path = g_strdup(path); - state_file_read(pc); + state_file_read(partition); save_state_source_id = g_timeout_add_seconds(5 * 60, timer_save_state_file, - pc); + &partition); } void -state_file_finish(struct player_control *pc) +state_file_finish(Partition &partition) { if (state_file_path == NULL) /* no state file configured, no cleanup required */ @@ -156,7 +155,7 @@ state_file_finish(struct player_control *pc) if (save_state_source_id != 0) g_source_remove(save_state_source_id); - state_file_write(pc); + state_file_write(partition); g_free(state_file_path); } |