Code

command.c: turn while loops into for loops
authorJonathan Neuschäfer <j.neuschaefer@gmx.net>
Wed, 21 Dec 2011 20:40:52 +0000 (21:40 +0100)
committerJonathan Neuschäfer <j.neuschaefer@gmx.net>
Wed, 21 Dec 2011 20:40:52 +0000 (21:40 +0100)
src/command.c

index 6d06368123e47918c02dcd083eae8e0b7a124cbe..af6afd15a01ee7e93ab01a31e72cfa6a4f301e2d 100644 (file)
@@ -326,13 +326,9 @@ key2str(int key)
 void
 command_dump_keys(void)
 {
-       int i = 0;
-
-       while (cmds[i].description) {
+       for (int i = 0; cmds[i].description; i++)
                if (cmds[i].command != CMD_NONE)
                        printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
-               i++;
-       }
 }
 
 #ifndef NCMPC_MINI
@@ -340,14 +336,11 @@ command_dump_keys(void)
 static int
 set_key_flags(command_definition_t *cp, command_t command, int flags)
 {
-       int i = 0;
-
-       while (cp[i].name) {
+       for (int i = 0; cp[i].name; i++) {
                if (cp[i].command == command) {
                        cp[i].flags |= flags;
                        return 0;
                }
-               i++;
        }
 
        return 1;
@@ -358,25 +351,21 @@ set_key_flags(command_definition_t *cp, command_t command, int flags)
 const char *
 get_key_names(command_t command, int all)
 {
-       int i = 0;
-
-       while (cmds[i].description) {
+       for (int i = 0; cmds[i].description; i++) {
                if (cmds[i].command == command) {
-                       int j;
                        static char keystr[80];
 
                        g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
                        if (!all)
                                return keystr;
-                       j=1;
-                       while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
+
+                       for (int j = 1; j < MAX_COMMAND_KEYS &&
+                                       cmds[i].keys[j] > 0; j++) {
                                g_strlcat(keystr, " ", sizeof(keystr));
                                g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
-                               j++;
                        }
                        return keystr;
                }
-               i++;
        }
        return NULL;
 }
@@ -384,13 +373,9 @@ get_key_names(command_t command, int all)
 const char *
 get_key_description(command_t command)
 {
-       int i = 0;
-
-       while (cmds[i].description) {
+       for (int i = 0; cmds[i].description; i++)
                if (cmds[i].command == command)
                        return _(cmds[i].description);
-               i++;
-       }
 
        return NULL;
 }
@@ -398,26 +383,19 @@ get_key_description(command_t command)
 const char *
 get_key_command_name(command_t command)
 {
-       int i = 0;
-
-       while (cmds[i].name) {
+       for (int i = 0; cmds[i].name; i++)
                if (cmds[i].command == command)
                        return cmds[i].name;
-               i++;
-       }
+
        return NULL;
 }
 
 command_t
 get_key_command_from_name(char *name)
 {
-       int i = 0;
-
-       while (cmds[i].name) {
+       for (int i = 0; cmds[i].name; i++)
                if (strcmp(name, cmds[i].name) == 0)
                        return cmds[i].command;
-               i++;
-       }
 
        return CMD_NONE;
 }
@@ -425,17 +403,14 @@ get_key_command_from_name(char *name)
 command_t
 find_key_command(int key, command_definition_t *c)
 {
-       int i = 0;
-
        assert(key != 0);
        assert(c != NULL);
 
-       while (c[i].name) {
+       for (int i = 0; c[i].name; i++) {
                if (c[i].keys[0] == key ||
                    c[i].keys[1] == key ||
                    c[i].keys[2] == key)
                        return c[i].command;
-               i++;
        }
 
        return CMD_NONE;
@@ -467,9 +442,7 @@ get_keyboard_command(void)
 int
 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
 {
-       int i = 0;
-
-       while (cmds[i].name) {
+       for (int i = 0; cmds[i].name; i++) {
                if (cmds[i].command == command) {
                        memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
 #ifndef NCMPC_MINI
@@ -477,8 +450,8 @@ assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
 #endif
                        return 0;
                }
-               i++;
        }
+
        return -1;
 }
 
@@ -530,19 +503,17 @@ check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
 int
 write_key_bindings(FILE *f, int flags)
 {
-       int i = 0, j;
-
        if (flags & KEYDEF_WRITE_HEADER)
                fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
 
-       while (cmds[i].name && !ferror(f)) {
+       for (int i = 0; cmds[i].name && !ferror(f); i++) {
                if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
                    flags & KEYDEF_WRITE_ALL) {
                        fprintf(f, "## %s\n", cmds[i].description);
                        if (flags & KEYDEF_COMMENT_ALL)
                                fprintf(f, "#");
                        fprintf(f, "key %s = ", cmds[i].name);
-                       for (j = 0; j < MAX_COMMAND_KEYS; j++) {
+                       for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
                                if (j && cmds[i].keys[j])
                                        fprintf(f, ",  ");
                                if (!j || cmds[i].keys[j]) {
@@ -555,7 +526,6 @@ write_key_bindings(FILE *f, int flags)
                        }
                        fprintf(f,"\n\n");
                }
-               i++;
        }
 
        return ferror(f);