typo fixups
[platform/upstream/glib.git] / gstrfuncs.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <locale.h>
24 #include <ctype.h>              /* For tolower() */
25 #include "glib.h"
26 /* do not include <unistd.h> in this place since it
27  * inteferes with g_strsignal() on some OSes
28  */
29
30 gchar*
31 g_strdup (const gchar *str)
32 {
33   gchar *new_str;
34   
35   new_str = NULL;
36   if (str)
37     {
38       new_str = g_new (char, strlen (str) + 1);
39       strcpy (new_str, str);
40     }
41   
42   return new_str;
43 }
44
45 gchar*
46 g_strconcat (const gchar *string1, ...)
47 {
48   guint   l;
49   va_list args;
50   gchar   *s;
51   gchar   *concat;
52   
53   g_return_val_if_fail (string1 != NULL, NULL);
54   
55   l = 1 + strlen (string1);
56   va_start (args, string1);
57   s = va_arg (args, gchar*);
58   while (s)
59     {
60       l += strlen (s);
61       s = va_arg (args, gchar*);
62     }
63   va_end (args);
64   
65   concat = g_new (gchar, l);
66   concat[0] = 0;
67   
68   strcat (concat, string1);
69   va_start (args, string1);
70   s = va_arg (args, gchar*);
71   while (s)
72     {
73       strcat (concat, s);
74       s = va_arg (args, gchar*);
75     }
76   va_end (args);
77   
78   return concat;
79 }
80
81 gdouble
82 g_strtod (const gchar *nptr,
83           gchar **endptr)
84 {
85   gchar *fail_pos_1;
86   gchar *fail_pos_2;
87   gdouble val_1;
88   gdouble val_2 = 0;
89
90   g_return_val_if_fail (nptr != NULL, 0);
91
92   fail_pos_1 = NULL;
93   fail_pos_2 = NULL;
94
95   val_1 = strtod (nptr, &fail_pos_1);
96
97   if (fail_pos_1 && fail_pos_1[0] != 0)
98     {
99       gchar *old_locale;
100
101       old_locale = setlocale (LC_NUMERIC, "C");
102       val_2 = strtod (nptr, &fail_pos_2);
103       setlocale (LC_NUMERIC, old_locale);
104     }
105
106   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
107     {
108       if (endptr)
109         *endptr = fail_pos_1;
110       return val_1;
111     }
112   else
113     {
114       if (endptr)
115         *endptr = fail_pos_2;
116       return val_2;
117     }
118 }
119
120 gchar*
121 g_strerror (gint errnum)
122 {
123   static char msg[64];
124   
125 #ifdef HAVE_STRERROR
126   return strerror (errnum);
127 #elif NO_SYS_ERRLIST
128   switch (errnum)
129     {
130 #ifdef E2BIG
131     case E2BIG: return "argument list too long";
132 #endif
133 #ifdef EACCES
134     case EACCES: return "permission denied";
135 #endif
136 #ifdef EADDRINUSE
137     case EADDRINUSE: return "address already in use";
138 #endif
139 #ifdef EADDRNOTAVAIL
140     case EADDRNOTAVAIL: return "can't assign requested address";
141 #endif
142 #ifdef EADV
143     case EADV: return "advertise error";
144 #endif
145 #ifdef EAFNOSUPPORT
146     case EAFNOSUPPORT: return "address family not supported by protocol family";
147 #endif
148 #ifdef EAGAIN
149     case EAGAIN: return "try again";
150 #endif
151 #ifdef EALIGN
152     case EALIGN: return "EALIGN";
153 #endif
154 #ifdef EALREADY
155     case EALREADY: return "operation already in progress";
156 #endif
157 #ifdef EBADE
158     case EBADE: return "bad exchange descriptor";
159 #endif
160 #ifdef EBADF
161     case EBADF: return "bad file number";
162 #endif
163 #ifdef EBADFD
164     case EBADFD: return "file descriptor in bad state";
165 #endif
166 #ifdef EBADMSG
167     case EBADMSG: return "not a data message";
168 #endif
169 #ifdef EBADR
170     case EBADR: return "bad request descriptor";
171 #endif
172 #ifdef EBADRPC
173     case EBADRPC: return "RPC structure is bad";
174 #endif
175 #ifdef EBADRQC
176     case EBADRQC: return "bad request code";
177 #endif
178 #ifdef EBADSLT
179     case EBADSLT: return "invalid slot";
180 #endif
181 #ifdef EBFONT
182     case EBFONT: return "bad font file format";
183 #endif
184 #ifdef EBUSY
185     case EBUSY: return "mount device busy";
186 #endif
187 #ifdef ECHILD
188     case ECHILD: return "no children";
189 #endif
190 #ifdef ECHRNG
191     case ECHRNG: return "channel number out of range";
192 #endif
193 #ifdef ECOMM
194     case ECOMM: return "communication error on send";
195 #endif
196 #ifdef ECONNABORTED
197     case ECONNABORTED: return "software caused connection abort";
198 #endif
199 #ifdef ECONNREFUSED
200     case ECONNREFUSED: return "connection refused";
201 #endif
202 #ifdef ECONNRESET
203     case ECONNRESET: return "connection reset by peer";
204 #endif
205 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
206     case EDEADLK: return "resource deadlock avoided";
207 #endif
208 #ifdef EDEADLOCK
209     case EDEADLOCK: return "resource deadlock avoided";
210 #endif
211 #ifdef EDESTADDRREQ
212     case EDESTADDRREQ: return "destination address required";
213 #endif
214 #ifdef EDIRTY
215     case EDIRTY: return "mounting a dirty fs w/o force";
216 #endif
217 #ifdef EDOM
218     case EDOM: return "math argument out of range";
219 #endif
220 #ifdef EDOTDOT
221     case EDOTDOT: return "cross mount point";
222 #endif
223 #ifdef EDQUOT
224     case EDQUOT: return "disk quota exceeded";
225 #endif
226 #ifdef EDUPPKG
227     case EDUPPKG: return "duplicate package name";
228 #endif
229 #ifdef EEXIST
230     case EEXIST: return "file already exists";
231 #endif
232 #ifdef EFAULT
233     case EFAULT: return "bad address in system call argument";
234 #endif
235 #ifdef EFBIG
236     case EFBIG: return "file too large";
237 #endif
238 #ifdef EHOSTDOWN
239     case EHOSTDOWN: return "host is down";
240 #endif
241 #ifdef EHOSTUNREACH
242     case EHOSTUNREACH: return "host is unreachable";
243 #endif
244 #ifdef EIDRM
245     case EIDRM: return "identifier removed";
246 #endif
247 #ifdef EINIT
248     case EINIT: return "initialization error";
249 #endif
250 #ifdef EINPROGRESS
251     case EINPROGRESS: return "operation now in progress";
252 #endif
253 #ifdef EINTR
254     case EINTR: return "interrupted system call";
255 #endif
256 #ifdef EINVAL
257     case EINVAL: return "invalid argument";
258 #endif
259 #ifdef EIO
260     case EIO: return "I/O error";
261 #endif
262 #ifdef EISCONN
263     case EISCONN: return "socket is already connected";
264 #endif
265 #ifdef EISDIR
266     case EISDIR: return "illegal operation on a directory";
267 #endif
268 #ifdef EISNAME
269     case EISNAM: return "is a name file";
270 #endif
271 #ifdef ELBIN
272     case ELBIN: return "ELBIN";
273 #endif
274 #ifdef EL2HLT
275     case EL2HLT: return "level 2 halted";
276 #endif
277 #ifdef EL2NSYNC
278     case EL2NSYNC: return "level 2 not synchronized";
279 #endif
280 #ifdef EL3HLT
281     case EL3HLT: return "level 3 halted";
282 #endif
283 #ifdef EL3RST
284     case EL3RST: return "level 3 reset";
285 #endif
286 #ifdef ELIBACC
287     case ELIBACC: return "can not access a needed shared library";
288 #endif
289 #ifdef ELIBBAD
290     case ELIBBAD: return "accessing a corrupted shared library";
291 #endif
292 #ifdef ELIBEXEC
293     case ELIBEXEC: return "can not exec a shared library directly";
294 #endif
295 #ifdef ELIBMAX
296     case ELIBMAX: return "attempting to link in more shared libraries than system limit";
297 #endif
298 #ifdef ELIBSCN
299     case ELIBSCN: return ".lib section in a.out corrupted";
300 #endif
301 #ifdef ELNRNG
302     case ELNRNG: return "link number out of range";
303 #endif
304 #ifdef ELOOP
305     case ELOOP: return "too many levels of symbolic links";
306 #endif
307 #ifdef EMFILE
308     case EMFILE: return "too many open files";
309 #endif
310 #ifdef EMLINK
311     case EMLINK: return "too many links";
312 #endif
313 #ifdef EMSGSIZE
314     case EMSGSIZE: return "message too long";
315 #endif
316 #ifdef EMULTIHOP
317     case EMULTIHOP: return "multihop attempted";
318 #endif
319 #ifdef ENAMETOOLONG
320     case ENAMETOOLONG: return "file name too long";
321 #endif
322 #ifdef ENAVAIL
323     case ENAVAIL: return "not available";
324 #endif
325 #ifdef ENET
326     case ENET: return "ENET";
327 #endif
328 #ifdef ENETDOWN
329     case ENETDOWN: return "network is down";
330 #endif
331 #ifdef ENETRESET
332     case ENETRESET: return "network dropped connection on reset";
333 #endif
334 #ifdef ENETUNREACH
335     case ENETUNREACH: return "network is unreachable";
336 #endif
337 #ifdef ENFILE
338     case ENFILE: return "file table overflow";
339 #endif
340 #ifdef ENOANO
341     case ENOANO: return "anode table overflow";
342 #endif
343 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
344     case ENOBUFS: return "no buffer space available";
345 #endif
346 #ifdef ENOCSI
347     case ENOCSI: return "no CSI structure available";
348 #endif
349 #ifdef ENODATA
350     case ENODATA: return "no data available";
351 #endif
352 #ifdef ENODEV
353     case ENODEV: return "no such device";
354 #endif
355 #ifdef ENOENT
356     case ENOENT: return "no such file or directory";
357 #endif
358 #ifdef ENOEXEC
359     case ENOEXEC: return "exec format error";
360 #endif
361 #ifdef ENOLCK
362     case ENOLCK: return "no locks available";
363 #endif
364 #ifdef ENOLINK
365     case ENOLINK: return "link has be severed";
366 #endif
367 #ifdef ENOMEM
368     case ENOMEM: return "not enough memory";
369 #endif
370 #ifdef ENOMSG
371     case ENOMSG: return "no message of desired type";
372 #endif
373 #ifdef ENONET
374     case ENONET: return "machine is not on the network";
375 #endif
376 #ifdef ENOPKG
377     case ENOPKG: return "package not installed";
378 #endif
379 #ifdef ENOPROTOOPT
380     case ENOPROTOOPT: return "bad proocol option";
381 #endif
382 #ifdef ENOSPC
383     case ENOSPC: return "no space left on device";
384 #endif
385 #ifdef ENOSR
386     case ENOSR: return "out of stream resources";
387 #endif
388 #ifdef ENOSTR
389     case ENOSTR: return "not a stream device";
390 #endif
391 #ifdef ENOSYM
392     case ENOSYM: return "unresolved symbol name";
393 #endif
394 #ifdef ENOSYS
395     case ENOSYS: return "function not implemented";
396 #endif
397 #ifdef ENOTBLK
398     case ENOTBLK: return "block device required";
399 #endif
400 #ifdef ENOTCONN
401     case ENOTCONN: return "socket is not connected";
402 #endif
403 #ifdef ENOTDIR
404     case ENOTDIR: return "not a directory";
405 #endif
406 #ifdef ENOTEMPTY
407     case ENOTEMPTY: return "directory not empty";
408 #endif
409 #ifdef ENOTNAM
410     case ENOTNAM: return "not a name file";
411 #endif
412 #ifdef ENOTSOCK
413     case ENOTSOCK: return "socket operation on non-socket";
414 #endif
415 #ifdef ENOTTY
416     case ENOTTY: return "inappropriate device for ioctl";
417 #endif
418 #ifdef ENOTUNIQ
419     case ENOTUNIQ: return "name not unique on network";
420 #endif
421 #ifdef ENXIO
422     case ENXIO: return "no such device or address";
423 #endif
424 #ifdef EOPNOTSUPP
425     case EOPNOTSUPP: return "operation not supported on socket";
426 #endif
427 #ifdef EPERM
428     case EPERM: return "not owner";
429 #endif
430 #ifdef EPFNOSUPPORT
431     case EPFNOSUPPORT: return "protocol family not supported";
432 #endif
433 #ifdef EPIPE
434     case EPIPE: return "broken pipe";
435 #endif
436 #ifdef EPROCLIM
437     case EPROCLIM: return "too many processes";
438 #endif
439 #ifdef EPROCUNAVAIL
440     case EPROCUNAVAIL: return "bad procedure for program";
441 #endif
442 #ifdef EPROGMISMATCH
443     case EPROGMISMATCH: return "program version wrong";
444 #endif
445 #ifdef EPROGUNAVAIL
446     case EPROGUNAVAIL: return "RPC program not available";
447 #endif
448 #ifdef EPROTO
449     case EPROTO: return "protocol error";
450 #endif
451 #ifdef EPROTONOSUPPORT
452     case EPROTONOSUPPORT: return "protocol not suppored";
453 #endif
454 #ifdef EPROTOTYPE
455     case EPROTOTYPE: return "protocol wrong type for socket";
456 #endif
457 #ifdef ERANGE
458     case ERANGE: return "math result unrepresentable";
459 #endif
460 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
461     case EREFUSED: return "EREFUSED";
462 #endif
463 #ifdef EREMCHG
464     case EREMCHG: return "remote address changed";
465 #endif
466 #ifdef EREMDEV
467     case EREMDEV: return "remote device";
468 #endif
469 #ifdef EREMOTE
470     case EREMOTE: return "pathname hit remote file system";
471 #endif
472 #ifdef EREMOTEIO
473     case EREMOTEIO: return "remote i/o error";
474 #endif
475 #ifdef EREMOTERELEASE
476     case EREMOTERELEASE: return "EREMOTERELEASE";
477 #endif
478 #ifdef EROFS
479     case EROFS: return "read-only file system";
480 #endif
481 #ifdef ERPCMISMATCH
482     case ERPCMISMATCH: return "RPC version is wrong";
483 #endif
484 #ifdef ERREMOTE
485     case ERREMOTE: return "object is remote";
486 #endif
487 #ifdef ESHUTDOWN
488     case ESHUTDOWN: return "can't send afer socket shutdown";
489 #endif
490 #ifdef ESOCKTNOSUPPORT
491     case ESOCKTNOSUPPORT: return "socket type not supported";
492 #endif
493 #ifdef ESPIPE
494     case ESPIPE: return "invalid seek";
495 #endif
496 #ifdef ESRCH
497     case ESRCH: return "no such process";
498 #endif
499 #ifdef ESRMNT
500     case ESRMNT: return "srmount error";
501 #endif
502 #ifdef ESTALE
503     case ESTALE: return "stale remote file handle";
504 #endif
505 #ifdef ESUCCESS
506     case ESUCCESS: return "Error 0";
507 #endif
508 #ifdef ETIME
509     case ETIME: return "timer expired";
510 #endif
511 #ifdef ETIMEDOUT
512     case ETIMEDOUT: return "connection timed out";
513 #endif
514 #ifdef ETOOMANYREFS
515     case ETOOMANYREFS: return "too many references: can't splice";
516 #endif
517 #ifdef ETXTBSY
518     case ETXTBSY: return "text file or pseudo-device busy";
519 #endif
520 #ifdef EUCLEAN
521     case EUCLEAN: return "structure needs cleaning";
522 #endif
523 #ifdef EUNATCH
524     case EUNATCH: return "protocol driver not attached";
525 #endif
526 #ifdef EUSERS
527     case EUSERS: return "too many users";
528 #endif
529 #ifdef EVERSION
530     case EVERSION: return "version mismatch";
531 #endif
532 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
533     case EWOULDBLOCK: return "operation would block";
534 #endif
535 #ifdef EXDEV
536     case EXDEV: return "cross-domain link";
537 #endif
538 #ifdef EXFULL
539     case EXFULL: return "message tables full";
540 #endif
541     }
542 #else /* NO_SYS_ERRLIST */
543   extern int sys_nerr;
544   extern char *sys_errlist[];
545
546   if ((errnum > 0) && (errnum <= sys_nerr))
547     return sys_errlist [errnum];
548 #endif /* NO_SYS_ERRLIST */
549
550   sprintf (msg, "unknown error (%d)", errnum);
551   return msg;
552 }
553
554 gchar*
555 g_strsignal (gint signum)
556 {
557   static char msg[64];
558
559 #ifdef HAVE_STRSIGNAL
560   extern char *strsignal (int sig);
561   return strsignal (signum);
562 #elif NO_SYS_SIGLIST
563   switch (signum)
564     {
565 #ifdef SIGHUP
566     case SIGHUP: return "Hangup";
567 #endif
568 #ifdef SIGINT
569     case SIGINT: return "Interrupt";
570 #endif
571 #ifdef SIGQUIT
572     case SIGQUIT: return "Quit";
573 #endif
574 #ifdef SIGILL
575     case SIGILL: return "Illegal instruction";
576 #endif
577 #ifdef SIGTRAP
578     case SIGTRAP: return "Trace/breakpoint trap";
579 #endif
580 #ifdef SIGABRT
581     case SIGABRT: return "IOT trap/Abort";
582 #endif
583 #ifdef SIGBUS
584     case SIGBUS: return "Bus error";
585 #endif
586 #ifdef SIGFPE
587     case SIGFPE: return "Floating point exception";
588 #endif
589 #ifdef SIGKILL
590     case SIGKILL: return "Killed";
591 #endif
592 #ifdef SIGUSR1
593     case SIGUSR1: return "User defined signal 1";
594 #endif
595 #ifdef SIGSEGV
596     case SIGSEGV: return "Segmentation fault";
597 #endif
598 #ifdef SIGUSR2
599     case SIGUSR2: return "User defined signal 2";
600 #endif
601 #ifdef SIGPIPE
602     case SIGPIPE: return "Broken pipe";
603 #endif
604 #ifdef SIGALRM
605     case SIGALRM: return "Alarm clock";
606 #endif
607 #ifdef SIGTERM
608     case SIGTERM: return "Terminated";
609 #endif
610 #ifdef SIGSTKFLT
611     case SIGSTKFLT: return "Stack fault";
612 #endif
613 #ifdef SIGCHLD
614     case SIGCHLD: return "Child exited";
615 #endif
616 #ifdef SIGCONT
617     case SIGCONT: return "Continued";
618 #endif
619 #ifdef SIGSTOP
620     case SIGSTOP: return "Stopped (signal)";
621 #endif
622 #ifdef SIGTSTP
623     case SIGTSTP: return "Stopped";
624 #endif
625 #ifdef SIGTTIN
626     case SIGTTIN: return "Stopped (tty input)";
627 #endif
628 #ifdef SIGTTOU
629     case SIGTTOU: return "Stopped (tty output)";
630 #endif
631 #ifdef SIGURG
632     case SIGURG: return "Urgent condition";
633 #endif
634 #ifdef SIGXCPU
635     case SIGXCPU: return "CPU time limit exceeded";
636 #endif
637 #ifdef SIGXFSZ
638     case SIGXFSZ: return "File size limit exceeded";
639 #endif
640 #ifdef SIGVTALRM
641     case SIGVTALRM: return "Virtual time alarm";
642 #endif
643 #ifdef SIGPROF
644     case SIGPROF: return "Profile signal";
645 #endif
646 #ifdef SIGWINCH
647     case SIGWINCH: return "Window size changed";
648 #endif
649 #ifdef SIGIO
650     case SIGIO: return "Possible I/O";
651 #endif
652 #ifdef SIGPWR
653     case SIGPWR: return "Power failure";
654 #endif
655 #ifdef SIGUNUSED
656     case SIGUNUSED: return "Unused signal";
657 #endif
658     }
659 #else /* NO_SYS_SIGLIST */
660   extern char *sys_siglist[];
661   return sys_siglist [signum];
662 #endif /* NO_SYS_SIGLIST */
663
664   sprintf (msg, "unknown signal (%d)", signum);
665   return msg;
666 }
667
668 void
669 g_strdown (gchar  *string)
670 {
671   register gchar *s;
672
673   g_return_if_fail (string != NULL);
674
675   s = string;
676
677   while (*s)
678     {
679       *s = tolower (*s);
680       s++;
681     }
682 }
683
684 void
685 g_strup (gchar  *string)
686 {
687   register gchar *s;
688
689   g_return_if_fail (string != NULL);
690
691   s = string;
692
693   while (*s)
694     {
695       *s = toupper (*s);
696       s++;
697     }
698 }
699
700 void
701 g_strreverse (gchar       *string)
702 {
703   g_return_if_fail (string != NULL);
704
705   if (*string)
706     {
707       register gchar *h, *t;
708
709       h = string;
710       t = string + strlen (string) - 1;
711       
712       while (h < t)
713         {
714           register gchar c;
715           
716           c = *h;
717           *h = *t;
718           h++;
719           *t = c;
720           t--;
721         }
722     }
723 }
724
725 gint
726 g_strcasecmp (const gchar *s1,
727               const gchar *s2)
728 {
729 #ifdef HAVE_STRCASECMP
730   return strcasecmp (s1, s2);
731 #else
732   gint c1, c2;
733
734   while (*s1 && *s2)
735     {
736       /* According to A. Cox, some platforms have islower's that
737        * don't work right on non-uppercase
738        */
739       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
740       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
741       if (c1 != c2)
742         return (c1 - c2);
743       s1++; s2++;
744     }
745
746   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
747 #endif
748 }
749
750 void
751 g_strdelimit (gchar       *string,
752               const gchar *delimiters,
753               gchar        new_delim)
754 {
755   register gchar *c;
756
757   g_return_if_fail (string != NULL);
758
759   if (!delimiters)
760     delimiters = G_STR_DELIMITERS;
761
762   for (c = string; *c; c++)
763     {
764       if (strchr (delimiters, *c))
765         *c = new_delim;
766     }
767 }