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