Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / protocols / gg / oauth / oauth-parameter.c
blob566c30d5df7820040b1725f8aa8256329ecdd587
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * Code adapted from libgadu (C) 2008 Wojtek Kaniewski <wojtekka@irc.pl>
8 * (http://toxygen.net/libgadu/) during Google Summer of Code 2012
9 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "oauth-parameter.h"
28 struct gg_oauth_parameter {
29 char *key;
30 char *value;
31 struct gg_oauth_parameter *next;
34 int gg_oauth_parameter_set(gg_oauth_parameter_t **list, const char *key, const char *value)
36 gg_oauth_parameter_t *p, *new_p;
37 char *new_key;
38 char *new_value;
40 if (value == NULL)
41 return 0;
43 if (list == NULL)
44 return -1;
46 new_key = g_strdup(key);
48 if (new_key == NULL)
49 return -1;
51 new_value = g_strdup(value);
53 if (new_value == NULL) {
54 g_free(new_key);
55 return -1;
58 new_p = g_new0(gg_oauth_parameter_t, 1);
60 if (new_p == NULL) {
61 g_free(new_key);
62 g_free(new_value);
63 return -1;
66 memset(new_p, 0, sizeof(gg_oauth_parameter_t));
67 new_p->key = new_key;
68 new_p->value = new_value;
70 if (*list != NULL) {
71 p = *list;
73 while (p != NULL && p->next != NULL)
74 p = p->next;
76 p->next = new_p;
77 } else {
78 *list = new_p;
81 return 0;
84 char *gg_oauth_parameter_join(gg_oauth_parameter_t *list, int header)
86 gg_oauth_parameter_t *p;
87 int len = 0;
88 char *res, *out;
90 if (header)
91 len += strlen("OAuth ");
93 for (p = list; p; p = p->next) {
94 gchar *escaped;
95 len += strlen(p->key);
97 len += (header) ? 3 : 1;
99 escaped = g_uri_escape_string(p->value, NULL, FALSE);
100 len += strlen(escaped);
101 g_free(escaped);
103 if (p->next)
104 len += 1;
107 res = g_malloc(len + 1);
109 if (res == NULL)
110 return NULL;
112 out = res;
114 *out = 0;
116 if (header) {
117 strcpy(out, "OAuth ");
118 out += strlen(out);
121 for (p = list; p; p = p->next) {
122 gchar *escaped;
123 strcpy(out, p->key);
124 out += strlen(p->key);
126 strcpy(out++, "=");
128 if (header)
129 strcpy(out++, "\"");
131 escaped = g_uri_escape_string(p->value, NULL, FALSE);
132 strcpy(out, escaped);
133 out += strlen(escaped);
134 g_free(escaped);
136 if (header)
137 strcpy(out++, "\"");
139 if (p->next != NULL)
140 strcpy(out++, (header) ? "," : "&");
143 return res;
146 void gg_oauth_parameter_free(gg_oauth_parameter_t *list)
148 while (list != NULL) {
149 gg_oauth_parameter_t *next;
151 next = list->next;
153 g_free(list->key);
154 g_free(list->value);
155 g_free(list);
157 list = next;