utils: use g_str_has_prefix() instead of prefixcmp()
Remove duplicated code from MPD.
This commit is contained in:
parent
4a71f66256
commit
016d996131
@ -28,6 +28,8 @@
|
||||
#include "utils.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#define AUDIO_DEVICE_STATE "audio_device_state:"
|
||||
#define AUDIO_BUFFER_SIZE 2*MPD_PATH_MAX
|
||||
|
||||
@ -434,7 +436,7 @@ void readAudioDevicesState(FILE *fp)
|
||||
while (myFgets(buffer, AUDIO_BUFFER_SIZE, fp)) {
|
||||
char *c, *name;
|
||||
|
||||
if (prefixcmp(buffer, AUDIO_DEVICE_STATE))
|
||||
if (!g_str_has_prefix(buffer, AUDIO_DEVICE_STATE))
|
||||
continue;
|
||||
|
||||
c = strchr(buffer, ':');
|
||||
|
@ -280,11 +280,11 @@ db_load(void)
|
||||
|
||||
while (myFgets(buffer, sizeof(buffer), fp) &&
|
||||
0 != strcmp(DIRECTORY_INFO_END, buffer)) {
|
||||
if (!prefixcmp(buffer, DIRECTORY_MPD_VERSION)) {
|
||||
if (g_str_has_prefix(buffer, DIRECTORY_MPD_VERSION)) {
|
||||
if (foundVersion)
|
||||
FATAL("already found version in db\n");
|
||||
foundVersion = 1;
|
||||
} else if (!prefixcmp(buffer, DIRECTORY_FS_CHARSET)) {
|
||||
} else if (g_str_has_prefix(buffer, DIRECTORY_FS_CHARSET)) {
|
||||
char *fsCharset;
|
||||
char *tempCharset;
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "../utils.h"
|
||||
#include "../conf.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <mad.h>
|
||||
|
||||
#ifdef HAVE_ID3TAG
|
||||
@ -564,7 +565,7 @@ static int parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen)
|
||||
/* This is technically incorrect, since the encoder might not be lame.
|
||||
* But there's no other way to determine if this is a lame tag, and we
|
||||
* wouldn't want to go reading a tag that's not there. */
|
||||
if (prefixcmp(lame->encoder, "LAME"))
|
||||
if (!g_str_has_prefix(lame->encoder, "LAME"))
|
||||
return 0;
|
||||
|
||||
if (sscanf(lame->encoder+4, "%u.%u",
|
||||
|
@ -71,22 +71,22 @@ directory_load(FILE *fp, struct directory *directory)
|
||||
char *name;
|
||||
|
||||
while (myFgets(buffer, bufferSize, fp)
|
||||
&& prefixcmp(buffer, DIRECTORY_END)) {
|
||||
if (!prefixcmp(buffer, DIRECTORY_DIR)) {
|
||||
&& !g_str_has_prefix(buffer, DIRECTORY_END)) {
|
||||
if (g_str_has_prefix(buffer, DIRECTORY_DIR)) {
|
||||
struct directory *subdir;
|
||||
|
||||
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
|
||||
if (!myFgets(buffer, bufferSize, fp))
|
||||
FATAL("Error reading db, fgets\n");
|
||||
/* for compatibility with db's prior to 0.11 */
|
||||
if (!prefixcmp(buffer, DIRECTORY_MTIME)) {
|
||||
if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) {
|
||||
if (!myFgets(buffer, bufferSize, fp))
|
||||
FATAL("Error reading db, fgets\n");
|
||||
}
|
||||
if (prefixcmp(buffer, DIRECTORY_BEGIN))
|
||||
if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN))
|
||||
FATAL("Error reading db at line: %s\n", buffer);
|
||||
name = &(buffer[strlen(DIRECTORY_BEGIN)]);
|
||||
if (prefixcmp(name, directory->path) != 0)
|
||||
if (!g_str_has_prefix(name, directory->path) != 0)
|
||||
FATAL("Wrong path in database: '%s' in '%s'\n",
|
||||
name, directory->path);
|
||||
|
||||
@ -98,7 +98,7 @@ directory_load(FILE *fp, struct directory *directory)
|
||||
dirvec_add(&directory->children, subdir);
|
||||
}
|
||||
directory_load(fp, subdir);
|
||||
} else if (!prefixcmp(buffer, SONG_BEGIN)) {
|
||||
} else if (g_str_has_prefix(buffer, SONG_BEGIN)) {
|
||||
readSongInfoIntoList(fp, &directory->songs, directory);
|
||||
} else {
|
||||
FATAL("Unknown line in db: %s\n", buffer);
|
||||
|
3
src/ls.c
3
src/ls.c
@ -21,7 +21,6 @@
|
||||
#include "path.h"
|
||||
#include "client.h"
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
#include "list.h"
|
||||
#include "stored_playlist.h"
|
||||
#include "os_compat.h"
|
||||
@ -92,7 +91,7 @@ int isRemoteUrl(const char *url)
|
||||
|
||||
while (*urlPrefixes) {
|
||||
count++;
|
||||
if (!prefixcmp(url, *urlPrefixes))
|
||||
if (g_str_has_prefix(url, *urlPrefixes))
|
||||
return count;
|
||||
urlPrefixes++;
|
||||
}
|
||||
|
@ -46,12 +46,13 @@
|
||||
#include "tag.h"
|
||||
#include "dbUtils.h"
|
||||
#include "../config.h"
|
||||
#include "utils.h"
|
||||
#include "normalize.h"
|
||||
#include "zeroconf.h"
|
||||
#include "main_notify.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#define SYSTEM_CONFIG_FILE_LOCATION "/etc/mpd.conf"
|
||||
#define USER_CONFIG_FILE_LOCATION "/.mpdconf"
|
||||
|
||||
@ -156,7 +157,7 @@ static void parseOptions(int argc, char **argv, Options * options)
|
||||
if (argc > 1) {
|
||||
int i = 1;
|
||||
while (i < argc) {
|
||||
if (!prefixcmp(argv[i], "--")) {
|
||||
if (g_str_has_prefix(argv[i], "--")) {
|
||||
if (strcmp(argv[i], "--help") == 0) {
|
||||
usage(argv);
|
||||
exit(EXIT_SUCCESS);
|
||||
|
@ -301,7 +301,7 @@ void readPlaylistState(FILE *fp)
|
||||
char buffer[PLAYLIST_BUFFER_SIZE];
|
||||
|
||||
while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
|
||||
if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_STATE)) {
|
||||
if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_STATE)) {
|
||||
if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
|
||||
PLAYLIST_STATE_FILE_STATE_PLAY) == 0) {
|
||||
state = PLAYER_STATE_PLAY;
|
||||
@ -312,24 +312,24 @@ void readPlaylistState(FILE *fp)
|
||||
== 0) {
|
||||
state = PLAYER_STATE_PAUSE;
|
||||
}
|
||||
} else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_TIME)) {
|
||||
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_TIME)) {
|
||||
seek_time =
|
||||
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
|
||||
} else
|
||||
if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_REPEAT)) {
|
||||
if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) {
|
||||
if (strcmp
|
||||
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
|
||||
"1") == 0) {
|
||||
setPlaylistRepeatStatus(true);
|
||||
} else
|
||||
setPlaylistRepeatStatus(false);
|
||||
} else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
|
||||
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
|
||||
setPlayerCrossFade(atoi
|
||||
(&
|
||||
(buffer
|
||||
[strlen
|
||||
(PLAYLIST_STATE_FILE_CROSSFADE)])));
|
||||
} else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_RANDOM)) {
|
||||
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_RANDOM)) {
|
||||
if (strcmp
|
||||
(&
|
||||
(buffer
|
||||
@ -338,15 +338,15 @@ void readPlaylistState(FILE *fp)
|
||||
setPlaylistRandomStatus(true);
|
||||
} else
|
||||
setPlaylistRandomStatus(false);
|
||||
} else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_CURRENT)) {
|
||||
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) {
|
||||
if (strlen(buffer) ==
|
||||
strlen(PLAYLIST_STATE_FILE_CURRENT))
|
||||
state_file_fatal();
|
||||
current = atoi(&(buffer
|
||||
[strlen
|
||||
(PLAYLIST_STATE_FILE_CURRENT)]));
|
||||
} else if (!prefixcmp(buffer,
|
||||
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
|
||||
} else if (g_str_has_prefix(buffer,
|
||||
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
|
||||
if (state == PLAYER_STATE_STOP)
|
||||
current = -1;
|
||||
loadPlaylistFromStateFile(fp, buffer, state,
|
||||
|
10
src/utils.c
10
src/utils.c
@ -261,13 +261,3 @@ void xpthread_cond_destroy(pthread_cond_t *cond)
|
||||
if ((err = pthread_cond_destroy(cond)))
|
||||
FATAL("failed to destroy cond: %s\n", strerror(err));
|
||||
}
|
||||
|
||||
int prefixcmp(const char *str, const char *prefix)
|
||||
{
|
||||
for (; ; str++, prefix++)
|
||||
if (!*prefix)
|
||||
return 0;
|
||||
else if (*str != *prefix)
|
||||
return (unsigned char)*prefix - (unsigned char)*str;
|
||||
}
|
||||
|
||||
|
@ -104,6 +104,4 @@ void xpthread_mutex_destroy(pthread_mutex_t *mutex);
|
||||
|
||||
void xpthread_cond_destroy(pthread_cond_t *cond);
|
||||
|
||||
int prefixcmp(const char *str, const char *prefix);
|
||||
|
||||
#endif
|
||||
|
@ -26,6 +26,8 @@
|
||||
|
||||
#include "../config.h"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#ifdef HAVE_OSS
|
||||
#include <sys/soundcard.h>
|
||||
#endif
|
||||
@ -518,7 +520,7 @@ void read_sw_volume_state(FILE *fp)
|
||||
if (volume_mixerType != VOLUME_MIXER_TYPE_SOFTWARE)
|
||||
return;
|
||||
while (myFgets(buf, sizeof(buf), fp)) {
|
||||
if (prefixcmp(buf, SW_VOLUME_STATE))
|
||||
if (!g_str_has_prefix(buf, SW_VOLUME_STATE))
|
||||
continue;
|
||||
sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10);
|
||||
if (mpd_likely(!*end))
|
||||
|
Loading…
Reference in New Issue
Block a user