34 #define FROM_HEX(c) ((c) < 'A' ? (c) - '0' : (c) < 'a' ? 10 + (c) - 'A' : 10 + (c) - 'a')
35 #define TO_HEX(i) ((i) < 10 ? '0' + (i) : 'A' + (i) - 10)
36 #define IS_LEGAL(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') \
37 || ((c) >= '0' && (c) <= '9') || (strchr ("-_.~/", (c))))
41 return ! g_ascii_strncasecmp (str, prefix, strlen (prefix));
46 int len1 = strlen (str);
47 int len2 = strlen (suffix);
52 return ! g_ascii_strcasecmp (str + len1 - len2, suffix);
55 static char * (* str_to_utf8_impl) (
const char *) =
NULL;
56 static char * (* str_to_utf8_full_impl) (
const char *,
int,
int *,
int *) =
NULL;
59 char * (* stuf_impl) (
const char *,
int,
int *,
int *))
71 EXPORT
char *
str_to_utf8_full (
const char * str,
int len,
int * bytes_read,
int * bytes_written)
79 while ((
string = strchr (
string, old_c)))
98 if (c ==
'%' && len >= 2 && str[0] && str[1])
131 * out ++ =
TO_HEX ((
unsigned char) c >> 4);
132 * out ++ =
TO_HEX (c & 0xF);
145 char * utf8 = g_locale_to_utf8 (name, -1,
NULL,
NULL,
NULL);
148 const char * locale = setlocale (LC_ALL,
NULL);
149 fprintf (stderr,
"Cannot convert filename from system locale (%s): %s\n", locale, name);
156 char enc[3 * strlen (utf8) + 1];
162 return g_strdup_printf (
"file:///%s", enc);
164 return g_strdup_printf (
"file://%s", enc);
175 g_return_val_if_fail (! strncmp (uri,
"file:///", 8),
NULL);
176 char buf[strlen (uri + 8) + 1];
179 g_return_val_if_fail (! strncmp (uri,
"file://", 7),
NULL);
180 char buf[strlen (uri + 7) + 1];
190 const char * locale = setlocale (LC_ALL,
NULL);
191 fprintf (stderr,
"Cannot convert filename to system locale (%s): %s\n", locale, buf);
202 if (! strncmp (uri,
"cdda://?", 8))
203 return g_strdup_printf (
_(
"Audio CD, track %s"), uri + 8);
205 char buf[strlen (uri) + 1];
208 if (! strncmp (uri,
"file:///", 8))
214 if (! strncmp (uri,
"file://", 7))
220 return g_strdup (buf);
223 EXPORT
void uri_parse (
const char * uri,
const char * * base_p,
const char * * ext_p,
224 const char * * sub_p,
int * isub_p)
226 const char * end = uri + strlen (uri);
227 const char * base, * ext, * sub, * c;
231 if ((c = strrchr (uri,
'/')))
236 if ((c = strrchr (base,
'?')) && sscanf (c + 1,
"%d%c", & isub, & junk) == 1)
241 char buf[sub - base + 1];
242 memcpy (buf, base, sub - base);
245 if ((c = strrchr (buf,
'.')))
246 ext = base + (c - buf);
268 g_strlcpy (buf, ext + 1, buflen);
272 if ((qmark = strchr (buf,
'?')))
275 return (buf[0] != 0);
285 return (bp ==
NULL) ? 0 : -1;
289 unsigned char a = * ap ++,
b = * bp ++;
290 for (; a ||
b; a = * ap ++, b = * bp ++)
292 if (a >
'9' || b >
'9' || a <
'0' || b <
'0')
294 if (a <= 'Z' && a >=
'A')
296 if (b <= 'Z' && b >=
'A')
307 for (; (a = * ap) <=
'9' && a >=
'0'; ap ++)
308 x = 10 * x + (a -
'0');
311 for (; (b = * bp) >=
'0' && b <=
'9'; bp ++)
312 y = 10 * y + (b -
'0');
329 return (bp ==
NULL) ? 0 : -1;
333 unsigned char a = * ap ++,
b = * bp ++;
334 for (; a ||
b; a = * ap ++, b = * bp ++)
336 if (a ==
'%' && ap[0] && ap[1])
341 if (b ==
'%' && bp[0] && bp[1])
347 if (a >
'9' || b >
'9' || a <
'0' || b <
'0')
349 if (a <= 'Z' && a >=
'A')
351 if (b <= 'Z' && b >=
'A')
362 for (; (a = * ap) <=
'9' && a >=
'0'; ap ++)
363 x = 10 * x + (a -
'0');
366 for (; (b = * bp) >=
'0' && b <=
'9'; bp ++)
367 y = 10 * y + (b -
'0');
383 int left = strlen(s);
384 int avail = size - (left + 1);
385 int oldlen = strlen(old);
386 int newlen = strlen(
new);
387 int diff = newlen - oldlen;
389 while (left >= oldlen)
391 if (strncmp(ptr, old, oldlen))
402 memmove(ptr + oldlen + diff, ptr + oldlen, left + 1 - oldlen);
404 memcpy(ptr,
new, newlen);
429 bool_t neg = (
string[0] ==
'-');
436 while ((c = *
string ++))
438 if (c < '0' || c >
'9' || val > 100000000)
441 val = val * 10 + (c -
'0');
444 if (val > 1000000000)
447 * addr = neg ? -val : val;
456 bool_t neg = (
string[0] ==
'-');
460 const char * p = strchr (
string,
'.');
472 memcpy (buf,
string, len);
478 len = strlen (p + 1);
482 memcpy (buf, p + 1, len);
483 memset (buf + len,
'0', 6 - len);
497 double val = i + (double) f / 1000000;
498 if (val > 1000000000)
501 * addr = neg ? -val : val;
510 g_return_val_if_fail (val >= -1000000000 && val <= 1000000000,
NULL);
511 return g_strdup_printf (
"%d", val);
516 g_return_val_if_fail (val >= -1000000000 && val <= 1000000000,
NULL);
523 int f = round ((val - i) * 1000000);
531 char *
s = neg ? g_strdup_printf (
"-%d.%06d", i, f) : g_strdup_printf (
"%d.%06d", i, f);
533 char * c = s + strlen (s);
534 while (* (c - 1) ==
'0')
536 if (* (c - 1) ==
'.')
545 char * * split = g_strsplit (
string,
",", -1);
546 if (g_strv_length (split) != count)
549 for (
int i = 0; i < count; i ++)
565 char * * split = g_malloc0 (
sizeof (
char *) * (count + 1));
567 for (
int i = 0; i < count; i ++)
574 char *
string = g_strjoinv (
",", split);
585 char * * split = g_strsplit (
string,
",", -1);
586 if (g_strv_length (split) != count)
589 for (
int i = 0; i < count; i ++)
605 char * * split = g_malloc0 (
sizeof (
char *) * (count + 1));
607 for (
int i = 0; i < count; i ++)
614 char *
string = g_strjoinv (
",", split);