/*
- * Copyright (C) 2012,2013 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
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
-#include <ncurses.h>
#include "date.h"
#include "cal.h"
#include "view.h"
#include "util.h"
-/* For testing */
-#pragma weak COMPACT
+#pragma weak view_exit
+#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)
{
fprintf(output_fd, "%s: ", prefix);
vfprintf(output_fd, fmt, tmp);
fprintf(output_fd, "\n");
+ fflush(output_fd);
}
/* Log to debug file */
fprintf(debug_fd, "%s: ", prefix);
vfprintf(debug_fd, fmt, tmp);
fprintf(debug_fd, "\n");
+ fflush(debug_fd);
}
/* Log to status bar */
- if (&COMPACT && stdscr) {
- int rev = COMPACT ? A_BOLD : 0;
+ if (&view_debug) {
va_copy(tmp, ap);
- if (!COMPACT)
- mvhline(LINES-2, 0, ACS_HLINE, COLS);
- move(LINES-1, 0);
- attron(COLOR_PAIR(COLOR_ERROR) | rev);
- vwprintw(stdscr, fmt, tmp);
- attroff(COLOR_PAIR(COLOR_ERROR) | rev);
- if (!COMPACT)
- clrtoeol();
+ view_debug(fmt, tmp);
}
}
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, ...)
{
fflush(stderr);
message(stderr, "error", fmt, ap);
va_end(ap);
- if (stdscr) {
- getch();
- endwin();
- }
+ if (view_exit)
+ view_exit();
exit(-1);
}