33 #define FROM_HEX(c) ((c) < 'A' ? (c) - '0' : (c) < 'a' ? 10 + (c) - 'A' : 10 + (c) - 'a')
34 #define TO_HEX(i) ((i) < 10 ? '0' + (i) : 'A' + (i) - 10)
35 #define IS_LEGAL(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') \
36 || ((c) >= '0' && (c) <= '9') || (strchr ("-_.~/", (c))))
40 return ! g_ascii_strncasecmp (str, prefix, strlen (prefix));
45 int len1 = strlen (str);
46 int len2 = strlen (suffix);
51 return ! g_ascii_strcasecmp (str + len1 - len2, suffix);
54 static char * (* str_to_utf8_impl) (
const char *) =
NULL;
55 static char * (* str_to_utf8_full_impl) (
const char *,
int,
int *,
int *) =
NULL;
58 char * (* stuf_impl) (
const char *,
int,
int *,
int *))
70 EXPORT
char *
str_to_utf8_full (
const char * str,
int len,
int * bytes_read,
int * bytes_written)
78 while ((
string = strchr (
string, old_c)))
97 if (c ==
'%' && len >= 2 && str[0] && str[1])
130 * out ++ =
TO_HEX ((
unsigned char) c >> 4);
131 * out ++ =
TO_HEX (c & 0xF);
144 char * utf8 = g_locale_to_utf8 (name, -1,
NULL,
NULL,
NULL);
147 const char * locale = setlocale (LC_ALL,
NULL);
148 fprintf (stderr,
"Cannot convert filename from system locale (%s): %s\n", locale, name);
155 char enc[3 * strlen (utf8) + 1];
161 return g_strdup_printf (
"file:///%s", enc);
163 return g_strdup_printf (
"file://%s", enc);
174 g_return_val_if_fail (! strncmp (uri,
"file:///", 8),
NULL);
175 char buf[strlen (uri + 8) + 1];
178 g_return_val_if_fail (! strncmp (uri,
"file://", 7),
NULL);
179 char buf[strlen (uri + 7) + 1];
189 const char * locale = setlocale (LC_ALL,
NULL);
190 fprintf (stderr,
"Cannot convert filename to system locale (%s): %s\n", locale, buf);
201 if (! strncmp (uri,
"cdda://?", 8))
202 return g_strdup_printf (
_(
"Audio CD, track %s"), uri + 8);
204 char buf[strlen (uri) + 1];
207 if (! strncmp (uri,
"file:///", 8))
213 if (! strncmp (uri,
"file://", 7))
219 return g_strdup (buf);
222 EXPORT
void uri_parse (
const char * uri,
const char * * base_p,
const char * * ext_p,
223 const char * * sub_p,
int * isub_p)
225 const char * end = uri + strlen (uri);
226 const char * base, * ext, * sub, * c;
230 if ((c = strrchr (uri,
'/')))
235 if ((c = strrchr (base,
'?')) && sscanf (c + 1,
"%d%c", & isub, & junk) == 1)
240 char buf[sub - base + 1];
241 memcpy (buf, base, sub - base);
244 if ((c = strrchr (buf,
'.')))
245 ext = base + (c - buf);
267 g_strlcpy (buf, ext + 1, buflen);
271 if ((qmark = strchr (buf,
'?')))
274 return (buf[0] != 0);
284 return (bp ==
NULL) ? 0 : -1;
288 unsigned char a = * ap ++,
b = * bp ++;
289 for (; a ||
b; a = * ap ++, b = * bp ++)
291 if (a >
'9' || b >
'9' || a <
'0' || b <
'0')
293 if (a <= 'Z' && a >=
'A')
295 if (b <= 'Z' && b >=
'A')
306 for (; (a = * ap) <=
'9' && a >=
'0'; ap ++)
307 x = 10 * x + (a -
'0');
310 for (; (b = * bp) >=
'0' && b <=
'9'; bp ++)
311 y = 10 * y + (b -
'0');
328 return (bp ==
NULL) ? 0 : -1;
332 unsigned char a = * ap ++,
b = * bp ++;
333 for (; a ||
b; a = * ap ++, b = * bp ++)
335 if (a ==
'%' && ap[0] && ap[1])
340 if (b ==
'%' && bp[0] && bp[1])
346 if (a >
'9' || b >
'9' || a <
'0' || b <
'0')
348 if (a <= 'Z' && a >=
'A')
350 if (b <= 'Z' && b >=
'A')
361 for (; (a = * ap) <=
'9' && a >=
'0'; ap ++)
362 x = 10 * x + (a -
'0');
365 for (; (b = * bp) >=
'0' && b <=
'9'; bp ++)
366 y = 10 * y + (b -
'0');
382 int left = strlen(s);
383 int avail = size - (left + 1);
384 int oldlen = strlen(old);
385 int newlen = strlen(
new);
386 int diff = newlen - oldlen;
388 while (left >= oldlen)
390 if (strncmp(ptr, old, oldlen))
401 memmove(ptr + oldlen + diff, ptr + oldlen, left + 1 - oldlen);
403 memcpy(ptr,
new, newlen);
428 bool_t neg = (
string[0] ==
'-');
435 while ((c = *
string ++))
437 if (c < '0' || c >
'9' || val > 100000000)
440 val = val * 10 + (c -
'0');
443 if (val > 1000000000)
446 * addr = neg ? -val : val;
455 bool_t neg = (
string[0] ==
'-');
459 const char * p = strchr (
string,
'.');
471 memcpy (buf,
string, len);
477 len = strlen (p + 1);
481 memcpy (buf, p + 1, len);
482 memset (buf + len,
'0', 6 - len);
496 double val = i + (double) f / 1000000;
497 if (val > 1000000000)
500 * addr = neg ? -val : val;
509 g_return_val_if_fail (val >= -1000000000 && val <= 1000000000,
NULL);
510 return g_strdup_printf (
"%d", val);
515 g_return_val_if_fail (val >= -1000000000 && val <= 1000000000,
NULL);
522 int f = round ((val - i) * 1000000);
530 char *
s = neg ? g_strdup_printf (
"-%d.%06d", i, f) : g_strdup_printf (
"%d.%06d", i, f);
532 char * c = s + strlen (s);
533 while (* (c - 1) ==
'0')
535 if (* (c - 1) ==
'.')
544 char * * split = g_strsplit (
string,
",", -1);
545 if (g_strv_length (split) != count)
548 for (
int i = 0; i < count; i ++)
564 char * * split = g_malloc0 (
sizeof (
char *) * (count + 1));
566 for (
int i = 0; i < count; i ++)
573 char *
string = g_strjoinv (
",", split);
584 char * * split = g_strsplit (
string,
",", -1);
585 if (g_strv_length (split) != count)
588 for (
int i = 0; i < count; i ++)
604 char * * split = g_malloc0 (
sizeof (
char *) * (count + 1));
606 for (
int i = 0; i < count; i ++)
613 char *
string = g_strjoinv (
",", split);