ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/misc.C
(Generate patch)

Comparing rxvt-unicode/src/misc.C (file contents):
Revision 1.53 by root, Sun Apr 26 01:59:53 2009 UTC vs.
Revision 1.60 by sf-exg, Thu May 13 19:40:19 2021 UTC

4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston 6 * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston
7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> 7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
8 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> 8 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com>
9 * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> 9 * Copyright (c) 2003-2006 Marc Lehmann <schmorp@schmorp.de>
10 * 10 *
11 * This program is free software; you can redistribute it and/or modify 11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by 12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or 13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version. 14 * (at your option) any later version.
15 * 15 *
16 * This program is distributed in the hope that it will be useful, 16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *----------------------------------------------------------------------*/ 24 *----------------------------------------------------------------------*/
25 25
26#include "../config.h" /* NECESSARY */ 26#include "../config.h" /* NECESSARY */
27#include "rxvt.h" /* NECESSARY */ 27#include "rxvt.h" /* NECESSARY */
28
29#include <new>
30
31// alas new/delete cannot be specified as inline in C++11 (see 17.6.4.6)
32void *
33operator new (size_t s)
34{
35 return rxvt_malloc (s);
36}
37
38void
39operator delete (void *p) noexcept
40{
41 free (p);
42}
28 43
29char * 44char *
30rxvt_wcstombs (const wchar_t *str, int len) 45rxvt_wcstombs (const wchar_t *str, int len)
31{ 46{
32 if (len < 0) len = wcslen (str); 47 if (len < 0) len = wcslen (str);
166 181
167 return r; 182 return r;
168} 183}
169 184
170const char * 185const char *
171rxvt_basename (const char *str) NOTHROW 186rxvt_basename (const char *str) noexcept
172{ 187{
173 char *base = strrchr (str, '/'); 188 const char *base = strrchr (str, '/');
174 189
175 return (char *) (base ? base + 1 : str); 190 return base ? base + 1 : str;
176} 191}
177 192
178/* 193/*
179 * Print an error message 194 * Print an error message
180 */ 195 */
181void 196void
182rxvt_vlog (const char *fmt, va_list arg_ptr) NOTHROW 197rxvt_vlog (const char *fmt, va_list arg_ptr) noexcept
183{ 198{
184 char msg[1024]; 199 char msg[1024];
185 200
186 vsnprintf (msg, sizeof msg, fmt, arg_ptr); 201 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
187 202
190 else 205 else
191 write (STDOUT_FILENO, msg, strlen (msg)); 206 write (STDOUT_FILENO, msg, strlen (msg));
192} 207}
193 208
194void 209void
195rxvt_log (const char *fmt,...) NOTHROW 210rxvt_log (const char *fmt,...) noexcept
196{ 211{
197 va_list arg_ptr; 212 va_list arg_ptr;
198 213
199 va_start (arg_ptr, fmt); 214 va_start (arg_ptr, fmt);
200 rxvt_vlog (fmt, arg_ptr); 215 rxvt_vlog (fmt, arg_ptr);
203 218
204/* 219/*
205 * Print an error message 220 * Print an error message
206 */ 221 */
207void 222void
208rxvt_warn (const char *fmt,...) NOTHROW 223rxvt_warn (const char *fmt,...) noexcept
209{ 224{
210 va_list arg_ptr; 225 va_list arg_ptr;
211 226
212 rxvt_log ("%s: ", RESNAME); 227 rxvt_log ("%s: ", RESNAME);
213 228
215 rxvt_vlog (fmt, arg_ptr); 230 rxvt_vlog (fmt, arg_ptr);
216 va_end (arg_ptr); 231 va_end (arg_ptr);
217} 232}
218 233
219void 234void
220rxvt_fatal (const char *fmt,...) THROW ((class rxvt_failure_exception)) 235rxvt_fatal (const char *fmt,...)
221{ 236{
222 va_list arg_ptr; 237 va_list arg_ptr;
223 238
224 rxvt_log ("%s: ", RESNAME); 239 rxvt_log ("%s: ", RESNAME);
225 240
229 244
230 rxvt_exit_failure (); 245 rxvt_exit_failure ();
231} 246}
232 247
233void 248void
234rxvt_exit_failure () THROW ((class rxvt_failure_exception)) 249rxvt_exit_failure ()
235{ 250{
236 static class rxvt_failure_exception rxvt_failure_exception; 251 static class rxvt_failure_exception rxvt_failure_exception;
237 throw (rxvt_failure_exception); 252 throw (rxvt_failure_exception);
238} 253}
239 254
240/* 255/*
241 * remove leading/trailing space in place. 256 * remove leading/trailing space in place.
242 */ 257 */
243char * 258char *
244rxvt_strtrim (char *str) NOTHROW 259rxvt_strtrim (char *str) noexcept
245{ 260{
246 char *r, *s; 261 char *r, *s;
247 262
248 if (!str || !*str) /* shortcut */ 263 if (!str || !*str) /* shortcut */
249 return str; 264 return str;
263 278
264 return str; 279 return str;
265} 280}
266 281
267/* 282/*
268 * Split a comma-separated string into an array, stripping leading and 283 * Split a string into an array based on the given delimiter, stripping leading and
269 * trailing spaces from each entry. Empty strings are properly returned 284 * trailing spaces from each entry. Empty strings are properly returned
270 */ 285 */
271char ** 286char **
272rxvt_splitcommastring (const char *cs) NOTHROW 287rxvt_strsplit (char delim, const char *str) noexcept
273{ 288{
274 int l, n, p; 289 int l, n;
275 const char *s, *t; 290 char *s, *t;
276 char **ret; 291 char **ret;
277 292
278 if ((s = cs) == NULL) 293 s = strdup (str ? str : "");
279 s = "";
280 294
281 for (n = 1, t = s; *t; t++) 295 for (n = 1, t = s; *t; t++)
282 if (*t == ',') 296 if (*t == delim)
283 n++; 297 n++;
284 298
285 ret = (char **)malloc ((n + 1) * sizeof (char *)); 299 ret = (char **)malloc ((n + 1) * sizeof (char *));
286 ret[n] = NULL; 300 ret[n] = NULL;
287 301
288 for (l = 0, t = s; l < n; l++) 302 for (l = 0, t = s; l < n; l++)
289 { 303 {
290 for ( ; *t && *t != ','; t++) ; 304 for (; *t && *t != delim; t++)
291 p = t - s; 305 ;
292 ret[l] = (char *)malloc (p + 1); 306 *t = '\0';
293 memcpy (ret[l], s, p);
294 ret[l][p] = '\0'; 307 ret[l] = s;
295 rxvt_strtrim (ret[l]); 308 rxvt_strtrim (ret[l]);
296 s = ++t; 309 s = ++t;
297 } 310 }
298 311
299 return ret; 312 return ret;
300} 313}
301 314
302void
303rxvt_freecommastring (char **cs) NOTHROW
304{
305 for (int i = 0; cs[i]; ++i)
306 free (cs[i]);
307
308 free (cs);
309}
310
311void * 315void *
312rxvt_malloc (size_t size) 316rxvt_malloc (size_t size)
313{ 317{
314 void *p = malloc (size); 318 void *p = malloc (size);
315 319
338 if (!p) 342 if (!p)
339 rxvt_fatal ("memory allocation failure. aborting.\n"); 343 rxvt_fatal ("memory allocation failure. aborting.\n");
340 344
341 return p; 345 return p;
342} 346}
347
348KeySym
349rxvt_XKeycodeToKeysym (Display *dpy, KeyCode code, int index)
350{
351 int size;
352 KeySym *mapping = XGetKeyboardMapping (dpy, code, 1, &size);
353 KeySym keysym = IN_RANGE_EXC (index, 0, size) ? mapping[index] : NoSymbol;
354 XFree (mapping);
355 return keysym;
356}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines