From 84de45afbaed7ea27f7ac84f3adf1faa26f61879 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sun, 18 Jan 2009 18:09:50 +0100
Subject: [PATCH] state_file: added state_file_init() and state_file_finish()

The constructor/destructor interface should hide the functions
read_state_file() and write_state_file().
---
 src/main.c       |  5 +++--
 src/state_file.c | 52 +++++++++++++++++++++++++++++-------------------
 src/state_file.h |  7 ++++++-
 3 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/src/main.c b/src/main.c
index c7f9e9987..399a4f2a4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -262,7 +262,8 @@ int main(int argc, char *argv[])
 
 	decoder_thread_start();
 	player_create();
-	read_state_file();
+
+	state_file_init(config_get_path(CONF_STATE_FILE));
 
 	save_state_timer = g_timer_new();
 
@@ -280,7 +281,7 @@ int main(int argc, char *argv[])
 	g_source_remove(save_state_source_id);
 	g_timer_destroy(save_state_timer);
 
-	write_state_file();
+	state_file_finish();
 	playerKill();
 	finishZeroconf();
 	client_manager_deinit();
diff --git a/src/state_file.c b/src/state_file.c
index 52e920531..1139c7548 100644
--- a/src/state_file.c
+++ b/src/state_file.c
@@ -17,7 +17,6 @@
  */
 
 #include "state_file.h"
-#include "conf.h"
 #include "audio.h"
 #include "playlist.h"
 #include "volume.h"
@@ -38,29 +37,20 @@ static struct _sf_cb {
 	{ readPlaylistState, savePlaylistState },
 };
 
-static const char *sfpath;
-
-static void get_state_file_path(void)
-{
-	struct config_param *param;
-	if (sfpath)
-		return;
-	param = parseConfigFilePath(CONF_STATE_FILE, 0);
-	if (param)
-		sfpath = (const char *)param->value;
-}
+static char *state_file_path;
 
 void write_state_file(void)
 {
 	unsigned int i;
 	FILE *fp;
 
-	if (!sfpath)
+	if (state_file_path == NULL)
 		return;
-	fp = fopen(sfpath, "w");
+
+	fp = fopen(state_file_path, "w");
 	if (G_UNLIKELY(!fp)) {
 		g_warning("failed to create %s: %s",
-			  sfpath, strerror(errno));
+			  state_file_path, strerror(errno));
 		return;
 	}
 
@@ -70,19 +60,18 @@ void write_state_file(void)
 	while(fclose(fp) && errno == EINTR) /* nothing */;
 }
 
-void read_state_file(void)
+static void
+state_file_read(void)
 {
 	unsigned int i;
 	FILE *fp;
 
-	get_state_file_path();
-	if (!sfpath)
-		return;
+	assert(state_file_path != NULL);
 
-	while (!(fp = fopen(sfpath, "r")) && errno == EINTR);
+	fp = fopen(state_file_path, "r");
 	if (G_UNLIKELY(!fp)) {
 		g_warning("failed to open %s: %s",
-			  sfpath, strerror(errno));
+			  state_file_path, strerror(errno));
 		return;
 	}
 	for (i = 0; i < G_N_ELEMENTS(sf_callbacks); i++) {
@@ -92,3 +81,24 @@ void read_state_file(void)
 
 	while(fclose(fp) && errno == EINTR) /* nothing */;
 }
+
+void
+state_file_init(const char *path)
+{
+	assert(state_file_path == NULL);
+
+	if (path == NULL)
+		return;
+
+	state_file_path = g_strdup(path);
+	state_file_read();
+}
+
+void
+state_file_finish(void)
+{
+	if (state_file_path != NULL)
+		write_state_file();
+
+	g_free(state_file_path);
+}
diff --git a/src/state_file.h b/src/state_file.h
index e9c8d5d10..440083479 100644
--- a/src/state_file.h
+++ b/src/state_file.h
@@ -19,7 +19,12 @@
 #ifndef MPD_STATE_FILE_H
 #define MPD_STATE_FILE_H
 
+void
+state_file_init(const char *path);
+
+void
+state_file_finish(void);
+
 void write_state_file(void);
-void read_state_file(void);
 
 #endif /* STATE_FILE_H */