summaryrefslogtreecommitdiffstats
path: root/tinySAK/src/tsk_url.c
diff options
context:
space:
mode:
authorbossiel <bossiel@yahoo.fr>2011-08-10 22:59:15 +0000
committerbossiel <bossiel@yahoo.fr>2011-08-10 22:59:15 +0000
commit1ebf5a5fcda0c9154e22ed02404fd46525a7fd9f (patch)
tree4b6214a7142ab1035cb0e47444e88af38e712421 /tinySAK/src/tsk_url.c
downloaddoubango-1.0.zip
doubango-1.0.tar.gz
Move deprecated v1.0 from trunk to branches1.0
Diffstat (limited to 'tinySAK/src/tsk_url.c')
-rw-r--r--tinySAK/src/tsk_url.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/tinySAK/src/tsk_url.c b/tinySAK/src/tsk_url.c
new file mode 100644
index 0000000..68a9915
--- /dev/null
+++ b/tinySAK/src/tsk_url.c
@@ -0,0 +1,92 @@
+/*
+* Copyright (C) 2009-2010 Mamadou Diop.
+*
+* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
+*
+* This file is part of Open Source Doubango Framework.
+*
+* DOUBANGO is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* DOUBANGO is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with DOUBANGO.
+*
+*/
+
+/**@file tsk_url.c
+ * @brief Utility functions to encode/decode urls.
+ *
+ * @author Mamadou Diop <diopmamadou(at)doubango.org>
+ *
+ * @date Created: Sat Nov 8 16:54:58 2009 mdiop
+ */
+#include "tsk_url.h"
+#include "tsk_memory.h"
+#include "tsk_string.h"
+
+#include <ctype.h>
+#include <string.h>
+
+/**@defgroup tsk_url_group Utility functions to encode/decode urls.
+*/
+
+
+/**@ingroup tsk_url_group
+* Encode an url.
+* @param url The url to encode
+* @retval The encoded url. It is up to you to free the returned string.
+*
+* @sa tsk_url_decode
+*
+*/
+char* tsk_url_encode(const char* url) {
+ char *purl = (char*)url, *buf = tsk_malloc(tsk_strlen(url) * 3 + 1), *pbuf = buf;
+ while (*purl) {
+ if (isalnum(*purl) || *purl == '-' || *purl == '_' || *purl == '.' || *purl == '~'){
+ *pbuf++ = *purl;
+ }
+ else if (*purl == ' '){
+ *pbuf++ = '+';
+ }
+ else{
+ *pbuf++ = '%', *pbuf++ = tsk_b10tob16(*purl >> 4), *pbuf++ = tsk_b10tob16(*purl & 15);
+ }
+ purl++;
+ }
+ *pbuf = '\0';
+ return buf;
+}
+
+/**@ingroup tsk_url_group
+* Decode an url.
+* @param url The url to encode
+* @retval The decoded url. It is up to you to free the returned string.
+*
+* @sa tsk_url_encode
+*/
+char* tsk_url_decode(const char* url) {
+ char *purl = (char*)url, *buf = tsk_malloc(tsk_strlen(url) + 1), *pbuf = buf;
+ while (*purl) {
+ if (*purl == '%') {
+ if (purl[1] && purl[2]) {
+ *pbuf++ = tsk_b16tob10(purl[1]) << 4 | tsk_b16tob10(purl[2]);
+ purl += 2;
+ }
+ } else if (*purl == '+') {
+ *pbuf++ = ' ';
+ } else {
+ *pbuf++ = *purl;
+ }
+ purl++;
+ }
+ *pbuf = '\0';
+ return buf;
+}
+
OpenPOWER on IntegriCloud