PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/source/mydroid/external/bluetooth/glib/glib/glib/gstrfuncs.c

https://bitbucket.org/tfzxyinhao/kindle-fire
C | 3253 lines | 2447 code | 208 blank | 598 comment | 298 complexity | e6b89aaaec5fe676cfdb3099a7675c23 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, 0BSD, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.0, BSD-3-Clause, LGPL-2.1, Apache-2.0, AGPL-3.0, GPL-3.0
  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 Lesser 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. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser 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. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. /*
  26. * MT safe
  27. */
  28. #include "config.h"
  29. #define _GNU_SOURCE /* For stpcpy */
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <locale.h>
  35. #include <errno.h>
  36. #include <ctype.h> /* For tolower() */
  37. #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
  38. #include <signal.h>
  39. #endif
  40. #include "glib.h"
  41. #include "gprintf.h"
  42. #include "gprintfint.h"
  43. #include "glibintl.h"
  44. #include "galias.h"
  45. #ifdef G_OS_WIN32
  46. #include <windows.h>
  47. #endif
  48. /* do not include <unistd.h> in this place since it
  49. * interferes with g_strsignal() on some OSes
  50. */
  51. static const guint16 ascii_table_data[256] = {
  52. 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
  53. 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
  54. 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
  55. 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
  56. 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  57. 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  58. 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
  59. 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  60. 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
  61. 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
  62. 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
  63. 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  64. 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
  65. 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
  66. 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
  67. 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
  68. /* the upper 128 are all zeroes */
  69. };
  70. const guint16 * const g_ascii_table = ascii_table_data;
  71. /**
  72. * g_strdup:
  73. * @str: the string to duplicate
  74. *
  75. * Duplicates a string. If @str is %NULL it returns %NULL.
  76. * The returned string should be freed with g_free()
  77. * when no longer needed.
  78. *
  79. * Returns: a newly-allocated copy of @str
  80. */
  81. gchar*
  82. g_strdup (const gchar *str)
  83. {
  84. gchar *new_str;
  85. gsize length;
  86. if (str)
  87. {
  88. length = strlen (str) + 1;
  89. new_str = g_new (char, length);
  90. memcpy (new_str, str, length);
  91. }
  92. else
  93. new_str = NULL;
  94. return new_str;
  95. }
  96. gpointer
  97. g_memdup (gconstpointer mem,
  98. guint byte_size)
  99. {
  100. gpointer new_mem;
  101. if (mem)
  102. {
  103. new_mem = g_malloc (byte_size);
  104. memcpy (new_mem, mem, byte_size);
  105. }
  106. else
  107. new_mem = NULL;
  108. return new_mem;
  109. }
  110. /**
  111. * g_strndup:
  112. * @str: the string to duplicate
  113. * @n: the maximum number of bytes to copy from @str
  114. *
  115. * Duplicates the first @n bytes of a string, returning a newly-allocated
  116. * buffer @n + 1 bytes long which will always be nul-terminated.
  117. * If @str is less than @n bytes long the buffer is padded with nuls.
  118. * If @str is %NULL it returns %NULL.
  119. * The returned value should be freed when no longer needed.
  120. *
  121. * <note><para>
  122. * To copy a number of characters from a UTF-8 encoded string, use
  123. * g_utf8_strncpy() instead.
  124. * </para></note>
  125. *
  126. * Returns: a newly-allocated buffer containing the first @n bytes
  127. * of @str, nul-terminated
  128. */
  129. gchar*
  130. g_strndup (const gchar *str,
  131. gsize n)
  132. {
  133. gchar *new_str;
  134. if (str)
  135. {
  136. new_str = g_new (gchar, n + 1);
  137. strncpy (new_str, str, n);
  138. new_str[n] = '\0';
  139. }
  140. else
  141. new_str = NULL;
  142. return new_str;
  143. }
  144. /**
  145. * g_strnfill:
  146. * @length: the length of the new string
  147. * @fill_char: the byte to fill the string with
  148. *
  149. * Creates a new string @length bytes long filled with @fill_char.
  150. * The returned string should be freed when no longer needed.
  151. *
  152. * Returns: a newly-allocated string filled the @fill_char
  153. */
  154. gchar*
  155. g_strnfill (gsize length,
  156. gchar fill_char)
  157. {
  158. gchar *str;
  159. str = g_new (gchar, length + 1);
  160. memset (str, (guchar)fill_char, length);
  161. str[length] = '\0';
  162. return str;
  163. }
  164. /**
  165. * g_stpcpy:
  166. * @dest: destination buffer.
  167. * @src: source string.
  168. *
  169. * Copies a nul-terminated string into the dest buffer, include the
  170. * trailing nul, and return a pointer to the trailing nul byte.
  171. * This is useful for concatenating multiple strings together
  172. * without having to repeatedly scan for the end.
  173. *
  174. * Return value: a pointer to trailing nul byte.
  175. **/
  176. gchar *
  177. g_stpcpy (gchar *dest,
  178. const gchar *src)
  179. {
  180. #ifdef HAVE_STPCPY
  181. g_return_val_if_fail (dest != NULL, NULL);
  182. g_return_val_if_fail (src != NULL, NULL);
  183. return stpcpy (dest, src);
  184. #else
  185. register gchar *d = dest;
  186. register const gchar *s = src;
  187. g_return_val_if_fail (dest != NULL, NULL);
  188. g_return_val_if_fail (src != NULL, NULL);
  189. do
  190. *d++ = *s;
  191. while (*s++ != '\0');
  192. return d - 1;
  193. #endif
  194. }
  195. /**
  196. * g_strdup_vprintf:
  197. * @format: a standard printf() format string, but notice
  198. * <link linkend="string-precision">string precision pitfalls</link>
  199. * @args: the list of parameters to insert into the format string
  200. *
  201. * Similar to the standard C vsprintf() function but safer, since it
  202. * calculates the maximum space required and allocates memory to hold
  203. * the result. The returned string should be freed with g_free() when
  204. * no longer needed.
  205. *
  206. * See also g_vasprintf(), which offers the same functionality, but
  207. * additionally returns the length of the allocated string.
  208. *
  209. * Returns: a newly-allocated string holding the result
  210. */
  211. gchar*
  212. g_strdup_vprintf (const gchar *format,
  213. va_list args)
  214. {
  215. gchar *string = NULL;
  216. g_vasprintf (&string, format, args);
  217. return string;
  218. }
  219. /**
  220. * g_strdup_printf:
  221. * @format: a standard printf() format string, but notice
  222. * <link linkend="string-precision">string precision pitfalls</link>
  223. * @Varargs: the parameters to insert into the format string
  224. *
  225. * Similar to the standard C sprintf() function but safer, since it
  226. * calculates the maximum space required and allocates memory to hold
  227. * the result. The returned string should be freed with g_free() when no
  228. * longer needed.
  229. *
  230. * Returns: a newly-allocated string holding the result
  231. */
  232. gchar*
  233. g_strdup_printf (const gchar *format,
  234. ...)
  235. {
  236. gchar *buffer;
  237. va_list args;
  238. va_start (args, format);
  239. buffer = g_strdup_vprintf (format, args);
  240. va_end (args);
  241. return buffer;
  242. }
  243. /**
  244. * g_strconcat:
  245. * @string1: the first string to add, which must not be %NULL
  246. * @Varargs: a %NULL-terminated list of strings to append to the string
  247. *
  248. * Concatenates all of the given strings into one long string.
  249. * The returned string should be freed with g_free() when no longer needed.
  250. *
  251. *
  252. * <warning><para>The variable argument list <emphasis>must</emphasis> end
  253. * with %NULL. If you forget the %NULL, g_strconcat() will start appending
  254. * random memory junk to your string.</para></warning>
  255. *
  256. * Returns: a newly-allocated string containing all the string arguments
  257. */
  258. gchar*
  259. g_strconcat (const gchar *string1, ...)
  260. {
  261. gsize l;
  262. va_list args;
  263. gchar *s;
  264. gchar *concat;
  265. gchar *ptr;
  266. if (!string1)
  267. return NULL;
  268. l = 1 + strlen (string1);
  269. va_start (args, string1);
  270. s = va_arg (args, gchar*);
  271. while (s)
  272. {
  273. l += strlen (s);
  274. s = va_arg (args, gchar*);
  275. }
  276. va_end (args);
  277. concat = g_new (gchar, l);
  278. ptr = concat;
  279. ptr = g_stpcpy (ptr, string1);
  280. va_start (args, string1);
  281. s = va_arg (args, gchar*);
  282. while (s)
  283. {
  284. ptr = g_stpcpy (ptr, s);
  285. s = va_arg (args, gchar*);
  286. }
  287. va_end (args);
  288. return concat;
  289. }
  290. /**
  291. * g_strtod:
  292. * @nptr: the string to convert to a numeric value.
  293. * @endptr: if non-%NULL, it returns the character after
  294. * the last character used in the conversion.
  295. *
  296. * Converts a string to a #gdouble value.
  297. * It calls the standard strtod() function to handle the conversion, but
  298. * if the string is not completely converted it attempts the conversion
  299. * again with g_ascii_strtod(), and returns the best match.
  300. *
  301. * This function should seldomly be used. The normal situation when reading
  302. * numbers not for human consumption is to use g_ascii_strtod(). Only when
  303. * you know that you must expect both locale formatted and C formatted numbers
  304. * should you use this. Make sure that you don't pass strings such as comma
  305. * separated lists of values, since the commas may be interpreted as a decimal
  306. * point in some locales, causing unexpected results.
  307. *
  308. * Return value: the #gdouble value.
  309. **/
  310. gdouble
  311. g_strtod (const gchar *nptr,
  312. gchar **endptr)
  313. {
  314. gchar *fail_pos_1;
  315. gchar *fail_pos_2;
  316. gdouble val_1;
  317. gdouble val_2 = 0;
  318. g_return_val_if_fail (nptr != NULL, 0);
  319. fail_pos_1 = NULL;
  320. fail_pos_2 = NULL;
  321. val_1 = strtod (nptr, &fail_pos_1);
  322. if (fail_pos_1 && fail_pos_1[0] != 0)
  323. val_2 = g_ascii_strtod (nptr, &fail_pos_2);
  324. if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
  325. {
  326. if (endptr)
  327. *endptr = fail_pos_1;
  328. return val_1;
  329. }
  330. else
  331. {
  332. if (endptr)
  333. *endptr = fail_pos_2;
  334. return val_2;
  335. }
  336. }
  337. /**
  338. * g_ascii_strtod:
  339. * @nptr: the string to convert to a numeric value.
  340. * @endptr: if non-%NULL, it returns the character after
  341. * the last character used in the conversion.
  342. *
  343. * Converts a string to a #gdouble value.
  344. *
  345. * This function behaves like the standard strtod() function
  346. * does in the C locale. It does this without actually changing
  347. * the current locale, since that would not be thread-safe.
  348. * A limitation of the implementation is that this function
  349. * will still accept localized versions of infinities and NANs.
  350. *
  351. * This function is typically used when reading configuration
  352. * files or other non-user input that should be locale independent.
  353. * To handle input from the user you should normally use the
  354. * locale-sensitive system strtod() function.
  355. *
  356. * To convert from a #gdouble to a string in a locale-insensitive
  357. * way, use g_ascii_dtostr().
  358. *
  359. * If the correct value would cause overflow, plus or minus %HUGE_VAL
  360. * is returned (according to the sign of the value), and %ERANGE is
  361. * stored in %errno. If the correct value would cause underflow,
  362. * zero is returned and %ERANGE is stored in %errno.
  363. *
  364. * This function resets %errno before calling strtod() so that
  365. * you can reliably detect overflow and underflow.
  366. *
  367. * Return value: the #gdouble value.
  368. **/
  369. gdouble
  370. g_ascii_strtod (const gchar *nptr,
  371. gchar **endptr)
  372. {
  373. gchar *fail_pos;
  374. gdouble val;
  375. struct lconv *locale_data;
  376. const char *decimal_point;
  377. int decimal_point_len;
  378. const char *p, *decimal_point_pos;
  379. const char *end = NULL; /* Silence gcc */
  380. int strtod_errno;
  381. g_return_val_if_fail (nptr != NULL, 0);
  382. fail_pos = NULL;
  383. #ifndef ANDROID_STUB
  384. locale_data = localeconv ();
  385. decimal_point = locale_data->decimal_point;
  386. decimal_point_len = strlen (decimal_point);
  387. g_assert (decimal_point_len != 0);
  388. decimal_point_pos = NULL;
  389. end = NULL;
  390. if (decimal_point[0] != '.' ||
  391. decimal_point[1] != 0)
  392. {
  393. p = nptr;
  394. /* Skip leading space */
  395. while (g_ascii_isspace (*p))
  396. p++;
  397. /* Skip leading optional sign */
  398. if (*p == '+' || *p == '-')
  399. p++;
  400. if (p[0] == '0' &&
  401. (p[1] == 'x' || p[1] == 'X'))
  402. {
  403. p += 2;
  404. /* HEX - find the (optional) decimal point */
  405. while (g_ascii_isxdigit (*p))
  406. p++;
  407. if (*p == '.')
  408. decimal_point_pos = p++;
  409. while (g_ascii_isxdigit (*p))
  410. p++;
  411. if (*p == 'p' || *p == 'P')
  412. p++;
  413. if (*p == '+' || *p == '-')
  414. p++;
  415. while (g_ascii_isdigit (*p))
  416. p++;
  417. end = p;
  418. }
  419. else if (g_ascii_isdigit (*p) || *p == '.')
  420. {
  421. while (g_ascii_isdigit (*p))
  422. p++;
  423. if (*p == '.')
  424. decimal_point_pos = p++;
  425. while (g_ascii_isdigit (*p))
  426. p++;
  427. if (*p == 'e' || *p == 'E')
  428. p++;
  429. if (*p == '+' || *p == '-')
  430. p++;
  431. while (g_ascii_isdigit (*p))
  432. p++;
  433. end = p;
  434. }
  435. /* For the other cases, we need not convert the decimal point */
  436. }
  437. if (decimal_point_pos)
  438. {
  439. char *copy, *c;
  440. /* We need to convert the '.' to the locale specific decimal point */
  441. copy = g_malloc (end - nptr + 1 + decimal_point_len);
  442. c = copy;
  443. memcpy (c, nptr, decimal_point_pos - nptr);
  444. c += decimal_point_pos - nptr;
  445. memcpy (c, decimal_point, decimal_point_len);
  446. c += decimal_point_len;
  447. memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
  448. c += end - (decimal_point_pos + 1);
  449. *c = 0;
  450. errno = 0;
  451. val = strtod (copy, &fail_pos);
  452. strtod_errno = errno;
  453. if (fail_pos)
  454. {
  455. if (fail_pos - copy > decimal_point_pos - nptr)
  456. fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
  457. else
  458. fail_pos = (char *)nptr + (fail_pos - copy);
  459. }
  460. g_free (copy);
  461. }
  462. else if (end)
  463. {
  464. char *copy;
  465. copy = g_malloc (end - (char *)nptr + 1);
  466. memcpy (copy, nptr, end - nptr);
  467. *(copy + (end - (char *)nptr)) = 0;
  468. errno = 0;
  469. val = strtod (copy, &fail_pos);
  470. strtod_errno = errno;
  471. if (fail_pos)
  472. {
  473. fail_pos = (char *)nptr + (fail_pos - copy);
  474. }
  475. g_free (copy);
  476. }
  477. else
  478. #endif
  479. {
  480. errno = 0;
  481. val = strtod (nptr, &fail_pos);
  482. strtod_errno = errno;
  483. }
  484. if (endptr)
  485. *endptr = fail_pos;
  486. errno = strtod_errno;
  487. return val;
  488. }
  489. /**
  490. * g_ascii_dtostr:
  491. * @buffer: A buffer to place the resulting string in
  492. * @buf_len: The length of the buffer.
  493. * @d: The #gdouble to convert
  494. *
  495. * Converts a #gdouble to a string, using the '.' as
  496. * decimal point.
  497. *
  498. * This functions generates enough precision that converting
  499. * the string back using g_ascii_strtod() gives the same machine-number
  500. * (on machines with IEEE compatible 64bit doubles). It is
  501. * guaranteed that the size of the resulting string will never
  502. * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
  503. *
  504. * Return value: The pointer to the buffer with the converted string.
  505. **/
  506. gchar *
  507. g_ascii_dtostr (gchar *buffer,
  508. gint buf_len,
  509. gdouble d)
  510. {
  511. return g_ascii_formatd (buffer, buf_len, "%.17g", d);
  512. }
  513. /**
  514. * g_ascii_formatd:
  515. * @buffer: A buffer to place the resulting string in
  516. * @buf_len: The length of the buffer.
  517. * @format: The printf()-style format to use for the
  518. * code to use for converting.
  519. * @d: The #gdouble to convert
  520. *
  521. * Converts a #gdouble to a string, using the '.' as
  522. * decimal point. To format the number you pass in
  523. * a printf()-style format string. Allowed conversion
  524. * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
  525. *
  526. * If you just want to want to serialize the value into a
  527. * string, use g_ascii_dtostr().
  528. *
  529. * Return value: The pointer to the buffer with the converted string.
  530. */
  531. gchar *
  532. g_ascii_formatd (gchar *buffer,
  533. gint buf_len,
  534. const gchar *format,
  535. gdouble d)
  536. {
  537. struct lconv *locale_data;
  538. const char *decimal_point;
  539. int decimal_point_len;
  540. gchar *p;
  541. int rest_len;
  542. gchar format_char;
  543. g_return_val_if_fail (buffer != NULL, NULL);
  544. g_return_val_if_fail (format[0] == '%', NULL);
  545. g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
  546. format_char = format[strlen (format) - 1];
  547. g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
  548. format_char == 'f' || format_char == 'F' ||
  549. format_char == 'g' || format_char == 'G',
  550. NULL);
  551. if (format[0] != '%')
  552. return NULL;
  553. if (strpbrk (format + 1, "'l%"))
  554. return NULL;
  555. if (!(format_char == 'e' || format_char == 'E' ||
  556. format_char == 'f' || format_char == 'F' ||
  557. format_char == 'g' || format_char == 'G'))
  558. return NULL;
  559. _g_snprintf (buffer, buf_len, format, d);
  560. #ifndef ANDROID_STUB
  561. locale_data = localeconv ();
  562. decimal_point = locale_data->decimal_point;
  563. decimal_point_len = strlen (decimal_point);
  564. g_assert (decimal_point_len != 0);
  565. if (decimal_point[0] != '.' ||
  566. decimal_point[1] != 0)
  567. {
  568. p = buffer;
  569. while (g_ascii_isspace (*p))
  570. p++;
  571. if (*p == '+' || *p == '-')
  572. p++;
  573. while (isdigit ((guchar)*p))
  574. p++;
  575. if (strncmp (p, decimal_point, decimal_point_len) == 0)
  576. {
  577. *p = '.';
  578. p++;
  579. if (decimal_point_len > 1)
  580. {
  581. rest_len = strlen (p + (decimal_point_len-1));
  582. memmove (p, p + (decimal_point_len-1), rest_len);
  583. p[rest_len] = 0;
  584. }
  585. }
  586. }
  587. #endif
  588. return buffer;
  589. }
  590. static guint64
  591. g_parse_long_long (const gchar *nptr,
  592. const gchar **endptr,
  593. guint base,
  594. gboolean *negative)
  595. {
  596. /* this code is based on on the strtol(3) code from GNU libc released under
  597. * the GNU Lesser General Public License.
  598. *
  599. * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
  600. * Free Software Foundation, Inc.
  601. */
  602. #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
  603. (c) == '\r' || (c) == '\t' || (c) == '\v')
  604. #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
  605. #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
  606. #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
  607. #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
  608. #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
  609. gboolean overflow;
  610. guint64 cutoff;
  611. guint64 cutlim;
  612. guint64 ui64;
  613. const gchar *s, *save;
  614. guchar c;
  615. g_return_val_if_fail (nptr != NULL, 0);
  616. *negative = FALSE;
  617. if (base == 1 || base > 36)
  618. {
  619. errno = EINVAL;
  620. if (endptr)
  621. *endptr = nptr;
  622. return 0;
  623. }
  624. save = s = nptr;
  625. /* Skip white space. */
  626. while (ISSPACE (*s))
  627. ++s;
  628. if (G_UNLIKELY (!*s))
  629. goto noconv;
  630. /* Check for a sign. */
  631. if (*s == '-')
  632. {
  633. *negative = TRUE;
  634. ++s;
  635. }
  636. else if (*s == '+')
  637. ++s;
  638. /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
  639. if (*s == '0')
  640. {
  641. if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
  642. {
  643. s += 2;
  644. base = 16;
  645. }
  646. else if (base == 0)
  647. base = 8;
  648. }
  649. else if (base == 0)
  650. base = 10;
  651. /* Save the pointer so we can check later if anything happened. */
  652. save = s;
  653. cutoff = G_MAXUINT64 / base;
  654. cutlim = G_MAXUINT64 % base;
  655. overflow = FALSE;
  656. ui64 = 0;
  657. c = *s;
  658. for (; c; c = *++s)
  659. {
  660. if (c >= '0' && c <= '9')
  661. c -= '0';
  662. else if (ISALPHA (c))
  663. c = TOUPPER (c) - 'A' + 10;
  664. else
  665. break;
  666. if (c >= base)
  667. break;
  668. /* Check for overflow. */
  669. if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
  670. overflow = TRUE;
  671. else
  672. {
  673. ui64 *= base;
  674. ui64 += c;
  675. }
  676. }
  677. /* Check if anything actually happened. */
  678. if (s == save)
  679. goto noconv;
  680. /* Store in ENDPTR the address of one character
  681. past the last character we converted. */
  682. if (endptr)
  683. *endptr = s;
  684. if (G_UNLIKELY (overflow))
  685. {
  686. errno = ERANGE;
  687. return G_MAXUINT64;
  688. }
  689. return ui64;
  690. noconv:
  691. /* We must handle a special case here: the base is 0 or 16 and the
  692. first two characters are '0' and 'x', but the rest are no
  693. hexadecimal digits. This is no error case. We return 0 and
  694. ENDPTR points to the `x`. */
  695. if (endptr)
  696. {
  697. if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
  698. && save[-2] == '0')
  699. *endptr = &save[-1];
  700. else
  701. /* There was no number to convert. */
  702. *endptr = nptr;
  703. }
  704. return 0;
  705. }
  706. /**
  707. * g_ascii_strtoull:
  708. * @nptr: the string to convert to a numeric value.
  709. * @endptr: if non-%NULL, it returns the character after
  710. * the last character used in the conversion.
  711. * @base: to be used for the conversion, 2..36 or 0
  712. *
  713. * Converts a string to a #guint64 value.
  714. * This function behaves like the standard strtoull() function
  715. * does in the C locale. It does this without actually
  716. * changing the current locale, since that would not be
  717. * thread-safe.
  718. *
  719. * This function is typically used when reading configuration
  720. * files or other non-user input that should be locale independent.
  721. * To handle input from the user you should normally use the
  722. * locale-sensitive system strtoull() function.
  723. *
  724. * If the correct value would cause overflow, %G_MAXUINT64
  725. * is returned, and %ERANGE is stored in %errno. If the base is
  726. * outside the valid range, zero is returned, and %EINVAL is stored
  727. * in %errno. If the string conversion fails, zero is returned, and
  728. * @endptr returns @nptr (if @endptr is non-%NULL).
  729. *
  730. * Return value: the #guint64 value or zero on error.
  731. *
  732. * Since: 2.2
  733. */
  734. guint64
  735. g_ascii_strtoull (const gchar *nptr,
  736. gchar **endptr,
  737. guint base)
  738. {
  739. gboolean negative;
  740. guint64 result;
  741. result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
  742. /* Return the result of the appropriate sign. */
  743. return negative ? -result : result;
  744. }
  745. /**
  746. * g_ascii_strtoll:
  747. * @nptr: the string to convert to a numeric value.
  748. * @endptr: if non-%NULL, it returns the character after
  749. * the last character used in the conversion.
  750. * @base: to be used for the conversion, 2..36 or 0
  751. *
  752. * Converts a string to a #gint64 value.
  753. * This function behaves like the standard strtoll() function
  754. * does in the C locale. It does this without actually
  755. * changing the current locale, since that would not be
  756. * thread-safe.
  757. *
  758. * This function is typically used when reading configuration
  759. * files or other non-user input that should be locale independent.
  760. * To handle input from the user you should normally use the
  761. * locale-sensitive system strtoll() function.
  762. *
  763. * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
  764. * is returned, and %ERANGE is stored in %errno. If the base is
  765. * outside the valid range, zero is returned, and %EINVAL is stored
  766. * in %errno. If the string conversion fails, zero is returned, and
  767. * @endptr returns @nptr (if @endptr is non-%NULL).
  768. *
  769. * Return value: the #gint64 value or zero on error.
  770. *
  771. * Since: 2.12
  772. */
  773. gint64
  774. g_ascii_strtoll (const gchar *nptr,
  775. gchar **endptr,
  776. guint base)
  777. {
  778. gboolean negative;
  779. guint64 result;
  780. result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
  781. if (negative && result > (guint64) G_MININT64)
  782. {
  783. errno = ERANGE;
  784. return G_MININT64;
  785. }
  786. else if (!negative && result > (guint64) G_MAXINT64)
  787. {
  788. errno = ERANGE;
  789. return G_MAXINT64;
  790. }
  791. else if (negative)
  792. return - (gint64) result;
  793. else
  794. return (gint64) result;
  795. }
  796. /**
  797. * g_strerror:
  798. * @errnum: the system error number. See the standard C %errno
  799. * documentation
  800. *
  801. * Returns a string corresponding to the given error code, e.g.
  802. * "no such process". You should use this function in preference to
  803. * strerror(), because it returns a string in UTF-8 encoding, and since
  804. * not all platforms support the strerror() function.
  805. *
  806. * Returns: a UTF-8 string describing the error code. If the error code
  807. * is unknown, it returns "unknown error (&lt;code&gt;)". The string
  808. * can only be used until the next call to g_strerror()
  809. */
  810. G_CONST_RETURN gchar*
  811. g_strerror (gint errnum)
  812. {
  813. static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
  814. char *msg;
  815. int saved_errno = errno;
  816. #ifdef HAVE_STRERROR
  817. const char *msg_locale;
  818. msg_locale = strerror (errnum);
  819. if (g_get_charset (NULL))
  820. {
  821. errno = saved_errno;
  822. return msg_locale;
  823. }
  824. else
  825. {
  826. gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
  827. if (msg_utf8)
  828. {
  829. /* Stick in the quark table so that we can return a static result
  830. */
  831. GQuark msg_quark = g_quark_from_string (msg_utf8);
  832. g_free (msg_utf8);
  833. msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
  834. errno = saved_errno;
  835. return msg_utf8;
  836. }
  837. }
  838. #elif NO_SYS_ERRLIST
  839. switch (errnum)
  840. {
  841. #ifdef E2BIG
  842. case E2BIG: return "argument list too long";
  843. #endif
  844. #ifdef EACCES
  845. case EACCES: return "permission denied";
  846. #endif
  847. #ifdef EADDRINUSE
  848. case EADDRINUSE: return "address already in use";
  849. #endif
  850. #ifdef EADDRNOTAVAIL
  851. case EADDRNOTAVAIL: return "can't assign requested address";
  852. #endif
  853. #ifdef EADV
  854. case EADV: return "advertise error";
  855. #endif
  856. #ifdef EAFNOSUPPORT
  857. case EAFNOSUPPORT: return "address family not supported by protocol family";
  858. #endif
  859. #ifdef EAGAIN
  860. case EAGAIN: return "try again";
  861. #endif
  862. #ifdef EALIGN
  863. case EALIGN: return "EALIGN";
  864. #endif
  865. #ifdef EALREADY
  866. case EALREADY: return "operation already in progress";
  867. #endif
  868. #ifdef EBADE
  869. case EBADE: return "bad exchange descriptor";
  870. #endif
  871. #ifdef EBADF
  872. case EBADF: return "bad file number";
  873. #endif
  874. #ifdef EBADFD
  875. case EBADFD: return "file descriptor in bad state";
  876. #endif
  877. #ifdef EBADMSG
  878. case EBADMSG: return "not a data message";
  879. #endif
  880. #ifdef EBADR
  881. case EBADR: return "bad request descriptor";
  882. #endif
  883. #ifdef EBADRPC
  884. case EBADRPC: return "RPC structure is bad";
  885. #endif
  886. #ifdef EBADRQC
  887. case EBADRQC: return "bad request code";
  888. #endif
  889. #ifdef EBADSLT
  890. case EBADSLT: return "invalid slot";
  891. #endif
  892. #ifdef EBFONT
  893. case EBFONT: return "bad font file format";
  894. #endif
  895. #ifdef EBUSY
  896. case EBUSY: return "mount device busy";
  897. #endif
  898. #ifdef ECHILD
  899. case ECHILD: return "no children";
  900. #endif
  901. #ifdef ECHRNG
  902. case ECHRNG: return "channel number out of range";
  903. #endif
  904. #ifdef ECOMM
  905. case ECOMM: return "communication error on send";
  906. #endif
  907. #ifdef ECONNABORTED
  908. case ECONNABORTED: return "software caused connection abort";
  909. #endif
  910. #ifdef ECONNREFUSED
  911. case ECONNREFUSED: return "connection refused";
  912. #endif
  913. #ifdef ECONNRESET
  914. case ECONNRESET: return "connection reset by peer";
  915. #endif
  916. #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
  917. case EDEADLK: return "resource deadlock avoided";
  918. #endif
  919. #ifdef EDEADLOCK
  920. case EDEADLOCK: return "resource deadlock avoided";
  921. #endif
  922. #ifdef EDESTADDRREQ
  923. case EDESTADDRREQ: return "destination address required";
  924. #endif
  925. #ifdef EDIRTY
  926. case EDIRTY: return "mounting a dirty fs w/o force";
  927. #endif
  928. #ifdef EDOM
  929. case EDOM: return "math argument out of range";
  930. #endif
  931. #ifdef EDOTDOT
  932. case EDOTDOT: return "cross mount point";
  933. #endif
  934. #ifdef EDQUOT
  935. case EDQUOT: return "disk quota exceeded";
  936. #endif
  937. #ifdef EDUPPKG
  938. case EDUPPKG: return "duplicate package name";
  939. #endif
  940. #ifdef EEXIST
  941. case EEXIST: return "file already exists";
  942. #endif
  943. #ifdef EFAULT
  944. case EFAULT: return "bad address in system call argument";
  945. #endif
  946. #ifdef EFBIG
  947. case EFBIG: return "file too large";
  948. #endif
  949. #ifdef EHOSTDOWN
  950. case EHOSTDOWN: return "host is down";
  951. #endif
  952. #ifdef EHOSTUNREACH
  953. case EHOSTUNREACH: return "host is unreachable";
  954. #endif
  955. #ifdef EIDRM
  956. case EIDRM: return "identifier removed";
  957. #endif
  958. #ifdef EINIT
  959. case EINIT: return "initialization error";
  960. #endif
  961. #ifdef EINPROGRESS
  962. case EINPROGRESS: return "operation now in progress";
  963. #endif
  964. #ifdef EINTR
  965. case EINTR: return "interrupted system call";
  966. #endif
  967. #ifdef EINVAL
  968. case EINVAL: return "invalid argument";
  969. #endif
  970. #ifdef EIO
  971. case EIO: return "I/O error";
  972. #endif
  973. #ifdef EISCONN
  974. case EISCONN: return "socket is already connected";
  975. #endif
  976. #ifdef EISDIR
  977. case EISDIR: return "is a directory";
  978. #endif
  979. #ifdef EISNAME
  980. case EISNAM: return "is a name file";
  981. #endif
  982. #ifdef ELBIN
  983. case ELBIN: return "ELBIN";
  984. #endif
  985. #ifdef EL2HLT
  986. case EL2HLT: return "level 2 halted";
  987. #endif
  988. #ifdef EL2NSYNC
  989. case EL2NSYNC: return "level 2 not synchronized";
  990. #endif
  991. #ifdef EL3HLT
  992. case EL3HLT: return "level 3 halted";
  993. #endif
  994. #ifdef EL3RST
  995. case EL3RST: return "level 3 reset";
  996. #endif
  997. #ifdef ELIBACC
  998. case ELIBACC: return "can not access a needed shared library";
  999. #endif
  1000. #ifdef ELIBBAD
  1001. case ELIBBAD: return "accessing a corrupted shared library";
  1002. #endif
  1003. #ifdef ELIBEXEC
  1004. case ELIBEXEC: return "can not exec a shared library directly";
  1005. #endif
  1006. #ifdef ELIBMAX
  1007. case ELIBMAX: return "attempting to link in more shared libraries than system limit";
  1008. #endif
  1009. #ifdef ELIBSCN
  1010. case ELIBSCN: return ".lib section in a.out corrupted";
  1011. #endif
  1012. #ifdef ELNRNG
  1013. case ELNRNG: return "link number out of range";
  1014. #endif
  1015. #ifdef ELOOP
  1016. case ELOOP: return "too many levels of symbolic links";
  1017. #endif
  1018. #ifdef EMFILE
  1019. case EMFILE: return "too many open files";
  1020. #endif
  1021. #ifdef EMLINK
  1022. case EMLINK: return "too many links";
  1023. #endif
  1024. #ifdef EMSGSIZE
  1025. case EMSGSIZE: return "message too long";
  1026. #endif
  1027. #ifdef EMULTIHOP
  1028. case EMULTIHOP: return "multihop attempted";
  1029. #endif
  1030. #ifdef ENAMETOOLONG
  1031. case ENAMETOOLONG: return "file name too long";
  1032. #endif
  1033. #ifdef ENAVAIL
  1034. case ENAVAIL: return "not available";
  1035. #endif
  1036. #ifdef ENET
  1037. case ENET: return "ENET";
  1038. #endif
  1039. #ifdef ENETDOWN
  1040. case ENETDOWN: return "network is down";
  1041. #endif
  1042. #ifdef ENETRESET
  1043. case ENETRESET: return "network dropped connection on reset";
  1044. #endif
  1045. #ifdef ENETUNREACH
  1046. case ENETUNREACH: return "network is unreachable";
  1047. #endif
  1048. #ifdef ENFILE
  1049. case ENFILE: return "file table overflow";
  1050. #endif
  1051. #ifdef ENOANO
  1052. case ENOANO: return "anode table overflow";
  1053. #endif
  1054. #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
  1055. case ENOBUFS: return "no buffer space available";
  1056. #endif
  1057. #ifdef ENOCSI
  1058. case ENOCSI: return "no CSI structure available";
  1059. #endif
  1060. #ifdef ENODATA
  1061. case ENODATA: return "no data available";
  1062. #endif
  1063. #ifdef ENODEV
  1064. case ENODEV: return "no such device";
  1065. #endif
  1066. #ifdef ENOENT
  1067. case ENOENT: return "no such file or directory";
  1068. #endif
  1069. #ifdef ENOEXEC
  1070. case ENOEXEC: return "exec format error";
  1071. #endif
  1072. #ifdef ENOLCK
  1073. case ENOLCK: return "no locks available";
  1074. #endif
  1075. #ifdef ENOLINK
  1076. case ENOLINK: return "link has be severed";
  1077. #endif
  1078. #ifdef ENOMEM
  1079. case ENOMEM: return "not enough memory";
  1080. #endif
  1081. #ifdef ENOMSG
  1082. case ENOMSG: return "no message of desired type";
  1083. #endif
  1084. #ifdef ENONET
  1085. case ENONET: return "machine is not on the network";
  1086. #endif
  1087. #ifdef ENOPKG
  1088. case ENOPKG: return "package not installed";
  1089. #endif
  1090. #ifdef ENOPROTOOPT
  1091. case ENOPROTOOPT: return "bad proocol option";
  1092. #endif
  1093. #ifdef ENOSPC
  1094. case ENOSPC: return "no space left on device";
  1095. #endif
  1096. #ifdef ENOSR
  1097. case ENOSR: return "out of stream resources";
  1098. #endif
  1099. #ifdef ENOSTR
  1100. case ENOSTR: return "not a stream device";
  1101. #endif
  1102. #ifdef ENOSYM
  1103. case ENOSYM: return "unresolved symbol name";
  1104. #endif
  1105. #ifdef ENOSYS
  1106. case ENOSYS: return "function not implemented";
  1107. #endif
  1108. #ifdef ENOTBLK
  1109. case ENOTBLK: return "block device required";
  1110. #endif
  1111. #ifdef ENOTCONN
  1112. case ENOTCONN: return "socket is not connected";
  1113. #endif
  1114. #ifdef ENOTDIR
  1115. case ENOTDIR: return "not a directory";
  1116. #endif
  1117. #ifdef ENOTEMPTY
  1118. case ENOTEMPTY: return "directory not empty";
  1119. #endif
  1120. #ifdef ENOTNAM
  1121. case ENOTNAM: return "not a name file";
  1122. #endif
  1123. #ifdef ENOTSOCK
  1124. case ENOTSOCK: return "socket operation on non-socket";
  1125. #endif
  1126. #ifdef ENOTTY
  1127. case ENOTTY: return "inappropriate device for ioctl";
  1128. #endif
  1129. #ifdef ENOTUNIQ
  1130. case ENOTUNIQ: return "name not unique on network";
  1131. #endif
  1132. #ifdef ENXIO
  1133. case ENXIO: return "no such device or address";
  1134. #endif
  1135. #ifdef EOPNOTSUPP
  1136. case EOPNOTSUPP: return "operation not supported on socket";
  1137. #endif
  1138. #ifdef EPERM
  1139. case EPERM: return "not owner";
  1140. #endif
  1141. #ifdef EPFNOSUPPORT
  1142. case EPFNOSUPPORT: return "protocol family not supported";
  1143. #endif
  1144. #ifdef EPIPE
  1145. case EPIPE: return "broken pipe";
  1146. #endif
  1147. #ifdef EPROCLIM
  1148. case EPROCLIM: return "too many processes";
  1149. #endif
  1150. #ifdef EPROCUNAVAIL
  1151. case EPROCUNAVAIL: return "bad procedure for program";
  1152. #endif
  1153. #ifdef EPROGMISMATCH
  1154. case EPROGMISMATCH: return "program version wrong";
  1155. #endif
  1156. #ifdef EPROGUNAVAIL
  1157. case EPROGUNAVAIL: return "RPC program not available";
  1158. #endif
  1159. #ifdef EPROTO
  1160. case EPROTO: return "protocol error";
  1161. #endif
  1162. #ifdef EPROTONOSUPPORT
  1163. case EPROTONOSUPPORT: return "protocol not suppored";
  1164. #endif
  1165. #ifdef EPROTOTYPE
  1166. case EPROTOTYPE: return "protocol wrong type for socket";
  1167. #endif
  1168. #ifdef ERANGE
  1169. case ERANGE: return "math result unrepresentable";
  1170. #endif
  1171. #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
  1172. case EREFUSED: return "EREFUSED";
  1173. #endif
  1174. #ifdef EREMCHG
  1175. case EREMCHG: return "remote address changed";
  1176. #endif
  1177. #ifdef EREMDEV
  1178. case EREMDEV: return "remote device";
  1179. #endif
  1180. #ifdef EREMOTE
  1181. case EREMOTE: return "pathname hit remote file system";
  1182. #endif
  1183. #ifdef EREMOTEIO
  1184. case EREMOTEIO: return "remote i/o error";
  1185. #endif
  1186. #ifdef EREMOTERELEASE
  1187. case EREMOTERELEASE: return "EREMOTERELEASE";
  1188. #endif
  1189. #ifdef EROFS
  1190. case EROFS: return "read-only file system";
  1191. #endif
  1192. #ifdef ERPCMISMATCH
  1193. case ERPCMISMATCH: return "RPC version is wrong";
  1194. #endif
  1195. #ifdef ERREMOTE
  1196. case ERREMOTE: return "object is remote";
  1197. #endif
  1198. #ifdef ESHUTDOWN
  1199. case ESHUTDOWN: return "can't send afer socket shutdown";
  1200. #endif
  1201. #ifdef ESOCKTNOSUPPORT
  1202. case ESOCKTNOSUPPORT: return "socket type not supported";
  1203. #endif
  1204. #ifdef ESPIPE
  1205. case ESPIPE: return "invalid seek";
  1206. #endif
  1207. #ifdef ESRCH
  1208. case ESRCH: return "no such process";
  1209. #endif
  1210. #ifdef ESRMNT
  1211. case ESRMNT: return "srmount error";
  1212. #endif
  1213. #ifdef ESTALE
  1214. case ESTALE: return "stale remote file handle";
  1215. #endif
  1216. #ifdef ESUCCESS
  1217. case ESUCCESS: return "Error 0";
  1218. #endif
  1219. #ifdef ETIME
  1220. case ETIME: return "timer expired";
  1221. #endif
  1222. #ifdef ETIMEDOUT
  1223. case ETIMEDOUT: return "connection timed out";
  1224. #endif
  1225. #ifdef ETOOMANYREFS
  1226. case ETOOMANYREFS: return "too many references: can't splice";
  1227. #endif
  1228. #ifdef ETXTBSY
  1229. case ETXTBSY: return "text file or pseudo-device busy";
  1230. #endif
  1231. #ifdef EUCLEAN
  1232. case EUCLEAN: return "structure needs cleaning";
  1233. #endif
  1234. #ifdef EUNATCH
  1235. case EUNATCH: return "protocol driver not attached";
  1236. #endif
  1237. #ifdef EUSERS
  1238. case EUSERS: return "too many users";
  1239. #endif
  1240. #ifdef EVERSION
  1241. case EVERSION: return "version mismatch";
  1242. #endif
  1243. #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
  1244. case EWOULDBLOCK: return "operation would block";
  1245. #endif
  1246. #ifdef EXDEV
  1247. case EXDEV: return "cross-domain link";
  1248. #endif
  1249. #ifdef EXFULL
  1250. case EXFULL: return "message tables full";
  1251. #endif
  1252. }
  1253. #else /* NO_SYS_ERRLIST */
  1254. extern int sys_nerr;
  1255. extern char *sys_errlist[];
  1256. if ((errnum > 0) && (errnum <= sys_nerr))
  1257. return sys_errlist [errnum];
  1258. #endif /* NO_SYS_ERRLIST */
  1259. msg = g_static_private_get (&msg_private);
  1260. if (!msg)
  1261. {
  1262. msg = g_new (gchar, 64);
  1263. g_static_private_set (&msg_private, msg, g_free);
  1264. }
  1265. _g_sprintf (msg, "unknown error (%d)", errnum);
  1266. errno = saved_errno;
  1267. return msg;
  1268. }
  1269. /**
  1270. * g_strsignal:
  1271. * @signum: the signal number. See the <literal>signal</literal>
  1272. * documentation
  1273. *
  1274. * Returns a string describing the given signal, e.g. "Segmentation fault".
  1275. * You should use this function in preference to strsignal(), because it
  1276. * returns a string in UTF-8 encoding, and since not all platforms support
  1277. * the strsignal() function.
  1278. *
  1279. * Returns: a UTF-8 string describing the signal. If the signal is unknown,
  1280. * it returns "unknown signal (&lt;signum&gt;)". The string can only be
  1281. * used until the next call to g_strsignal()
  1282. */
  1283. G_CONST_RETURN gchar*
  1284. g_strsignal (gint signum)
  1285. {
  1286. static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
  1287. char *msg;
  1288. #ifdef HAVE_STRSIGNAL
  1289. const char *msg_locale;
  1290. #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
  1291. extern const char *strsignal(int);
  1292. #else
  1293. /* this is declared differently (const) in string.h on BeOS */
  1294. extern char *strsignal (int sig);
  1295. #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
  1296. msg_locale = strsignal (signum);
  1297. if (g_get_charset (NULL))
  1298. return msg_locale;
  1299. else
  1300. {
  1301. gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
  1302. if (msg_utf8)
  1303. {
  1304. /* Stick in the quark table so that we can return a static result
  1305. */
  1306. GQuark msg_quark = g_quark_from_string (msg_utf8);
  1307. g_free (msg_utf8);
  1308. return g_quark_to_string (msg_quark);
  1309. }
  1310. }
  1311. #elif NO_SYS_SIGLIST
  1312. switch (signum)
  1313. {
  1314. #ifdef SIGHUP
  1315. case SIGHUP: return "Hangup";
  1316. #endif
  1317. #ifdef SIGINT
  1318. case SIGINT: return "Interrupt";
  1319. #endif
  1320. #ifdef SIGQUIT
  1321. case SIGQUIT: return "Quit";
  1322. #endif
  1323. #ifdef SIGILL
  1324. case SIGILL: return "Illegal instruction";
  1325. #endif
  1326. #ifdef SIGTRAP
  1327. case SIGTRAP: return "Trace/breakpoint trap";
  1328. #endif
  1329. #ifdef SIGABRT
  1330. case SIGABRT: return "IOT trap/Abort";
  1331. #endif
  1332. #ifdef SIGBUS
  1333. case SIGBUS: return "Bus error";
  1334. #endif
  1335. #ifdef SIGFPE
  1336. case SIGFPE: return "Floating point exception";
  1337. #endif
  1338. #ifdef SIGKILL
  1339. case SIGKILL: return "Killed";
  1340. #endif
  1341. #ifdef SIGUSR1
  1342. case SIGUSR1: return "User defined signal 1";
  1343. #endif
  1344. #ifdef SIGSEGV
  1345. case SIGSEGV: return "Segmentation fault";
  1346. #endif
  1347. #ifdef SIGUSR2
  1348. case SIGUSR2: return "User defined signal 2";
  1349. #endif
  1350. #ifdef SIGPIPE
  1351. case SIGPIPE: return "Broken pipe";
  1352. #endif
  1353. #ifdef SIGALRM
  1354. case SIGALRM: return "Alarm clock";
  1355. #endif
  1356. #ifdef SIGTERM
  1357. case SIGTERM: return "Terminated";
  1358. #endif
  1359. #ifdef SIGSTKFLT
  1360. case SIGSTKFLT: return "Stack fault";
  1361. #endif
  1362. #ifdef SIGCHLD
  1363. case SIGCHLD: return "Child exited";
  1364. #endif
  1365. #ifdef SIGCONT
  1366. case SIGCONT: return "Continued";
  1367. #endif
  1368. #ifdef SIGSTOP
  1369. case SIGSTOP: return "Stopped (signal)";
  1370. #endif
  1371. #ifdef SIGTSTP
  1372. case SIGTSTP: return "Stopped";
  1373. #endif
  1374. #ifdef SIGTTIN
  1375. case SIGTTIN: return "Stopped (tty input)";
  1376. #endif
  1377. #ifdef SIGTTOU
  1378. case SIGTTOU: return "Stopped (tty output)";
  1379. #endif
  1380. #ifdef SIGURG
  1381. case SIGURG: return "Urgent condition";
  1382. #endif
  1383. #ifdef SIGXCPU
  1384. case SIGXCPU: return "CPU time limit exceeded";
  1385. #endif
  1386. #ifdef SIGXFSZ
  1387. case SIGXFSZ: return "File size limit exceeded";
  1388. #endif
  1389. #ifdef SIGVTALRM
  1390. case SIGVTALRM: return "Virtual time alarm";
  1391. #endif
  1392. #ifdef SIGPROF
  1393. case SIGPROF: return "Profile signal";
  1394. #endif
  1395. #ifdef SIGWINCH
  1396. case SIGWINCH: return "Window size changed";
  1397. #endif
  1398. #ifdef SIGIO
  1399. case SIGIO: return "Possible I/O";
  1400. #endif
  1401. #ifdef SIGPWR
  1402. case SIGPWR: return "Power failure";
  1403. #endif
  1404. #ifdef SIGUNUSED
  1405. case SIGUNUSED: return "Unused signal";
  1406. #endif
  1407. }
  1408. #else /* NO_SYS_SIGLIST */
  1409. #ifdef NO_SYS_SIGLIST_DECL
  1410. extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
  1411. #endif
  1412. return (char*) /* this function should return const --josh */ sys_siglist [signum];
  1413. #endif /* NO_SYS_SIGLIST */
  1414. msg = g_static_private_get (&msg_private);
  1415. if (!msg)
  1416. {
  1417. msg = g_new (gchar, 64);
  1418. g_static_private_set (&msg_private, msg, g_free);
  1419. }
  1420. _g_sprintf (msg, "unknown signal (%d)", signum);
  1421. return msg;
  1422. }
  1423. /* Functions g_strlcpy and g_strlcat were originally developed by
  1424. * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
  1425. * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
  1426. * for more information.
  1427. */
  1428. #ifdef HAVE_STRLCPY
  1429. /* Use the native ones, if available; they might be implemented in assembly */
  1430. gsize
  1431. g_strlcpy (gchar *dest,
  1432. const gchar *src,
  1433. gsize dest_size)
  1434. {
  1435. g_return_val_if_fail (dest != NULL, 0);
  1436. g_return_val_if_fail (src != NULL, 0);
  1437. return strlcpy (dest, src, dest_size);
  1438. }
  1439. gsize
  1440. g_strlcat (gchar *dest,
  1441. const gchar *src,
  1442. gsize dest_size)
  1443. {
  1444. g_return_val_if_fail (dest != NULL, 0);
  1445. g_return_val_if_fail (src != NULL, 0);
  1446. return strlcat (dest, src, dest_size);
  1447. }
  1448. #else /* ! HAVE_STRLCPY */
  1449. /**
  1450. * g_strlcpy:
  1451. * @dest: destination buffer
  1452. * @src: source buffer
  1453. * @dest_size: length of @dest in bytes
  1454. *
  1455. * Portability wrapper that calls strlcpy() on systems which have it,
  1456. * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is
  1457. * guaranteed to be nul-terminated; @src must be nul-terminated;
  1458. * @dest_size is the buffer size, not the number of chars to copy.
  1459. *
  1460. * At most dest_size - 1 characters will be copied. Always nul-terminates
  1461. * (unless dest_size == 0). This function does <emphasis>not</emphasis>
  1462. * allocate memory. Unlike strncpy(), this function doesn't pad dest (so
  1463. * it's often faster). It returns the size of the attempted result,
  1464. * strlen (src), so if @retval >= @dest_size, truncation occurred.
  1465. *
  1466. * <note><para>Caveat: strlcpy() is supposedly more secure than
  1467. * strcpy() or strncpy(), but if you really want to avoid screwups,
  1468. * g_strdup() is an even better idea.</para></note>
  1469. *
  1470. * Returns: length of @src
  1471. */
  1472. gsize
  1473. g_strlcpy (gchar *dest,
  1474. const gchar *src,
  1475. gsize dest_size)
  1476. {
  1477. register gchar *d = dest;
  1478. register const gchar *s = src;
  1479. register gsize n = dest_size;
  1480. g_return_val_if_fail (dest != NULL, 0);
  1481. g_return_val_if_fail (src != NULL, 0);
  1482. /* Copy as many bytes as will fit */
  1483. if (n != 0 && --n != 0)
  1484. do
  1485. {
  1486. register gchar c = *s++;
  1487. *d++ = c;
  1488. if (c == 0)
  1489. break;
  1490. }
  1491. while (--n != 0);
  1492. /* If not enough room in dest, add NUL and traverse rest of src */
  1493. if (n == 0)
  1494. {
  1495. if (dest_size != 0)
  1496. *d = 0;
  1497. while (*s++)
  1498. ;
  1499. }
  1500. return s - src - 1; /* count does not include NUL */
  1501. }
  1502. /**
  1503. * g_strlcat:
  1504. * @dest: destination buffer, already containing one nul-terminated string
  1505. * @src: source buffer
  1506. * @dest_size: length of @dest buffer in bytes (not length of existing string
  1507. * inside @dest)
  1508. *
  1509. * Portability wrapper that calls strlcat() on systems which have it,
  1510. * and emulates it otherwise. Appends nul-terminated @src string to @dest,
  1511. * guaranteeing nul-termination for @dest. The total size of @dest won't
  1512. * exceed @dest_size.
  1513. *
  1514. * At most dest_size - 1 characters will be copied.
  1515. * Unlike strncat, dest_size is the full size of dest, not the space left over.
  1516. * This function does NOT allocate memory.
  1517. * This always NUL terminates (unless siz == 0 or there were no NUL characters
  1518. * in the dest_size characters of dest to start with).
  1519. * Returns size of attempted result, which is
  1520. * MIN (dest_size, strlen (original dest)) + strlen (src),
  1521. * so if retval >= dest_size, truncation occurred.
  1522. *
  1523. * <note><para>Caveat: this is supposedly a more secure alternative to
  1524. * strcat() or strncat(), but for real security g_strconcat() is harder
  1525. * to mess up.</para></note>
  1526. *
  1527. */
  1528. gsize
  1529. g_strlcat (gchar *dest,
  1530. const gchar *src,
  1531. gsize dest_size)
  1532. {
  1533. register gchar *d = dest;
  1534. register const gchar *s = src;
  1535. register gsize bytes_left = dest_size;
  1536. gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
  1537. g_return_val_if_fail (dest != NULL, 0);
  1538. g_return_val_if_fail (src != NULL, 0);
  1539. /* Find the end of dst and adjust bytes left but don't go past end */
  1540. while (*d != 0 && bytes_left-- != 0)
  1541. d++;
  1542. dlength = d - dest;
  1543. bytes_left = dest_size - dlength;
  1544. if (bytes_left == 0)
  1545. return dlength + strlen (s);
  1546. while (*s != 0)
  1547. {
  1548. if (bytes_left != 1)
  1549. {
  1550. *d++ = *s;
  1551. bytes_left--;
  1552. }
  1553. s++;
  1554. }
  1555. *d = 0;
  1556. return dlength + (s - src); /* count does not include NUL */
  1557. }
  1558. #endif /* ! HAVE_STRLCPY */
  1559. /**
  1560. * g_ascii_strdown:
  1561. * @str: a string.
  1562. * @len: length of @str in bytes, or -1 if @str is nul-terminated.
  1563. *
  1564. * Converts all upper case ASCII letters to lower case ASCII letters.
  1565. *
  1566. * Return value: a newly-allocated string, with all the upper case
  1567. * characters in @str converted to lower case, with
  1568. * semantics that exactly match g_ascii_tolower(). (Note
  1569. * that this is unlike the old g_strdown(), which modified
  1570. * the string in place.)
  1571. **/
  1572. gchar*
  1573. g_ascii_strdown (const gchar *str,
  1574. gssize len)
  1575. {
  1576. gchar *result, *s;
  1577. g_return_val_if_fail (str != NULL, NULL);
  1578. if (len < 0)
  1579. len = strlen (str);
  1580. result = g_strndup (str, len);
  1581. for (s = result; *s; s++)
  1582. *s = g_ascii_tolower (*s);
  1583. return result;
  1584. }
  1585. /**
  1586. * g_ascii_strup:
  1587. * @str: a string.
  1588. * @len: length of @str in bytes, or -1 if @str is nul-terminated.
  1589. *
  1590. * Converts all lower case ASCII letters to upper case ASCII letters.
  1591. *
  1592. * Return value: a newly allocated string, with all the lower case
  1593. * characters in @str converted to upper case, with
  1594. * semantics that exactly match g_ascii_toupper(). (Note
  1595. * that this is unlike the old g_strup(), which modified
  1596. * the string in place.)
  1597. **/
  1598. gchar*
  1599. g_ascii_strup (const gchar *str,
  1600. gssize len)
  1601. {
  1602. gchar *result, *s;
  1603. g_return_val_if_fail (str != NULL, NULL);
  1604. if (len < 0)
  1605. len = strlen (str);
  1606. result = g_strndup (str, len);
  1607. for (s = result; *s; s++)
  1608. *s = g_ascii_toupper (*s);
  1609. return result;
  1610. }
  1611. /**
  1612. * g_strdown:
  1613. * @string: the string to convert.
  1614. *
  1615. * Converts a string to lower case.
  1616. *
  1617. * Return value: the string
  1618. *
  1619. * Deprecated:2.2: This function is totally broken for the reasons discussed
  1620. * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
  1621. * instead.
  1622. **/
  1623. gchar*
  1624. g_strdown (gchar *string)
  1625. {
  1626. register guchar *s;
  1627. g_return_val_if_fail (string != NULL, NULL);
  1628. s = (guchar *) string;
  1629. while (*s)
  1630. {
  1631. if (isupper (*s))
  1632. *s = tolower (*s);
  1633. s++;
  1634. }
  1635. return (gchar *) string;
  1636. }
  1637. /**
  1638. * g_strup:
  1639. * @string: the string to convert.
  1640. *
  1641. * Converts a string to upper case.
  1642. *
  1643. * Return value: the string
  1644. *
  1645. * Deprecated:2.2: This function is totally broken for the reasons discussed
  1646. * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
  1647. **/
  1648. gchar*
  1649. g_strup (gchar *string)
  1650. {
  1651. register guchar *s;
  1652. g_return_val_if_fail (string != NULL, NULL);
  1653. s = (guchar *) string;
  1654. while (*s)
  1655. {
  1656. if (islower (*s))
  1657. *s = toupper (*s);
  1658. s++;
  1659. }
  1660. return (gchar *) string;
  1661. }
  1662. /**
  1663. * g_strreverse:
  1664. * @string: the string to reverse
  1665. *
  1666. * Reverses all of the bytes in a string. For example,
  1667. * <literal>g_strreverse ("abcdef")</literal> will result
  1668. * in "fedcba".
  1669. *
  1670. * Note that g_strreverse() doesn't work on UTF-8 strings
  1671. * containing multibyte characters. For that purpose, use
  1672. * g_utf8_strreverse().
  1673. *
  1674. * Returns: the same pointer passed in as @string
  1675. */
  1676. gchar*
  1677. g_strreverse (gchar *string)
  1678. {
  1679. g_return_val_if_fail (string != NULL, NULL);
  1680. if (*string)
  1681. {
  1682. register gchar *h, *t;
  1683. h = string;
  1684. t = string + strlen (string) - 1;
  1685. while (h < t)
  1686. {
  1687. register gchar c;
  1688. c = *h;
  1689. *h = *t;
  1690. h++;
  1691. *t = c;
  1692. t--;
  1693. }
  1694. }
  1695. return string;
  1696. }
  1697. /**
  1698. * g_ascii_tolower:
  1699. * @c: any character.
  1700. *
  1701. * Convert a character to ASCII lower case.
  1702. *
  1703. * Unlike the standard C library tolower() function, this only
  1704. * recognizes standard ASCII letters and ignores the locale, returning
  1705. * all non-ASCII characters unchanged, even if they are lower case
  1706. * letters in a particular character set. Also unlike the standard
  1707. * library function, this takes and returns a char, not an int, so
  1708. * don't call it on %EOF but no need to worry about casting to #guchar
  1709. * before passing a possibly non-ASCII character in.
  1710. *
  1711. * Return value: the result of converting @c to lower case.
  1712. * If @c is not an ASCII upper case letter,
  1713. * @c is returned unchanged.
  1714. **/
  1715. gchar
  1716. g_ascii_tolower (gchar c)
  1717. {
  1718. return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
  1719. }
  1720. /**
  1721. * g_ascii_toupper:
  1722. * @c: any character.
  1723. *
  1724. * Convert a character to ASCII upper case.
  1725. *
  1726. * Unlike the standard C library toupper() function, this only
  1727. * recognizes standard ASCII letters and ignores the locale, returning
  1728. * all non-ASCII characters unchanged, even if they are upper case
  1729. * letters in a particular character set. Also unlike the standard
  1730. * library function, this takes and returns a char, not an int, so
  1731. * don't call it on %EOF but no need to worry about casting to #guchar
  1732. * before passing a possibly non-ASCII character in.
  1733. *
  1734. * Return value: the result of converting @c to upper case.
  1735. * If @c is not an ASCII lower case letter,
  1736. * @c is returned unchanged.
  1737. **/
  1738. gchar
  1739. g_ascii_toupper (gchar c)
  1740. {
  1741. return g_ascii_islower (c) ? c - 'a' + 'A' : c;
  1742. }
  1743. /**
  1744. * g_ascii_digit_value:
  1745. * @c: an ASCII character.
  1746. *
  1747. * Determines the numeric value of a character as a decimal
  1748. * digit. Differs from g_unichar_digit_value() because it takes
  1749. * a char, so there's no worry about sign extension if characters
  1750. * are signed.
  1751. *
  1752. * Return value: If @c is a decimal digit (according to
  1753. * g_ascii_isdigit()), its numeric value. Otherwise, -1.
  1754. **/
  1755. int
  1756. g_ascii_digit_value (gchar c)
  1757. {
  1758. if (g_ascii_isdigit (c))
  1759. return c - '0';
  1760. return -1;
  1761. }
  1762. /**
  1763. * g_ascii_xdigit_value:
  1764. * @c: an ASCII character.
  1765. *
  1766. * Determines the numeric value of a character as a hexidecimal
  1767. * digit. Differs from g_unichar_xdigit_value() because it takes
  1768. * a char, so there's no worry about sign extension if characters
  1769. * are signed.
  1770. *
  1771. * Return value: If @c is a hex digit (according to
  1772. * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
  1773. **/
  1774. int
  1775. g_ascii_xdigit_value (gchar c)
  1776. {
  1777. if (c >= 'A' && c <= 'F')
  1778. return c - 'A' + 10;
  1779. if (c >= 'a' && c <= 'f')
  1780. return c - 'a' + 10;
  1781. return g_ascii_digit_value (c);
  1782. }
  1783. /**
  1784. * g_ascii_strcasecmp:
  1785. * @s1: string to compare with @s2.
  1786. * @s2: string to compare with @s1.
  1787. *
  1788. * Compare two strings, ignoring the case of ASCII characters.
  1789. *
  1790. * Unlike the BSD strcasecmp() function, this only recognizes standard
  1791. * ASCII letters and ignores the locale, treating all non-ASCII
  1792. * bytes as if they are not letters.
  1793. *
  1794. * This function should be used only on strings that are known to be
  1795. * in encodings where the bytes corresponding to ASCII letters always
  1796. * represent themselves. This includes UTF-8 and the ISO-8859-*
  1797. * charsets, but not for instance double-byte encodings like the
  1798. * Windows Codepage 932, where the trailing bytes of double-byte
  1799. * characters include all ASCII letters. If you compare two CP932
  1800. * strings using this function, you will get false matches.
  1801. *
  1802. * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
  1803. * or a positive value if @s1 &gt; @s2.
  1804. **/
  1805. gint
  1806. g_ascii_strcasecmp (const gchar *s1,
  1807. const gchar *s2)
  1808. {
  1809. gint c1, c2;
  1810. g_return_val_if_fail (s1 != NULL, 0);
  1811. g_return_val_if_fail (s2 != NULL, 0);
  1812. while (*s1 && *s2)
  1813. {
  1814. c1 = (gint)(guchar) TOLOWER (*s1);
  1815. c2 = (gint)(guchar) TOLOWER (*s2);
  1816. if (c1 != c2)
  1817. return (c1 - c2);
  1818. s1++; s2++;
  1819. }
  1820. return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
  1821. }
  1822. /**
  1823. * g_ascii_strncasecmp:
  1824. * @s1: string to compare with @s2.
  1825. * @s2: string to compare with @s1.
  1826. * @n: number of characters to compare.
  1827. *
  1828. * Compare @s1 and @s2, ignoring the case of ASCII characters and any
  1829. * characters after the first @n in each string.
  1830. *
  1831. * Unlike the BSD strcasecmp() function, this only recognizes standard
  1832. * ASCII letters and ignores the locale, treating all non-ASCII
  1833. * characters as if they are not letters.
  1834. *
  1835. * The same warning as in g_ascii_strcasecmp() applies: Use this
  1836. * function only on strings known to be in encodings where bytes
  1837. * corresponding to ASCII letters always represent themselves.
  1838. *
  1839. * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
  1840. * or a positive value if @s1 &gt; @s2.
  1841. **/
  1842. gint
  1843. g_ascii_strncasecmp (const gchar *s1,
  1844. const gchar *s2,
  1845. gsize n)
  1846. {
  1847. gint c1, c2;
  1848. g_return_val_if_fail (s1 != NULL, 0);
  1849. g_return_val_if_fail (s2 != NULL, 0);
  1850. while (n && *s1 && *s2)
  1851. {
  1852. n -= 1;
  1853. c1 = (gint)(guchar) TOLOWER (*s1);
  1854. c2 = (gint)(guchar) TOLOWER (*s2);
  1855. if (c1 != c2)
  1856. return (c1 - c2);
  1857. s1++; s2++;
  1858. }
  1859. if (n)
  1860. return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
  1861. else
  1862. return 0;
  1863. }
  1864. /**
  1865. * g_strcasecmp:
  1866. * @s1: a string.
  1867. * @s2: a string to compare with @s1.
  1868. *
  1869. * A case-insensitive string comparison, corresponding to the standard
  1870. * strcasecmp() function on platforms which support it.
  1871. *
  1872. * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
  1873. * or a positive value if @s1 &gt; @s2.
  1874. *
  1875. * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
  1876. * is deprecated and how to replace it.
  1877. **/
  1878. gint
  1879. g_strcasecmp (const gchar *s1,
  1880. const gchar *s2)
  1881. {
  1882. #ifdef HAVE_STRCASECMP
  1883. g_return_val_if_fail (s1 != NULL, 0);
  1884. g_return_val_if_fail (s2 != NULL, 0);
  1885. return strcasecmp (s1, s2);
  1886. #else
  1887. gint c1, c2;
  1888. g_return_val_if_fail (s1 != NULL, 0);
  1889. g_return_val_if_fail (s2 != NULL, 0);
  1890. while (*s1 && *s2)
  1891. {
  1892. /* According to A. Cox, some platforms have islower's that
  1893. * don't work right on non-uppercase
  1894. */
  1895. c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
  1896. c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
  1897. if (c1 != c2)
  1898. return (c1 - c2);
  1899. s1++; s2++;
  1900. }
  1901. return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
  1902. #endif
  1903. }
  1904. /**
  1905. * g_strncasecmp:
  1906. * @s1: a string.
  1907. * @s2: a string to compare with @s1.
  1908. * @n: the maximum number of characters to compare.
  1909. *
  1910. * A case-insensitive string comparison, corresponding to the standard
  1911. * strncasecmp() function on platforms which support it.
  1912. * It is similar to g_strcasecmp() except it only compares the first @n
  1913. * characters of the strings.
  1914. *
  1915. * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
  1916. * or a positive value if @s1 &gt; @s2.
  1917. *
  1918. * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
  1919. * comparison by calling toupper()/tolower(). These functions are
  1920. * locale-specific and operate on single bytes. However, it is impossible
  1921. * to handle things correctly from an I18N standpoint by operating on
  1922. * bytes, since characters may be multibyte. Thus g_strncasecmp() is
  1923. * broken if your string is guaranteed to be ASCII, since it's
  1924. * locale-sensitive, and it's broken if your string is localized, since
  1925. * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
  1926. * etc.
  1927. *
  1928. * There are therefore two replacement functions: g_ascii_strncasecmp(),
  1929. * which only works on ASCII and is not locale-sensitive, and
  1930. * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
  1931. **/
  1932. gint
  1933. g_strncasecmp (const gchar *s1,
  1934. const gchar *s2,
  1935. guint n)
  1936. {
  1937. #ifdef HAVE_STRNCASECMP
  1938. return strncasecmp (s1, s2, n);
  1939. #else
  1940. gint c1, c2;
  1941. g_return_val_if_fail (s1 != NULL, 0);
  1942. g_return_val_if_fail (s2 != NULL, 0);
  1943. while (n && *s1 && *s2)
  1944. {
  1945. n -= 1;
  1946. /* According to A. Cox, some platforms have islower's that
  1947. * don't work right on non-uppercase
  1948. */
  1949. c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
  1950. c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
  1951. if (c1 != c2)
  1952. return (c1 - c2);
  1953. s1++; s2++;
  1954. }
  1955. if (n)
  1956. return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
  1957. else
  1958. return 0;
  1959. #endif
  1960. }
  1961. gchar*
  1962. g_strdelimit (gchar *string,
  1963. const gchar *delimiters,
  1964. gchar new_delim)
  1965. {
  1966. register gchar *c;
  1967. g_return_val_if_fail (string != NULL, NULL);
  1968. if (!delimiters)
  1969. delimiters = G_STR_DELIMITERS;
  1970. for (c = string; *c; c++)
  1971. {
  1972. if (strchr (delimiters, *c))
  1973. *c = new_delim;
  1974. }
  1975. return string;
  1976. }
  1977. gchar*
  1978. g_strcanon (gchar *string,
  1979. const gchar *valid_chars,
  1980. gchar substitutor)
  1981. {
  1982. register gchar *c;
  1983. g_return_val_if_fail (string != NULL, NULL);
  1984. g_return_val_if_fail (valid_chars != NULL, NULL);
  1985. for (c = string; *c; c++)
  1986. {
  1987. if (!strchr (valid_chars, *c))
  1988. *c = substitutor;
  1989. }
  1990. return string;
  1991. }
  1992. gchar*
  1993. g_strcompress (const gchar *source)
  1994. {
  1995. const gchar *p = source, *octal;
  1996. gchar *dest = g_malloc (strlen (source) + 1);
  1997. gchar *q = dest;
  1998. while (*p)
  1999. {
  2000. if (*p == '\\')
  2001. {
  2002. p++;
  2003. switch (*p)
  2004. {
  2005. case '\0':
  2006. g_warning ("g_strcompress: trailing \\");
  2007. goto out;
  2008. case '0': case '1': case '2': case '3': case '4':
  2009. case '5': case '6': case '7':
  2010. *q = 0;
  2011. octal = p;
  2012. while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
  2013. {
  2014. *q = (*q * 8) + (*p - '0');
  2015. p++;
  2016. }
  2017. q++;
  2018. p--;
  2019. break;
  2020. case 'b':
  2021. *q++ = '\b';
  2022. break;
  2023. case 'f':
  2024. *q++ = '\f';
  2025. break;
  2026. case 'n':
  2027. *q++ = '\n';
  2028. break;
  2029. case 'r':
  2030. *q++ = '\r';
  2031. break;
  2032. case 't':
  2033. *q++ = '\t';
  2034. break;
  2035. default: /* Also handles \" and \\ */
  2036. *q++ = *p;
  2037. break;
  2038. }
  2039. }
  2040. else
  2041. *q++ = *p;
  2042. p++;
  2043. }
  2044. out:
  2045. *q = 0;
  2046. return dest;
  2047. }
  2048. gchar *
  2049. g_strescape (const gchar *source,
  2050. const gchar *exceptions)
  2051. {
  2052. const guchar *p;
  2053. gchar *dest;
  2054. gchar *q;
  2055. guchar excmap[256];
  2056. g_return_val_if_fail (source != NULL, NULL);
  2057. p = (guchar *) source;
  2058. /* Each source byte needs maximally four destination chars (\777) */
  2059. q = dest = g_malloc (strlen (source) * 4 + 1);
  2060. memset (excmap, 0, 256);
  2061. if (exceptions)
  2062. {
  2063. guchar *e = (guchar *) exceptions;
  2064. while (*e)
  2065. {
  2066. excmap[*e] = 1;
  2067. e++;
  2068. }
  2069. }
  2070. while (*p)
  2071. {
  2072. if (excmap[*p])
  2073. *q++ = *p;
  2074. else
  2075. {
  2076. switch (*p)
  2077. {
  2078. case '\b':
  2079. *q++ = '\\';
  2080. *q++ = 'b';
  2081. break;
  2082. case '\f':
  2083. *q++ = '\\';
  2084. *q++ = 'f';
  2085. break;
  2086. case '\n':
  2087. *q++ = '\\';
  2088. *q++ = 'n';
  2089. break;
  2090. case '\r':
  2091. *q++ = '\\';
  2092. *q++ = 'r';
  2093. break;
  2094. case '\t':
  2095. *q++ = '\\';
  2096. *q++ = 't';
  2097. break;
  2098. case '\\':
  2099. *q++ = '\\';
  2100. *q++ = '\\';
  2101. break;
  2102. case '"':
  2103. *q++ = '\\';
  2104. *q++ = '"';
  2105. break;
  2106. default:
  2107. if ((*p < ' ') || (*p >= 0177))
  2108. {
  2109. *q++ = '\\';
  2110. *q++ = '0' + (((*p) >> 6) & 07);
  2111. *q++ = '0' + (((*p) >> 3) & 07);
  2112. *q++ = '0' + ((*p) & 07);
  2113. }
  2114. else
  2115. *q++ = *p;
  2116. break;
  2117. }
  2118. }
  2119. p++;
  2120. }
  2121. *q = 0;
  2122. return dest;
  2123. }
  2124. gchar*
  2125. g_strchug (gchar *string)
  2126. {
  2127. guchar *start;
  2128. g_return_val_if_fail (string != NULL, NULL);
  2129. for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
  2130. ;
  2131. g_memmove (string, start, strlen ((gchar *) start) + 1);
  2132. return string;
  2133. }
  2134. gchar*
  2135. g_strchomp (gchar *string)
  2136. {
  2137. gsize len;
  2138. g_return_val_if_fail (string != NULL, NULL);
  2139. len = strlen (string);
  2140. while (len--)
  2141. {
  2142. if (g_ascii_isspace ((guchar) string[len]))
  2143. string[len] = '\0';
  2144. else
  2145. break;
  2146. }
  2147. return string;
  2148. }
  2149. /**
  2150. * g_strsplit:
  2151. * @string: a string to split.
  2152. * @delimiter: a string which specifies the places at which to split the string.
  2153. * The delimiter is not included in any of the resulting strings, unless
  2154. * @max_tokens is reached.
  2155. * @max_tokens: the maximum number of pieces to split @string into. If this is
  2156. * less than 1, the string is split completely.
  2157. *
  2158. * Splits a string into a maximum of @max_tokens pieces, using the given
  2159. * @delimiter. If @max_tokens is reached, the remainder of @string is appended
  2160. * to the last token.
  2161. *
  2162. * As a special case, the result of splitting the empty string "" is an empty
  2163. * vector, not a vector containing a single string. The reason for this
  2164. * special case is that being able to represent a empty vector is typically
  2165. * more useful than consistent handling of empty elements. If you do need
  2166. * to represent empty elements, you'll need to check for the empty string
  2167. * before calling g_strsplit().
  2168. *
  2169. * Return value: a newly-allocated %NULL-terminated array of strings. Use
  2170. * g_strfreev() to free it.
  2171. **/
  2172. gchar**
  2173. g_strsplit (const gchar *string,
  2174. const gchar *delimiter,
  2175. gint max_tokens)
  2176. {
  2177. GSList *string_list = NULL, *slist;
  2178. gchar **str_array, *s;
  2179. guint n = 0;
  2180. const gchar *remainder;
  2181. g_return_val_if_fail (string != NULL, NULL);
  2182. g_return_val_if_fail (delimiter != NULL, NULL);
  2183. g_return_val_if_fail (delimiter[0] != '\0', NULL);
  2184. if (max_tokens < 1)
  2185. max_tokens = G_MAXINT;
  2186. remainder = string;
  2187. s = strstr (remainder, delimiter);
  2188. if (s)
  2189. {
  2190. gsize delimiter_len = strlen (delimiter);
  2191. while (--max_tokens && s)
  2192. {
  2193. gsize len;
  2194. len = s - remainder;
  2195. string_list = g_slist_prepend (string_list,
  2196. g_strndup (remainder, len));
  2197. n++;
  2198. remainder = s + delimiter_len;
  2199. s = strstr (remainder, delimiter);
  2200. }
  2201. }
  2202. if (*string)
  2203. {
  2204. n++;
  2205. string_list = g_slist_prepend (string_list, g_strdup (remainder));
  2206. }
  2207. str_array = g_new (gchar*, n + 1);
  2208. str_array[n--] = NULL;
  2209. for (slist = string_list; slist; slist = slist->next)
  2210. str_array[n--] = slist->data;
  2211. g_slist_free (string_list);
  2212. return str_array;
  2213. }
  2214. /**
  2215. * g_strsplit_set:
  2216. * @string: The string to be tokenized
  2217. * @delimiters: A nul-terminated string containing bytes that are used
  2218. * to split the string.
  2219. * @max_tokens: The maximum number of tokens to split @string into.
  2220. * If this is less than 1, the string is split completely
  2221. *
  2222. * Splits @string into a number of tokens not containing any of the characters
  2223. * in @delimiter. A token is the (possibly empty) longest string that does not
  2224. * contain any of the characters in @delimiters. If @max_tokens is reached, the
  2225. * remainder is appended to the last token.
  2226. *
  2227. * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
  2228. * %NULL-terminated vector containing the three strings "abc", "def",
  2229. * and "ghi".
  2230. *
  2231. * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
  2232. * vector containing the four strings "", "def", "ghi", and "".
  2233. *
  2234. * As a special case, the result of splitting the empty string "" is an empty
  2235. * vector, not a vector containing a single string. The reason for this
  2236. * special case is that being able to represent a empty vector is typically
  2237. * more useful than consistent handling of empty elements. If you do need
  2238. * to represent empty elements, you'll need to check for the empty string
  2239. * before calling g_strsplit_set().
  2240. *
  2241. * Note that this function works on bytes not characters, so it can't be used
  2242. * to delimit UTF-8 strings for anything but ASCII characters.
  2243. *
  2244. * Return value: a newly-allocated %NULL-terminated array of strings. Use
  2245. * g_strfreev() to free it.
  2246. *
  2247. * Since: 2.4
  2248. **/
  2249. gchar **
  2250. g_strsplit_set (const gchar *string,
  2251. const gchar *delimiters,
  2252. gint max_tokens)
  2253. {
  2254. gboolean delim_table[256];
  2255. GSList *tokens, *list;
  2256. gint n_tokens;
  2257. const gchar *s;
  2258. const gchar *current;
  2259. gchar *token;
  2260. gchar **result;
  2261. g_return_val_if_fail (string != NULL, NULL);
  2262. g_return_val_if_fail (delimiters != NULL, NULL);
  2263. if (max_tokens < 1)
  2264. max_tokens = G_MAXINT;
  2265. if (*string == '\0')
  2266. {
  2267. result = g_new (char *, 1);
  2268. result[0] = NULL;
  2269. return result;
  2270. }
  2271. memset (delim_table, FALSE, sizeof (delim_table));
  2272. for (s = delimiters; *s != '\0'; ++s)
  2273. delim_table[*(guchar *)s] = TRUE;
  2274. tokens = NULL;
  2275. n_tokens = 0;
  2276. s = current = string;
  2277. while (*s != '\0')
  2278. {
  2279. if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
  2280. {
  2281. token = g_strndup (current, s - current);
  2282. tokens = g_slist_prepend (tokens, token);
  2283. ++n_tokens;
  2284. current = s + 1;
  2285. }
  2286. ++s;
  2287. }
  2288. token = g_strndup (current, s - current);
  2289. tokens = g_slist_prepend (tokens, token);
  2290. ++n_tokens;
  2291. result = g_new (gchar *, n_tokens + 1);
  2292. result[n_tokens] = NULL;
  2293. for (list = tokens; list != NULL; list = list->next)
  2294. result[--n_tokens] = list->data;
  2295. g_slist_free (tokens);
  2296. return result;
  2297. }
  2298. /**
  2299. * g_strfreev:
  2300. * @str_array: a %NULL-terminated array of strings to free.
  2301. * Frees a %NULL-terminated array of strings, and the array itself.
  2302. * If called on a %NULL value, g_strfreev() simply returns.
  2303. **/
  2304. void
  2305. g_strfreev (gchar **str_array)
  2306. {
  2307. if (str_array)
  2308. {
  2309. int i;
  2310. for (i = 0; str_array[i] != NULL; i++)
  2311. g_free (str_array[i]);
  2312. g_free (str_array);
  2313. }
  2314. }
  2315. /**
  2316. * g_strdupv:
  2317. * @str_array: %NULL-terminated array of strings.
  2318. *
  2319. * Copies %NULL-terminated array of strings. The copy is a deep copy;
  2320. * the new array should be freed by first freeing each string, then
  2321. * the array itself. g_strfreev() does this for you. If called
  2322. * on a %NULL value, g_strdupv() simply returns %NULL.
  2323. *
  2324. * Return value: a new %NULL-terminated array of strings.
  2325. **/
  2326. gchar**
  2327. g_strdupv (gchar **str_array)
  2328. {
  2329. if (str_array)
  2330. {
  2331. gint i;
  2332. gchar **retval;
  2333. i = 0;
  2334. while (str_array[i])
  2335. ++i;
  2336. retval = g_new (gchar*, i + 1);
  2337. i = 0;
  2338. while (str_array[i])
  2339. {
  2340. retval[i] = g_strdup (str_array[i]);
  2341. ++i;
  2342. }
  2343. retval[i] = NULL;
  2344. return retval;
  2345. }
  2346. else
  2347. return NULL;
  2348. }
  2349. /**
  2350. * g_strjoinv:
  2351. * @separator: a string to insert between each of the strings, or %NULL
  2352. * @str_array: a %NULL-terminated array of strings to join
  2353. *
  2354. * Joins a number of strings together to form one long string, with the
  2355. * optional @separator inserted between each of them. The returned string
  2356. * should be freed with g_free().
  2357. *
  2358. * Returns: a newly-allocated string containing all of the strings joined
  2359. * together, with @separator between them
  2360. */
  2361. gchar*
  2362. g_strjoinv (const gchar *separator,
  2363. gchar **str_array)
  2364. {
  2365. gchar *string;
  2366. gchar *ptr;
  2367. g_return_val_if_fail (str_array != NULL, NULL);
  2368. if (separator == NULL)
  2369. separator = "";
  2370. if (*str_array)
  2371. {
  2372. gint i;
  2373. gsize len;
  2374. gsize separator_len;
  2375. separator_len = strlen (separator);
  2376. /* First part, getting length */
  2377. len = 1 + strlen (str_array[0]);
  2378. for (i = 1; str_array[i] != NULL; i++)
  2379. len += strlen (str_array[i]);
  2380. len += separator_len * (i - 1);
  2381. /* Second part, building string */
  2382. string = g_new (gchar, len);
  2383. ptr = g_stpcpy (string, *str_array);
  2384. for (i = 1; str_array[i] != NULL; i++)
  2385. {
  2386. ptr = g_stpcpy (ptr, separator);
  2387. ptr = g_stpcpy (ptr, str_array[i]);
  2388. }
  2389. }
  2390. else
  2391. string = g_strdup ("");
  2392. return string;
  2393. }
  2394. /**
  2395. * g_strjoin:
  2396. * @separator: a string to insert between each of the strings, or %NULL
  2397. * @Varargs: a %NULL-terminated list of strings to join
  2398. *
  2399. * Joins a number of strings together to form one long string, with the
  2400. * optional @separator inserted between each of them. The returned string
  2401. * should be freed with g_free().
  2402. *
  2403. * Returns: a newly-allocated string containing all of the strings joined
  2404. * together, with @separator between them
  2405. */
  2406. gchar*
  2407. g_strjoin (const gchar *separator,
  2408. ...)
  2409. {
  2410. gchar *string, *s;
  2411. va_list args;
  2412. gsize len;
  2413. gsize separator_len;
  2414. gchar *ptr;
  2415. if (separator == NULL)
  2416. separator = "";
  2417. separator_len = strlen (separator);
  2418. va_start (args, separator);
  2419. s = va_arg (args, gchar*);
  2420. if (s)
  2421. {
  2422. /* First part, getting length */
  2423. len = 1 + strlen (s);
  2424. s = va_arg (args, gchar*);
  2425. while (s)
  2426. {
  2427. len += separator_len + strlen (s);
  2428. s = va_arg (args, gchar*);
  2429. }
  2430. va_end (args);
  2431. /* Second part, building string */
  2432. string = g_new (gchar, len);
  2433. va_start (args, separator);
  2434. s = va_arg (args, gchar*);
  2435. ptr = g_stpcpy (string, s);
  2436. s = va_arg (args, gchar*);
  2437. while (s)
  2438. {
  2439. ptr = g_stpcpy (ptr, separator);
  2440. ptr = g_stpcpy (ptr, s);
  2441. s = va_arg (args, gchar*);
  2442. }
  2443. }
  2444. else
  2445. string = g_strdup ("");
  2446. va_end (args);
  2447. return string;
  2448. }
  2449. /**
  2450. * g_strstr_len:
  2451. * @haystack: a string.
  2452. * @haystack_len: the maximum length of @haystack. Note that -1 is
  2453. * a valid length, if @haystack is nul-terminated, meaning it will
  2454. * search through the whole string.
  2455. * @needle: the string to search for.
  2456. *
  2457. * Searches the string @haystack for the first occurrence
  2458. * of the string @needle, limiting the length of the search
  2459. * to @haystack_len.
  2460. *
  2461. * Return value: a pointer to the found occurrence, or
  2462. * %NULL if not found.
  2463. **/
  2464. gchar *
  2465. g_strstr_len (const gchar *haystack,
  2466. gssize haystack_len,
  2467. const gchar *needle)
  2468. {
  2469. g_return_val_if_fail (haystack != NULL, NULL);
  2470. g_return_val_if_fail (needle != NULL, NULL);
  2471. if (haystack_len < 0)
  2472. return strstr (haystack, needle);
  2473. else
  2474. {
  2475. const gchar *p = haystack;
  2476. gsize needle_len = strlen (needle);
  2477. const gchar *end;
  2478. gsize i;
  2479. if (needle_len == 0)
  2480. return (gchar *)haystack;
  2481. if (haystack_len < needle_len)
  2482. return NULL;
  2483. end = haystack + haystack_len - needle_len;
  2484. while (p <= end && *p)
  2485. {
  2486. for (i = 0; i < needle_len; i++)
  2487. if (p[i] != needle[i])
  2488. goto next;
  2489. return (gchar *)p;
  2490. next:
  2491. p++;
  2492. }
  2493. return NULL;
  2494. }
  2495. }
  2496. /**
  2497. * g_strrstr:
  2498. * @haystack: a nul-terminated string.
  2499. * @needle: the nul-terminated string to search for.
  2500. *
  2501. * Searches the string @haystack for the last occurrence
  2502. * of the string @needle.
  2503. *
  2504. * Return value: a pointer to the found occurrence, or
  2505. * %NULL if not found.
  2506. **/
  2507. gchar *
  2508. g_strrstr (const gchar *haystack,
  2509. const gchar *needle)
  2510. {
  2511. gsize i;
  2512. gsize needle_len;
  2513. gsize haystack_len;
  2514. const gchar *p;
  2515. g_return_val_if_fail (haystack != NULL, NULL);
  2516. g_return_val_if_fail (needle != NULL, NULL);
  2517. needle_len = strlen (needle);
  2518. haystack_len = strlen (haystack);
  2519. if (needle_len == 0)
  2520. return (gchar *)haystack;
  2521. if (haystack_len < needle_len)
  2522. return NULL;
  2523. p = haystack + haystack_len - needle_len;
  2524. while (p >= haystack)
  2525. {
  2526. for (i = 0; i < needle_len; i++)
  2527. if (p[i] != needle[i])
  2528. goto next;
  2529. return (gchar *)p;
  2530. next:
  2531. p--;
  2532. }
  2533. return NULL;
  2534. }
  2535. /**
  2536. * g_strrstr_len:
  2537. * @haystack: a nul-terminated string.
  2538. * @haystack_len: the maximum length of @haystack.
  2539. * @needle: the nul-terminated string to search for.
  2540. *
  2541. * Searches the string @haystack for the last occurrence
  2542. * of the string @needle, limiting the length of the search
  2543. * to @haystack_len.
  2544. *
  2545. * Return value: a pointer to the found occurrence, or
  2546. * %NULL if not found.
  2547. **/
  2548. gchar *
  2549. g_strrstr_len (const gchar *haystack,
  2550. gssize haystack_len,
  2551. const gchar *needle)
  2552. {
  2553. g_return_val_if_fail (haystack != NULL, NULL);
  2554. g_return_val_if_fail (needle != NULL, NULL);
  2555. if (haystack_len < 0)
  2556. return g_strrstr (haystack, needle);
  2557. else
  2558. {
  2559. gsize needle_len = strlen (needle);
  2560. const gchar *haystack_max = haystack + haystack_len;
  2561. const gchar *p = haystack;
  2562. gsize i;
  2563. while (p < haystack_max && *p)
  2564. p++;
  2565. if (p < haystack + needle_len)
  2566. return NULL;
  2567. p -= needle_len;
  2568. while (p >= haystack)
  2569. {
  2570. for (i = 0; i < needle_len; i++)
  2571. if (p[i] != needle[i])
  2572. goto next;
  2573. return (gchar *)p;
  2574. next:
  2575. p--;
  2576. }
  2577. return NULL;
  2578. }
  2579. }
  2580. /**
  2581. * g_str_has_suffix:
  2582. * @str: a nul-terminated string.
  2583. * @suffix: the nul-terminated suffix to look for.
  2584. *
  2585. * Looks whether the string @str ends with @suffix.
  2586. *
  2587. * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
  2588. *
  2589. * Since: 2.2
  2590. **/
  2591. gboolean
  2592. g_str_has_suffix (const gchar *str,
  2593. const gchar *suffix)
  2594. {
  2595. int str_len;
  2596. int suffix_len;
  2597. g_return_val_if_fail (str != NULL, FALSE);
  2598. g_return_val_if_fail (suffix != NULL, FALSE);
  2599. str_len = strlen (str);
  2600. suffix_len = strlen (suffix);
  2601. if (str_len < suffix_len)
  2602. return FALSE;
  2603. return strcmp (str + str_len - suffix_len, suffix) == 0;
  2604. }
  2605. /**
  2606. * g_str_has_prefix:
  2607. * @str: a nul-terminated string.
  2608. * @prefix: the nul-terminated prefix to look for.
  2609. *
  2610. * Looks whether the string @str begins with @prefix.
  2611. *
  2612. * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
  2613. *
  2614. * Since: 2.2
  2615. **/
  2616. gboolean
  2617. g_str_has_prefix (const gchar *str,
  2618. const gchar *prefix)
  2619. {
  2620. int str_len;
  2621. int prefix_len;
  2622. g_return_val_if_fail (str != NULL, FALSE);
  2623. g_return_val_if_fail (prefix != NULL, FALSE);
  2624. str_len = strlen (str);
  2625. prefix_len = strlen (prefix);
  2626. if (str_len < prefix_len)
  2627. return FALSE;
  2628. return strncmp (str, prefix, prefix_len) == 0;
  2629. }
  2630. /**
  2631. * g_strip_context:
  2632. * @msgid: a string
  2633. * @msgval: another string
  2634. *
  2635. * An auxiliary function for gettext() support (see Q_()).
  2636. *
  2637. * Return value: @msgval, unless @msgval is identical to @msgid and contains
  2638. * a '|' character, in which case a pointer to the substring of msgid after
  2639. * the first '|' character is returned.
  2640. *
  2641. * Since: 2.4
  2642. **/
  2643. G_CONST_RETURN gchar *
  2644. g_strip_context (const gchar *msgid,
  2645. const gchar *msgval)
  2646. {
  2647. if (msgval == msgid)
  2648. {
  2649. const char *c = strchr (msgid, '|');
  2650. if (c != NULL)
  2651. return c + 1;
  2652. }
  2653. return msgval;
  2654. }
  2655. /**
  2656. * g_strv_length:
  2657. * @str_array: a %NULL-terminated array of strings.
  2658. *
  2659. * Returns the length of the given %NULL-terminated
  2660. * string array @str_array.
  2661. *
  2662. * Return value: length of @str_array.
  2663. *
  2664. * Since: 2.6
  2665. **/
  2666. guint
  2667. g_strv_length (gchar **str_array)
  2668. {
  2669. guint i = 0;
  2670. g_return_val_if_fail (str_array != NULL, 0);
  2671. while (str_array[i])
  2672. ++i;
  2673. return i;
  2674. }
  2675. /**
  2676. * g_dpgettext:
  2677. * @domain: the translation domain to use, or %NULL to use
  2678. * the domain set with textdomain()
  2679. * @msgctxtid: a combined message context and message id, separated
  2680. * by a \004 character
  2681. * @msgidoffset: the offset of the message id in @msgctxid
  2682. *
  2683. * This function is a variant of g_dgettext() which supports
  2684. * a disambiguating message context. GNU gettext uses the
  2685. * '\004' character to separate the message context and
  2686. * message id in @msgctxtid.
  2687. * If 0 is passed as @msgidoffset, this function will fall back to
  2688. * trying to use the deprecated convention of using "|" as a separation
  2689. * character.
  2690. *
  2691. * This uses g_dgettext() internally. See that functions for differences
  2692. * with dgettext() proper.
  2693. *
  2694. * Applications should normally not use this function directly,
  2695. * but use the C_() macro for translations with context.
  2696. *
  2697. * Returns: The translated string
  2698. *
  2699. * Since: 2.16
  2700. */
  2701. G_CONST_RETURN gchar *
  2702. g_dpgettext (const gchar *domain,
  2703. const gchar *msgctxtid,
  2704. gsize msgidoffset)
  2705. {
  2706. const gchar *translation;
  2707. gchar *sep;
  2708. translation = g_dgettext (domain, msgctxtid);
  2709. if (translation == msgctxtid)
  2710. {
  2711. if (msgidoffset > 0)
  2712. return msgctxtid + msgidoffset;
  2713. sep = strchr (msgctxtid, '|');
  2714. if (sep)
  2715. {
  2716. /* try with '\004' instead of '|', in case
  2717. * xgettext -kQ_:1g was used
  2718. */
  2719. gchar *tmp = g_alloca (strlen (msgctxtid) + 1);
  2720. strcpy (tmp, msgctxtid);
  2721. tmp[sep - msgctxtid] = '\004';
  2722. translation = g_dgettext (domain, tmp);
  2723. if (translation == tmp)
  2724. return sep + 1;
  2725. }
  2726. }
  2727. return translation;
  2728. }
  2729. /* This function is taken from gettext.h
  2730. * GNU gettext uses '\004' to separate context and msgid in .mo files.
  2731. */
  2732. /**
  2733. * g_dpgettext2:
  2734. * @domain: the translation domain to use, or %NULL to use
  2735. * the domain set with textdomain()
  2736. * @context: the message context
  2737. * @msgid: the message
  2738. *
  2739. * This function is a variant of g_dgettext() which supports
  2740. * a disambiguating message context. GNU gettext uses the
  2741. * '\004' character to separate the message context and
  2742. * message id in @msgctxtid.
  2743. *
  2744. * This uses g_dgettext() internally. See that functions for differences
  2745. * with dgettext() proper.
  2746. *
  2747. * This function differs from C_() in that it is not a macro and
  2748. * thus you may use non-string-literals as context and msgid arguments.
  2749. *
  2750. * Returns: The translated string
  2751. *
  2752. * Since: 2.18
  2753. */
  2754. G_CONST_RETURN char *
  2755. g_dpgettext2 (const char *domain,
  2756. const char *msgctxt,
  2757. const char *msgid)
  2758. {
  2759. size_t msgctxt_len = strlen (msgctxt) + 1;
  2760. size_t msgid_len = strlen (msgid) + 1;
  2761. const char *translation;
  2762. char* msg_ctxt_id;
  2763. msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
  2764. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  2765. msg_ctxt_id[msgctxt_len - 1] = '\004';
  2766. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  2767. translation = g_dgettext (domain, msg_ctxt_id);
  2768. if (translation == msg_ctxt_id)
  2769. {
  2770. /* try the old way of doing message contexts, too */
  2771. msg_ctxt_id[msgctxt_len - 1] = '|';
  2772. translation = g_dgettext (domain, msg_ctxt_id);
  2773. if (translation == msg_ctxt_id)
  2774. return msgid;
  2775. }
  2776. return translation;
  2777. }
  2778. static gboolean
  2779. _g_dgettext_should_translate (void)
  2780. {
  2781. static gsize translate = 0;
  2782. enum {
  2783. SHOULD_TRANSLATE = 1,
  2784. SHOULD_NOT_TRANSLATE = 2
  2785. };
  2786. if (G_UNLIKELY (g_once_init_enter (&translate)))
  2787. {
  2788. gboolean should_translate = TRUE;
  2789. const char *default_domain = textdomain (NULL);
  2790. const char *translator_comment = gettext ("");
  2791. #ifndef G_OS_WIN32
  2792. const char *translate_locale = setlocale (LC_MESSAGES, NULL);
  2793. #else
  2794. const char *translate_locale = g_win32_getlocale ();
  2795. #endif
  2796. /* We should NOT translate only if all the following hold:
  2797. * - user has called textdomain() and set textdomain to non-default
  2798. * - default domain has no translations
  2799. * - locale does not start with "en_" and is not "C"
  2800. *
  2801. * Rationale:
  2802. * - If text domain is still the default domain, maybe user calls
  2803. * it later. Continue with old behavior of translating.
  2804. * - If locale starts with "en_", we can continue using the
  2805. * translations even if the app doesn't have translations for
  2806. * this locale. That is, en_UK and en_CA for example.
  2807. * - If locale is "C", maybe user calls setlocale(LC_ALL,"") later.
  2808. * Continue with old behavior of translating.
  2809. */
  2810. if (0 != strcmp (default_domain, "messages") &&
  2811. '\0' == *translator_comment &&
  2812. 0 != strncmp (translate_locale, "en_", 3) &&
  2813. 0 != strcmp (translate_locale, "C"))
  2814. should_translate = FALSE;
  2815. g_once_init_leave (&translate,
  2816. should_translate ?
  2817. SHOULD_TRANSLATE :
  2818. SHOULD_NOT_TRANSLATE);
  2819. }
  2820. return translate == SHOULD_TRANSLATE;
  2821. }
  2822. /**
  2823. * g_dgettext:
  2824. * @domain: the translation domain to use, or %NULL to use
  2825. * the domain set with textdomain()
  2826. * @msgid: message to translate
  2827. *
  2828. * This function is a wrapper of dgettext() which does not translate
  2829. * the message if the default domain as set with textdomain() has no
  2830. * translations for the current locale.
  2831. *
  2832. * The advantage of using this function over dgettext() proper is that
  2833. * libraries using this function (like GTK+) will not use translations
  2834. * if the application using the library does not have translations for
  2835. * the current locale. This results in a consistent English-only
  2836. * interface instead of one having partial translations. For this
  2837. * feature to work, the call to textdomain() and setlocale() should
  2838. * precede any g_dgettext() invocations. For GTK+, it means calling
  2839. * textdomain() before gtk_init or its variants.
  2840. *
  2841. * This function disables translations if and only if upon its first
  2842. * call all the following conditions hold:
  2843. * <itemizedlist>
  2844. * <listitem>@domain is not %NULL</listitem>
  2845. * <listitem>textdomain() has been called to set a default text domain</listitem>
  2846. * <listitem>there is no translations available for the default text domain
  2847. * and the current locale</listitem>
  2848. * <listitem>current locale is not "C" or any English locales (those
  2849. * starting with "en_")</listitem>
  2850. * </itemizedlist>
  2851. *
  2852. * Note that this behavior may not be desired for example if an application
  2853. * has its untranslated messages in a language other than English. In those
  2854. * cases the application should call textdomain() after initializing GTK+.
  2855. *
  2856. * Applications should normally not use this function directly,
  2857. * but use the _() macro for translations.
  2858. *
  2859. * Returns: The translated string
  2860. *
  2861. * Since: 2.18
  2862. */
  2863. G_CONST_RETURN gchar *
  2864. g_dgettext (const gchar *domain,
  2865. const gchar *msgid)
  2866. {
  2867. if (domain && G_UNLIKELY (!_g_dgettext_should_translate ()))
  2868. return msgid;
  2869. return dgettext (domain, msgid);
  2870. }
  2871. /**
  2872. * g_dngettext:
  2873. * @domain: the translation domain to use, or %NULL to use
  2874. * the domain set with textdomain()
  2875. * @msgid: message to translate
  2876. * @msgid_plural: plural form of the message
  2877. * @n: the quantity for which translation is needed
  2878. *
  2879. * This function is a wrapper of dngettext() which does not translate
  2880. * the message if the default domain as set with textdomain() has no
  2881. * translations for the current locale.
  2882. *
  2883. * See g_dgettext() for details of how this differs from dngettext()
  2884. * proper.
  2885. *
  2886. * Returns: The translated string
  2887. *
  2888. * Since: 2.18
  2889. */
  2890. G_CONST_RETURN gchar *
  2891. g_dngettext (const gchar *domain,
  2892. const gchar *msgid,
  2893. const gchar *msgid_plural,
  2894. gulong n)
  2895. {
  2896. if (domain && G_UNLIKELY (!_g_dgettext_should_translate ()))
  2897. return n == 1 ? msgid : msgid_plural;
  2898. return dngettext (domain, msgid, msgid_plural, n);
  2899. }
  2900. #define __G_STRFUNCS_C__
  2901. #include "galiasdef.c"