]> Pileus Git - lackey/blobdiff - src/util.c
Some organization
[lackey] / src / util.c
index e40fa8e2c6c747dfb575f4ec84cc93004f96e47a..53f5cef71f691aa6b7ad3be9bd43520f9a609328 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* Time Keeping Bugs Abound! */
-
 #include <stdio.h>
-#include <time.h>
+#include <ncurses.h>
 
-#include "util.h"
+#include "screen.h"
 
-/* Time functions */
-int is_leap_year(year_t year)
-{
-       return (year % 400 == 0) ? 1 :
-              (year % 100 == 0) ? 0 :
-              (year % 4   == 0) ? 1 : 0;
-}
+/* Static data */
+static FILE *debug_fd = NULL;
 
-int days_in_year(year_t year)
+/* Initialize */
+void util_init(void)
 {
-       return 365 + is_leap_year(year);
+       debug_fd = fopen("/tmp/lackey.log", "w+");
 }
 
-int days_in_month(year_t year, month_t month)
+/* Debugging functions */
+int debug(char *fmt, ...)
 {
-       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;
-}
+       int rval;
+       va_list ap;
 
-int weeks_in_month(year_t year, month_t month)
-{
-       int start = start_of_month(year, month);
-       int days  = days_in_month(year, month);
-       return ((start + days)-1) / 7 + 1;
-}
-
-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 < MAR)
-               year--;
-       int start = year + year / 4
-                        - year / 100
-                        + year / 400
-                        + tmp[month];
-       return (start + day + 1) % 7;
-}
-
-wday_t start_of_month(year_t year, month_t month)
-{
-       return day_of_week(year, month, 0);
-}
-
-void add_days(year_t *year, month_t *month, day_t *day, int days)
-{
-       time_t time = mktime(&(struct tm){
-                       .tm_year = *year-1900,
-                       .tm_mon  = *month,
-                       .tm_mday = *day+1,
-                       .tm_hour = 12});
-       time  += days*24*60*60;
-       struct tm *tm = localtime(&time);
-       *year  = tm->tm_year+1900;
-       *month = tm->tm_mon;
-       *day   = tm->tm_mday-1;
-}
-
-void add_months(year_t *year, month_t *month, int months)
-{
-       int total = *year*12 + *month + months;
-       *year  = total / 12;
-       *month = total % 12;
-}
-
-/* Debug functions */
-const char *month_to_str(month_t month)
-{
-       static const char *map[] =
-               { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
-       return map[month%12];
-}
-const char *month_to_string(month_t month)
-{
-       static const char *map[] =
-               { "January",   "February", "March",    "April",
-                 "May",       "June",     "July",     "August",
-                 "September", "October",  "November", "December" };
-       return map[month%12];
-}
-
-const char *day_to_st(wday_t day)
-{
-       static const char *map[] =
-               { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
-       return map[day%7];
-}
-const char *day_to_str(wday_t day)
-{
-       static const char *map[] =
-               { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
-       return map[day%7];
-}
-const char *day_to_string(wday_t day)
-{
-       static const char *map[] =
-               { "Sunday",   "Monday", "Tuesday", "Wednesday",
-                 "Thursday", "Friday", "Saturday" };
-       return map[day%7];
-}
+       /* Log to debug file */
+       if (debug_fd) {
+               va_start(ap, fmt);
+               vfprintf(debug_fd, "debug: ", ap);
+               rval = vfprintf(debug_fd, fmt, ap);
+       }
 
-/* Test functions */
-void test_time(void)
-{
-       printf("Info\n");
-       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");
+       /* 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();
        }
+
+       va_end(ap);
+       return rval;
 }