]> Pileus Git - lackey/blobdiff - src/util.c
Add string copy and match functions
[lackey] / src / util.c
index e3f3a43a8015cb630302d507b45c66f8f7b43a02..197f3d8ea17ca09f95b485701357d296feb1190c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2012,2013 Andy Spencer <andy753421@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -15,6 +15,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _XOPEN_SOURCE
+#define _XOPEN_SOURCE_EXTENDED
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -25,6 +28,9 @@
 #include "view.h"
 #include "util.h"
 
+/* For testing */
+#pragma weak COMPACT
+
 /* Static data */
 static FILE *debug_fd = NULL;
 
@@ -50,14 +56,17 @@ static void message(FILE *output_fd, const char *prefix, const char *fmt, va_lis
        }
 
        /* Log to status bar */
-       if (stdscr) {
+       if (&COMPACT && stdscr) {
+               int rev = COMPACT ? A_BOLD : 0;
                va_copy(tmp, ap);
-               mvhline(LINES-2, 0, ACS_HLINE, COLS);
+               if (!COMPACT)
+                       mvhline(LINES-2, 0, ACS_HLINE, COLS);
                move(LINES-1, 0);
-               attron(COLOR_PAIR(COLOR_ERROR));
+               attron(COLOR_PAIR(COLOR_ERROR) | rev);
                vwprintw(stdscr, fmt, tmp);
-               attroff(COLOR_PAIR(COLOR_ERROR));
-               clrtoeol();
+               attroff(COLOR_PAIR(COLOR_ERROR) | rev);
+               if (!COMPACT)
+                       clrtoeol();
        }
 }
 
@@ -75,6 +84,31 @@ void strsub(char *str, char find, char repl)
                        *cur = repl;
 }
 
+char *strcopy(const char *str)
+{
+       if (str == NULL)
+               return NULL;
+       return strdup(str);
+}
+
+int match(const char *a, const char *b)
+{
+       if (a == b)
+               return 1;
+       if (!a || !b)
+               return 0;
+       return !strcmp(a, b);
+}
+
+/* Memory functions */
+void *alloc0(int size)
+{
+       void *data = calloc(1, size);
+       if (!data)
+               error("memory allocation failed");
+       return data;
+}
+
 /* Debugging functions */
 void debug(char *fmt, ...)
 {