i3 - improved tiling WM


Reduce calls to strncmp in print_battery_info.c.

Patch status: needinfo

Patch by Kim Svensson ks6g10

Long description:

Just some tweaks which reduces the number of calls to strncmp in print_battery_info.c.

1. In the for loop around line 67, added a check on walk, if walk point to the null char '\0' then break, as this prevents it from parsing past the information given by slurp. Before it always ran 1024(buf size).

2. Added a check on each line which checks if the variable have
 already been set, if so no need to do the strncmp and just jump to next
 branch.

3. Also changed the last strncmp branch from: 
if (!BEGINS_WITH(last,"POWER_SUPPLY_ENERGY_FULL") &&                                    
 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))

to

if (!BEGINS_WITH(last,"POWER_SUPPLY_ENERGY_FULL=") &&                                    
 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))

Reason is that it is not mutually exclusive with the line below

if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
    !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))

hence full_design = atoi(walk+1); is set twice.

If 2 is accepted, 3 have to be accepted as well, else the battery will not show the correct information as the first time full_design is set,  it is for the wrong value if one have last_full_capacity enabled.

To apply this patch, use:
curl http://cr.i3wm.org/patch/446/raw.patch | git am

b/src/print_battery_info.c

39
@@ -65,7 +65,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
40
                 return;
41
         }
42
 
43
-        for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
44
+        for (walk = buf, last = buf; (walk-buf) < 1024 && *walk != '\0'; walk++) {
45
                 if (*walk == '\n') {
46
                         last = walk+1;
47
                         continue;
48
@@ -74,40 +74,40 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
49
                 if (*walk != '=')
50
                         continue;
51
 
52
-                if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
53
+                if (remaining == -1 && BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
54
                         watt_as_unit = true;
55
                         remaining = atoi(walk+1);
56
                 }
57
-                else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
58
+                else if (remaining == -1 && BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
59
                         watt_as_unit = false;
60
                         remaining = atoi(walk+1);
61
                 }
62
-                else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
63
+                else if ( present_rate == -1 &&  BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
64
                         present_rate = atoi(walk+1);
65
-                else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
66
+                else if ( voltage == -1 &&  BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
67
                         voltage = atoi(walk+1);
68
                 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
69
                  * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
70
                  * unit instead of μAh. We will calculate it as we need it
71
                  * later. */
72
-                else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
73
+                else if ( present_rate == -1 &&  BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
74
                         present_rate = atoi(walk+1);
75
-                else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
76
+                else if ( status == CS_DISCHARGING &&  BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
77
                         status = CS_CHARGING;
78
-                else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
79
+                else if (status == CS_DISCHARGING && BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full")) {
80
                         status = CS_FULL;
81
-                else {
82
+		}
83
+                else if(full_design == -1){
84
                         /* The only thing left is the full capacity */
85
                         if (last_full_capacity) {
86
-                                if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
87
-                                    !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
88
+                                if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") &&
89
+                                    !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
90
                                         continue;
91
                         } else {
92
                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
93
                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
94
                                         continue;
95
                         }
96
-
97
                         full_design = atoi(walk+1);
98
                 }
99
         }