home · contact · privacy
Server/py: Fix some thing memorization bugs.
[plomrogue] / src / client / wincontrol.c
index 816934a425e17a7d4d1851bdebc81f13fcb92658..eb9270b411130e5f4ce38471de247a817060857c 100644 (file)
@@ -1,4 +1,9 @@
-/* src/client/wincontrol.c */
+/* src/client/wincontrol.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #include "wincontrol.h"
 #include <ncurses.h> /* getmaxx(), getmaxy(), wresize() */
@@ -7,10 +12,9 @@
 #include <stdio.h> /* sprintf() */
 #include <stdlib.h> /* free() */
 #include <string.h> /* memcpy(), memset(), strchr(), strlen() */
-#include "../common/rexit.h" /* exit_err() */
+#include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
-#include "../common/yx_uint16.h" /* struct yx_uint16 */
-#include "windows.h" /* struct Win, get_win_by_id(), get_win_pos_in_order() */
+#include "windows.h" /* Win,yx_uint16, get_win_by_id(),get_win_pos_in_order() */
 #include "world.h" /* global world */
 
 
@@ -109,9 +113,7 @@ static void place_win(struct Win * w)
          * top, fit w's top left corner to that predecessor's top right corner.
          */
         struct Win * w_top = w_prev;
-        for (;
-             w_top->start.y != 0 + sep;
-             w_top = get_win_before(w_top->id));
+        for (; w_top->start.y != 0 + sep; w_top = get_win_before(w_top->id));
         w->start.x = w_top->start.x + w_top->frame_size.x + sep;
 
         /* If enough space is found below w's predecessor, fit w's top left
@@ -180,9 +182,8 @@ static void set_win_target_size(struct Win * w)
 
 static void append_win(struct Win * w)
 {
-    char * f_name = "append_win()";
     uint8_t old_size = strlen(world.winDB.order) + 1;
-    char * new_order = try_malloc(old_size + 1, f_name);
+    char * new_order = try_malloc(old_size + 1, __func__);
     memcpy(new_order, world.winDB.order, old_size - 1);
     new_order[old_size - 1] = w->id;
     new_order[old_size] = '\0';
@@ -196,14 +197,14 @@ static void append_win(struct Win * w)
 
 static void suspend_win(struct Win * w)
 {
-    char * f_name = "suspend_win()";
     uint8_t new_size = strlen(world.winDB.order);
-    char * new_order = try_malloc(new_size, f_name);
+    char * new_order = try_malloc(new_size, __func__);
     uint8_t i = get_win_pos_in_order(w->id);
     char next_char = world.winDB.order[i + 1];
     world.winDB.order[i] = '\0';
     char * second_part = &world.winDB.order[i + 1];
-    sprintf(new_order, "%s%s", world.winDB.order, second_part);
+    int test = sprintf(new_order, "%s%s", world.winDB.order, second_part);
+    exit_trouble(test < 0, __func__, "sprintf");
     free(world.winDB.order);
     world.winDB.order = new_order;
     world.winDB.active = world.winDB.order[i];
@@ -224,7 +225,7 @@ static void suspend_win(struct Win * w)
 extern void toggle_window(char id)
 {
     struct Win * win = get_win_by_id(id);
-    if (NULL == strchr(world.winDB.order, id))
+    if (!strchr(world.winDB.order, id))
     {
         append_win(win);
         return;
@@ -334,7 +335,7 @@ extern void shift_active_win(char dir)
     uint8_t len_order = strlen(world.winDB.order);
     if (1 < len_order)
     {
-        char tmp[len_order + 1];
+        char * tmp = try_malloc(len_order + 1, __func__);
         tmp[len_order] = '\0';
         uint8_t pos = get_win_pos_in_order(world.winDB.active);
         if ('f' == dir)
@@ -365,6 +366,7 @@ extern void shift_active_win(char dir)
                 world.winDB.order[pos - 1] = world.winDB.active;
             }
         }
+        free(tmp);
         update_wins(get_win_by_id(world.winDB.order[0]));
     }
 }