📄 uri.c
字号:
/** * gnet_uri_hash * @p: a #GURI * * Creates a hash code for @p for use with GHashTable. * * Returns: hash code for @p. * **/guintgnet_uri_hash (gconstpointer p){ const GURI* uri = (const GURI*) p; guint h = 0; g_return_val_if_fail (uri, 0); if (uri->scheme) h = g_str_hash (uri->scheme); if (uri->user) h ^= g_str_hash (uri->user); if (uri->passwd) h ^= g_str_hash (uri->passwd); if (uri->hostname) h ^= g_str_hash (uri->hostname); h ^= uri->port; if (uri->path) h ^= g_str_hash (uri->path); if (uri->query) h ^= g_str_hash (uri->query); if (uri->fragment) h ^= g_str_hash (uri->fragment); return h;}/** * gnet_uri_escape * @uri: a #GURI * * Escapes the fields in a #GURI. Network protocols use escaped * URIs. People use unescaped URIs. * **/voidgnet_uri_escape (GURI* uri){ g_return_if_fail (uri); uri->user = field_escape (uri->user, USERINFO_ESCAPE_MASK); uri->passwd = field_escape (uri->passwd, USERINFO_ESCAPE_MASK); uri->path = field_escape (uri->path, PATH_ESCAPE_MASK); uri->query = field_escape (uri->query, QUERY_ESCAPE_MASK); uri->fragment = field_escape (uri->fragment, FRAGMENT_ESCAPE_MASK);}/** * gnet_uri_unescape * @uri: a #GURI * * Unescapes the fields in the URI. Network protocols use escaped * URIs. People use unescaped URIs. * **/voidgnet_uri_unescape (GURI* uri){ g_return_if_fail (uri); if (uri->user) field_unescape (uri->user); if (uri->passwd) field_unescape (uri->passwd); if (uri->path) field_unescape (uri->path); if (uri->query) field_unescape (uri->query); if (uri->fragment) field_unescape (uri->fragment);}static char*field_escape (char* str, unsigned char mask){ gint len; gint i; int must_escape = FALSE; char* dst; gint j; if (str == NULL) return NULL; /* Roughly calculate buffer size */ len = 0; for (i = 0; str[i]; i++) { if (neednt_escape_table[(guint) str[i]] & mask) len++; else { len += 3; must_escape = TRUE; } } /* Don't escape if unnecessary */ if (must_escape == FALSE) return str; /* Allocate buffer */ dst = (char*) g_malloc(len + 1); /* Copy */ for (i = j = 0; str[i]; i++, j++) { /* Unescaped character */ if (neednt_escape_table[(guint) str[i]] & mask) { dst[j] = str[i]; } /* Escaped character */ else { dst[j] = '%'; if (((str[i] & 0xf0) >> 4) < 10) dst[j+1] = ((str[i] & 0xf0) >> 4) + '0'; else dst[j+1] = ((str[i] & 0xf0) >> 4) + 'a' - 10; if ((str[i] & 0x0f) < 10) dst[j+2] = (str[i] & 0x0f) + '0'; else dst[j+2] = (str[i] & 0x0f) + 'a' - 10; j += 2; /* and j is incremented in loop too */ } } dst[j] = '\0'; g_free (str); return dst;}static voidfield_unescape (char* s){ char* src; char* dst; for (src = dst = s; *src; ++src, ++dst) { if (src[0] == '%' && src[1] != '\0' && src[2] != '\0') { gint high, low; if ('a' <= src[1] && src[1] <= 'f') high = src[1] - 'a' + 10; else if ('A' <= src[1] && src[1] <= 'F') high = src[1] - 'A' + 10; else if ('0' <= src[1] && src[1] <= '9') high = src[1] - '0'; else /* malformed */ goto regular_copy; if ('a' <= src[2] && src[2] <= 'f') low = src[2] - 'a' + 10; else if ('A' <= src[2] && src[2] <= 'F') low = src[2] - 'A' + 10; else if ('0' <= src[2] && src[2] <= '9') low = src[2] - '0'; else /* malformed */ goto regular_copy; *dst = (char)((high << 4) + low); src += 2; } else { regular_copy: *dst = *src; } } *dst = '\0';}/** * gnet_uri_get_string * @uri: a #GURI * * Gets a string representation of a #GURI. This function does not * escape or unescape the fields first. Call gnet_uri_escape() or * gnet_uri_unescape first if necessary. * * Returns: a string. * **/char*gnet_uri_get_string (const GURI* uri){ char* rv = NULL; GString* buffer = NULL; g_return_val_if_fail (uri, NULL); buffer = g_string_sized_new (16); if (uri->scheme) g_string_sprintfa (buffer, "%s:", uri->scheme); if (uri->user || uri->passwd || uri->hostname || uri->port) g_string_append (buffer, "//"); if (uri->user) { buffer = g_string_append (buffer, uri->user); buffer = g_string_append_c (buffer, '@'); } if (uri->passwd) { buffer = g_string_append (buffer, uri->passwd); buffer = g_string_append_c (buffer, '@'); } /* Add brackets around the hostname if it's IPv6 */ if (uri->hostname) { if (strchr(uri->hostname, ':') == NULL) buffer = g_string_append (buffer, uri->hostname); else g_string_sprintfa (buffer, "[%s]", uri->hostname); } if (uri->port) g_string_sprintfa (buffer, ":%d", uri->port); if (uri->path) { if (*uri->path == '/' || !(uri->user || uri->passwd || uri->hostname || uri->port)) g_string_append (buffer, uri->path); else g_string_sprintfa (buffer, "/%s", uri->path); } if (uri->query) g_string_sprintfa (buffer, "?%s", uri->query); if (uri->fragment) g_string_sprintfa (buffer, "#%s", uri->fragment); /* Free only GString not data contained, return the data instead */ rv = buffer->str; g_string_free (buffer, FALSE); return rv;}/** * gnet_uri_set_scheme * @uri: a #GURI * @scheme: scheme * * Sets a #GURI's scheme. * **/voidgnet_uri_set_scheme (GURI* uri, const char* scheme){ g_return_if_fail (uri); if (uri->scheme) { g_free (uri->scheme); uri->scheme = NULL; } if (scheme) uri->scheme = g_strdup (scheme);}/** * gnet_uri_set_userinfo * @uri: a #GURI * @userinfo: user info * * Sets a #GURI's user info. * **/voidgnet_uri_set_userinfo (GURI* uri, const char* user, const char* passwd){ g_return_if_fail (uri); if (uri->user) { g_free (uri->user); uri->user = NULL; } if (uri->passwd) { g_free (uri->passwd); uri->passwd = NULL; } if (user) uri->user = g_strdup (user); if (passwd) uri->passwd = g_strdup (passwd);}/** * gnet_uri_set_hostname * @uri: a #GURI * @hostname: host name * * Sets a #GURI's host name. * **/voidgnet_uri_set_hostname (GURI* uri, const char* hostname){ g_return_if_fail (uri); if (uri->hostname) { g_free (uri->hostname); uri->hostname = NULL; } if (hostname) uri->hostname = g_strdup (hostname);}/** * gnet_uri_set_port * @uri: a #GURI * @port: port * * Set a #GURI's port. * **/void gnet_uri_set_port (GURI* uri, gint port){ uri->port = port;}/** * gnet_uri_set_path * @uri: a #GURI * @path: path * * Set a #GURI's path. * **/voidgnet_uri_set_path (GURI* uri, const char* path){ g_return_if_fail (uri); if (uri->path) { g_free (uri->path); uri->path = NULL; } if (path) uri->path = g_strdup (path);}/** * gnet_uri_set_query * @uri: a #GURI * @query: query * * Set a #GURI's query. * **/voidgnet_uri_set_query (GURI* uri, const char* query){ g_return_if_fail (uri); if (uri->query) { g_free (uri->query); uri->query = NULL; } if (query) uri->query = g_strdup (query);}/** * gnet_uri_set_fragment * @uri: a #GURI * @fragment: fragment * * Set a #GURI's fragment. * **/voidgnet_uri_set_fragment (GURI* uri, const char* fragment){ g_return_if_fail (uri); if (uri->fragment) { g_free (uri->fragment); uri->fragment = NULL; } if (fragment) uri->fragment = g_strdup (fragment);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -