author | Mikael Berthe <mikael@lilotux.net> |
Mon, 25 Jan 2016 18:35:10 +0100 | |
changeset 685 | dfa02c1c7fda |
parent 607 | e2bd14357c9a |
child 690 | 7ccf2113ec5f |
permissions | -rw-r--r-- |
516
4dd3aa6b83e5
Change the tab width to 4 steps in the emacs headers
Mikael Hallendal <micke@imendio.com>
parents:
515
diff
changeset
|
1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
1 | 2 |
/* |
607 | 3 |
* Copyright (C) 2003 Imendio AB |
1 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU Lesser General Public License as |
|
7 |
* published by the Free Software Foundation; either version 2 of the |
|
8 |
* License, or (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 GNU |
|
13 |
* Lesser General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU Lesser General Public |
|
16 |
* License along with this program; if not, write to the |
|
17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 |
* Boston, MA 02111-1307, USA. |
|
19 |
*/ |
|
20 |
||
417
364c17252e5f
Added inlined documentation from generated through the Gtk-doc migration script.
Mikael Hallendal <micke@imendio.com>
parents:
407
diff
changeset
|
21 |
/** |
420
f40434cca022
Fixed the SECTION parts of the inlined docs so that gtk-doc finds them.
Mikael Hallendal <micke@imendio.com>
parents:
417
diff
changeset
|
22 |
* SECTION:lm-utils |
421
2a91651d22de
Added Title to the inlined documentation
Mikael Hallendal <micke@imendio.com>
parents:
420
diff
changeset
|
23 |
* @Title: Miscellaneous Utility Functions |
420
f40434cca022
Fixed the SECTION parts of the inlined docs so that gtk-doc finds them.
Mikael Hallendal <micke@imendio.com>
parents:
417
diff
changeset
|
24 |
* @Short_description: Miscellaneous Utility Functions |
607 | 25 |
* |
417
364c17252e5f
Added inlined documentation from generated through the Gtk-doc migration script.
Mikael Hallendal <micke@imendio.com>
parents:
407
diff
changeset
|
26 |
* Functions to help application developers when writing XMPP applications using Loudmouth. |
364c17252e5f
Added inlined documentation from generated through the Gtk-doc migration script.
Mikael Hallendal <micke@imendio.com>
parents:
407
diff
changeset
|
27 |
*/ |
364c17252e5f
Added inlined documentation from generated through the Gtk-doc migration script.
Mikael Hallendal <micke@imendio.com>
parents:
407
diff
changeset
|
28 |
|
1 | 29 |
#include <config.h> |
30 |
#include <string.h> |
|
31 |
#include <stdio.h> |
|
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
32 |
#include <stdlib.h> |
1 | 33 |
#include <time.h> |
34 |
||
140
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
99
diff
changeset
|
35 |
#include <glib.h> |
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
99
diff
changeset
|
36 |
|
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
99
diff
changeset
|
37 |
#ifndef G_OS_WIN32 |
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
99
diff
changeset
|
38 |
#include <unistd.h> |
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
99
diff
changeset
|
39 |
#endif |
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
99
diff
changeset
|
40 |
|
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
41 |
#ifdef HAVE_IDN |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
42 |
#include <stringprep.h> |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
43 |
#include <punycode.h> |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
44 |
#include <idna.h> |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
45 |
#endif |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
46 |
|
1 | 47 |
#include "lm-internals.h" |
48 |
#include "lm-utils.h" |
|
49 |
||
50 |
LmCallback * |
|
607 | 51 |
_lm_utils_new_callback (gpointer func, |
533
c45243823756
Added some TODO items and minor style fixes
Mikael Hallendal <micke@imendio.com>
parents:
518
diff
changeset
|
52 |
gpointer user_data, |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
53 |
GDestroyNotify notify) |
1 | 54 |
{ |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
55 |
LmCallback *cb; |
607 | 56 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
57 |
cb = g_new0 (LmCallback, 1); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
58 |
cb->func = func; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
59 |
cb->user_data = user_data; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
60 |
cb->notify = notify; |
1 | 61 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
62 |
return cb; |
1 | 63 |
} |
64 |
||
65 |
void |
|
66 |
_lm_utils_free_callback (LmCallback *cb) |
|
67 |
{ |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
68 |
if (!cb) { |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
69 |
return; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
70 |
} |
64
dd835d5cee71
2004-01-06 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
57
diff
changeset
|
71 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
72 |
if (cb->notify) { |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
73 |
(* cb->notify) (cb->user_data); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
74 |
} |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
75 |
g_free (cb); |
1 | 76 |
} |
77 |
||
78 |
gchar * |
|
79 |
_lm_utils_generate_id (void) |
|
80 |
{ |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
81 |
static guint last_id = 0; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
82 |
GTimeVal tv; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
83 |
glong val; |
1 | 84 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
85 |
g_get_current_time (&tv); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
86 |
val = (tv.tv_sec & tv.tv_usec) + last_id++; |
607 | 87 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
88 |
return g_strdup_printf ("%ld%ld", val, tv.tv_usec); |
1 | 89 |
} |
90 |
||
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
91 |
gchar* |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
92 |
_lm_utils_hostname_to_punycode (const gchar *hostname) |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
93 |
{ |
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
94 |
#ifdef HAVE_IDN |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
95 |
char *s; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
96 |
uint32_t *q; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
97 |
int rc; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
98 |
gchar *result; |
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
99 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
100 |
q = stringprep_utf8_to_ucs4 (hostname, -1, NULL); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
101 |
if (q == NULL) { |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
102 |
return g_strdup (hostname); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
103 |
} |
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
104 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
105 |
rc = idna_to_ascii_4z (q, &s, IDNA_ALLOW_UNASSIGNED); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
106 |
free(q); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
107 |
if (rc != IDNA_SUCCESS) { |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
108 |
return g_strdup (hostname); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
109 |
} |
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
110 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
111 |
/* insures result is allocated through glib */ |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
112 |
result = g_strdup(s); |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
113 |
free(s); |
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
114 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
115 |
return result; |
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
116 |
#else |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
117 |
return g_strdup(hostname); |
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
118 |
#endif |
168
ac1affcd5d22
2006-09-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
155
diff
changeset
|
119 |
} |
ac1affcd5d22
2006-09-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
155
diff
changeset
|
120 |
|
179 | 121 |
/** |
236
b4e5c1cb114e
Some tweaks while trying to solve LM-52.
Mikael Hallendal <micke@imendio.com>
parents:
179
diff
changeset
|
122 |
* lm_utils_get_localtime: |
179 | 123 |
* @stamp: An XMPP timestamp |
124 |
* |
|
125 |
* Converts an XMPP timestamp to a struct tm showing local time. |
|
607 | 126 |
* |
179 | 127 |
* Return value: The local time struct. |
128 |
**/ |
|
168
ac1affcd5d22
2006-09-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
155
diff
changeset
|
129 |
struct tm * |
1 | 130 |
lm_utils_get_localtime (const gchar *stamp) |
131 |
{ |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
132 |
struct tm tm; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
133 |
time_t t; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
134 |
gint year, month; |
607 | 135 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
136 |
g_return_val_if_fail (stamp != NULL, NULL); |
168
ac1affcd5d22
2006-09-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
155
diff
changeset
|
137 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
138 |
/* 20021209T23:51:30 */ |
1 | 139 |
|
607 | 140 |
sscanf (stamp, "%4d%2d%2dT%2d:%2d:%2d", |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
141 |
&year, &month, &tm.tm_mday, &tm.tm_hour, |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
142 |
&tm.tm_min, &tm.tm_sec); |
65 | 143 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
144 |
tm.tm_year = year - 1900; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
145 |
tm.tm_mon = month - 1; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
146 |
tm.tm_isdst = -1; |
65 | 147 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
148 |
t = mktime (&tm); |
1 | 149 |
|
65 | 150 |
#if defined(HAVE_TM_GMTOFF) |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
151 |
t += tm.tm_gmtoff; |
65 | 152 |
#elif defined(HAVE_TIMEZONE) |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
153 |
t -= timezone; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
154 |
if (tm.tm_isdst > 0) { |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
155 |
t += 3600; |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
156 |
} |
607 | 157 |
#endif |
1 | 158 |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
516
diff
changeset
|
159 |
return localtime (&t); |
1 | 160 |
} |
161 |
||
155
d24c4392d4e3
2006-06-16 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
162 |