Update copyright year to 2015.
[cboard.git] / src / misc.c
blob4ef1d9989b8ffc4cc04e13be4f5b61cda85e6abc
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2015 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <pwd.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <err.h>
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #endif
38 #include "common.h"
39 #include "misc.h"
41 void *Malloc(size_t size)
43 void *ptr;
45 if ((ptr = malloc(size)) == NULL)
46 err(EXIT_FAILURE, "malloc()");
48 return ptr;
51 void *Realloc(void *ptr, size_t size)
53 void *ptr2;
55 if ((ptr2 = realloc(ptr, size)) == NULL)
56 err(EXIT_FAILURE, "realloc()");
58 return ptr2;
61 void *Calloc(size_t n, size_t size)
63 void *p;
65 if ((p = calloc(n, size)) == NULL)
66 err(EXIT_FAILURE, "calloc()");
68 return p;
71 char *rtrim(char *str)
73 int i;
75 if (!*str)
76 return str;
78 for (i = strlen(str) - 1; isspace(str[i]); i--)
79 str[i] = 0;
81 return str;
84 char *trim(char *str)
86 if (!str)
87 return NULL;
89 while (isspace(*str))
90 str++;
92 return rtrim(str);
95 char *itoa(long n, char *buf)
97 sprintf(buf, "%li", n);
98 return buf;
101 char *trim_multi(char *value)
103 char *p;
104 int lastc;
105 char *str, *s;
107 if (!value || !*value)
108 return value;
110 str = Malloc(strlen(value) + 1);
112 for (p = value, lastc = 0, s = str; *p; p++) {
113 if (isspace(lastc) && isspace(*p))
114 continue;
116 lastc = *s++ = *p;
119 *s = 0;
120 return str;
123 int integer_len(long n)
125 char buf[32];
127 itoa (n, buf);
128 return strlen (buf);
131 int isinteger(const char *str)
133 int i = 0;
134 int len = strlen(str);
136 if (*str == '-' && isdigit(*(str + 1)))
137 i = 1;
139 for (; i < len; i++) {
140 if (!isdigit(str[i]))
141 return 0;
144 return 1;
147 char *pathfix(const char *str)
149 static char buf[FILENAME_MAX] = {0};
150 struct passwd *pw;
152 if (*str == '~') {
153 pw = getpwuid(getuid());
154 strncpy(buf, pw->pw_dir, sizeof(buf)-1);
155 strncat(buf, str + 1, sizeof(buf)-1);
157 else
158 strncpy(buf, str, sizeof(buf)-1);
160 return buf;
163 wchar_t *str_etc(const char *str, int maxlen, int rev)
165 wchar_t *p;
166 wchar_t *buf = NULL;
168 if (!str)
169 return NULL;
171 p = str_to_wchar (str);
173 if (wcslen (p) > maxlen) {
174 wchar_t *dot = str_to_wchar (_("..."));
176 if (rev) {
177 buf = str_to_wchar_len (_("..."), maxlen);
178 buf[wcslen (dot)] = 0;
179 wcsncat (buf, p, maxlen-wcslen (dot));
181 else {
182 buf = str_to_wchar_len (str, maxlen);
183 buf[wcslen (buf)-wcslen (dot)]=0;
184 wcscat (buf, dot);
187 free (dot);
188 free (p);
190 else
191 buf = p;
193 return buf;
196 char **split_str(char *str, char *delim, int *n, int *width, int force_trim)
198 char *tmp;
199 int total = 0;
200 char **lines = NULL;
201 int w = 0;
203 if (!str || !delim)
204 return NULL;
206 while ((tmp = strsep(&str, delim)) != NULL) {
207 char *p;
208 wchar_t *wc;
209 int len;
211 tmp = rtrim(tmp);
213 if (!*tmp)
214 continue;
216 lines = Realloc(lines, (total + 2) * sizeof(char *));
217 p = force_trim ? strdup(trim(tmp)) : strdup(tmp);
218 wc = str_to_wchar (p);
219 len = wcslen (wc);
220 if (w < len)
221 w = len;
223 free (wc);
224 lines[total++] = p;
227 if (total) {
228 lines[total] = NULL;
229 *n += total;
231 if (*width < w + 2)
232 *width = w + 2;
235 return lines;
238 // 'max' will always allocate that amount when not 0.
239 wchar_t *str_to_wchar_len (const char *str, int max)
241 wchar_t *wc;
242 mbstate_t ps;
243 const char *p = str;
244 size_t len = max;
246 memset (&ps, 0, sizeof(mbstate_t));
247 if (!len)
248 len = mbsrtowcs (NULL, &p, 0, &ps);
250 wc = Calloc (len+1, sizeof(wchar_t));
251 p = str;
252 memset (&ps, 0, sizeof(mbstate_t));
253 len = mbsrtowcs (wc, &p, len, &ps);
254 return wc;
257 wchar_t *str_to_wchar (const char *str)
259 return str_to_wchar_len (str, 0);
262 char *wchar_to_str (const wchar_t *str)
264 size_t len = wcstombs (NULL, str, 0);
265 char *s = Malloc (len*sizeof(char)+1);
266 len = wcstombs (s, str, len);
267 s[len] = 0;
268 return s;