]> git.sur5r.net Git - i3/i3status/commitdiff
Unify use of string comparisons
authorMats <d912e3@gmail.com>
Tue, 11 Mar 2014 19:15:36 +0000 (20:15 +0100)
committerMichael Stapelberg <michael@stapelberg.de>
Wed, 12 Mar 2014 07:28:16 +0000 (08:28 +0100)
* strncmp(s1, s2, strlen(s2)) → BEGINS_WITH(s1, s2)
* strncmp(s1, s2, strlen(s1)) → strcmp(s1, s2)
* Prefer case-insensitive comparison for options

src/print_battery_info.c
src/print_cpu_temperature.c
src/print_cpu_usage.c
src/print_disk_info.c
src/print_eth_info.c
src/print_ipv6_addr.c
src/print_path_exists.c
src/print_run_watch.c

index aaef46eb04d71007c04c6e760aaaaff8ecce4c00..72d291ee8d4c0968b5b82abbf2482ccad9b2d918 100644 (file)
@@ -153,11 +153,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
                 seconds -= (minutes * 60);
 
                 if (status == CS_DISCHARGING && low_threshold > 0) {
-                        if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0
+                        if (strcasecmp(threshold_type, "percentage") == 0
                                 && percentage_remaining < low_threshold) {
                                 START_COLOR("color_bad");
                                 colorful_output = true;
-                        } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0
+                        } else if (strcasecmp(threshold_type, "time") == 0
                                 && seconds_remaining < 60 * low_threshold) {
                                 START_COLOR("color_bad");
                                 colorful_output = true;
@@ -191,7 +191,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
                  * we colorize the output if threshold_type is set to percentage
                  * (since we don't have any information on remaining time). */
                 if (status == CS_DISCHARGING && low_threshold > 0) {
-                        if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0
+                        if (strcasecmp(threshold_type, "percentage") == 0
                                 && percentage_remaining < low_threshold) {
                                 START_COLOR("color_bad");
                                 colorful_output = true;
@@ -242,11 +242,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
                 minutes -= (hours * 60);
                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
                                max(hours, 0), max(minutes, 0));
-               if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0
+               if (strcasecmp(threshold_type, "percentage") == 0
                    && present_rate < low_threshold) {
                        START_COLOR("color_bad");
                        colorful_output = true;
-               } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0
+               } else if (strcasecmp(threshold_type, "time") == 0
                           && remaining < (u_int) low_threshold) {
                        START_COLOR("color_bad");
                        colorful_output = true;
@@ -296,11 +296,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
        (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.00d%%", apm_info.battery_life);
 
        if (status == CS_DISCHARGING && low_threshold > 0) {
-               if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0
+               if (strcasecmp(threshold_type, "percentage") == 0
                    && apm_info.battery_life < low_threshold) {
                        START_COLOR("color_bad");
                        colorful_output = true;
-               } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0
+               } else if (strcasecmp(threshold_type, "time") == 0
                           && apm_info.minutes_left < (u_int) low_threshold) {
                        START_COLOR("color_bad");
                        colorful_output = true;
@@ -334,21 +334,21 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
                         continue;
                 }
 
-                if (strncmp(walk+1, "status", strlen("status")) == 0) {
+                if (BEGINS_WITH(walk+1, "status")) {
                         outwalk += sprintf(outwalk, "%s", statusbuf);
                         walk += strlen("status");
-                } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "percentage")) {
                         outwalk += sprintf(outwalk, "%s", percentagebuf);
                         walk += strlen("percentage");
-                } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "remaining")) {
                         outwalk += sprintf(outwalk, "%s", remainingbuf);
                         walk += strlen("remaining");
                         EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf);
-                } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "emptytime")) {
                         outwalk += sprintf(outwalk, "%s", emptytimebuf);
                         walk += strlen("emptytime");
                         EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf);
-                } else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "consumption")) {
                         outwalk += sprintf(outwalk, "%s", consumptionbuf);
                         walk += strlen("consumption");
                         EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf);
index 306ad1d5a2684786dc1402ae597d1da138024e7d..48bbf91bd16cdb163525332fa050807acacfd984 100644 (file)
@@ -117,7 +117,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
                         goto error;
                 }
                 /* 'path' is the node within the full path (defaults to acpitz0). */
-                if (strncmp(sensordev.xname, thermal_zone, strlen(thermal_zone)) == 0) {
+                if (BEGINS_WITH(sensordev.xname, thermal_zone)) {
                         mib[3] = SENSOR_TEMP;
                         /* Limit to temo0, but should retrieve from a full path... */
                         for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
index 80944c54efad7534a9217cc895e9501a26f84df5..26a9d4f44084a95c887c18f8593606512560981c 100644 (file)
@@ -97,7 +97,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
                         continue;
                 }
 
-                if (strncmp(walk+1, "usage", strlen("usage")) == 0) {
+                if (BEGINS_WITH(walk+1, "usage")) {
                         outwalk += sprintf(outwalk, "%02d%%", diff_usage);
                         walk += strlen("usage");
                 }
index d58683908673308241f7a134955de9e07ee0498f..167121045cebcced788d7b8d810bccffdc53bf14 100644 (file)
@@ -43,9 +43,9 @@ static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char
  *
  */
 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
-        if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
+        if (strcasecmp(prefix_type, "decimal") == 0) {
                 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
-        } else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
+        } else if (strcasecmp(prefix_type, "custom") == 0) {
                 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
         } else {
                 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
index 45a44a4b9a8aaea6c38126dede1b26f619965853..c446da499fb45bb549b955d6721a402bfb5e9777 100644 (file)
@@ -90,8 +90,8 @@ static int print_eth_speed(char *outwalk, const char *interface) {
                 * Skip these non-informative values and go right ahead to the
                 * actual speeds.
                 */
-               if (strncmp(desc->ifmt_string, "autoselect", strlen("autoselect")) == 0 ||
-                   strncmp(desc->ifmt_string, "auto", strlen("auto")) == 0)
+               if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
+                   BEGINS_WITH(desc->ifmt_string, "auto"))
                        continue;
 
                if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
@@ -131,10 +131,10 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons
                         continue;
                 }
 
-                if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
+                if (BEGINS_WITH(walk+1, "ip")) {
                         outwalk += sprintf(outwalk, "%s", ip_address);
                         walk += strlen("ip");
-                } else if (strncmp(walk+1, "speed", strlen("speed")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "speed")) {
                         outwalk += print_eth_speed(outwalk, interface);
                         walk += strlen("speed");
                 }
index fa50ea5791eb4994f3c16ef44dcff99f55c069a7..7ff9c97120aa16bd4f94918bb4f2fcb76dcb414d 100644 (file)
@@ -136,7 +136,7 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con
                         continue;
                 }
 
-                if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
+                if (BEGINS_WITH(walk+1, "ip")) {
                         outwalk += sprintf(outwalk, "%s", addr_string);
                         walk += strlen("ip");
                 }
index c5225a137405e4c3982666ce11ecd7fe0524a4f1..a2fcb149b041dcb163e1f71761c8e7ea49c3c606 100644 (file)
@@ -21,10 +21,10 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const
                         continue;
                 }
 
-                if (strncmp(walk+1, "title", strlen("title")) == 0) {
+                if (BEGINS_WITH(walk+1, "title")) {
                         outwalk += sprintf(outwalk, "%s", title);
                         walk += strlen("title");
-                } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "status")) {
                         outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
                         walk += strlen("status");
                 }
index 893e2cd80f36d8b76cc7da679ab4ef6b65c85c3f..bc0d6dc1635037bd5fbf54b7ec04f2f0131ba2dd 100644 (file)
@@ -19,10 +19,10 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c
                         continue;
                 }
 
-                if (strncmp(walk+1, "title", strlen("title")) == 0) {
+                if (BEGINS_WITH(walk+1, "title")) {
                        outwalk += sprintf(outwalk, "%s", title);
                         walk += strlen("title");
-                } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
+                } else if (BEGINS_WITH(walk+1, "status")) {
                        outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no"));
                         walk += strlen("status");
                 }