]> Pileus Git - lackey/blob - src/util.c
Use CURL simple API for EWS calendars
[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 void append(buf_t *buf, const void *data, int len)
110 {
111         if (buf->len + len + 1 > buf->max) {
112                 buf->max += (((buf->len+len)/4096)+1)*4096;
113                 buf->data = realloc(buf->data, buf->max);
114                 if (!buf->data)
115                         error("buffer reallocation allocation failed");
116         }
117         memcpy(buf->data + buf->len, data, len);
118         buf->len += len;
119         ((char*)buf->data)[buf->len] = '\0';
120 }
121
122 void release(buf_t *buf)
123 {
124         free(buf->data);
125         buf->data = 0;
126         buf->len  = 0;
127         buf->max  = 0;
128 }
129
130 /* File functions */
131 char *read_file(const char *path, int *len)
132 {
133         /* we could use stat, but we'll try to be portable */
134         FILE *fd = fopen(path, "rt+");
135         if (!fd)
136                 return NULL;
137
138         int   block = 512; // read size
139         int   size  = 512; // buffer size
140         int   slen  = 0;   // string length
141         char *buf   = malloc(size);
142         if (!buf)
143                 goto err;
144
145         while (!feof(fd)) {
146                 if (slen + block + 1 > size) {
147                         size *= 2;
148                         buf   = realloc(buf, size);
149                         if (!buf)
150                                 goto err;
151                 }
152                 slen += fread(&buf[slen], 1, block, fd);
153                 buf[slen] = '\0';
154         }
155
156 err:
157         if (len)
158                 *len = slen;
159         fclose(fd);
160         return buf;
161 }
162
163 /* Debugging functions */
164 void debug(char *fmt, ...)
165 {
166         va_list ap;
167         va_start(ap, fmt);
168         message(NULL, "debug", fmt, ap);
169         va_end(ap);
170 }
171
172 void error(char *fmt, ...)
173 {
174         va_list ap;
175         va_start(ap, fmt);
176         fflush(stdout);
177         fflush(stderr);
178         message(stderr, "error", fmt, ap);
179         va_end(ap);
180         if (view_exit)
181                 view_exit();
182         exit(-1);
183 }