]> Pileus Git - lackey/blob - src/util.c
37b004d94896899b95312fdc3abc387fe5d725b6
[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                 fflush(output_fd);
49         }
50
51         /* Log to debug file */
52         if (debug_fd) {
53                 va_copy(tmp, ap);
54                 fprintf(debug_fd, "%s: ", prefix);
55                 vfprintf(debug_fd, fmt, tmp);
56                 fprintf(debug_fd, "\n");
57                 fflush(debug_fd);
58         }
59
60         /* Log to status bar */
61         if (&COMPACT && stdscr) {
62                 int rev = COMPACT ? A_BOLD : 0;
63                 va_copy(tmp, ap);
64                 if (!COMPACT)
65                         mvhline(LINES-2, 0, ACS_HLINE, COLS);
66                 move(LINES-1, 0);
67                 attron(COLOR_PAIR(COLOR_ERROR) | rev);
68                 vwprintw(stdscr, fmt, tmp);
69                 attroff(COLOR_PAIR(COLOR_ERROR) | rev);
70                 if (!COMPACT)
71                         clrtoeol();
72         }
73 }
74
75 /* Initialize */
76 void util_init(void)
77 {
78         debug_fd = fopen("/tmp/lackey.log", "w+");
79 }
80
81 /* String functions */
82 void strsub(char *str, char find, char repl)
83 {
84         for (char *cur = str; *cur; cur++)
85                 if (*cur == find)
86                         *cur = repl;
87 }
88
89 char *strcopy(const char *str)
90 {
91         if (str == NULL)
92                 return NULL;
93         return strdup(str);
94 }
95
96 int match(const char *a, const char *b)
97 {
98         if (a == b)
99                 return 1;
100         if (!a || !b)
101                 return 0;
102         return !strcmp(a, b);
103 }
104
105 /* Memory functions */
106 void *alloc0(int size)
107 {
108         void *data = calloc(1, size);
109         if (!data)
110                 error("memory allocation failed");
111         return data;
112 }
113
114 /* File functions */
115 char *read_file(const char *path, int *len)
116 {
117         /* we could use stat, but we'll try to be portable */
118         FILE *fd = fopen(path, "rt+");
119         if (!fd)
120                 return NULL;
121
122         int   block = 512; // read size
123         int   size  = 512; // buffer size
124         int   slen  = 0;   // string length
125         char *buf   = malloc(size);
126         if (!buf)
127                 goto err;
128
129         while (!feof(fd)) {
130                 if (slen + block + 1 > size) {
131                         size *= 2;
132                         buf   = realloc(buf, size);
133                         if (!buf)
134                                 goto err;
135                 }
136                 slen += fread(&buf[slen], 1, block, fd);
137                 buf[slen] = '\0';
138         }
139
140 err:
141         if (len)
142                 *len = slen;
143         fclose(fd);
144         return buf;
145 }
146
147 /* Debugging functions */
148 void debug(char *fmt, ...)
149 {
150         va_list ap;
151         va_start(ap, fmt);
152         message(NULL, "debug", fmt, ap);
153         va_end(ap);
154 }
155
156 void error(char *fmt, ...)
157 {
158         va_list ap;
159         va_start(ap, fmt);
160         fflush(stdout);
161         fflush(stderr);
162         message(stderr, "error", fmt, ap);
163         va_end(ap);
164         if (stdscr) {
165                 getch();
166                 endwin();
167         }
168         exit(-1);
169 }