state_file: use the text_file library

Don't use a large stack buffer.
This commit is contained in:
Max Kellermann 2010-07-25 11:01:05 +02:00
parent 1ff2d5b689
commit 838790fc2d
3 changed files with 42 additions and 38 deletions

View File

@ -28,6 +28,7 @@
#include "player_control.h"
#include "queue_save.h"
#include "path.h"
#include "text_file.h"
#include <string.h>
#include <stdlib.h>
@ -98,21 +99,21 @@ playlist_state_save(FILE *fp, const struct playlist *playlist)
}
static void
playlist_state_load(FILE *fp, struct playlist *playlist, char *buffer)
playlist_state_load(FILE *fp, GString *buffer, struct playlist *playlist)
{
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");
return;
}
while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
g_strchomp(buffer);
while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
song = queue_load_song(&playlist->queue, line);
song = queue_load_song(&playlist->queue, buffer);
if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
line = read_text_line(fp, buffer);
if (line == NULL) {
g_warning("'" PLAYLIST_STATE_FILE_PLAYLIST_END
"' not found in state file");
break;
@ -123,12 +124,12 @@ playlist_state_load(FILE *fp, struct playlist *playlist, char *buffer)
}
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 seek_time = 0;
int state = PLAYER_STATE_STOP;
char buffer[PLAYLIST_BUFFER_SIZE];
bool random_mode = false;
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)
else if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PAUSE) == 0)
state = PLAYER_STATE_PAUSE;
while (fgets(buffer, sizeof(buffer), fp)) {
g_strchomp(buffer);
if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_TIME)) {
while ((line = read_text_line(fp, buffer)) != NULL) {
if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_TIME)) {
seek_time =
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) {
atoi(&(line[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_REPEAT)) {
if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) {
playlist_set_repeat(playlist, true);
} else
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
(&(buffer[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]),
"1") == 0) {
playlist_set_single(playlist, true);
} else
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
(&(buffer[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]),
"1") == 0) {
playlist_set_consume(playlist, true);
} else
playlist_set_consume(playlist, false);
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
pc_set_cross_fade(atoi(buffer + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
pc_set_mixramp_db(atof(buffer + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
pc_set_mixramp_delay(atof(buffer + 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_CROSSFADE)) {
pc_set_cross_fade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
pc_set_mixramp_db(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
pc_set_mixramp_delay(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY)));
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_RANDOM)) {
random_mode =
strcmp(buffer + strlen(PLAYLIST_STATE_FILE_RANDOM),
strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM),
"1") == 0;
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) {
current = atoi(&(buffer
} else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CURRENT)) {
current = atoi(&(line
[strlen
(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_load(fp, playlist, buffer);
playlist_state_load(fp, buffer, playlist);
}
}

View File

@ -25,6 +25,7 @@
#ifndef PLAYLIST_STATE_H
#define PLAYLIST_STATE_H
#include <glib.h>
#include <stdbool.h>
#include <stdio.h>
@ -34,7 +35,8 @@ void
playlist_state_save(FILE *fp, const struct playlist *playlist);
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

View File

@ -23,6 +23,7 @@
#include "playlist.h"
#include "playlist_state.h"
#include "volume.h"
#include "text_file.h"
#include "glib_compat.h"
#include <glib.h>
@ -76,7 +77,6 @@ static void
state_file_read(void)
{
FILE *fp;
char line[1024];
bool success;
assert(state_file_path != NULL);
@ -90,12 +90,12 @@ state_file_read(void)
return;
}
while (fgets(line, sizeof(line), fp) != NULL) {
g_strchomp(line);
GString *buffer = g_string_sized_new(1024);
const char *line;
while ((line = read_text_line(fp, buffer)) != NULL) {
success = read_sw_volume_state(line) ||
audio_output_state_read(line) ||
playlist_state_restore(line, fp, &g_playlist);
playlist_state_restore(line, fp, buffer, &g_playlist);
if (!success)
g_warning("Unrecognized line in state file: %s", line);
}
@ -105,6 +105,9 @@ state_file_read(void)
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);
g_string_free(buffer, true);
}
/**