X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Futil.c;h=0d64fc4b2bb70598a4ccb26bb275a64f49d6385d;hb=296fd1bb5f87b1961e98c7ea4c224219012f7161;hp=131e9dea2e6261ccf4823030e4bde638a2bcff1d;hpb=6e84acb58056ce793a08041d202bb96861874025;p=lackey diff --git a/src/util.c b/src/util.c index 131e9de..0d64fc4 100644 --- a/src/util.c +++ b/src/util.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 Andy Spencer + * Copyright (C) 2012-2013 Andy Spencer * * 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 . */ +#define _XOPEN_SOURCE +#define _XOPEN_SOURCE_EXTENDED + #include #include #include @@ -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,22 @@ 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) { @@ -84,6 +109,39 @@ void *alloc0(int size) return data; } +/* File functions */ +char *read_file(const char *path, int *len) +{ + /* we could use stat, but we'll try to be portable */ + FILE *fd = fopen(path, "rt+"); + if (!fd) + return NULL; + + int block = 512; // read size + int size = 512; // buffer size + int slen = 0; // string length + char *buf = malloc(size); + if (!buf) + goto err; + + while (!feof(fd)) { + if (slen + block + 1 > size) { + size *= 2; + buf = realloc(buf, size); + if (!buf) + goto err; + } + slen += fread(&buf[slen], 1, block, fd); + buf[slen] = '\0'; + } + +err: + if (len) + *len = slen; + fclose(fd); + return buf; +} + /* Debugging functions */ void debug(char *fmt, ...) {