GETENV: check for proper UTF-8.
[gnumeric.git] / src / gutils.c
blobfd1337a50b47816f7ca1541c18eb74ba13ccfc56
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * utils.c: Various utility routines that do not depend on the GUI of Gnumeric
5 * Authors:
6 * Miguel de Icaza (miguel@gnu.org)
7 * Jukka-Pekka Iivonen (iivonen@iki.fi)
8 * Zbigniew Chyla (cyba@gnome.pl)
9 */
10 #include <gnumeric-config.h>
11 #include <glib/gi18n-lib.h>
12 #include "gnumeric.h"
13 #include "gutils.h"
14 #include "gnumeric-paths.h"
16 #include "sheet.h"
17 #include "ranges.h"
18 #include "mathfunc.h"
20 #include <goffice/goffice.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <locale.h>
30 #include <gsf/gsf-impl-utils.h>
31 #include <gsf/gsf-doc-meta-data.h>
32 #include <gsf/gsf-timestamp.h>
34 static char *gnumeric_lib_dir;
35 static char *gnumeric_data_dir;
36 static char *gnumeric_locale_dir;
37 static char *gnumeric_usr_dir;
38 static char *gnumeric_usr_dir_unversioned;
39 static char *gnumeric_extern_plugin_dir;
40 static GSList *gutils_xml_in_docs;
42 static gboolean
43 running_in_tree (void)
45 const char *argv0 = g_get_prgname ();
47 if (!argv0)
48 return FALSE;
50 /* Sometime we see, e.g., "lt-gnumeric" as basename. */
52 char *base = g_path_get_basename (argv0);
53 gboolean has_lt_prefix = (strncmp (base, "lt-", 3) == 0);
54 g_free (base);
55 if (has_lt_prefix)
56 return TRUE;
59 /* Look for ".libs" as final path element. */
61 const char *dotlibs = strstr (argv0, ".libs/");
62 if (dotlibs &&
63 (dotlibs == argv0 || G_IS_DIR_SEPARATOR (dotlibs[-1])) &&
64 strchr (dotlibs + 6, G_DIR_SEPARATOR) == NULL)
65 return TRUE;
68 return FALSE;
71 void
72 gutils_init (void)
74 char const *home_dir;
75 #ifdef G_OS_WIN32
76 gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
77 gnumeric_lib_dir = g_build_filename (dir, "lib",
78 "gnumeric", GNM_VERSION_FULL,
79 NULL);
80 gnumeric_data_dir = g_build_filename (dir, "share",
81 "gnumeric", GNM_VERSION_FULL,
82 NULL);
83 gnumeric_locale_dir = g_build_filename (dir, "share", "locale", NULL);
84 gnumeric_extern_plugin_dir = g_build_filename
85 (dir, "lib", "gnumeric", GNM_API_VERSION, "plugins",
86 NULL);
87 g_free (dir);
88 #else
89 if (running_in_tree ()) {
90 const char *argv0 = g_get_prgname ();
91 char *dotlibs = g_path_get_dirname (argv0);
92 char *top = g_build_filename (dotlibs, "..", "../", NULL);
93 char *plugins = g_build_filename (top, PLUGIN_SUBDIR, NULL);
94 if (g_file_test (plugins, G_FILE_TEST_IS_DIR))
95 gnumeric_lib_dir =
96 go_filename_simplify (top, GO_DOTDOT_SYNTACTIC,
97 FALSE);
98 g_free (top);
99 g_free (plugins);
100 g_free (dotlibs);
101 if (0) g_printerr ("Running in-tree\n");
104 if (!gnumeric_lib_dir)
105 gnumeric_lib_dir = g_strdup (GNUMERIC_LIBDIR);
106 gnumeric_data_dir = g_strdup (GNUMERIC_DATADIR);
107 gnumeric_locale_dir = g_strdup (GNUMERIC_LOCALEDIR);
108 gnumeric_extern_plugin_dir = g_strdup (GNUMERIC_EXTERNPLUGINDIR);
109 #endif
110 home_dir = g_get_home_dir ();
111 gnumeric_usr_dir_unversioned = home_dir
112 ? g_build_filename (home_dir, ".gnumeric", NULL)
113 : NULL;
114 gnumeric_usr_dir = gnumeric_usr_dir_unversioned
115 ? g_build_filename (gnumeric_usr_dir_unversioned, GNM_VERSION_FULL, NULL)
116 : NULL;
119 void
120 gutils_shutdown (void)
122 GSList *l;
124 g_free (gnumeric_lib_dir);
125 gnumeric_lib_dir = NULL;
126 g_free (gnumeric_data_dir);
127 gnumeric_data_dir = NULL;
128 g_free (gnumeric_locale_dir);
129 gnumeric_locale_dir = NULL;
130 g_free (gnumeric_usr_dir);
131 gnumeric_usr_dir = NULL;
132 g_free (gnumeric_usr_dir_unversioned);
133 gnumeric_usr_dir_unversioned = NULL;
134 g_free (gnumeric_extern_plugin_dir);
135 gnumeric_extern_plugin_dir = NULL;
137 for (l = gutils_xml_in_docs; l; l = l->next) {
138 GsfXMLInDoc **pdoc = l->data;
139 gsf_xml_in_doc_free (*pdoc);
140 *pdoc = NULL;
142 g_slist_free (gutils_xml_in_docs);
143 gutils_xml_in_docs = NULL;
146 char const *
147 gnm_sys_lib_dir (void)
149 return gnumeric_lib_dir;
152 char const *
153 gnm_sys_data_dir (void)
155 return gnumeric_data_dir;
158 char const *
159 gnm_sys_extern_plugin_dir (void)
161 return gnumeric_extern_plugin_dir;
164 char const *
165 gnm_locale_dir (void)
167 return gnumeric_locale_dir;
170 char const *
171 gnm_usr_dir (gboolean versioned)
173 return versioned ? gnumeric_usr_dir : gnumeric_usr_dir_unversioned;
177 static gboolean
178 all_ascii (const char *s)
180 while ((guchar)*s < 0x7f) {
181 if (*s)
182 s++;
183 else
184 return TRUE;
186 return FALSE;
190 * Like strto[ld], but...
191 * 1. handles non-ascii characters
192 * 2. disallows 0x000.0p+00 and 0.0d+00
193 * 3. ensures sane errno on exit
195 gnm_float
196 gnm_utf8_strto (const char *s, char **end)
198 const char *p;
199 int sign;
200 char *dummy_end;
201 GString *ascii;
202 GString const *decimal = go_locale_get_decimal ();
203 gboolean seen_decimal = FALSE;
204 gboolean seen_digit = FALSE;
205 size_t spaces = 0;
206 gnm_float res;
207 int save_errno;
209 if (all_ascii (s)) {
210 res = gnm_strto (s, end);
211 goto handle_denormal;
214 ascii = g_string_sized_new (100);
216 if (!end)
217 end = &dummy_end;
219 p = s;
220 while (g_unichar_isspace (g_utf8_get_char (p))) {
221 p = g_utf8_next_char (p);
222 spaces++;
225 sign = go_unichar_issign (g_utf8_get_char (p));
226 if (sign) {
227 g_string_append_c (ascii, "-/+"[sign + 1]);
228 p = g_utf8_next_char (p);
231 do {
232 if (strncmp (p, decimal->str, decimal->len) == 0) {
233 if (seen_decimal)
234 break;
235 seen_decimal = TRUE;
236 go_string_append_gstring (ascii, decimal);
237 p += decimal->len;
238 } else if (g_unichar_isdigit (g_utf8_get_char (p))) {
239 g_string_append_c (ascii, '0' + g_unichar_digit_value (g_utf8_get_char (p)));
240 p = g_utf8_next_char (p);
241 seen_digit = TRUE;
242 } else
243 break;
244 } while (1);
246 if (!seen_digit) {
247 /* No conversion, bail to gnm_strto for nan etc. */
248 g_string_free (ascii, TRUE);
249 return gnm_strto (s, end);
252 if (*p == 'e' || *p == 'E') {
253 int sign;
255 g_string_append_c (ascii, 'e');
256 p = g_utf8_next_char (p);
258 sign = go_unichar_issign (g_utf8_get_char (p));
259 if (sign) {
260 g_string_append_c (ascii, "-/+"[sign + 1]);
261 p = g_utf8_next_char (p);
263 while (g_unichar_isdigit (g_utf8_get_char (p))) {
264 g_string_append_c (ascii, '0' + g_unichar_digit_value (g_utf8_get_char (p)));
265 p = g_utf8_next_char (p);
269 res = gnm_strto (ascii->str, end);
270 save_errno = errno;
271 *end = g_utf8_offset_to_pointer
272 (s, spaces + g_utf8_pointer_to_offset (ascii->str, *end));
273 g_string_free (ascii, TRUE);
275 errno = save_errno;
277 handle_denormal:
278 save_errno = errno;
279 if (res != 0 && gnm_abs (res) < GNM_MIN)
280 errno = 0;
281 else
282 errno = save_errno;
284 return res;
288 * Like strtol, but...
289 * 1. handles non-ascii characters
290 * 2. assumes base==10
291 * 3. ensures sane errno on exit
293 long
294 gnm_utf8_strtol (const char *s, char **end)
296 const char *p;
297 int sign;
298 char *dummy_end;
299 unsigned long res = 0, lim, limd;
301 if (!end)
302 end = &dummy_end;
304 p = s;
305 while (g_unichar_isspace (g_utf8_get_char (p)))
306 p = g_utf8_next_char (p);
308 sign = go_unichar_issign (g_utf8_get_char (p));
309 if (sign)
310 p = g_utf8_next_char (p);
311 if (sign < 0) {
312 lim = (-(unsigned long)LONG_MIN) / 10u;
313 limd = (-(unsigned long)LONG_MIN) % 10u;
314 } else {
315 lim = (unsigned long)LONG_MAX / 10u;
316 limd = (unsigned long)LONG_MAX % 10u;
319 if (!g_unichar_isdigit (g_utf8_get_char (p))) {
320 errno = 0;
321 *end = (char *)s;
322 return 0;
325 while (g_unichar_isdigit (g_utf8_get_char (p))) {
326 guint8 dig = g_unichar_digit_value (g_utf8_get_char (p));
327 p = g_utf8_next_char (p);
329 if (res > lim || (res == lim && dig > limd)) {
330 /* Overflow */
331 while (g_unichar_isdigit (g_utf8_get_char (p)))
332 p = g_utf8_next_char (p);
333 *end = (char *)p;
334 errno = ERANGE;
335 return sign < 0 ? LONG_MIN : LONG_MAX;
338 res = res * 10u + dig;
340 *end = (char *)p;
341 errno = 0;
342 return sign < 0 ? (long)-res : (long)res;
347 gnm_regcomp_XL (GORegexp *preg, char const *pattern, int cflags,
348 gboolean anchor_start, gboolean anchor_end)
350 GString *res = g_string_new (NULL);
351 int retval;
353 if (anchor_start)
354 g_string_append_c (res, '^');
356 while (*pattern) {
357 switch (*pattern) {
358 case '*':
359 g_string_append (res, ".*");
360 pattern++;
361 break;
363 case '?':
364 g_string_append_c (res, '.');
365 pattern++;
366 break;
368 case '~':
369 if (pattern[1] == '*' ||
370 pattern[1] == '?' ||
371 pattern[1] == '~')
372 pattern++;
373 /* Fall through */
374 default:
375 pattern = go_regexp_quote1 (res, pattern);
379 if (anchor_end)
380 g_string_append_c (res, '$');
382 retval = go_regcomp (preg, res->str, cflags);
383 g_string_free (res, TRUE);
384 return retval;
388 * gnm_excel_search_impl:
389 * @needle: the pattern to search for, see gnm_regcomp_XL.
390 * @haystack: the string to search in.
391 * @skip: zero-based search start point in characters.
393 * Returns: -1 for a non-match, or zero-based location in
394 * characters.
396 * The is the implementation of Excel's SEARCH function.
397 * However, note that @skip and return value are zero-based.
400 gnm_excel_search_impl (const char *needle, const char *haystack,
401 size_t skip)
403 const char *hay2;
404 size_t i;
405 GORegexp r;
407 for (i = skip, hay2 = haystack; i > 0; i--) {
408 if (*hay2 == 0)
409 return -1;
410 hay2 = g_utf8_next_char (hay2);
413 if (gnm_regcomp_XL (&r, needle, GO_REG_ICASE, FALSE, FALSE) == GO_REG_OK) {
414 GORegmatch rm;
416 switch (go_regexec (&r, hay2, 1, &rm, 0)) {
417 case GO_REG_NOMATCH:
418 break;
419 case GO_REG_OK:
420 go_regfree (&r);
421 return skip +
422 g_utf8_pointer_to_offset (hay2, hay2 + rm.rm_so);
423 default:
424 g_warning ("Unexpected go_regexec result");
426 go_regfree (&r);
427 } else {
428 g_warning ("Unexpected regcomp result");
431 return -1;
435 #if 0
436 static char const *
437 color_to_string (PangoColor color)
439 static char result[100];
440 sprintf (result, "%04x:%04x:%04x", color.red, color.green, color.blue);
441 return result;
444 static const char *
445 enum_name (GType typ, int i)
447 static char result[100];
448 GEnumClass *ec = g_type_class_ref (typ);
450 if (ec) {
451 GEnumValue *ev = g_enum_get_value (ec, i);
452 g_type_class_unref (ec);
454 if (ev && ev->value_nick)
455 return ev->value_nick;
456 if (ev && ev->value_name)
457 return ev->value_name;
460 sprintf (result, "%d", i);
461 return result;
464 static gboolean
465 cb_gnm_pango_attr_dump (PangoAttribute *attr, gpointer user_data)
467 g_print (" start=%u; end=%u\n", attr->start_index, attr->end_index);
468 switch (attr->klass->type) {
469 case PANGO_ATTR_FAMILY:
470 g_print (" family=\"%s\"\n", ((PangoAttrString *)attr)->value);
471 break;
472 case PANGO_ATTR_LANGUAGE:
473 g_print (" language=\"%s\"\n", pango_language_to_string (((PangoAttrLanguage *)attr)->value));
474 break;
475 case PANGO_ATTR_STYLE:
476 g_print (" style=%s\n",
477 enum_name (PANGO_TYPE_STYLE, ((PangoAttrInt *)attr)->value));
478 break;
479 case PANGO_ATTR_WEIGHT:
480 g_print (" weight=%s\n",
481 enum_name (PANGO_TYPE_WEIGHT, ((PangoAttrInt *)attr)->value));
482 break;
483 case PANGO_ATTR_VARIANT:
484 g_print (" variant=%s\n",
485 enum_name (PANGO_TYPE_VARIANT, ((PangoAttrInt *)attr)->value));
486 break;
487 case PANGO_ATTR_STRETCH:
488 g_print (" stretch=%s\n",
489 enum_name (PANGO_TYPE_STRETCH, ((PangoAttrInt *)attr)->value));
490 break;
491 case PANGO_ATTR_UNDERLINE:
492 g_print (" underline=%s\n",
493 enum_name (PANGO_TYPE_UNDERLINE, ((PangoAttrInt *)attr)->value));
494 break;
495 case PANGO_ATTR_STRIKETHROUGH:
496 g_print (" strikethrough=%d\n", ((PangoAttrInt *)attr)->value);
497 break;
498 case PANGO_ATTR_RISE:
499 g_print (" rise=%d\n", ((PangoAttrInt *)attr)->value);
500 break;
501 case PANGO_ATTR_FALLBACK:
502 g_print (" fallback=%d\n", ((PangoAttrInt *)attr)->value);
503 break;
504 case PANGO_ATTR_LETTER_SPACING:
505 g_print (" letter_spacing=%d\n", ((PangoAttrInt *)attr)->value);
506 break;
507 case PANGO_ATTR_SIZE:
508 g_print (" size=%d%s\n",
509 ((PangoAttrSize *)attr)->size,
510 ((PangoAttrSize *)attr)->absolute ? " abs" : "");
511 break;
512 case PANGO_ATTR_SCALE:
513 g_print (" scale=%g\n", ((PangoAttrFloat *)attr)->value);
514 break;
515 case PANGO_ATTR_FOREGROUND:
516 g_print (" foreground=%s\n", color_to_string (((PangoAttrColor *)attr)->color));
517 break;
518 case PANGO_ATTR_BACKGROUND:
519 g_print (" background=%s\n", color_to_string (((PangoAttrColor *)attr)->color));
520 break;
521 case PANGO_ATTR_UNDERLINE_COLOR:
522 g_print (" underline_color=%s\n", color_to_string (((PangoAttrColor *)attr)->color));
523 break;
524 case PANGO_ATTR_STRIKETHROUGH_COLOR:
525 g_print (" strikethrough_color=%s\n", color_to_string (((PangoAttrColor *)attr)->color));
526 break;
527 case PANGO_ATTR_FONT_DESC: {
528 char *desc = pango_font_description_to_string (((PangoAttrFontDesc*)attr)->desc);
529 g_print (" font=\"%s\"\n", desc);
530 g_free (desc);
531 break;
533 default:
534 g_print (" type=%s\n", enum_name (PANGO_TYPE_ATTR_TYPE, attr->klass->type));
537 return FALSE;
540 void
541 gnm_pango_attr_dump (PangoAttrList *list)
543 g_print ("PangoAttrList at %p\n", list);
544 pango_attr_list_filter (list, cb_gnm_pango_attr_dump, NULL);
546 #endif
549 static gboolean
550 cb_gnm_pango_attr_list_equal (PangoAttribute *a, gpointer _sl)
552 GSList **sl = _sl;
553 *sl = g_slist_prepend (*sl, a);
554 return FALSE;
558 * This is a bit of a hack. It might claim a difference even when things
559 * actually are equal. But not the other way around.
561 gboolean
562 gnm_pango_attr_list_equal (PangoAttrList const *l1, PangoAttrList const *l2)
564 if (l1 == l2)
565 return TRUE;
566 else if (l1 == NULL || l2 == NULL)
567 return FALSE;
568 else {
569 gboolean res;
570 GSList *sl1 = NULL, *sl2 = NULL;
571 (void)pango_attr_list_filter ((PangoAttrList *)l1,
572 cb_gnm_pango_attr_list_equal,
573 &sl1);
574 (void)pango_attr_list_filter ((PangoAttrList *)l2,
575 cb_gnm_pango_attr_list_equal,
576 &sl2);
578 while (sl1 && sl2) {
579 const PangoAttribute *a1 = sl1->data;
580 const PangoAttribute *a2 = sl2->data;
581 if (a1->start_index != a2->start_index ||
582 a1->end_index != a2->end_index ||
583 !pango_attribute_equal (a1, a2))
584 break;
585 sl1 = g_slist_delete_link (sl1, sl1);
586 sl2 = g_slist_delete_link (sl2, sl2);
589 res = (sl1 == sl2);
590 g_slist_free (sl1);
591 g_slist_free (sl2);
592 return res;
596 /* ------------------------------------------------------------------------- */
598 struct _GnmLocale {
599 char *num_locale;
600 char *monetary_locale;
603 * gnm_push_C_locale: (skip)
605 * Returns the current locale, and sets the locale and the value-format
606 * engine's locale to 'C'. The caller must call gnm_pop_C_locale to free the
607 * result and restore the previous locale.
609 GnmLocale *
610 gnm_push_C_locale (void)
612 GnmLocale *old = g_new0 (GnmLocale, 1);
614 old->num_locale = g_strdup (go_setlocale (LC_NUMERIC, NULL));
615 go_setlocale (LC_NUMERIC, "C");
616 old->monetary_locale = g_strdup (go_setlocale (LC_MONETARY, NULL));
617 go_setlocale (LC_MONETARY, "C");
618 go_locale_untranslated_booleans ();
620 return old;
624 * gnm_pop_C_locale: (skip)
625 * @locale: #GnmLocale
627 * Frees the result of gnm_push_C_locale and restores the original locale.
629 void
630 gnm_pop_C_locale (GnmLocale *locale)
632 /* go_setlocale restores bools to locale translation */
633 go_setlocale (LC_MONETARY, locale->monetary_locale);
634 g_free (locale->monetary_locale);
635 go_setlocale (LC_NUMERIC, locale->num_locale);
636 g_free (locale->num_locale);
637 g_free (locale);
640 /* ------------------------------------------------------------------------- */
642 gboolean
643 gnm_debug_flag (const char *flag)
645 GDebugKey key;
646 key.key = (char *)flag;
647 key.value = 1;
649 return g_parse_debug_string (g_getenv ("GNM_DEBUG"), &key, 1) != 0;
652 /* ------------------------------------------------------------------------- */
654 void
655 gnm_string_add_number (GString *buf, gnm_float d)
657 size_t old_len = buf->len;
658 double d2;
659 static int digits;
661 if (digits == 0) {
662 gnm_float l10 = gnm_log10 (FLT_RADIX);
663 digits = (int)gnm_ceil (GNM_MANT_DIG * l10) +
664 (l10 == (int)l10 ? 0 : 1);
667 g_string_append_printf (buf, "%.*" GNM_FORMAT_g, digits - 1, d);
668 d2 = gnm_strto (buf->str + old_len, NULL);
670 if (d != d2) {
671 g_string_truncate (buf, old_len);
672 g_string_append_printf (buf, "%.*" GNM_FORMAT_g, digits, d);
676 /* ------------------------------------------------------------------------- */
678 void
679 gnm_insert_meta_date (GODoc *doc, char const *name)
681 GValue *value = g_new0 (GValue, 1);
682 GTimeVal tm;
683 GsfTimestamp *ts = gsf_timestamp_new ();
685 g_get_current_time (&tm);
686 tm.tv_usec = 0L;
688 gsf_timestamp_set_time (ts, tm.tv_sec);
689 g_value_init (value, GSF_TIMESTAMP_TYPE);
690 gsf_timestamp_to_value (ts, value);
691 gsf_timestamp_free (ts);
693 gsf_doc_meta_data_insert (go_doc_get_meta_data (doc),
694 g_strdup (name),
695 value);
698 /* ------------------------------------------------------------------------- */
700 gboolean
701 gnm_object_get_bool (gpointer o, const char *name)
703 gboolean b;
704 g_object_get (o, name, &b, NULL);
705 return b;
708 gboolean
709 gnm_object_has_readable_prop (gconstpointer obj, const char *property,
710 GType typ, gpointer pres)
712 GObjectClass *klass;
713 GParamSpec *spec;
715 if (!obj)
716 return FALSE;
718 klass = G_OBJECT_GET_CLASS (G_OBJECT (obj));
719 spec = g_object_class_find_property (klass, property);
720 if (!spec ||
721 !(G_PARAM_READABLE & spec->flags) ||
722 (typ != G_TYPE_NONE && spec->value_type != typ))
723 return FALSE;
725 if (pres)
726 g_object_get (G_OBJECT (obj), property, pres, NULL);
727 return TRUE;
733 gint
734 gnm_float_equal (gnm_float const *a, const gnm_float *b)
736 return (*a == *b);
739 /* ------------------------------------------------------------------------- */
741 guint
742 gnm_float_hash (gnm_float const *d)
744 int expt;
745 gnm_float mant = gnm_frexp (gnm_abs (*d), &expt);
746 guint h = ((guint)(0x80000000u * mant)) ^ expt;
747 if (*d >= 0)
748 h ^= 0x55555555;
749 return h;
752 /* ------------------------------------------------------------------------- */
754 struct cb_compare {
755 GnmHashTableOrder order;
756 gpointer user;
759 static gint
760 cb_compare (gconstpointer a_, gconstpointer b_, gpointer user_data)
762 struct cb_compare *user = user_data;
763 gpointer *a = (gpointer )a_;
764 gpointer *b = (gpointer )b_;
766 return user->order (a[0], a[1], b[0], b[1], user->user);
771 * gnm_hash_table_foreach_ordered:
772 * @h: Hash table
773 * @callback: (scope async): #GHFunc
774 * @order: (scope async): Ordering function
775 * @user: user data for callback and order
777 * Like g_hash_table_foreach, but with an ordering imposed.
779 void
780 gnm_hash_table_foreach_ordered (GHashTable *h,
781 GHFunc callback,
782 GnmHashTableOrder order,
783 gpointer user)
785 unsigned ui;
786 GPtrArray *data;
787 struct cb_compare u;
788 GHashTableIter hiter;
789 gpointer key, value;
791 /* Gather all key-value pairs */
792 data = g_ptr_array_new ();
793 g_hash_table_iter_init (&hiter, h);
794 while (g_hash_table_iter_next (&hiter, &key, &value)) {
795 g_ptr_array_add (data, key);
796 g_ptr_array_add (data, value);
799 /* Sort according to given ordering */
800 u.order = order;
801 u.user = user;
802 g_qsort_with_data (data->pdata,
803 data->len / 2, 2 * sizeof (gpointer),
804 cb_compare,
805 &u);
807 /* Call user callback with all pairs */
808 for (ui = 0; ui < data->len; ui += 2)
809 callback (g_ptr_array_index (data, ui),
810 g_ptr_array_index (data, ui + 1),
811 user);
813 /* Clean up */
814 g_ptr_array_free (data, TRUE);
817 /* ------------------------------------------------------------------------- */
819 void
820 gnm_xml_in_doc_dispose_on_exit (GsfXMLInDoc **pdoc)
822 gutils_xml_in_docs = g_slist_prepend (gutils_xml_in_docs, pdoc);
825 /* ------------------------------------------------------------------------- */