1 /*
2  * util.c: misc utility routines
3  * Copyright (C) 2004 Saulius Menkevicius
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * $Id: util.c,v 1.6 2004/12/24 03:16:04 bobas Exp $
20  */
21 
22 #include <time.h>
23 #include <glib.h>
24 #include <stdio.h>
25 
26 #include "user.h"
27 #include "sess.h"
28 #include "util.h"
29 
util_list_free_with_data(GList * list,GDestroyNotify destroy_notify_cb)30 void util_list_free_with_data(GList * list, GDestroyNotify destroy_notify_cb)
31 {
32 	while(list) {
33 		destroy_notify_cb(list->data);
34 		list = g_list_delete_link(list, list);
35 	}
36 }
37 
util_list_copy_with_data(GList * list,util_list_data_copy_func_t * data_copy_cb)38 GList * util_list_copy_with_data(GList * list, util_list_data_copy_func_t * data_copy_cb)
39 {
40 	GList * copy = NULL;
41 
42 	for(; list; list = list->next)
43 		copy = g_list_append(copy, data_copy_cb(list->data));
44 
45 	return copy;
46 }
47 
util_utf8_strcasecmp(const gchar * str1,const gchar * str2)48 gint util_utf8_strcasecmp(const gchar * str1, const gchar * str2)
49 {
50 	gchar * folded1 = g_utf8_casefold(str1, -1),
51 		* folded2 = g_utf8_casefold(str2, -1);
52 	gint result;
53 
54 	result = g_utf8_collate(folded1, folded2);
55 
56 	g_free(folded1);
57 	g_free(folded2);
58 
59 	return result;
60 }
61 
62 gboolean
util_parse_ipv4(const gchar * text,guint32 * p_address)63 util_parse_ipv4(const gchar * text, guint32 * p_address)
64 {
65 	unsigned int a1, a2, a3, a4;
66 	if(sscanf(text, "%u.%u.%u.%u", &a1, &a2, &a3, &a4)!=4)
67 		return FALSE;
68 
69 	if((a1 | a2 | a3 | a4) & ~0xff)
70 		return FALSE;
71 
72 	*p_address = (a1 << 24) | (a2 << 16) | (a3 << 8) | a4;
73 	return TRUE;
74 }
75 
76 char *
util_inet_ntoa(guint32 ip)77 util_inet_ntoa(guint32 ip)
78 {
79 	return g_strdup_printf(
80 		"%u.%u.%u.%u",
81 		(ip >> 24) & 255,
82 		(ip >> 16) & 255,
83 		(ip >> 8) & 255,
84 		ip & 255
85 	);
86 }
87 
88 gchar *
util_time_stamp()89 util_time_stamp()
90 {
91 	gchar * stamp = g_malloc(128);
92 	time_t current_time = time(NULL);
93 	struct tm * curr_tm;
94 
95 #ifndef _WIN32
96 	curr_tm = g_malloc(sizeof(struct tm));
97 	gmtime_r(&current_time, curr_tm);
98 #else
99 	curr_tm = gmtime(&current_time);
100 #endif
101 
102 	strftime(stamp, 128, "%X, %Y-%m-%d", curr_tm);
103 #ifndef _WIN32
104 	g_free(curr_tm);
105 #endif
106 
107 	return stamp;
108 }
109 
110 gpointer
util_session_for_notifies_from(gpointer user)111 util_session_for_notifies_from(gpointer user)
112 {
113 	gpointer session;
114 
115 	g_assert(user);
116 
117 	session = sess_find(SESSTYPE_PRIVATE, user_name_of(user));
118 	if(!session)
119 		session = sess_find(SESSTYPE_STATUS, NULL);
120 
121 	return session;
122 }
123 
124