]> Pileus Git - lackey/blobdiff - src/util.c
Move main loop and curses init to view
[lackey] / src / util.c
index 55cd265a14b45812ea66a743f39ff139f83303f5..1ebecfc02dae764fead047a08765eecbc4c0e3ca 100644 (file)
-/* Time Keeping Bugs Abound! */
+/*
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * 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 <stdarg.h>
+#include <string.h>
 
+#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 int is_leap_year(year_t year)
+static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
 {
-       return (year % 400 == 0) ? 1 :
-              (year % 100 == 0) ? 0 :
-              (year % 4   == 0) ? 1 : 0;
-}
+       va_list tmp;
 
-static wday_t day_of_week(year_t year, month_t month, day_t day)
-{
-       static int tmp[] = {0, 3, 2, 5, 0, 3,
-                           5, 1, 4, 6, 2, 4};
-       if (month < 3)
-               year--;
-       int start = year + year / 4
-                        - year / 100
-                        + year / 400
-                        + tmp[month];
-       return (start + day) % 7;
-}
+       /* 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);
+       }
 
-/* Time functions */
-int days_in_year(year_t year)
-{
-       return 365 + is_leap_year(year);
-}
+       /* 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);
+       }
 
-int days_in_month(year_t year, month_t month)
-{
-       static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-       int days = mdays[month];
-       if (month == FEB)
-               days += is_leap_year(year);
-       return days;
+       /* Log to status bar */
+       if (&view_debug) {
+               va_copy(tmp, ap);
+               view_debug(fmt, tmp);
+       }
 }
 
-int weeks_in_month(year_t year, month_t month)
+/* Initialize */
+void util_init(void)
 {
-       int start = start_of_month(year, month);
-       int days  = days_in_month(year, month);
-       return ((start + days)-1) / 7 + 1;
+       debug_fd = fopen("/tmp/lackey.log", "w+");
 }
 
-wday_t start_of_month(year_t year, month_t month)
+/* String functions */
+void strsub(char *str, char find, char repl)
 {
-       return day_of_week(year, month, 1);
+       for (char *cur = str; *cur; cur++)
+               if (*cur == find)
+                       *cur = repl;
 }
 
-/* Debug functions */
-const char *month_to_str(month_t month)
+char *strcopy(const char *str)
 {
-       static const char *map[] =
-               { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
-       return map[month];
+       if (str == NULL)
+               return NULL;
+       return strdup(str);
 }
-const char *month_to_string(month_t month)
+
+int match(const char *a, const char *b)
 {
-       static const char *map[] =
-               { "January",   "February", "March",    "April",
-                 "May",       "June",     "July",     "August",
-                 "September", "October",  "November", "December" };
-       return map[month];
+       if (a == b)
+               return 1;
+       if (!a || !b)
+               return 0;
+       return !strcmp(a, b);
 }
 
-const char *day_to_st(wday_t day)
+/* Memory functions */
+void *alloc0(int size)
 {
-       static const char *map[] =
-               { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
-       return map[day];
+       void *data = calloc(1, size);
+       if (!data)
+               error("memory allocation failed");
+       return data;
 }
-const char *day_to_str(wday_t day)
+
+/* File functions */
+char *read_file(const char *path, int *len)
 {
-       static const char *map[] =
-               { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
-       return map[day];
+       /* 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;
 }
-const char *day_to_string(wday_t day)
+
+/* Debugging functions */
+void debug(char *fmt, ...)
 {
-       static const char *map[] =
-               { "Sunday",   "Monday", "Tuesday", "Wednesday",
-                 "Thursday", "Friday", "Saturday" };
-       return map[day];
+       va_list ap;
+       va_start(ap, fmt);
+       message(NULL, "debug", fmt, ap);
+       va_end(ap);
 }
 
-/* Test functions */
-void test_time(void)
+void error(char *fmt, ...)
 {
-       printf("Year Month     Start Weeks Days\n");
-       for (int y = 2012; y <= 2012; y++)
-       for (int m = JAN;  m <= DEC;  m++) {
-               printf("%-5d",  y);
-               printf("%-10s", month_to_string(m));
-               printf("%-6s",  day_to_str(start_of_month(y,m)));
-               printf("%-6d",  weeks_in_month(y,m));
-               printf("%-2d",  days_in_month(y,m));
-               printf("\n");
-       }
+       va_list ap;
+       va_start(ap, fmt);
+       fflush(stdout);
+       fflush(stderr);
+       message(stderr, "error", fmt, ap);
+       va_end(ap);
+       view_exit();
+       exit(-1);
 }