X-Git-Url: http://pileus.org/git/?p=lackey;a=blobdiff_plain;f=src%2Futil.c;h=1ebecfc02dae764fead047a08765eecbc4c0e3ca;hp=582b65deff8b70adc0c2f06dd248ac7fd92f6250;hb=569a0db79c00169e15d5abed2a82f28d1b2391c4;hpb=daded0f3fb930e7758e1009683c0cdb46eada3f6 diff --git a/src/util.c b/src/util.c index 582b65d..1ebecfc 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,18 +15,57 @@ * along with this program. If not, see . */ +#define _XOPEN_SOURCE +#define _XOPEN_SOURCE_EXTENDED + #include #include +#include #include -#include #include "date.h" #include "cal.h" #include "view.h" +#include "util.h" + +#pragma weak view_debug /* Static data */ static FILE *debug_fd = NULL; +/* View debugging */ +extern void view_debug(const char *fmt, va_list ap); + +/* Helper functions */ +static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap) +{ + va_list tmp; + + /* Log to standard out */ + if (output_fd) { + va_copy(tmp, ap); + fprintf(output_fd, "%s: ", prefix); + vfprintf(output_fd, fmt, tmp); + fprintf(output_fd, "\n"); + fflush(output_fd); + } + + /* Log to debug file */ + if (debug_fd) { + va_copy(tmp, ap); + fprintf(debug_fd, "%s: ", prefix); + vfprintf(debug_fd, fmt, tmp); + fprintf(debug_fd, "\n"); + fflush(debug_fd); + } + + /* Log to status bar */ + if (&view_debug) { + va_copy(tmp, ap); + view_debug(fmt, tmp); + } +} + /* Initialize */ void util_init(void) { @@ -41,30 +80,81 @@ void strsub(char *str, char find, char repl) *cur = repl; } -/* Debugging functions */ -int debug(char *fmt, ...) +char *strcopy(const char *str) { - int rval; - va_list ap; + if (str == NULL) + return NULL; + return strdup(str); +} - /* Log to debug file */ - if (debug_fd) { - va_start(ap, fmt); - vfprintf(debug_fd, "debug: ", ap); - rval = vfprintf(debug_fd, fmt, ap); - } +int match(const char *a, const char *b) +{ + if (a == b) + return 1; + if (!a || !b) + return 0; + return !strcmp(a, b); +} - /* Log to status bar */ - if (stdscr) { - va_start(ap, fmt); - mvhline(LINES-2, 0, ACS_HLINE, COLS); - move(LINES-1, 0); - attron(COLOR_PAIR(COLOR_ERROR)); - vwprintw(stdscr, fmt, ap); - attroff(COLOR_PAIR(COLOR_ERROR)); - clrtoeol(); +/* Memory functions */ +void *alloc0(int size) +{ + void *data = calloc(1, size); + if (!data) + error("memory allocation failed"); + 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, ...) +{ + va_list ap; + va_start(ap, fmt); + message(NULL, "debug", fmt, ap); + va_end(ap); +} + +void error(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fflush(stdout); + fflush(stderr); + message(stderr, "error", fmt, ap); va_end(ap); - return rval; + view_exit(); + exit(-1); }