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 "utils.h"
|
||||||
#include "os_compat.h"
|
#include "os_compat.h"
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
#define AUDIO_DEVICE_STATE "audio_device_state:"
|
#define AUDIO_DEVICE_STATE "audio_device_state:"
|
||||||
#define AUDIO_BUFFER_SIZE 2*MPD_PATH_MAX
|
#define AUDIO_BUFFER_SIZE 2*MPD_PATH_MAX
|
||||||
|
|
||||||
@ -434,7 +436,7 @@ void readAudioDevicesState(FILE *fp)
|
|||||||
while (myFgets(buffer, AUDIO_BUFFER_SIZE, fp)) {
|
while (myFgets(buffer, AUDIO_BUFFER_SIZE, fp)) {
|
||||||
char *c, *name;
|
char *c, *name;
|
||||||
|
|
||||||
if (prefixcmp(buffer, AUDIO_DEVICE_STATE))
|
if (!g_str_has_prefix(buffer, AUDIO_DEVICE_STATE))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
c = strchr(buffer, ':');
|
c = strchr(buffer, ':');
|
||||||
|
@ -280,11 +280,11 @@ db_load(void)
|
|||||||
|
|
||||||
while (myFgets(buffer, sizeof(buffer), fp) &&
|
while (myFgets(buffer, sizeof(buffer), fp) &&
|
||||||
0 != strcmp(DIRECTORY_INFO_END, buffer)) {
|
0 != strcmp(DIRECTORY_INFO_END, buffer)) {
|
||||||
if (!prefixcmp(buffer, DIRECTORY_MPD_VERSION)) {
|
if (g_str_has_prefix(buffer, DIRECTORY_MPD_VERSION)) {
|
||||||
if (foundVersion)
|
if (foundVersion)
|
||||||
FATAL("already found version in db\n");
|
FATAL("already found version in db\n");
|
||||||
foundVersion = 1;
|
foundVersion = 1;
|
||||||
} else if (!prefixcmp(buffer, DIRECTORY_FS_CHARSET)) {
|
} else if (g_str_has_prefix(buffer, DIRECTORY_FS_CHARSET)) {
|
||||||
char *fsCharset;
|
char *fsCharset;
|
||||||
char *tempCharset;
|
char *tempCharset;
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include "../utils.h"
|
#include "../utils.h"
|
||||||
#include "../conf.h"
|
#include "../conf.h"
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
#include <mad.h>
|
#include <mad.h>
|
||||||
|
|
||||||
#ifdef HAVE_ID3TAG
|
#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.
|
/* 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
|
* 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. */
|
* 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;
|
return 0;
|
||||||
|
|
||||||
if (sscanf(lame->encoder+4, "%u.%u",
|
if (sscanf(lame->encoder+4, "%u.%u",
|
||||||
|
@ -71,22 +71,22 @@ directory_load(FILE *fp, struct directory *directory)
|
|||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
while (myFgets(buffer, bufferSize, fp)
|
while (myFgets(buffer, bufferSize, fp)
|
||||||
&& prefixcmp(buffer, DIRECTORY_END)) {
|
&& !g_str_has_prefix(buffer, DIRECTORY_END)) {
|
||||||
if (!prefixcmp(buffer, DIRECTORY_DIR)) {
|
if (g_str_has_prefix(buffer, DIRECTORY_DIR)) {
|
||||||
struct directory *subdir;
|
struct directory *subdir;
|
||||||
|
|
||||||
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
|
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
|
||||||
if (!myFgets(buffer, bufferSize, fp))
|
if (!myFgets(buffer, bufferSize, fp))
|
||||||
FATAL("Error reading db, fgets\n");
|
FATAL("Error reading db, fgets\n");
|
||||||
/* for compatibility with db's prior to 0.11 */
|
/* 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))
|
if (!myFgets(buffer, bufferSize, fp))
|
||||||
FATAL("Error reading db, fgets\n");
|
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);
|
FATAL("Error reading db at line: %s\n", buffer);
|
||||||
name = &(buffer[strlen(DIRECTORY_BEGIN)]);
|
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",
|
FATAL("Wrong path in database: '%s' in '%s'\n",
|
||||||
name, directory->path);
|
name, directory->path);
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ directory_load(FILE *fp, struct directory *directory)
|
|||||||
dirvec_add(&directory->children, subdir);
|
dirvec_add(&directory->children, subdir);
|
||||||
}
|
}
|
||||||
directory_load(fp, 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);
|
readSongInfoIntoList(fp, &directory->songs, directory);
|
||||||
} else {
|
} else {
|
||||||
FATAL("Unknown line in db: %s\n", buffer);
|
FATAL("Unknown line in db: %s\n", buffer);
|
||||||
|
3
src/ls.c
3
src/ls.c
@ -21,7 +21,6 @@
|
|||||||
#include "path.h"
|
#include "path.h"
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "utils.h"
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "stored_playlist.h"
|
#include "stored_playlist.h"
|
||||||
#include "os_compat.h"
|
#include "os_compat.h"
|
||||||
@ -92,7 +91,7 @@ int isRemoteUrl(const char *url)
|
|||||||
|
|
||||||
while (*urlPrefixes) {
|
while (*urlPrefixes) {
|
||||||
count++;
|
count++;
|
||||||
if (!prefixcmp(url, *urlPrefixes))
|
if (g_str_has_prefix(url, *urlPrefixes))
|
||||||
return count;
|
return count;
|
||||||
urlPrefixes++;
|
urlPrefixes++;
|
||||||
}
|
}
|
||||||
|
@ -46,12 +46,13 @@
|
|||||||
#include "tag.h"
|
#include "tag.h"
|
||||||
#include "dbUtils.h"
|
#include "dbUtils.h"
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
#include "utils.h"
|
|
||||||
#include "normalize.h"
|
#include "normalize.h"
|
||||||
#include "zeroconf.h"
|
#include "zeroconf.h"
|
||||||
#include "main_notify.h"
|
#include "main_notify.h"
|
||||||
#include "os_compat.h"
|
#include "os_compat.h"
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
#define SYSTEM_CONFIG_FILE_LOCATION "/etc/mpd.conf"
|
#define SYSTEM_CONFIG_FILE_LOCATION "/etc/mpd.conf"
|
||||||
#define USER_CONFIG_FILE_LOCATION "/.mpdconf"
|
#define USER_CONFIG_FILE_LOCATION "/.mpdconf"
|
||||||
|
|
||||||
@ -156,7 +157,7 @@ static void parseOptions(int argc, char **argv, Options * options)
|
|||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while (i < argc) {
|
while (i < argc) {
|
||||||
if (!prefixcmp(argv[i], "--")) {
|
if (g_str_has_prefix(argv[i], "--")) {
|
||||||
if (strcmp(argv[i], "--help") == 0) {
|
if (strcmp(argv[i], "--help") == 0) {
|
||||||
usage(argv);
|
usage(argv);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
@ -301,7 +301,7 @@ void readPlaylistState(FILE *fp)
|
|||||||
char buffer[PLAYLIST_BUFFER_SIZE];
|
char buffer[PLAYLIST_BUFFER_SIZE];
|
||||||
|
|
||||||
while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
|
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)]),
|
if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
|
||||||
PLAYLIST_STATE_FILE_STATE_PLAY) == 0) {
|
PLAYLIST_STATE_FILE_STATE_PLAY) == 0) {
|
||||||
state = PLAYER_STATE_PLAY;
|
state = PLAYER_STATE_PLAY;
|
||||||
@ -312,24 +312,24 @@ void readPlaylistState(FILE *fp)
|
|||||||
== 0) {
|
== 0) {
|
||||||
state = PLAYER_STATE_PAUSE;
|
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 =
|
seek_time =
|
||||||
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
|
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
|
||||||
} else
|
} else
|
||||||
if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_REPEAT)) {
|
if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) {
|
||||||
if (strcmp
|
if (strcmp
|
||||||
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
|
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
|
||||||
"1") == 0) {
|
"1") == 0) {
|
||||||
setPlaylistRepeatStatus(true);
|
setPlaylistRepeatStatus(true);
|
||||||
} else
|
} else
|
||||||
setPlaylistRepeatStatus(false);
|
setPlaylistRepeatStatus(false);
|
||||||
} else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
|
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
|
||||||
setPlayerCrossFade(atoi
|
setPlayerCrossFade(atoi
|
||||||
(&
|
(&
|
||||||
(buffer
|
(buffer
|
||||||
[strlen
|
[strlen
|
||||||
(PLAYLIST_STATE_FILE_CROSSFADE)])));
|
(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
|
if (strcmp
|
||||||
(&
|
(&
|
||||||
(buffer
|
(buffer
|
||||||
@ -338,14 +338,14 @@ void readPlaylistState(FILE *fp)
|
|||||||
setPlaylistRandomStatus(true);
|
setPlaylistRandomStatus(true);
|
||||||
} else
|
} else
|
||||||
setPlaylistRandomStatus(false);
|
setPlaylistRandomStatus(false);
|
||||||
} else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_CURRENT)) {
|
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) {
|
||||||
if (strlen(buffer) ==
|
if (strlen(buffer) ==
|
||||||
strlen(PLAYLIST_STATE_FILE_CURRENT))
|
strlen(PLAYLIST_STATE_FILE_CURRENT))
|
||||||
state_file_fatal();
|
state_file_fatal();
|
||||||
current = atoi(&(buffer
|
current = atoi(&(buffer
|
||||||
[strlen
|
[strlen
|
||||||
(PLAYLIST_STATE_FILE_CURRENT)]));
|
(PLAYLIST_STATE_FILE_CURRENT)]));
|
||||||
} else if (!prefixcmp(buffer,
|
} else if (g_str_has_prefix(buffer,
|
||||||
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
|
PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) {
|
||||||
if (state == PLAYER_STATE_STOP)
|
if (state == PLAYER_STATE_STOP)
|
||||||
current = -1;
|
current = -1;
|
||||||
|
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)))
|
if ((err = pthread_cond_destroy(cond)))
|
||||||
FATAL("failed to destroy cond: %s\n", strerror(err));
|
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);
|
void xpthread_cond_destroy(pthread_cond_t *cond);
|
||||||
|
|
||||||
int prefixcmp(const char *str, const char *prefix);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
|
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
#ifdef HAVE_OSS
|
#ifdef HAVE_OSS
|
||||||
#include <sys/soundcard.h>
|
#include <sys/soundcard.h>
|
||||||
#endif
|
#endif
|
||||||
@ -518,7 +520,7 @@ void read_sw_volume_state(FILE *fp)
|
|||||||
if (volume_mixerType != VOLUME_MIXER_TYPE_SOFTWARE)
|
if (volume_mixerType != VOLUME_MIXER_TYPE_SOFTWARE)
|
||||||
return;
|
return;
|
||||||
while (myFgets(buf, sizeof(buf), fp)) {
|
while (myFgets(buf, sizeof(buf), fp)) {
|
||||||
if (prefixcmp(buf, SW_VOLUME_STATE))
|
if (!g_str_has_prefix(buf, SW_VOLUME_STATE))
|
||||||
continue;
|
continue;
|
||||||
sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10);
|
sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10);
|
||||||
if (mpd_likely(!*end))
|
if (mpd_likely(!*end))
|
||||||
|
Loading…
Reference in New Issue
Block a user