]> Pileus Git - lackey/blob - src/util.c
Add string copy and match functions
[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 /* Debugging functions */
113 void debug(char *fmt, ...)
114 {
115         va_list ap;
116         va_start(ap, fmt);
117         message(NULL, "debug", fmt, ap);
118         va_end(ap);
119 }
120
121 void error(char *fmt, ...)
122 {
123         va_list ap;
124         va_start(ap, fmt);
125         fflush(stdout);
126         fflush(stderr);
127         message(stderr, "error", fmt, ap);
128         va_end(ap);
129         if (stdscr) {
130                 getch();
131                 endwin();
132         }
133         exit(-1);
134 }