]> Pileus Git - lackey/blob - src/util.c
0d64fc4b2bb70598a4ccb26bb275a64f49d6385d
[lackey] / src / util.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _XOPEN_SOURCE
19 #define _XOPEN_SOURCE_EXTENDED
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ncurses.h>
25
26 #include "date.h"
27 #include "cal.h"
28 #include "view.h"
29 #include "util.h"
30
31 /* For testing */
32 #pragma weak COMPACT
33
34 /* Static data */
35 static FILE *debug_fd = NULL;
36
37 /* Helper functions */
38 static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
39 {
40         va_list tmp;
41
42         /* Log to standard out */
43         if (output_fd) {
44                 va_copy(tmp, ap);
45                 fprintf(output_fd, "%s: ", prefix);
46                 vfprintf(output_fd, fmt, tmp);
47                 fprintf(output_fd, "\n");
48         }
49
50         /* Log to debug file */
51         if (debug_fd) {
52                 va_copy(tmp, ap);
53                 fprintf(debug_fd, "%s: ", prefix);
54                 vfprintf(debug_fd, fmt, tmp);
55                 fprintf(debug_fd, "\n");
56         }
57
58         /* Log to status bar */
59         if (&COMPACT && stdscr) {
60                 int rev = COMPACT ? A_BOLD : 0;
61                 va_copy(tmp, ap);
62                 if (!COMPACT)
63                         mvhline(LINES-2, 0, ACS_HLINE, COLS);
64                 move(LINES-1, 0);
65                 attron(COLOR_PAIR(COLOR_ERROR) | rev);
66                 vwprintw(stdscr, fmt, tmp);
67                 attroff(COLOR_PAIR(COLOR_ERROR) | rev);
68                 if (!COMPACT)
69                         clrtoeol();
70         }
71 }
72
73 /* Initialize */
74 void util_init(void)
75 {
76         debug_fd = fopen("/tmp/lackey.log", "w+");
77 }
78
79 /* String functions */
80 void strsub(char *str, char find, char repl)
81 {
82         for (char *cur = str; *cur; cur++)
83                 if (*cur == find)
84                         *cur = repl;
85 }
86
87 char *strcopy(const char *str)
88 {
89         if (str == NULL)
90                 return NULL;
91         return strdup(str);
92 }
93
94 int match(const char *a, const char *b)
95 {
96         if (a == b)
97                 return 1;
98         if (!a || !b)
99                 return 0;
100         return !strcmp(a, b);
101 }
102
103 /* Memory functions */
104 void *alloc0(int size)
105 {
106         void *data = calloc(1, size);
107         if (!data)
108                 error("memory allocation failed");
109         return data;
110 }
111
112 /* File functions */
113 char *read_file(const char *path, int *len)
114 {
115         /* we could use stat, but we'll try to be portable */
116         FILE *fd = fopen(path, "rt+");
117         if (!fd)
118                 return NULL;
119
120         int   block = 512; // read size
121         int   size  = 512; // buffer size
122         int   slen  = 0;   // string length
123         char *buf   = malloc(size);
124         if (!buf)
125                 goto err;
126
127         while (!feof(fd)) {
128                 if (slen + block + 1 > size) {
129                         size *= 2;
130                         buf   = realloc(buf, size);
131                         if (!buf)
132                                 goto err;
133                 }
134                 slen += fread(&buf[slen], 1, block, fd);
135                 buf[slen] = '\0';
136         }
137
138 err:
139         if (len)
140                 *len = slen;
141         fclose(fd);
142         return buf;
143 }
144
145 /* Debugging functions */
146 void debug(char *fmt, ...)
147 {
148         va_list ap;
149         va_start(ap, fmt);
150         message(NULL, "debug", fmt, ap);
151         va_end(ap);
152 }
153
154 void error(char *fmt, ...)
155 {
156         va_list ap;
157         va_start(ap, fmt);
158         fflush(stdout);
159         fflush(stderr);
160         message(stderr, "error", fmt, ap);
161         va_end(ap);
162         if (stdscr) {
163                 getch();
164                 endwin();
165         }
166         exit(-1);
167 }