Initial multibyte string output.
[cboard.git] / src / misc.c
blob25b6a88bc7779f78a15e773f052ec3875d771877
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2013 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 #ifdef WITH_DMALLOC
39 #include <dmalloc.h>
40 #endif
42 #include "misc.h"
44 void *Malloc(size_t size)
46 void *ptr;
48 if ((ptr = malloc(size)) == NULL)
49 err(EXIT_FAILURE, "malloc()");
51 return ptr;
54 void *Realloc(void *ptr, size_t size)
56 void *ptr2;
58 if ((ptr2 = realloc(ptr, size)) == NULL)
59 err(EXIT_FAILURE, "realloc()");
61 return ptr2;
64 void *Calloc(size_t n, size_t size)
66 void *p;
68 if ((p = calloc(n, size)) == NULL)
69 err(EXIT_FAILURE, "calloc()");
71 return p;
74 char *rtrim(char *str)
76 int i;
78 if (!*str)
79 return str;
81 for (i = strlen(str) - 1; isspace(str[i]); i--)
82 str[i] = 0;
84 return str;
87 char *trim(char *str)
89 if (!str)
90 return NULL;
92 while (isspace(*str))
93 str++;
95 return rtrim(str);
98 char *itoa(long n)
100 static char buf[16];
102 snprintf(buf, sizeof(buf), "%li", n);
103 return buf;
106 char *trim_multi(char *value)
108 char *p;
109 int lastc;
110 char *str, *s;
112 if (!value || !*value)
113 return value;
115 str = Malloc(strlen(value) + 1);
117 for (p = value, lastc = 0, s = str; *p; p++) {
118 if (isspace(lastc) && isspace(*p))
119 continue;
121 lastc = *s++ = *p;
124 *s = 0;
125 return str;
128 int integer_len(long n)
130 return strlen(itoa(n));
133 int isinteger(const char *str)
135 int i = 0;
136 int len = strlen(str);
138 if (*str == '-' && isdigit(*(str + 1)))
139 i = 1;
141 for (; i < len; i++) {
142 if (!isdigit(str[i]))
143 return 0;
146 return 1;
149 char *pathfix(const char *str)
151 static char buf[FILENAME_MAX];
152 struct passwd *pw;
154 if (*str == '~') {
155 pw = getpwuid(getuid());
156 strncpy(buf, pw->pw_dir, sizeof(buf));
157 strncat(buf, str + 1, sizeof(buf));
159 else
160 strncpy(buf, str, sizeof(buf));
162 return buf;
165 wchar_t *str_etc(const char *str, int maxlen, int rev)
167 int len;
168 wchar_t *p;
169 wchar_t *buf;
171 if (!str)
172 return NULL;
174 p = str_to_wchar (str);
175 len = wcslen(p);
177 if (len > maxlen) {
178 wchar_t *dot = str_to_wchar ("...");
180 buf = Malloc (maxlen+4*sizeof(wchar_t));
181 buf[0] = 0;
183 if (rev) {
184 wcsncat (buf, dot, maxlen);
185 wcsncat (buf, p, maxlen);
187 else {
188 wcsncat (buf, p, maxlen);
189 wcsncat (buf, dot, maxlen);
192 free (dot);
193 free (p);
195 else
196 buf = p;
198 return buf;
201 char **split_str(char *str, char *delim, int *n, int *width, int force_trim)
203 char *tmp;
204 int total = 0;
205 char **lines = NULL;
206 int w = 0;
208 if (!str || !delim)
209 return NULL;
211 while ((tmp = strsep(&str, delim)) != NULL) {
212 char *p;
214 tmp = rtrim(tmp);
216 if (!*tmp)
217 continue;
219 lines = Realloc(lines, (total + 2) * sizeof(char *));
220 p = force_trim ? strdup(trim(tmp)) : strdup(tmp);
222 if (w < strlen(p))
223 w = strlen(p);
225 lines[total++] = p;
228 lines[total] = NULL;
229 *n += total;
231 if (*width < w + 2)
232 *width = w + 2;
234 return lines;
237 wchar_t *
238 str_to_wchar (const char *str)
240 wchar_t *wc;
241 mbstate_t ps;
242 const char *p = str;
244 memset (&ps, 0, sizeof(mbstate_t));
245 size_t len = mbsrtowcs (NULL, &p, 0, &ps)+1;
246 wc = Calloc (1, len * sizeof(wchar_t));
247 p = str;
248 memset (&ps, 0, sizeof(mbstate_t));
249 len = mbsrtowcs (wc, &p, len, &ps);
250 return wc;