GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-glib-utils.c
1 /********************************************************************\
2  * gnc-glib-utils.c -- utility functions based on glib functions *
3  * Copyright (C) 2006 David Hampton <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the 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 *
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, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21  * *
22 \********************************************************************/
23 
24 #include "config.h"
25 #include <errno.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <string.h>
29 
30 #include "gnc-glib-utils.h"
31 
32 #ifdef G_OS_WIN32
33 #include <windows.h>
34 #endif
35 
36 int
37 safe_utf8_collate (const char * da, const char * db)
38 {
39  if (da && !(*da))
40  da = NULL;
41  if (db && !(*db))
42  db = NULL;
43 
44  if (da && db)
45  return g_utf8_collate(da, db);
46  if (da)
47  return 1;
48  if (db)
49  return -1;
50  return 0;
51 }
52 
53 /********************************************************************
54  * The following definitions are from gutf8.c, for use by
55  * gnc_utf8_validate(). These are all verbatim copies, except for
56  * UNICODE_VALID() which has been modified to look for the strict
57  * subset of UTF-8 that is valid XML text.
58  */
59 
60 #define UTF8_COMPUTE(Char, Mask, Len) \
61  if (Char < 128) \
62  { \
63  Len = 1; \
64  Mask = 0x7f; \
65  } \
66  else if ((Char & 0xe0) == 0xc0) \
67  { \
68  Len = 2; \
69  Mask = 0x1f; \
70  } \
71  else if ((Char & 0xf0) == 0xe0) \
72  { \
73  Len = 3; \
74  Mask = 0x0f; \
75  } \
76  else if ((Char & 0xf8) == 0xf0) \
77  { \
78  Len = 4; \
79  Mask = 0x07; \
80  } \
81  else if ((Char & 0xfc) == 0xf8) \
82  { \
83  Len = 5; \
84  Mask = 0x03; \
85  } \
86  else if ((Char & 0xfe) == 0xfc) \
87  { \
88  Len = 6; \
89  Mask = 0x01; \
90  } \
91  else \
92  Len = -1;
93 
94 #define UTF8_LENGTH(Char) \
95  ((Char) < 0x80 ? 1 : \
96  ((Char) < 0x800 ? 2 : \
97  ((Char) < 0x10000 ? 3 : \
98  ((Char) < 0x200000 ? 4 : \
99  ((Char) < 0x4000000 ? 5 : 6)))))
100 
101 
102 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
103  (Result) = (Chars)[0] & (Mask); \
104  for ((Count) = 1; (Count) < (Len); ++(Count)) \
105  { \
106  if (((Chars)[(Count)] & 0xc0) != 0x80) \
107  { \
108  (Result) = -1; \
109  break; \
110  } \
111  (Result) <<= 6; \
112  (Result) |= ((Chars)[(Count)] & 0x3f); \
113  }
114 
115 #define UNICODE_VALID(Char) \
116  ((Char) < 0x110000 && \
117  (((Char) & 0xFFFFF800) != 0xD800) && \
118  ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
119  ((Char) >= 0x20 || (Char) == 0x09 || (Char) == 0x0A || (Char) == 0x0D) && \
120  ((Char) & 0xFFFE) != 0xFFFE)
121 
122 gboolean
123 gnc_utf8_validate(const gchar *str,
124  gssize max_len,
125  const gchar **end)
126 {
127 
128  const gchar *p;
129 
130  g_return_val_if_fail (str != NULL, FALSE);
131 
132  if (end)
133  *end = str;
134 
135  p = str;
136 
137  while ((max_len < 0 || (p - str) < max_len) && *p)
138  {
139  int i, mask = 0, len;
140  gunichar result;
141  unsigned char c = (unsigned char) * p;
142 
143  UTF8_COMPUTE (c, mask, len);
144 
145  if (len == -1)
146  break;
147 
148  /* check that the expected number of bytes exists in str */
149  if (max_len >= 0 &&
150  ((max_len - (p - str)) < len))
151  break;
152 
153  UTF8_GET (result, p, i, mask, len);
154 
155  if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
156  break;
157 
158  if (result == (gunichar) - 1)
159  break;
160 
161  if (!UNICODE_VALID (result))
162  break;
163 
164  p += len;
165  }
166 
167  if (end)
168  *end = p;
169 
170  /* See that we covered the entire length if a length was
171  * passed in, or that we ended on a nul if not
172  */
173  if (max_len >= 0 &&
174  p != (str + max_len))
175  return FALSE;
176  else if (max_len < 0 &&
177  *p != '\0')
178  return FALSE;
179  else
180  return TRUE;
181 }
182 
183 void
185 {
186  gchar *end;
187  gint len;
188 
189  g_return_if_fail(str);
190 
191  if (gnc_utf8_validate(str, -1, (const gchar **)&end))
192  return;
193 
194  g_warning("Invalid utf8 string: %s", str);
195  do
196  {
197  len = strlen(end);
198  memmove(end, end + 1, len); /* shuffle the remainder one byte */
199  }
200  while (!gnc_utf8_validate(str, -1, (const gchar **)&end));
201 }
202 
203 gchar *
205 {
206  gchar *result = g_strdup (str);
207  gnc_utf8_strip_invalid (result);
208  return result;
209 }
210 
211 gchar *
212 gnc_locale_from_utf8(const gchar* str)
213 {
214  gchar * locale_str;
215  gsize bytes_written = 0;
216  GError * err = NULL;
217 
218  /* Convert from UTF-8 to the encoding used in the current locale. */
219  locale_str = g_locale_from_utf8(str, -1, NULL, &bytes_written, &err);
220  if (err)
221  {
222  g_warning("g_locale_from_utf8 failed: %s", err->message);
223  g_error_free(err);
224  }
225 
226  return locale_str;
227 }
228 
229 gchar *
230 gnc_locale_to_utf8(const gchar* str)
231 {
232  gchar * utf8_str;
233  gsize bytes_written = 0;
234  GError * err = NULL;
235 
236  /* Convert to UTF-8 from the encoding used in the current locale. */
237  utf8_str = g_locale_to_utf8(str, -1, NULL, &bytes_written, &err);
238  if (err)
239  {
240  g_warning("g_locale_to_utf8 failed: %s", err->message);
241  g_error_free(err);
242  }
243 
244  return utf8_str;
245 }
246 
247 GList*
248 gnc_g_list_map(GList* list, GncGMapFunc fn, gpointer user_data)
249 {
250  GList *rtn = NULL;
251  for (; list != NULL; list = list->next)
252  {
253  rtn = g_list_append(rtn, (*fn)(list->data, user_data));
254  }
255  return rtn;
256 }
257 
258 void
259 gnc_g_list_cut(GList **list, GList *cut_point)
260 {
261  if (list == NULL || *list == NULL)
262  return;
263 
264  // if it's the first element.
265  if (cut_point->prev == NULL)
266  {
267  *list = NULL;
268  return;
269  }
270 
271  cut_point->prev->next = NULL;
272  cut_point->prev = NULL;
273 }
274 
275 void
276 gnc_scm_log_warn(const gchar *msg)
277 {
278  g_log("gnc.scm", G_LOG_LEVEL_WARNING, "%s", msg);
279 }
280 
281 void
282 gnc_scm_log_error(const gchar *msg)
283 {
284  g_log("gnc.scm", G_LOG_LEVEL_CRITICAL, "%s", msg);
285 }
286 
287 void
288 gnc_scm_log_msg(const gchar *msg)
289 {
290  g_log("gnc.scm", G_LOG_LEVEL_MESSAGE, "%s", msg);
291 }
292 
293 void
294 gnc_scm_log_debug(const gchar *msg)
295 {
296  g_log("gnc.scm", G_LOG_LEVEL_DEBUG, "%s", msg);
297 }
298 
299 void gnc_gpid_kill(GPid pid)
300 {
301 #ifdef G_OS_WIN32
302  if (!TerminateProcess((HANDLE) pid, 0))
303  {
304  gchar *msg = g_win32_error_message(GetLastError());
305  g_warning("Could not kill child process: %s", msg ? msg : "(null)");
306  g_free(msg);
307  }
308 #else /* !G_OS_WIN32 */
309  if (kill(pid, SIGKILL))
310  {
311  g_warning("Could not kill child process: %s", g_strerror(errno));
312  }
313 #endif /* G_OS_WIN32 */
314 }
GList * gnc_g_list_map(GList *list, GncGMapFunc fn, gpointer user_data)
int safe_utf8_collate(const char *da, const char *db)
gchar * gnc_locale_from_utf8(const gchar *str)
Converts a string from UTF-8 to the encoding used for strings in the current locale.
gboolean gnc_utf8_validate(const gchar *str, gssize max_len, const gchar **end)
Validates UTF-8 encoded text for use in GnuCash.
void gnc_g_list_cut(GList **list, GList *cut_point)
void gnc_utf8_strip_invalid(gchar *str)
GLib helper routines.
gchar * gnc_locale_to_utf8(const gchar *str)
Converts a string to UTF-8 from the encoding used for strings in the current locale.
void gnc_gpid_kill(GPid pid)
gchar * gnc_utf8_strip_invalid_strdup(const gchar *str)