rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / gg / utils.c
blob1b636943af0b360a3c5fe54b2d7ba7642d6e9a96
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 * Rewritten from scratch during Google Summer of Code 2012
8 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
10 * Previously implemented by:
11 * - Arkadiusz Miskiewicz <misiek@pld.org.pl> - first implementation (2001);
12 * - Bartosz Oler <bartosz@bzimage.us> - reimplemented during GSoC 2005;
13 * - Krzysztof Klinikowski <grommasher@gmail.com> - some parts (2009-2011).
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #include "utils.h"
32 #include "gg.h"
34 #include <debug.h>
36 uin_t ggp_str_to_uin(const char *str)
38 char *endptr;
39 uin_t uin;
41 if (!str || str[0] < '0' || str[0] > '9')
42 return 0;
44 errno = 0;
45 uin = strtoul(str, &endptr, 10);
47 if (errno == ERANGE || endptr[0] != '\0')
48 return 0;
50 return uin;
53 const char * ggp_uin_to_str(uin_t uin)
55 static char buff[GGP_UIN_LEN_MAX + 1];
57 g_snprintf(buff, GGP_UIN_LEN_MAX + 1, "%u", uin);
59 return buff;
62 uin_t ggp_get_my_uin(PurpleConnection *gc)
64 g_return_val_if_fail(gc != NULL, 0);
66 return ggp_str_to_uin(purple_account_get_username(
67 purple_connection_get_account(gc)));
70 static gchar * ggp_convert(const gchar *src, const char *srcenc,
71 const char *dstenc)
73 gchar *dst;
74 GError *err = NULL;
76 if (src == NULL)
77 return NULL;
79 dst = g_convert_with_fallback(src, strlen(src), dstenc, srcenc, "?",
80 NULL, NULL, &err);
81 if (err != NULL) {
82 purple_debug_error("gg", "error converting from %s to %s: %s\n",
83 srcenc, dstenc, err->message);
84 g_error_free(err);
87 if (dst == NULL)
88 dst = g_strdup(src);
90 return dst;
93 gchar * ggp_convert_to_cp1250(const gchar *src)
95 return ggp_convert(src, "UTF-8", "CP1250");
98 gchar * ggp_convert_from_cp1250(const gchar *src)
100 return ggp_convert(src, "CP1250", "UTF-8");
103 gboolean ggp_password_validate(const gchar *password)
105 const int len = strlen(password);
106 if (len < 6 || len > 15)
107 return FALSE;
108 return g_regex_match_simple("^[ a-zA-Z0-9~`!@#$%^&*()_+=[\\]{};':\",./?"
109 "<>\\\\|-]+$", password, 0, 0);
112 gchar * ggp_utf8_strndup(const gchar *str, gsize n)
114 size_t raw_len;
115 gchar *end_ptr;
116 if (str == NULL)
117 return NULL;
118 raw_len = strlen(str);
119 if (raw_len <= n)
120 return g_strdup(str);
122 end_ptr = g_utf8_offset_to_pointer(str, g_utf8_pointer_to_offset(str, &str[n]));
123 raw_len = end_ptr - str;
125 if (raw_len > n) {
126 end_ptr = g_utf8_prev_char(end_ptr);
127 raw_len = end_ptr - str;
130 g_assert(raw_len <= n);
132 return g_strndup(str, raw_len);
135 GSList * ggp_list_copy_to_slist_deep(GList *list, GCopyFunc func,
136 gpointer user_data)
138 GSList *new_list = NULL;
139 GList *it;
141 it = g_list_first(list);
142 while (it) {
143 new_list = g_slist_append(new_list, func(it->data, user_data));
144 it = g_list_next(it);
146 return new_list;
149 GList * ggp_strsplit_list(const gchar *string, const gchar *delimiter,
150 gint max_tokens)
152 gchar **splitted, **it;
153 GList *list = NULL;
155 it = splitted = g_strsplit(string, delimiter, max_tokens);
156 while (*it) {
157 list = g_list_append(list, *it);
158 it++;
160 g_free(splitted);
162 return list;
165 gchar * ggp_strjoin_list(const gchar *separator, GList *list)
167 gchar **str_array;
168 gchar *joined;
169 gint list_len, i;
170 GList *it;
172 list_len = g_list_length(list);
173 str_array = g_new(gchar*, list_len + 1);
175 it = g_list_first(list);
176 i = 0;
177 while (it) {
178 str_array[i++] = it->data;
179 it = g_list_next(it);
181 str_array[i] = NULL;
183 joined = g_strjoinv(separator, str_array);
184 g_free(str_array);
186 return joined;
189 const gchar * ggp_ipv4_to_str(uint32_t raw_ip)
191 static gchar buff[INET_ADDRSTRLEN];
192 buff[0] = '\0';
194 g_snprintf(buff, sizeof(buff), "%d.%d.%d.%d",
195 ((raw_ip >> 0) & 0xFF),
196 ((raw_ip >> 8) & 0xFF),
197 ((raw_ip >> 16) & 0xFF),
198 ((raw_ip >> 24) & 0xFF));
200 return buff;
203 GList * ggp_list_truncate(GList *list, guint length, GDestroyNotify free_func)
205 while (g_list_length(list) > length) {
206 GList *last = g_list_last(list);
207 free_func(last->data);
208 list = g_list_delete_link(list, last);
210 return list;
213 gchar * ggp_free_if_equal(gchar *str, const gchar *pattern)
215 if (g_strcmp0(str, pattern) == 0) {
216 g_free(str);
217 return NULL;
219 return str;
222 const gchar * ggp_date_strftime(const gchar *format, time_t date)
224 GDate g_date;
225 static gchar buff[30];
227 g_date_set_time_t(&g_date, date);
228 if (0 == g_date_strftime(buff, sizeof(buff), format, &g_date))
229 return NULL;
230 return buff;
233 time_t ggp_date_from_iso8601(const gchar *str)
235 GTimeVal g_timeval;
237 if (!str)
238 return 0;
239 if (!g_time_val_from_iso8601(str, &g_timeval))
240 return 0;
241 return g_timeval.tv_sec;
244 uint64_t * ggp_uint64dup(uint64_t val)
246 uint64_t *ptr = g_new(uint64_t, 1);
247 *ptr = val;
248 return ptr;
251 gint ggp_int64_compare(gconstpointer _a, gconstpointer _b)
253 const int64_t *ap = _a, *bp = _b;
254 const int64_t a = *ap, b = *bp;
255 if (a == b)
256 return 0;
257 if (a < b)
258 return -1;
259 else
260 return 1;
263 JsonParser * ggp_json_parse(const gchar *data)
265 JsonParser *parser;
267 parser = json_parser_new();
268 if (json_parser_load_from_data(parser, data, -1, NULL))
269 return parser;
271 if (purple_debug_is_unsafe())
272 purple_debug_warning("gg", "Invalid JSON: %s\n", data);
273 return NULL;