Commit 838790fc authored by Max Kellermann's avatar Max Kellermann

state_file: use the text_file library

Don't use a large stack buffer.
parent 1ff2d5b6
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "player_control.h" #include "player_control.h"
#include "queue_save.h" #include "queue_save.h"
#include "path.h" #include "path.h"
#include "text_file.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -98,21 +99,21 @@ playlist_state_save(FILE *fp, const struct playlist *playlist) ...@@ -98,21 +99,21 @@ playlist_state_save(FILE *fp, const struct playlist *playlist)
} }
static void static void
playlist_state_load(FILE *fp, struct playlist *playlist, char *buffer) playlist_state_load(FILE *fp, GString *buffer, struct playlist *playlist)
{ {
int song; int song;
if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { const char *line = read_text_line(fp, buffer);
if (line == NULL) {
g_warning("No playlist in state file"); g_warning("No playlist in state file");
return; return;
} }
while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) { while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
g_strchomp(buffer); song = queue_load_song(&playlist->queue, line);
song = queue_load_song(&playlist->queue, buffer); line = read_text_line(fp, buffer);
if (line == NULL) {
if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
g_warning("'" PLAYLIST_STATE_FILE_PLAYLIST_END g_warning("'" PLAYLIST_STATE_FILE_PLAYLIST_END
"' not found in state file"); "' not found in state file");
break; break;
...@@ -123,12 +124,12 @@ playlist_state_load(FILE *fp, struct playlist *playlist, char *buffer) ...@@ -123,12 +124,12 @@ playlist_state_load(FILE *fp, struct playlist *playlist, char *buffer)
} }
bool bool
playlist_state_restore(const char *line, FILE *fp, struct playlist *playlist) playlist_state_restore(const char *line, FILE *fp, GString *buffer,
struct playlist *playlist)
{ {
int current = -1; int current = -1;
int seek_time = 0; int seek_time = 0;
int state = PLAYER_STATE_STOP; int state = PLAYER_STATE_STOP;
char buffer[PLAYLIST_BUFFER_SIZE];
bool random_mode = false; bool random_mode = false;
if (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_STATE)) if (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_STATE))
...@@ -141,50 +142,48 @@ playlist_state_restore(const char *line, FILE *fp, struct playlist *playlist) ...@@ -141,50 +142,48 @@ playlist_state_restore(const char *line, FILE *fp, struct playlist *playlist)
else if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PAUSE) == 0) else if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PAUSE) == 0)
state = PLAYER_STATE_PAUSE; state = PLAYER_STATE_PAUSE;
while (fgets(buffer, sizeof(buffer), fp)) { while ((line = read_text_line(fp, buffer)) != NULL) {
g_strchomp(buffer); if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_TIME)) {
if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_TIME)) {
seek_time = seek_time =
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)])); atoi(&(line[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_REPEAT)) {
if (strcmp if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]), (&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) { "1") == 0) {
playlist_set_repeat(playlist, true); playlist_set_repeat(playlist, true);
} else } else
playlist_set_repeat(playlist, false); playlist_set_repeat(playlist, false);
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_SINGLE)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_SINGLE)) {
if (strcmp if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_SINGLE)]), (&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
"1") == 0) { "1") == 0) {
playlist_set_single(playlist, true); playlist_set_single(playlist, true);
} else } else
playlist_set_single(playlist, false); playlist_set_single(playlist, false);
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CONSUME)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CONSUME)) {
if (strcmp if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_CONSUME)]), (&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
"1") == 0) { "1") == 0) {
playlist_set_consume(playlist, true); playlist_set_consume(playlist, true);
} else } else
playlist_set_consume(playlist, false); playlist_set_consume(playlist, false);
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
pc_set_cross_fade(atoi(buffer + strlen(PLAYLIST_STATE_FILE_CROSSFADE))); pc_set_cross_fade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_MIXRAMPDB)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
pc_set_mixramp_db(atof(buffer + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB))); pc_set_mixramp_db(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
pc_set_mixramp_delay(atof(buffer + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY))); pc_set_mixramp_delay(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY)));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_RANDOM)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_RANDOM)) {
random_mode = random_mode =
strcmp(buffer + strlen(PLAYLIST_STATE_FILE_RANDOM), strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM),
"1") == 0; "1") == 0;
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) { } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CURRENT)) {
current = atoi(&(buffer current = atoi(&(line
[strlen [strlen
(PLAYLIST_STATE_FILE_CURRENT)])); (PLAYLIST_STATE_FILE_CURRENT)]));
} else if (g_str_has_prefix(buffer, } else if (g_str_has_prefix(line,
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) { PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
playlist_state_load(fp, playlist, buffer); playlist_state_load(fp, buffer, playlist);
} }
} }
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#ifndef PLAYLIST_STATE_H #ifndef PLAYLIST_STATE_H
#define PLAYLIST_STATE_H #define PLAYLIST_STATE_H
#include <glib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
...@@ -34,7 +35,8 @@ void ...@@ -34,7 +35,8 @@ void
playlist_state_save(FILE *fp, const struct playlist *playlist); playlist_state_save(FILE *fp, const struct playlist *playlist);
bool bool
playlist_state_restore(const char *line, FILE *fp, struct playlist *playlist); playlist_state_restore(const char *line, FILE *fp, GString *buffer,
struct playlist *playlist);
/** /**
* Generates a hash number for the current state of the playlist and * Generates a hash number for the current state of the playlist and
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "playlist.h" #include "playlist.h"
#include "playlist_state.h" #include "playlist_state.h"
#include "volume.h" #include "volume.h"
#include "text_file.h"
#include "glib_compat.h" #include "glib_compat.h"
#include <glib.h> #include <glib.h>
...@@ -76,7 +77,6 @@ static void ...@@ -76,7 +77,6 @@ static void
state_file_read(void) state_file_read(void)
{ {
FILE *fp; FILE *fp;
char line[1024];
bool success; bool success;
assert(state_file_path != NULL); assert(state_file_path != NULL);
...@@ -90,12 +90,12 @@ state_file_read(void) ...@@ -90,12 +90,12 @@ state_file_read(void)
return; return;
} }
while (fgets(line, sizeof(line), fp) != NULL) { GString *buffer = g_string_sized_new(1024);
g_strchomp(line); const char *line;
while ((line = read_text_line(fp, buffer)) != NULL) {
success = read_sw_volume_state(line) || success = read_sw_volume_state(line) ||
audio_output_state_read(line) || audio_output_state_read(line) ||
playlist_state_restore(line, fp, &g_playlist); playlist_state_restore(line, fp, buffer, &g_playlist);
if (!success) if (!success)
g_warning("Unrecognized line in state file: %s", line); g_warning("Unrecognized line in state file: %s", line);
} }
...@@ -105,6 +105,9 @@ state_file_read(void) ...@@ -105,6 +105,9 @@ state_file_read(void)
prev_volume_version = sw_volume_state_get_hash(); prev_volume_version = sw_volume_state_get_hash();
prev_output_version = audio_output_state_get_version(); prev_output_version = audio_output_state_get_version();
prev_playlist_version = playlist_state_get_hash(&g_playlist); prev_playlist_version = playlist_state_get_hash(&g_playlist);
g_string_free(buffer, true);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment