diff mbox

[4/5] amixer: arrange validation logic

Message ID 1428510659-30393-5-git-send-email-o-takashi@sakamocchi.jp (mailing list archive)
State New, archived
Headers show

Commit Message

Takashi Sakamoto April 8, 2015, 4:30 p.m. UTC
For better reading and easy understanding.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 amixer/amixer.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

Comments

Takashi Iwai April 9, 2015, 6:17 a.m. UTC | #1
At Thu,  9 Apr 2015 01:30:57 +0900,
Takashi Sakamoto wrote:
> 
> For better reading and easy understanding.
> 
> Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
> ---
>  amixer/amixer.c | 28 ++++++++++++++++++----------
>  1 file changed, 18 insertions(+), 10 deletions(-)
> 
> diff --git a/amixer/amixer.c b/amixer/amixer.c
> index 65ebf20..aec8d01 100644
> --- a/amixer/amixer.c
> +++ b/amixer/amixer.c
> @@ -1271,14 +1271,18 @@ static int get_enum_item_index(snd_mixer_elem_t *elem, char **ptrp)
>  		if (snd_mixer_selem_get_enum_item_name(elem, i, sizeof(name)-1, name) < 0)
>  			continue;
>  
> +		/* Check given strings itself. */
>  		len = strlen(name);
> -		if (! strncmp(name, ptr, len)) {
> -			if (! ptr[len] || ptr[len] == ',' || ptr[len] == '\n') {
> -				ptr += len;
> -				*ptrp = ptr;
> -				return i;
> -			}
> -		}
> +		if (strncmp(name, ptr, len) != 0)
> +			continue;
> +
> +		/* Lack of separators between channels. */
> +		if (ptr[len] != '\0' && ptr[len] != ',' && ptr[len] != '\n')
> +			continue;

In general, the negation makes the logic harder to follow.
That is, a code like below (it's almost same as the original one) is
easier, more understandable.

		/* EOS or separator? */
		if (ptr[len] || ptr[len] == ',' || ptr[len] == '\n') {
			*ptrp = ptr + len;
			return i;
		}


>  	}
>  	return -1;
>  }
> @@ -1294,9 +1298,9 @@ static int sset_enum(snd_mixer_elem_t *elem, unsigned int argc, char **argv)
>  	for (idx = 1; idx < argc; idx++) {
>  		ptr = argv[idx];
>  		chn = 0;
> -		while (*ptr) {
> +		while (ptr[0] != '\0') {

ptr[0] is less common expression than *ptr, IMO.


Takashi

>  			/* Skip separators between the value of each channel. */
> -			while (*ptr == ',' || isspace(*ptr)) {
> +			while (ptr[0] == ',' || isspace(ptr[0])) {
>  				ptr++;
>  				chn++;
>  			}
> @@ -1304,8 +1308,12 @@ static int sset_enum(snd_mixer_elem_t *elem, unsigned int argc, char **argv)
>  			/* Get index for given argument as enumerated item. */
>  			ival = get_enum_item_index(elem, &ptr);
>  			if (ival < 0)
> -				return check_flag;
> +				break;
>  
> +			/*
> +			 * Stand success flag when at least one channel is
> +			 * changed.
> +			 */
>  			if (snd_mixer_selem_set_enum_item(elem, chn, ival) >= 0)
>  				check_flag = 1;
>  		}
> -- 
> 2.1.0
>
diff mbox

Patch

diff --git a/amixer/amixer.c b/amixer/amixer.c
index 65ebf20..aec8d01 100644
--- a/amixer/amixer.c
+++ b/amixer/amixer.c
@@ -1271,14 +1271,18 @@  static int get_enum_item_index(snd_mixer_elem_t *elem, char **ptrp)
 		if (snd_mixer_selem_get_enum_item_name(elem, i, sizeof(name)-1, name) < 0)
 			continue;
 
+		/* Check given strings itself. */
 		len = strlen(name);
-		if (! strncmp(name, ptr, len)) {
-			if (! ptr[len] || ptr[len] == ',' || ptr[len] == '\n') {
-				ptr += len;
-				*ptrp = ptr;
-				return i;
-			}
-		}
+		if (strncmp(name, ptr, len) != 0)
+			continue;
+
+		/* Lack of separators between channels. */
+		if (ptr[len] != '\0' && ptr[len] != ',' && ptr[len] != '\n')
+			continue;
+
+		/* OK. The string is exactly one of items. */
+		*ptrp = ptr + len;
+		return i;
 	}
 	return -1;
 }
@@ -1294,9 +1298,9 @@  static int sset_enum(snd_mixer_elem_t *elem, unsigned int argc, char **argv)
 	for (idx = 1; idx < argc; idx++) {
 		ptr = argv[idx];
 		chn = 0;
-		while (*ptr) {
+		while (ptr[0] != '\0') {
 			/* Skip separators between the value of each channel. */
-			while (*ptr == ',' || isspace(*ptr)) {
+			while (ptr[0] == ',' || isspace(ptr[0])) {
 				ptr++;
 				chn++;
 			}
@@ -1304,8 +1308,12 @@  static int sset_enum(snd_mixer_elem_t *elem, unsigned int argc, char **argv)
 			/* Get index for given argument as enumerated item. */
 			ival = get_enum_item_index(elem, &ptr);
 			if (ival < 0)
-				return check_flag;
+				break;
 
+			/*
+			 * Stand success flag when at least one channel is
+			 * changed.
+			 */
 			if (snd_mixer_selem_set_enum_item(elem, chn, ival) >= 0)
 				check_flag = 1;
 		}