]> git.sur5r.net Git - i3/i3/blobdiff - src/util.c
replace remaining printf()s with D?LOG
[i3/i3] / src / util.c
index f672cc2e994b1ff86c4d8b9f6eda165609a70d90..83a2d57bab8ad23a6f45cbf1b40dbbc7b5db8d43 100644 (file)
@@ -21,6 +21,7 @@
 #include <pwd.h>
 #include <yajl/yajl_version.h>
 #include <libgen.h>
+#include <ctype.h>
 
 #define SN_API_NOT_YET_FROZEN 1
 #include <libsn/sn-launcher.h>
@@ -47,6 +48,38 @@ Rect rect_add(Rect a, Rect b) {
                   a.height + b.height};
 }
 
+/*
+ * Returns true if the name consists of only digits.
+ *
+ */
+__attribute__ ((pure)) bool name_is_digits(const char *name) {
+    /* positive integers and zero are interpreted as numbers */
+    for (int i = 0; i < strlen(name); i++)
+        if (!isdigit(name[i]))
+            return false;
+
+    return true;
+}
+
+/*
+ * Parses the workspace name as a number. Returns -1 if the workspace should be
+ * interpreted as a "named workspace".
+ *
+ */
+long ws_name_to_number(const char *name) {
+    /* positive integers and zero are interpreted as numbers */
+    char *endptr = NULL;
+    long parsed_num = strtol(name, &endptr, 10);
+    if (parsed_num == LONG_MIN ||
+            parsed_num == LONG_MAX ||
+            parsed_num < 0 ||
+            endptr == name) {
+        parsed_num = -1;
+    }
+
+    return parsed_num;
+}
+
 /*
  * Updates *destination with new_value and returns true if it was changed or false
  * if it was the same
@@ -188,22 +221,14 @@ static char **append_argument(char **original, char *argument) {
 
 char *store_restart_layout(void) {
     setlocale(LC_NUMERIC, "C");
-#if YAJL_MAJOR >= 2
     yajl_gen gen = yajl_gen_alloc(NULL);
-#else
-    yajl_gen gen = yajl_gen_alloc(NULL, NULL);
-#endif
 
     dump_node(gen, croot, true);
 
     setlocale(LC_NUMERIC, "");
 
     const unsigned char *payload;
-#if YAJL_MAJOR >= 2
     size_t length;
-#else
-    unsigned int length;
-#endif
     y(get_buf, &payload, &length);
 
     /* create a temporary file if one hasn't been specified, or just
@@ -235,22 +260,18 @@ char *store_restart_layout(void) {
             return NULL;
         }
         if (n == 0) {
-            printf("write == 0?\n");
+            DLOG("write == 0?\n");
             free(filename);
             close(fd);
             return NULL;
         }
         written += n;
-#if YAJL_MAJOR >= 2
         DLOG("written: %zd of %zd\n", written, length);
-#else
-        DLOG("written: %d of %d\n", written, length);
-#endif
     }
     close(fd);
 
     if (length > 0) {
-        printf("layout: %.*s\n", (int)length, payload);
+        DLOG("layout: %.*s\n", (int)length, payload);
     }
 
     y(free);