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.21 by root, Mon Dec 13 07:26:18 2004 UTC vs.
Revision 1.31 by root, Fri Jan 6 20:50:58 2006 UTC

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#include "misc.intpro" /* PROTOS for internal routines */
29 28
30char * 29char *
31rxvt_wcstombs (const wchar_t *str, int len) 30rxvt_wcstombs (const wchar_t *str, int len)
32{ 31{
33 if (len < 0) len = wcslen (str); 32 if (len < 0) len = wcslen (str);
36 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); 35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
37 36
38 char *dst = r; 37 char *dst = r;
39 while (len--) 38 while (len--)
40 { 39 {
41 int l = wcrtomb (dst, *str++, mbs); 40 ssize_t l = wcrtomb (dst, *str++, mbs);
41
42 if (l < 0) 42 if (l < 0)
43 *dst++ = '?'; 43 *dst++ = '?';
44 else 44 else
45 dst += l; 45 dst += l;
46 } 46 }
47 47
48 *dst++ = 0; 48 *dst++ = 0;
49 49
50 return r; 50 return (char *)rxvt_realloc (r, dst - r);
51} 51}
52 52
53wchar_t * 53wchar_t *
54rxvt_mbstowcs (const char *str, int len) 54rxvt_mbstowcs (const char *str, int len)
55{ 55{
56 if (len < 0) len = strlen (str); 56 if (len < 0) len = strlen (str);
57 57
58 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); 58 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
59 59
60 if (mbstowcs (r, str, len + 1) < 0) 60 if ((ssize_t)mbstowcs (r, str, len + 1) < 0)
61 *r = 0; 61 *r = 0;
62 62
63 return r; 63 return r;
64} 64}
65 65
71 char *r = (char *)rxvt_malloc (len * 4 + 1); 71 char *r = (char *)rxvt_malloc (len * 4 + 1);
72 char *p = r; 72 char *p = r;
73 73
74 while (len--) 74 while (len--)
75 { 75 {
76 unicode_t w = *str++; 76 unicode_t w = *str++ & UNICODE_MASK;
77 77
78 if (w < 0x000080) 78 if (w < 0x000080)
79 *p++ = w; 79 *p++ = w;
80 else if (w < 0x000800) 80 else if (w < 0x000800)
81 *p++ = 0xc0 | ( w >> 6), 81 *p++ = 0xc0 | ( w >> 6),
91 *p++ = 0x80 | ( w & 0x3f); 91 *p++ = 0x80 | ( w & 0x3f);
92 else 92 else
93 *p++ = '?'; 93 *p++ = '?';
94 } 94 }
95 95
96 *p = 0; 96 *p++ = 0;
97 97
98 return r; 98 return (char *)rxvt_realloc (r, p - r);
99} 99}
100 100
101wchar_t * 101wchar_t *
102rxvt_utf8towcs (const char *str, int len) 102rxvt_utf8towcs (const char *str, int len)
103{ 103{
168rxvt_strdup (const char *str) 168rxvt_strdup (const char *str)
169{ 169{
170 return str ? strdup (str) : 0; 170 return str ? strdup (str) : 0;
171} 171}
172 172
173/* INTPROTO */
174char * 173char *
175rxvt_r_basename (const char *str) 174rxvt_r_basename (const char *str)
176{ 175{
177 char *base = strrchr (str, '/'); 176 char *base = strrchr (str, '/');
178 177
180} 179}
181 180
182/* 181/*
183 * Print an error message 182 * Print an error message
184 */ 183 */
185/* INTPROTO */
186void 184void
187rxvt_vlog (const char *fmt, va_list arg_ptr) 185rxvt_vlog (const char *fmt, va_list arg_ptr)
188{ 186{
189 char msg[1024]; 187 char msg[1024];
190 188
194 (*GET_R->log_hook) (msg); 192 (*GET_R->log_hook) (msg);
195 else 193 else
196 write (STDOUT_FILENO, msg, strlen (msg)); 194 write (STDOUT_FILENO, msg, strlen (msg));
197} 195}
198 196
199/* INTPROTO */
200void 197void
201rxvt_log (const char *fmt,...) 198rxvt_log (const char *fmt,...)
202{ 199{
203 va_list arg_ptr; 200 va_list arg_ptr;
204 201
208} 205}
209 206
210/* 207/*
211 * Print an error message 208 * Print an error message
212 */ 209 */
213/* INTPROTO */
214void 210void
215rxvt_warn (const char *fmt,...) 211rxvt_warn (const char *fmt,...)
216{ 212{
217 va_list arg_ptr; 213 va_list arg_ptr;
218 214
221 va_start (arg_ptr, fmt); 217 va_start (arg_ptr, fmt);
222 rxvt_vlog (fmt, arg_ptr); 218 rxvt_vlog (fmt, arg_ptr);
223 va_end (arg_ptr); 219 va_end (arg_ptr);
224} 220}
225 221
226/* INTPROTO */
227void 222void
228rxvt_fatal (const char *fmt,...) 223rxvt_fatal (const char *fmt,...)
229{ 224{
230 va_list arg_ptr; 225 va_list arg_ptr;
231 226
238 rxvt_exit_failure (); 233 rxvt_exit_failure ();
239} 234}
240 235
241class rxvt_failure_exception rxvt_failure_exception; 236class rxvt_failure_exception rxvt_failure_exception;
242 237
243/* INTPROTO */
244void 238void
245rxvt_exit_failure () 239rxvt_exit_failure ()
246{ 240{
247 throw (rxvt_failure_exception); 241 throw (rxvt_failure_exception);
248} 242}
253 * No Match 247 * No Match
254 * return: 0 248 * return: 0
255 * Match 249 * Match
256 * return: strlen (S2) 250 * return: strlen (S2)
257 */ 251 */
258/* INTPROTO */
259int 252int
260rxvt_Str_match (const char *s1, const char *s2) 253rxvt_Str_match (const char *s1, const char *s2)
261{ 254{
262 int n = strlen (s2); 255 int n = strlen (s2);
263 256
264 return ((strncmp (s1, s2, n) == 0) ? n : 0); 257 return ((strncmp (s1, s2, n) == 0) ? n : 0);
265} 258}
266 259
267/* INTPROTO */
268const char * 260const char *
269rxvt_Str_skip_space (const char *str) 261rxvt_Str_skip_space (const char *str)
270{ 262{
271 if (str) 263 if (str)
272 while (*str && isspace (*str)) 264 while (*str && isspace (*str))
277 269
278/* 270/*
279 * remove leading/trailing space and strip-off leading/trailing quotes. 271 * remove leading/trailing space and strip-off leading/trailing quotes.
280 * in place. 272 * in place.
281 */ 273 */
282/* INTPROTO */
283char * 274char *
284rxvt_Str_trim (char *str) 275rxvt_Str_trim (char *str)
285{ 276{
286 char *r, *s; 277 char *r, *s;
287 int n;
288 278
289 if (!str || !*str) /* shortcut */ 279 if (!str || !*str) /* shortcut */
290 return str; 280 return str;
291 281
292 /* skip leading spaces */ 282 /* skip leading spaces */
293 for (s = str; *s && isspace (*s); s++) ; 283 for (s = str; *s && isspace (*s); s++) ;
284
294 /* goto end of string */ 285 /* goto end of string */
295 for (n = 0, r = s; *r++; n++) ; 286 r = s + strlen (s) - 1;
296 r -= 2; 287
297 /* dump return */ 288 /* dump return and other trailing whitespace */
298 if (n > 0 && *r == '\n') 289 while (r > s && isspace (*r))
299 n--, r--; 290 r--;
300 /* backtrack along trailing spaces */ 291
301 for (; n > 0 && isspace (*r); r--, n--) ; 292#if 0
302 /* skip matching leading/trailing quotes */ 293 /* skip matching leading/trailing quotes */
303 if (*s == '"' && *r == '"' && n > 1) 294 if (*s == '"' && *r == '"' && n > 1)
304 { 295 {
305 s++; 296 s++;
306 n -= 2; 297 n -= 2;
307 } 298 }
299#endif
308 300
309 /* copy back over: forwards copy */ 301 memmove (str, s, r + 1 - s);
310 for (r = str; n; n--) 302 str[r + 1 - s] = 0;
311 *r++ = *s++;
312 *r = '\0';
313 303
314 return str; 304 return str;
315} 305}
316 306
317/* 307/*
326 * "M-x" prefixed strings, append "\r" if needed 316 * "M-x" prefixed strings, append "\r" if needed
327 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 317 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
328 * 318 *
329 * returns the converted string length 319 * returns the converted string length
330 */ 320 */
331/* INTPROTO */
332int 321int
333rxvt_Str_escaped (char *str) 322rxvt_Str_escaped (char *str)
334{ 323{
335 char ch, *s, *d; 324 char ch, *s, *d;
336 int i, num, append = 0; 325 int i, num, append = 0;
400 return (d - str); 389 return (d - str);
401} 390}
402 391
403/* 392/*
404 * Split a comma-separated string into an array, stripping leading and 393 * Split a comma-separated string into an array, stripping leading and
405 * trailing spaces (and paired quotes) from each entry. Empty strings 394 * trailing spaces from each entry. Empty strings are properly returned
406 * are properly returned
407 * Caller should free each entry and array when done 395 * Caller should free each entry and array when done
408 */ 396 */
409/* INTPROTO */
410char ** 397char **
411rxvt_splitcommastring (const char *cs) 398rxvt_splitcommastring (const char *cs)
412{ 399{
413 int l, n, p; 400 int l, n, p;
414 const char *s, *t; 401 const char *s, *t;
418 s = ""; 405 s = "";
419 406
420 for (n = 1, t = s; *t; t++) 407 for (n = 1, t = s; *t; t++)
421 if (*t == ',') 408 if (*t == ',')
422 n++; 409 n++;
410
423 ret = (char **)malloc ((n + 1) * sizeof (char *)); 411 ret = (char **)malloc ((n + 1) * sizeof (char *));
424 ret[n] = NULL; 412 ret[n] = NULL;
425 413
426 for (l = 0, t = s; l < n; l++) 414 for (l = 0, t = s; l < n; l++)
427 { 415 {
431 strncpy (ret[l], s, p); 419 strncpy (ret[l], s, p);
432 ret[l][p] = '\0'; 420 ret[l][p] = '\0';
433 rxvt_Str_trim (ret[l]); 421 rxvt_Str_trim (ret[l]);
434 s = ++t; 422 s = ++t;
435 } 423 }
424
436 return ret; 425 return ret;
437} 426}
438 427
439void 428void
440rxvt_freecommastring (char **cs) 429rxvt_freecommastring (char **cs)
456 * search for FILE in the current working directory, and within the 445 * search for FILE in the current working directory, and within the
457 * colon-delimited PATHLIST, adding the file extension EXT if required. 446 * colon-delimited PATHLIST, adding the file extension EXT if required.
458 * 447 *
459 * FILE is either semi-colon or zero terminated 448 * FILE is either semi-colon or zero terminated
460 */ 449 */
461/* INTPROTO */
462char * 450char *
463rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) 451rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
464{ 452{
465 int maxpath, len; 453 int maxpath, len;
466 const char *p, *path; 454 const char *p, *path;
529 } 517 }
530 } 518 }
531 return NULL; 519 return NULL;
532} 520}
533 521
534/* INTPROTO */
535char * 522char *
536rxvt_File_find (const char *file, const char *ext, const char *path) 523rxvt_File_find (const char *file, const char *ext, const char *path)
537{ 524{
538 char *f; 525 char *f;
539 526
562 549
563/* 550/*
564 * Draw top/left and bottom/right border shadows around windows 551 * Draw top/left and bottom/right border shadows around windows
565 */ 552 */
566#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 553#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
567/* INTPROTO */
568void 554void
569rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 555rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
570{ 556{
571 int shadow; 557 int shadow;
572 558
573 shadow = (w == 0 || h == 0) ? 1 : SHADOW; 559 shadow = (w == 0 || h == 0) ? 1 : MENU_SHADOW;
574 w += x - 1; 560 w += x - 1;
575 h += y - 1; 561 h += y - 1;
576 for (; shadow-- > 0; x++, y++, w--, h--) 562 for (; shadow-- > 0; x++, y++, w--, h--)
577 { 563 {
578 XDrawLine (display, win, topShadow, x, y, w, y); 564 XDrawLine (display, win, topShadow, x, y, w, y);
583} 569}
584#endif 570#endif
585 571
586/* button shapes */ 572/* button shapes */
587#ifdef MENUBAR 573#ifdef MENUBAR
588/* INTPROTO */
589void 574void
590rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 575rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
591{ 576{
592 switch (type) 577 switch (type)
593 { 578 {
624#endif 609#endif
625 610
626 } 611 }
627} 612}
628#endif 613#endif
614
615// should not be used in interactive programs, for obvious reasons
616void rxvt_usleep (int usecs)
617{
618#if HAVE_NANOSLEEP
619 struct timespec ts;
620
621 ts.tv_sec = 0;
622 ts.tv_nsec = usecs * 1000;
623 nanosleep (&ts, NULL);
624#else
625 /* use select for timing */
626 struct timeval tv;
627
628 tv.tv_sec = 0;
629 tv.tv_usec = usecs;
630 select (0, NULL, NULL, NULL, &tv);
631#endif
632}
633
629/*----------------------- end-of-file (C source) -----------------------*/ 634/*----------------------- end-of-file (C source) -----------------------*/
635

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines