summary refs log tree commit diff stats
path: root/yuri.c
diff options
context:
space:
mode:
authorAli Fardan <raiz@stellarbound.space>2020-11-18 10:55:20 +0300
committerAli Fardan <raiz@stellarbound.space>2020-11-18 10:55:20 +0300
commit6f3d10a3ec09cc278041ced4d4e506cb89a7c69a (patch)
tree5b5c7db3104d805d71029534cffcfd5d9c2aca0b /yuri.c
downloadlibyuri-6f3d10a3ec09cc278041ced4d4e506cb89a7c69a.tar.gz
initial commit; todo: thorough testing
Diffstat (limited to 'yuri.c')
-rw-r--r--yuri.c293
1 files changed, 293 insertions, 0 deletions
diff --git a/yuri.c b/yuri.c
new file mode 100644
index 0000000..3ffcc53
--- /dev/null
+++ b/yuri.c
@@ -0,0 +1,293 @@
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "yuri.h"
+
+struct uri *
+uri_new(void)
+{
+	struct uri *ret;
+
+	ret = malloc(sizeof(*ret));
+	if (ret == NULL)
+		return NULL;
+	memset(ret, 0, sizeof(*ret));
+	return ret;
+}
+
+int
+uri_set_scheme(struct uri *uri, const char *scheme)
+{
+	const char *newscheme;
+
+	newscheme = strdup(scheme);
+	if (newscheme == NULL)
+		return -1;
+	free(uri->scheme);
+	uri->scheme = newscheme;
+
+	return 0;
+}
+
+int
+uri_set_authority(struct uri *uri, int type, const char *user, const char *host, int port)
+{
+	const char *newuser;
+	const char *newhost;
+
+	newhost = strdup(host);
+	if (newhost == NULL)
+		return -1;
+
+	if (user) {
+		newuser = strdup(user);
+		if (newuser == NULL) {
+			free(newhost);
+			return -1;
+		}
+	} else {
+		newuser = NULL;
+	}
+
+	free(uri->authority.user);
+	free(uri->authority.host);
+
+	uri->authority.user = newuser;
+	uri->authority.host = newhost;
+	uri->authority.port = port;
+	uri->authority.type = type;
+
+	return 0;
+}
+
+int
+uri_set_authority_type(struct uri *u, int type)
+{
+	u->authority.type = type;
+	return 0;
+}
+
+int
+uri_set_authority_user(struct uri *u, const char *user)
+{
+	const char *newuser;
+
+	newuser = strdup(user);
+	if (newuser == NULL)
+		return -1;
+	free(u->authority.user);
+	u->authority.user = newuser;
+
+	return 0;
+}
+
+int
+uri_set_authority_host(struct uri *u, const char *host)
+{
+	const char *newhost;
+
+	newhost = strdup(host);
+	if (newhost == NULL)
+		return -1;
+	free(u->authority.host);
+	u->authority.host = newhost;
+
+	return 0;
+}
+
+int
+uri_set_authority_port(struct uri *u, int port)
+{
+	u->authority.port = port;
+	return 0;
+}
+
+int
+uri_append_path(struct uri *uri, const char *item)
+{
+	char **path;
+	int npath;
+
+	if (uri->npath == 0)
+		npath = 1;
+	else
+		npath = uri->npath + 1;
+
+	path = realloc(uri->path, sizeof(*uri->path)*npath);
+	if (path == NULL)
+		return -1;
+	uri->path = path;
+	uri->path[npath-1] = strdup(item);
+	if (uri->path[npath-1] == NULL)
+		return -1;
+	uri->npath = npath;
+
+	return 0;
+}
+
+int
+uri_set_query(struct uri *uri, const char *query)
+{
+	const char *newquery;
+
+	newquery = strdup(query);
+	if (newquery == NULL)
+		return -1;
+	free(uri->query);
+	uri->query = newquery;
+
+	return 0;
+}
+
+int
+uri_set_fragment(struct uri *uri, const char *fragment)
+{
+	const char *newfragment;
+
+	newfragment = strdup(fragment);
+	if (newfragment == NULL)
+		return -1;
+	free(uri->fragment);
+	uri->fragment = newfragment;
+
+	return 0;
+}
+
+void
+uri_unset_scheme(struct uri *u)
+{
+	free(u->scheme);
+	u->scheme = NULL;
+}
+
+void
+uri_unset_authority(struct uri *u)
+{
+	uri_unset_authority_type(u);
+	uri_unset_authority_user(u);
+	uri_unset_authority_host(u);
+	uri_unset_authority_port(u);
+}
+
+void
+uri_unset_authority_type(struct uri *u)
+{
+	u->authority.type = 0;
+}
+
+void
+uri_unset_authority_user(struct uri *u)
+{
+	free(u->authority.user);
+	u->authority.user = NULL;
+}
+
+void
+uri_unset_authority_host(struct uri *u)
+{
+	free(u->authority.host);
+	u->authority.host = NULL;
+}
+
+void
+uri_unset_authority_port(struct uri *u)
+{
+	u->authority.port = 0;
+}
+
+void
+uri_unset_path(struct uri *u)
+{
+	int i;
+	for (i = 0; i < u->npath; i++)
+		free(u->path[i]);
+	free(u->path);
+	u->path = NULL;
+	u->npath = 0;
+}
+
+void
+uri_unset_query(struct uri *u)
+{
+	free(u->query);
+	u->query = NULL;
+}
+
+void
+uri_unset_fragment(struct uri *u)
+{
+	free(u->fragment);
+	u->fragment = NULL;
+}
+
+const char *
+uri_get_scheme(struct uri *u)
+{
+	return u->scheme;
+}
+
+int
+uri_get_authority_type(struct uri *u)
+{
+	return u->authority.type;
+}
+
+const char *
+uri_get_authority_user(struct uri *u)
+{
+	return u->authority.user;
+}
+
+const char *
+uri_get_authority_host(struct uri *u)
+{
+	return u->authority.host;
+}
+
+int
+uri_get_authority_port(struct uri *u)
+{
+	return u->authority.port;
+}
+
+const char **
+uri_get_path(struct uri *u)
+{
+	return u->path;
+}
+
+int
+uri_get_npath(struct uri *u)
+{
+	return u->npath;
+}
+
+const char *
+uri_get_query(struct uri *u)
+{
+	return u->query;
+}
+
+const char *
+uri_get_fragment(struct uri *u)
+{
+	return u->fragment;
+}
+
+void
+uri_free(struct uri *u)
+{
+	int i;
+	if (!u)
+		return;
+	free(u->scheme);
+	free(u->authority.user);
+	free(u->authority.host);
+	for (i = 0; i < u->npath; i++)
+		free(u->path[i]);
+	free(u->path);
+	free(u->query);
+	free(u->fragment);
+	free(u);
+}