]> Pileus Git - lackey/blob - src/util.c
Move main loop and curses init to view
[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 <stdarg.h>
24 #include <string.h>
25
26 #include "date.h"
27 #include "cal.h"
28 #include "view.h"
29 #include "util.h"
30
31 #pragma weak view_debug
32
33 /* Static data */
34 static FILE *debug_fd = NULL;
35
36 /* View debugging */
37 extern void view_debug(const char *fmt, va_list ap);
38
39 /* Helper functions */
40 static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
41 {
42         va_list tmp;
43
44         /* Log to standard out */
45         if (output_fd) {
46                 va_copy(tmp, ap);
47                 fprintf(output_fd, "%s: ", prefix);
48                 vfprintf(output_fd, fmt, tmp);
49                 fprintf(output_fd, "\n");
50                 fflush(output_fd);
51         }
52
53         /* Log to debug file */
54         if (debug_fd) {
55                 va_copy(tmp, ap);
56                 fprintf(debug_fd, "%s: ", prefix);
57                 vfprintf(debug_fd, fmt, tmp);
58                 fprintf(debug_fd, "\n");
59                 fflush(debug_fd);
60         }
61
62         /* Log to status bar */
63         if (&view_debug) {
64                 va_copy(tmp, ap);
65                 view_debug(fmt, tmp);
66         }
67 }
68
69 /* Initialize */
70 void util_init(void)
71 {
72         debug_fd = fopen("/tmp/lackey.log", "w+");
73 }
74
75 /* String functions */
76 void strsub(char *str, char find, char repl)
77 {
78         for (char *cur = str; *cur; cur++)
79                 if (*cur == find)
80                         *cur = repl;
81 }
82
83 char *strcopy(const char *str)
84 {
85         if (str == NULL)
86                 return NULL;
87         return strdup(str);
88 }
89
90 int match(const char *a, const char *b)
91 {
92         if (a == b)
93                 return 1;
94         if (!a || !b)
95                 return 0;
96         return !strcmp(a, b);
97 }
98
99 /* Memory functions */
100 void *alloc0(int size)
101 {
102         void *data = calloc(1, size);
103         if (!data)
104                 error("memory allocation failed");
105         return data;
106 }
107
108 /* File functions */
109 char *read_file(const char *path, int *len)
110 {
111         /* we could use stat, but we'll try to be portable */
112         FILE *fd = fopen(path, "rt+");
113         if (!fd)
114                 return NULL;
115
116         int   block = 512; // read size
117         int   size  = 512; // buffer size
118         int   slen  = 0;   // string length
119         char *buf   = malloc(size);
120         if (!buf)
121                 goto err;
122
123         while (!feof(fd)) {
124                 if (slen + block + 1 > size) {
125                         size *= 2;
126                         buf   = realloc(buf, size);
127                         if (!buf)
128                                 goto err;
129                 }
130                 slen += fread(&buf[slen], 1, block, fd);
131                 buf[slen] = '\0';
132         }
133
134 err:
135         if (len)
136                 *len = slen;
137         fclose(fd);
138         return buf;
139 }
140
141 /* Debugging functions */
142 void debug(char *fmt, ...)
143 {
144         va_list ap;
145         va_start(ap, fmt);
146         message(NULL, "debug", fmt, ap);
147         va_end(ap);
148 }
149
150 void error(char *fmt, ...)
151 {
152         va_list ap;
153         va_start(ap, fmt);
154         fflush(stdout);
155         fflush(stderr);
156         message(stderr, "error", fmt, ap);
157         va_end(ap);
158         view_exit();
159         exit(-1);
160 }