StateFile: use StringAfterPrefix() instead of StringStartsWith()

This commit is contained in:
Max Kellermann 2015-11-11 15:34:36 +01:00
parent c513478c31
commit 738583e3d4
4 changed files with 30 additions and 41 deletions

View File

@ -98,10 +98,10 @@ read_sw_volume_state(const char *line, MultipleOutputs &outputs)
char *end = nullptr; char *end = nullptr;
long int sv; long int sv;
if (!StringStartsWith(line, SW_VOLUME_STATE)) line = StringAfterPrefix(line, SW_VOLUME_STATE);
if (line == nullptr)
return false; return false;
line += sizeof(SW_VOLUME_STATE) - 1;
sv = strtol(line, &end, 10); sv = strtol(line, &end, 10);
if (*end == 0 && sv >= 0 && sv <= 100) if (*end == 0 && sv >= 0 && sv <= 100)
software_volume_change(outputs, sv); software_volume_change(outputs, sv);

View File

@ -56,11 +56,10 @@ audio_output_state_read(const char *line, MultipleOutputs &outputs)
char *endptr; char *endptr;
const char *name; const char *name;
if (!StringStartsWith(line, AUDIO_DEVICE_STATE)) line = StringAfterPrefix(line, AUDIO_DEVICE_STATE);
if (line == nullptr)
return false; return false;
line += sizeof(AUDIO_DEVICE_STATE) - 1;
value = strtol(line, &endptr, 10); value = strtol(line, &endptr, 10);
if (*endptr != ':' || (value != 0 && value != 1)) if (*endptr != ':' || (value != 0 && value != 1))
return false; return false;

View File

@ -33,6 +33,7 @@
#include "config/ConfigGlobal.hxx" #include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx" #include "config/ConfigOption.hxx"
#include "util/CharUtil.hxx" #include "util/CharUtil.hxx"
#include "util/StringAPI.hxx"
#include "util/StringCompare.hxx" #include "util/StringCompare.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -132,11 +133,10 @@ playlist_state_restore(const char *line, TextFile &file,
SongTime seek_time = SongTime::zero(); SongTime seek_time = SongTime::zero();
bool random_mode = false; bool random_mode = false;
if (!StringStartsWith(line, PLAYLIST_STATE_FILE_STATE)) line = StringAfterPrefix(line, PLAYLIST_STATE_FILE_STATE);
if (line == nullptr)
return false; return false;
line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1;
PlayerState state; PlayerState state;
if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PLAY) == 0) if (strcmp(line, PLAYLIST_STATE_FILE_STATE_PLAY) == 0)
state = PlayerState::PLAY; state = PlayerState::PLAY;
@ -146,39 +146,28 @@ playlist_state_restore(const char *line, TextFile &file,
state = PlayerState::STOP; state = PlayerState::STOP;
while ((line = file.ReadLine()) != nullptr) { while ((line = file.ReadLine()) != nullptr) {
if (StringStartsWith(line, PLAYLIST_STATE_FILE_TIME)) { const char *p;
double seconds = atof(line + strlen(PLAYLIST_STATE_FILE_TIME)); if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_TIME))) {
seek_time = SongTime::FromS(seconds); seek_time = SongTime::FromS(atof(p));
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_REPEAT)) { } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_REPEAT))) {
playlist.SetRepeat(pc, playlist.SetRepeat(pc, StringIsEqual(p, "1"));
strcmp(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]), } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_SINGLE))) {
"1") == 0); playlist.SetSingle(pc, StringIsEqual(p, "1"));
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_SINGLE)) { } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CONSUME))) {
playlist.SetSingle(pc, playlist.SetConsume(StringIsEqual(p, "1"));
strcmp(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]), } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CROSSFADE))) {
"1") == 0); pc.SetCrossFade(atoi(p));
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CONSUME)) { } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB))) {
playlist.SetConsume(strcmp(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]), pc.SetMixRampDb(atof(p));
"1") == 0); } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY))) {
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CROSSFADE)) {
pc.SetCrossFade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE)));
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) {
pc.SetMixRampDb(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB)));
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) {
const char *p = line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY);
/* this check discards "nan" which was used /* this check discards "nan" which was used
prior to MPD 0.18 */ prior to MPD 0.18 */
if (IsDigitASCII(*p)) if (IsDigitASCII(*p))
pc.SetMixRampDelay(atof(p)); pc.SetMixRampDelay(atof(p));
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_RANDOM)) { } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_RANDOM))) {
random_mode = random_mode = StringIsEqual(p, "1");
strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM), } else if ((p = StringAfterPrefix(line, PLAYLIST_STATE_FILE_CURRENT))) {
"1") == 0; current = atoi(p);
} else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CURRENT)) {
current = atoi(&(line
[strlen
(PLAYLIST_STATE_FILE_CURRENT)]));
} else if (StringStartsWith(line, } else if (StringStartsWith(line,
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) { PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
playlist_state_load(file, song_loader, playlist); playlist_state_load(file, song_loader, playlist);

View File

@ -83,8 +83,9 @@ queue_load_song(TextFile &file, const SongLoader &loader,
return; return;
uint8_t priority = 0; uint8_t priority = 0;
if (StringStartsWith(line, PRIO_LABEL)) { const char *p;
priority = strtoul(line + sizeof(PRIO_LABEL) - 1, nullptr, 10); if ((p = StringAfterPrefix(line, PRIO_LABEL))) {
priority = strtoul(p, nullptr, 10);
line = file.ReadLine(); line = file.ReadLine();
if (line == nullptr) if (line == nullptr)
@ -93,8 +94,8 @@ queue_load_song(TextFile &file, const SongLoader &loader,
DetachedSong *song; DetachedSong *song;
if (StringStartsWith(line, SONG_BEGIN)) { if ((p = StringAfterPrefix(line, PRIO_LABEL))) {
const char *uri = line + sizeof(SONG_BEGIN) - 1; const char *uri = p;
Error error; Error error;
song = song_load(file, uri, error); song = song_load(file, uri, error);