From 95817fee8a01f1cdef2f0fb66d351dd031b91c7e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 20 Sep 2008 16:07:54 -0700 Subject: start using prefixcmp() LOC reduction and less noise makes things easier for tired old folks to follow. --- src/audio.c | 3 +-- src/directory.c | 23 ++++++++--------------- src/inputPlugins/mp3_plugin.c | 2 +- src/ls.c | 3 +-- src/main.c | 2 +- src/playlist.c | 31 ++++++++----------------------- src/song.c | 11 +++++------ src/volume.c | 12 ++++-------- 8 files changed, 29 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/audio.c b/src/audio.c index 5eacf7422..a10f5cf42 100644 --- a/src/audio.c +++ b/src/audio.c @@ -24,7 +24,6 @@ #include "os_compat.h" #define AUDIO_DEVICE_STATE "audio_device_state:" -#define AUDIO_DEVICE_STATE_LEN (sizeof(AUDIO_DEVICE_STATE)-1) #define AUDIO_BUFFER_SIZE 2*MPD_PATH_MAX static AudioFormat audio_format; @@ -483,7 +482,7 @@ void readAudioDevicesState(FILE *fp) while (myFgets(buffer, AUDIO_BUFFER_SIZE, fp)) { char *c, *name; - if (strncmp(buffer, AUDIO_DEVICE_STATE, AUDIO_DEVICE_STATE_LEN)) + if (prefixcmp(buffer, AUDIO_DEVICE_STATE)) continue; c = strchr(buffer, ':'); diff --git a/src/directory.c b/src/directory.c index 06a39d7bf..dbf578857 100644 --- a/src/directory.c +++ b/src/directory.c @@ -895,22 +895,18 @@ static void readDirectoryInfo(FILE * fp, Directory * directory) ListNode *nodeTemp; while (myFgets(buffer, bufferSize, fp) - && 0 != strncmp(DIRECTORY_END, buffer, strlen(DIRECTORY_END))) { - if (0 == strncmp(DIRECTORY_DIR, buffer, strlen(DIRECTORY_DIR))) { + && prefixcmp(buffer, DIRECTORY_END)) { + if (!prefixcmp(buffer, DIRECTORY_DIR)) { 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 (0 == strncmp(DIRECTORY_MTIME, buffer, - strlen(DIRECTORY_MTIME))) { + if (!prefixcmp(buffer, DIRECTORY_MTIME)) { if (!myFgets(buffer, bufferSize, fp)) FATAL("Error reading db, fgets\n"); } - if (strncmp - (DIRECTORY_BEGIN, buffer, - strlen(DIRECTORY_BEGIN))) { + if (prefixcmp(buffer, DIRECTORY_BEGIN)) FATAL("Error reading db at line: %s\n", buffer); - } name = &(buffer[strlen(DIRECTORY_BEGIN)]); while (nextDirNode && (strcmpRet = @@ -938,7 +934,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory) } readDirectoryInfo(fp, subDirectory); - } else if (0 == strncmp(SONG_BEGIN, buffer, strlen(SONG_BEGIN))) { + } else if (!prefixcmp(buffer, SONG_BEGIN)) { readSongInfoIntoList(fp, directory->songs, directory); } else { FATAL("Unknown line in db: %s\n", buffer); @@ -1097,16 +1093,13 @@ int readDirectoryDB(void) if (0 == strcmp(DIRECTORY_INFO_BEGIN, buffer)) { while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(DIRECTORY_INFO_END, buffer)) { - if (0 == strncmp(DIRECTORY_MPD_VERSION, buffer, - strlen(DIRECTORY_MPD_VERSION))) + if (!prefixcmp(buffer, DIRECTORY_MPD_VERSION)) { if (foundVersion) FATAL("already found version in db\n"); foundVersion = 1; - } else if (0 == - strncmp(DIRECTORY_FS_CHARSET, buffer, - strlen - (DIRECTORY_FS_CHARSET))) { + } else if (!prefixcmp(buffer, + DIRECTORY_FS_CHARSET)) { char *fsCharset; char *tempCharset; diff --git a/src/inputPlugins/mp3_plugin.c b/src/inputPlugins/mp3_plugin.c index a9dad8904..1d6333fd6 100644 --- a/src/inputPlugins/mp3_plugin.c +++ b/src/inputPlugins/mp3_plugin.c @@ -616,7 +616,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 (strncmp(lame->encoder, "LAME", 4) != 0) + if (prefixcmp(lame->encoder, "LAME")) return 0; if (sscanf(lame->encoder+4, "%u.%u", diff --git a/src/ls.c b/src/ls.c index f87e09fdb..d7cef0bfe 100644 --- a/src/ls.c +++ b/src/ls.c @@ -89,9 +89,8 @@ int isRemoteUrl(const char *url) while (*urlPrefixes) { count++; - if (strncmp(*urlPrefixes, url, strlen(*urlPrefixes)) == 0) { + if (!prefixcmp(url, *urlPrefixes)) return count; - } urlPrefixes++; } diff --git a/src/main.c b/src/main.c index a5941b848..dcfb7f64d 100644 --- a/src/main.c +++ b/src/main.c @@ -149,7 +149,7 @@ static void parseOptions(int argc, char **argv, Options * options) if (argc > 1) { int i = 1; while (i < argc) { - if (strncmp(argv[i], "--", 2) == 0) { + if (!prefixcmp(argv[i], "--")) { if (strcmp(argv[i], "--help") == 0) { usage(argv); exit(EXIT_SUCCESS); diff --git a/src/playlist.c b/src/playlist.c index e62e71583..d454b4ad2 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -323,8 +323,7 @@ void readPlaylistState(FILE *fp) char buffer[PLAYLIST_BUFFER_SIZE]; while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { - if (strncmp(buffer, PLAYLIST_STATE_FILE_STATE, - strlen(PLAYLIST_STATE_FILE_STATE)) == 0) { + if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_STATE)) { if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]), PLAYLIST_STATE_FILE_STATE_PLAY) == 0) { state = OB_STATE_PLAY; @@ -335,31 +334,22 @@ void readPlaylistState(FILE *fp) == 0) { state = OB_STATE_PAUSE; } - } else if (strncmp(buffer, PLAYLIST_STATE_FILE_TIME, - strlen(PLAYLIST_STATE_FILE_TIME)) == 0) { + } else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_TIME)) { seek_time = atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)])); } else - if (strncmp - (buffer, PLAYLIST_STATE_FILE_REPEAT, - strlen(PLAYLIST_STATE_FILE_REPEAT)) == 0) { + if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_REPEAT)) { if (strcmp (&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]), "1") == 0) { setPlaylistRepeatStatus(1); } else setPlaylistRepeatStatus(0); - } else - if (strncmp - (buffer, PLAYLIST_STATE_FILE_CROSSFADE, - strlen(PLAYLIST_STATE_FILE_CROSSFADE)) == 0) { + } else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) { ob_set_xfade(atoi(&(buffer [strlen (PLAYLIST_STATE_FILE_CROSSFADE)]))); - } else - if (strncmp - (buffer, PLAYLIST_STATE_FILE_RANDOM, - strlen(PLAYLIST_STATE_FILE_RANDOM)) == 0) { + } else if (!prefixcmp(buffer, PLAYLIST_STATE_FILE_RANDOM)) { if (strcmp (& (buffer @@ -368,20 +358,15 @@ void readPlaylistState(FILE *fp) setPlaylistRandomStatus(1); } else setPlaylistRandomStatus(0); - } else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT, - strlen(PLAYLIST_STATE_FILE_CURRENT)) - == 0) { + } else if (!prefixcmp(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 (strncmp - (buffer, PLAYLIST_STATE_FILE_PLAYLIST_BEGIN, - strlen(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN) - ) == 0) { + } else if (!prefixcmp(buffer, + PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) { if (state == OB_STATE_STOP) current = -1; loadPlaylistFromStateFile(fp, buffer, state, diff --git a/src/song.c b/src/song.c index ab900a6b4..ff0ada620 100644 --- a/src/song.c +++ b/src/song.c @@ -218,8 +218,7 @@ static int matchesAnMpdTagItemKey(char *buffer, int *itemType) int i; for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) { - if (0 == strncmp(mpdTagItemKeys[i], buffer, - strlen(mpdTagItemKeys[i]))) { + if (!prefixcmp(buffer, mpdTagItemKeys[i])) { *itemType = i; return 1; } @@ -238,7 +237,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) int itemType; while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) { - if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) { + if (!prefixcmp(buffer, SONG_KEY)) { if (song) { insertSongIntoList(list, &nextSongNode, song->url, song); @@ -254,7 +253,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) /* ignore empty lines (starting with '\0') */ } else if (song == NULL) { FATAL("Problems reading song info\n"); - } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) { + } else if (!prefixcmp(buffer, SONG_FILE)) { /* we don't need this info anymore song->url = xstrdup(&(buffer[strlen(SONG_FILE)])); */ @@ -268,14 +267,14 @@ void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) &(buffer [strlen(mpdTagItemKeys[itemType]) + 2])); - } else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) { + } else if (!prefixcmp(buffer, SONG_TIME)) { if (!song->tag) { song->tag = tag_new(); tag_begin_add(song->tag); } song->tag->time = atoi(&(buffer[strlen(SONG_TIME)])); - } else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) { + } else if (!prefixcmp(buffer, SONG_MTIME)) { song->mtime = atoi(&(buffer[strlen(SONG_MTIME)])); } else diff --git a/src/volume.c b/src/volume.c index 6d8411165..bdb911916 100644 --- a/src/volume.c +++ b/src/volume.c @@ -511,26 +511,22 @@ int changeVolumeLevel(int change, int rel) void read_sw_volume_state(FILE *fp) { - /* strlen(SW_VOLUME_STATE) + strlen('100') + '\0' */ - #define bufsize 16 - char buf[bufsize]; - const size_t len = strlen(SW_VOLUME_STATE); + char buf[sizeof(SW_VOLUME_STATE) + sizeof("100") - 1]; char *end = NULL; long int sv; if (volume_mixerType != VOLUME_MIXER_TYPE_SOFTWARE) return; - while (myFgets(buf, bufsize, fp)) { - if (strncmp(buf, SW_VOLUME_STATE, len)) + while (myFgets(buf, sizeof(buf), fp)) { + if (prefixcmp(buf, SW_VOLUME_STATE)) continue; - sv = strtol(buf + len, &end, 10); + sv = strtol(buf + strlen(SW_VOLUME_STATE), &end, 10); if (mpd_likely(!*end)) changeSoftwareVolume(sv, 0); else ERROR("Can't parse software volume: %s\n", buf); return; } - #undef bufsize } void save_sw_volume_state(int fd) -- cgit v1.2.3