2 * utils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * General utility functions, non-GTK related.
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
45 #include <glib/gstdio.h>
54 #include "filetypes.h"
63 * Tries to open the given URI in a browser.
64 * On Windows, the system's default browser is opened.
65 * On non-Windows systems, the browser command set in the preferences dialog is used. In case
66 * that fails or it is unset, @c xdg-open is used as fallback as well as some other known
69 * @param uri The URI to open in the web browser.
73 void utils_open_browser(const gchar
*uri
)
76 g_return_if_fail(uri
!= NULL
);
77 win32_open_browser(uri
);
81 g_return_if_fail(uri
!= NULL
);
83 cmdline
= g_strconcat(tool_prefs
.browser_cmd
, " \"", uri
, "\"", NULL
);
84 if (! g_spawn_command_line_async(cmdline
, NULL
))
91 if (! g_spawn_async(NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
,
92 NULL
, NULL
, NULL
, NULL
))
95 if (! g_spawn_async(NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
,
96 NULL
, NULL
, NULL
, NULL
))
99 if (! g_spawn_async(NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
103 if (! g_spawn_async(NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
,
104 NULL
, NULL
, NULL
, NULL
))
106 argv
[0] = "konqueror";
107 if (! g_spawn_async(NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
,
108 NULL
, NULL
, NULL
, NULL
))
110 argv
[0] = "netscape";
111 g_spawn_async(NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
,
112 NULL
, NULL
, NULL
, NULL
);
124 /* taken from anjuta, to determine the EOL mode of the file */
125 gint
utils_get_line_endings(const gchar
* buffer
, glong size
)
128 guint cr
, lf
, crlf
, max_mode
;
133 for (i
= 0; i
< size
; i
++)
135 if (buffer
[i
] == 0x0a)
140 else if (buffer
[i
] == 0x0d)
149 if (buffer
[i
+ 1] != 0x0a)
164 /* Vote for the maximum */
182 gboolean
utils_isbrace(gchar c
, gboolean include_angles
)
188 return include_angles
;
195 case ']': return TRUE
;
196 default: return FALSE
;
201 gboolean
utils_is_opening_brace(gchar c
, gboolean include_angles
)
206 return include_angles
;
210 case '[': return TRUE
;
211 default: return FALSE
;
217 * Writes the given @a text into a file with @a filename.
218 * If the file doesn't exist, it will be created.
219 * If it already exists, it will be overwritten.
221 * @param filename The filename of the file to write, in locale encoding.
222 * @param text The text to write into the file.
224 * @return 0 if the file was successfully written, otherwise the @c errno of the
225 * failed operation is returned.
227 gint
utils_write_file(const gchar
*filename
, const gchar
*text
)
229 g_return_val_if_fail(filename
!= NULL
, ENOENT
);
230 g_return_val_if_fail(text
!= NULL
, EINVAL
);
232 if (file_prefs
.use_safe_file_saving
)
234 GError
*error
= NULL
;
235 if (! g_file_set_contents(filename
, text
, -1, &error
))
237 geany_debug("%s: could not write to file %s (%s)", G_STRFUNC
, filename
, error
->message
);
245 gint bytes_written
, len
;
247 if (filename
== NULL
)
251 fp
= g_fopen(filename
, "w");
254 bytes_written
= fwrite(text
, sizeof (gchar
), len
, fp
);
257 if (len
!= bytes_written
)
260 "utils_write_file(): written only %d bytes, had to write %d bytes to %s",
261 bytes_written
, len
, filename
);
267 geany_debug("utils_write_file(): could not write to file %s (%s)",
268 filename
, g_strerror(errno
));
277 * (stolen from anjuta and modified)
278 * Search backward through size bytes looking for a '<', then return the tag, if any.
279 * @return The tag name
281 gchar
*utils_find_open_xml_tag(const gchar sel
[], gint size
, gboolean check_tag
)
283 const gchar
*begin
, *cur
;
285 if (G_UNLIKELY(size
< 3))
286 { /* Smallest tag is "<p>" which is 3 characters */
291 cur
= &sel
[size
- 3];
293 cur
= &sel
[size
- 1];
295 /* Skip to the character before the closing brace */
303 /* skip whitespace */
304 while (cur
> begin
&& isspace(*cur
))
307 return NULL
; /* we found a short tag which doesn't need to be closed */
312 else if (! check_tag
&& *cur
== '>')
319 GString
*result
= g_string_sized_new(64);
322 while (strchr(":_-.", *cur
) || isalnum(*cur
))
324 g_string_append_c(result
, *cur
);
327 return g_string_free(result
, FALSE
);
334 const gchar
*utils_get_eol_name(gint eol_mode
)
338 case SC_EOL_CRLF
: return _("Win (CRLF)"); break;
339 case SC_EOL_CR
: return _("Mac (CR)"); break;
340 default: return _("Unix (LF)"); break;
345 gboolean
utils_atob(const gchar
*str
)
347 if (G_UNLIKELY(str
== NULL
))
349 else if (strcmp(str
, "TRUE") == 0 || strcmp(str
, "true") == 0)
355 /* NULL-safe version of g_path_is_absolute(). */
356 gboolean
utils_is_absolute_path(const gchar
*path
)
361 return g_path_is_absolute(path
);
365 /* Skips root if path is absolute, do nothing otherwise.
366 * This is a relative-safe version of g_path_skip_root().
368 const gchar
*utils_path_skip_root(const gchar
*path
)
370 const gchar
*path_relative
;
372 path_relative
= g_path_skip_root(path
);
374 return (path_relative
!= NULL
) ? path_relative
: path
;
378 gdouble
utils_scale_round(gdouble val
, gdouble factor
)
380 /*val = floor(val * factor + 0.5);*/
383 val
= MIN(val
, factor
);
390 * A replacement function for g_strncasecmp() to compare strings case-insensitive.
391 * It converts both strings into lowercase using g_utf8_strdown() and then compare
392 * both strings using strcmp().
393 * This is not completely accurate regarding locale-specific case sorting rules
394 * but seems to be a good compromise between correctness and performance.
396 * The input strings should be in UTF-8 or locale encoding.
398 * @param s1 Pointer to first string or @c NULL.
399 * @param s2 Pointer to second string or @c NULL.
401 * @return an integer less than, equal to, or greater than zero if @a s1 is found, respectively,
402 * to be less than, to match, or to be greater than @a s2.
406 gint
utils_str_casecmp(const gchar
*s1
, const gchar
*s2
)
411 g_return_val_if_fail(s1
!= NULL
, 1);
412 g_return_val_if_fail(s2
!= NULL
, -1);
417 /* first ensure strings are UTF-8 */
418 if (! g_utf8_validate(s1
, -1, NULL
))
419 setptr(tmp1
, g_locale_to_utf8(s1
, -1, NULL
, NULL
, NULL
));
420 if (! g_utf8_validate(s2
, -1, NULL
))
421 setptr(tmp2
, g_locale_to_utf8(s2
, -1, NULL
, NULL
, NULL
));
434 /* then convert the strings into a case-insensitive form */
435 setptr(tmp1
, g_utf8_strdown(tmp1
, -1));
436 setptr(tmp2
, g_utf8_strdown(tmp2
, -1));
439 result
= strcmp(tmp1
, tmp2
);
448 * Truncates the input string to a given length.
449 * Characters are removed from the middle of the string, so the start and the end of string
452 * @param string Input string.
453 * @param truncate_length The length in characters of the resulting string.
455 * @return A copy of @a string which is truncated to @a truncate_length characters,
456 * should be freed when no longer needed.
460 /* This following function is taken from Gedit. */
461 gchar
*utils_str_middle_truncate(const gchar
*string
, guint truncate_length
)
466 guint num_left_chars
;
468 guint delimiter_length
;
469 const gchar
*delimiter
= "\342\200\246";
471 g_return_val_if_fail(string
!= NULL
, NULL
);
473 length
= strlen(string
);
475 g_return_val_if_fail(g_utf8_validate(string
, length
, NULL
), NULL
);
477 /* It doesnt make sense to truncate strings to less than the size of the delimiter plus 2
478 * characters (one on each side) */
479 delimiter_length
= g_utf8_strlen(delimiter
, -1);
480 if (truncate_length
< (delimiter_length
+ 2))
481 return g_strdup(string
);
483 n_chars
= g_utf8_strlen(string
, length
);
485 /* Make sure the string is not already small enough. */
486 if (n_chars
<= truncate_length
)
487 return g_strdup (string
);
489 /* Find the 'middle' where the truncation will occur. */
490 num_left_chars
= (truncate_length
- delimiter_length
) / 2;
491 right_offset
= n_chars
- truncate_length
+ num_left_chars
+ delimiter_length
;
493 truncated
= g_string_new_len(string
, g_utf8_offset_to_pointer(string
, num_left_chars
) - string
);
494 g_string_append(truncated
, delimiter
);
495 g_string_append(truncated
, g_utf8_offset_to_pointer(string
, right_offset
));
497 return g_string_free(truncated
, FALSE
);
502 * @c NULL-safe string comparison. Returns @c TRUE if both @a a and @a b are @c NULL
503 * or if @a a and @a b refer to valid strings which are equal.
505 * @param a Pointer to first string or @c NULL.
506 * @param b Pointer to second string or @c NULL.
508 * @return @c TRUE if @a a equals @a b, else @c FALSE.
510 gboolean
utils_str_equal(const gchar
*a
, const gchar
*b
)
512 /* (taken from libexo from os-cillation) */
513 if (a
== NULL
&& b
== NULL
) return TRUE
;
514 else if (a
== NULL
|| b
== NULL
) return FALSE
;
525 * Removes the extension from @a filename and return the result in a newly allocated string.
527 * @param filename The filename to operate on.
529 * @return A newly-allocated string, should be freed when no longer needed.
531 gchar
*utils_remove_ext_from_filename(const gchar
*filename
)
537 g_return_val_if_fail(filename
!= NULL
, NULL
);
539 last_dot
= strrchr(filename
, '.');
541 return g_strdup(filename
);
543 /* assumes extension is small, so extra bytes don't matter */
544 result
= g_malloc(strlen(filename
));
546 while ((filename
+ i
) != last_dot
)
548 result
[i
] = filename
[i
];
556 gchar
utils_brace_opposite(gchar ch
)
560 case '(': return ')';
561 case ')': return '(';
562 case '[': return ']';
563 case ']': return '[';
564 case '{': return '}';
565 case '}': return '{';
566 case '<': return '>';
567 case '>': return '<';
568 default: return '\0';
573 gchar
*utils_get_hostname(void)
576 return win32_get_hostname();
577 #elif defined(HAVE_GETHOSTNAME)
579 if (gethostname(hostname
, sizeof(hostname
)) == 0)
580 return g_strdup(hostname
);
582 return g_strdup("localhost");
586 /* Checks whether the given file can be written. locale_filename is expected in locale encoding.
587 * Returns 0 if it can be written, otherwise it returns errno */
588 gint
utils_is_file_writeable(const gchar
*locale_filename
)
593 if (! g_file_test(locale_filename
, G_FILE_TEST_EXISTS
) &&
594 ! g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
))
595 /* get the file's directory to check for write permission if it doesn't yet exist */
596 file
= g_path_get_dirname(locale_filename
);
598 file
= g_strdup(locale_filename
);
601 /* use _waccess on Windows, access() doesn't accept special characters */
602 ret
= win32_check_write_permission(file
);
605 /* access set also errno to "FILE NOT FOUND" even if locale_filename is writeable, so use
606 * errno only when access() explicitly returns an error */
607 if (access(file
, R_OK
| W_OK
) != 0)
617 /* Replaces all occurrences of needle in haystack with replacement.
618 * Warning: *haystack must be a heap address; it may be freed and reassigned.
619 * Note: utils_string_replace_all() will always be faster when @a replacement is longer
621 * All strings have to be NULL-terminated.
622 * See utils_string_replace_all() for details. */
623 void utils_str_replace_all(gchar
**haystack
, const gchar
*needle
, const gchar
*replacement
)
627 g_return_if_fail(*haystack
!= NULL
);
629 str
= g_string_new(*haystack
);
632 utils_string_replace_all(str
, needle
, replacement
);
634 *haystack
= g_string_free(str
, FALSE
);
638 gint
utils_strpos(const gchar
*haystack
, const gchar
*needle
)
640 gint haystack_length
= strlen(haystack
);
641 gint needle_length
= strlen(needle
);
644 if (needle_length
> haystack_length
)
650 for (i
= 0; (i
< haystack_length
) && pos
== -1; i
++)
652 if (haystack
[i
] == needle
[0] && needle_length
== 1)
654 else if (haystack
[i
] == needle
[0])
656 for (j
= 1; (j
< needle_length
); j
++)
658 if (haystack
[i
+ j
] == needle
[j
])
677 * Retrieves a formatted date/time string from strftime().
678 * This function should be preferred to directly calling strftime() since this function
679 * works on UTF-8 encoded strings.
681 * @param format The format string to pass to strftime(3). See the strftime(3)
682 * documentation for details, in UTF-8 encoding.
683 * @param time_to_use The date/time to use, in time_t format or NULL to use the current time.
685 * @return A newly-allocated string, should be freed when no longer needed.
689 gchar
*utils_get_date_time(const gchar
*format
, time_t *time_to_use
)
692 static gchar date
[1024];
693 gchar
*locale_format
;
696 g_return_val_if_fail(format
!= NULL
, NULL
);
698 if (! g_utf8_validate(format
, -1, NULL
))
700 locale_format
= g_locale_from_utf8(format
, -1, NULL
, NULL
, NULL
);
701 if (locale_format
== NULL
)
705 locale_format
= g_strdup(format
);
707 if (time_to_use
!= NULL
)
708 tm
= localtime(time_to_use
);
711 time_t tp
= time(NULL
);
715 len
= strftime(date
, 1024, locale_format
, tm
);
716 g_free(locale_format
);
720 if (! g_utf8_validate(date
, len
, NULL
))
721 return g_locale_to_utf8(date
, len
, NULL
, NULL
, NULL
);
723 return g_strdup(date
);
727 gchar
*utils_get_initials(const gchar
*name
)
730 gchar
*initials
= g_malloc0(5);
732 initials
[0] = name
[0];
733 while (name
[i
] != '\0' && j
< 4)
735 if (name
[i
] == ' ' && name
[i
+ 1] != ' ')
737 initials
[j
++] = name
[i
+ 1];
746 * Wraps g_key_file_get_integer() to add a default value argument.
748 * @param config A GKeyFile object.
749 * @param section The group name to look in for the key.
750 * @param key The key to find.
751 * @param default_value The default value which will be returned when @a section or @a key
754 * @return The value associated with @a key as an integer, or the given default value if the value
755 * could not be retrieved.
757 gint
utils_get_setting_integer(GKeyFile
*config
, const gchar
*section
, const gchar
*key
,
758 const gint default_value
)
761 GError
*error
= NULL
;
763 if (G_UNLIKELY(config
== NULL
))
764 return default_value
;
766 tmp
= g_key_file_get_integer(config
, section
, key
, &error
);
767 if (G_UNLIKELY(error
))
770 return default_value
;
777 * Wraps g_key_file_get_boolean() to add a default value argument.
779 * @param config A GKeyFile object.
780 * @param section The group name to look in for the key.
781 * @param key The key to find.
782 * @param default_value The default value which will be returned when @c section or @c key
785 * @return The value associated with @a key as a boolean, or the given default value if the value
786 * could not be retrieved.
788 gboolean
utils_get_setting_boolean(GKeyFile
*config
, const gchar
*section
, const gchar
*key
,
789 const gboolean default_value
)
792 GError
*error
= NULL
;
794 if (G_UNLIKELY(config
== NULL
))
795 return default_value
;
797 tmp
= g_key_file_get_boolean(config
, section
, key
, &error
);
798 if (G_UNLIKELY(error
))
801 return default_value
;
808 * Wraps g_key_file_get_string() to add a default value argument.
810 * @param config A GKeyFile object.
811 * @param section The group name to look in for the key.
812 * @param key The key to find.
813 * @param default_value The default value which will be returned when @a section or @a key
816 * @return A newly allocated string, either the value for @a key or a copy of the given
817 * default value if it could not be retrieved.
819 gchar
*utils_get_setting_string(GKeyFile
*config
, const gchar
*section
, const gchar
*key
,
820 const gchar
*default_value
)
824 if (G_UNLIKELY(config
== NULL
))
825 return g_strdup(default_value
);
827 tmp
= g_key_file_get_string(config
, section
, key
, NULL
);
828 if (G_UNLIKELY(!tmp
))
830 return g_strdup(default_value
);
836 gchar
*utils_get_hex_from_color(GdkColor
*color
)
838 gchar
*buffer
= g_malloc0(9);
840 g_return_val_if_fail(color
!= NULL
, NULL
);
842 g_snprintf(buffer
, 8, "#%02X%02X%02X",
843 (guint
) (utils_scale_round(color
->red
/ 256, 255)),
844 (guint
) (utils_scale_round(color
->green
/ 256, 255)),
845 (guint
) (utils_scale_round(color
->blue
/ 256, 255)));
851 guint
utils_invert_color(guint color
)
855 r
= 0xffffff - color
;
856 g
= 0xffffff - (color
>> 8);
857 b
= 0xffffff - (color
>> 16);
859 return (r
| (g
<< 8) | (b
<< 16));
863 /* Get directory from current file in the notebook.
864 * Returns dir string that should be freed or NULL, depending on whether current file is valid.
865 * Returned string is in UTF-8 encoding */
866 gchar
*utils_get_current_file_dir_utf8(void)
868 GeanyDocument
*doc
= document_get_current();
872 /* get current filename */
873 const gchar
*cur_fname
= doc
->file_name
;
875 if (cur_fname
!= NULL
)
877 /* get folder part from current filename */
878 return g_path_get_dirname(cur_fname
); /* returns "." if no path */
882 return NULL
; /* no file open */
886 /* very simple convenience function */
887 void utils_beep(void)
889 if (prefs
.beep_on_errors
)
894 /* taken from busybox, thanks */
895 gchar
*utils_make_human_readable_str(guint64 size
, gulong block_size
,
898 /* The code will adjust for additional (appended) units. */
899 static const gchar zero_and_units
[] = { '0', 0, 'K', 'M', 'G', 'T' };
900 static const gchar fmt
[] = "%Lu %c%c";
901 static const gchar fmt_tenths
[] = "%Lu.%d %c%c";
912 val
= size
* block_size
;
918 val
+= display_unit
/2; /* Deal with rounding. */
919 val
/= display_unit
; /* Don't combine with the line above!!! */
924 while ((val
>= 1024) && (u
< zero_and_units
+ sizeof(zero_and_units
) - 1))
928 frac
= ((((gint
)(val
% 1024)) * 10) + (1024 / 2)) / 1024;
932 { /* We need to round up here. */
938 /* If f==fmt then 'frac' and 'u' are ignored. */
939 return g_strdup_printf(f
, val
, frac
, *u
, 'b');
943 static guint
utils_get_value_of_hex(const gchar ch
)
945 if (ch
>= '0' && ch
<= '9')
947 else if (ch
>= 'A' && ch
<= 'F')
948 return ch
- 'A' + 10;
949 else if (ch
>= 'a' && ch
<= 'f')
950 return ch
- 'a' + 10;
956 /* utils_strtod() converts a string containing a hex colour ("0x00ff00") into an integer.
957 * Basically, it is the same as strtod() would do, but it does not understand hex colour values,
958 * before ANSI-C99. With with_route set, it takes strings of the format "#00ff00".
959 * Returns -1 on failure. */
960 gint
utils_strtod(const gchar
*source
, gchar
**end
, gboolean with_route
)
962 guint red
, green
, blue
, offset
= 0;
964 g_return_val_if_fail(source
!= NULL
, -1);
966 if (with_route
&& (strlen(source
) != 7 || source
[0] != '#'))
968 else if (! with_route
&& (strlen(source
) != 8 || source
[0] != '0' ||
969 (source
[1] != 'x' && source
[1] != 'X')))
974 /* offset is set to 1 when the string starts with 0x, otherwise it starts with #
975 * and we don't need to increase the index */
979 red
= utils_get_value_of_hex(
980 source
[1 + offset
]) * 16 + utils_get_value_of_hex(source
[2 + offset
]);
981 green
= utils_get_value_of_hex(
982 source
[3 + offset
]) * 16 + utils_get_value_of_hex(source
[4 + offset
]);
983 blue
= utils_get_value_of_hex(
984 source
[5 + offset
]) * 16 + utils_get_value_of_hex(source
[6 + offset
]);
986 return (red
| (green
<< 8) | (blue
<< 16));
990 /* Returns: newly allocated string with the current time formatted HH:MM:SS. */
991 gchar
*utils_get_current_time_string(void)
993 const time_t tp
= time(NULL
);
994 const struct tm
*tmval
= localtime(&tp
);
995 gchar
*result
= g_malloc0(9);
997 strftime(result
, 9, "%H:%M:%S", tmval
);
1003 GIOChannel
*utils_set_up_io_channel(
1004 gint fd
, GIOCondition cond
, gboolean nblock
, GIOFunc func
, gpointer data
)
1007 /*const gchar *encoding;*/
1010 ioc
= g_io_channel_win32_new_fd(fd
);
1012 ioc
= g_io_channel_unix_new(fd
);
1016 g_io_channel_set_flags(ioc
, G_IO_FLAG_NONBLOCK
, NULL
);
1018 g_io_channel_set_encoding(ioc
, NULL
, NULL
);
1020 if (! g_get_charset(&encoding))
1021 { // hope this works reliably
1022 GError *error = NULL;
1023 g_io_channel_set_encoding(ioc, encoding, &error);
1026 geany_debug("%s: %s", G_STRFUNC, error->message);
1027 g_error_free(error);
1032 /* "auto-close" ;-) */
1033 g_io_channel_set_close_on_unref(ioc
, TRUE
);
1035 g_io_add_watch(ioc
, cond
, func
, data
);
1036 g_io_channel_unref(ioc
);
1042 gchar
**utils_read_file_in_array(const gchar
*filename
)
1044 gchar
**result
= NULL
;
1047 g_return_val_if_fail(filename
!= NULL
, NULL
);
1049 g_file_get_contents(filename
, &data
, NULL
, NULL
);
1053 result
= g_strsplit_set(data
, "\r\n", -1);
1061 /* Contributed by Stefan Oltmanns, thanks.
1062 * Replaces \\, \r, \n, \t and \uXXX by their real counterparts.
1063 * keep_backslash is used for regex strings to leave '\\' and '\?' in place */
1064 gboolean
utils_str_replace_escape(gchar
*string
, gboolean keep_backslash
)
1069 g_return_val_if_fail(string
!= NULL
, FALSE
);
1072 len
= strlen(string
);
1073 for (i
= 0; i
< len
; i
++)
1075 if (string
[i
]=='\\')
1077 if (i
++ >= strlen(string
))
1098 case 'x': /* Warning: May produce illegal utf-8 string! */
1100 if (i
>= strlen(string
))
1104 if (isdigit(string
[i
- 1])) string
[j
] = string
[i
- 1] - 48;
1105 else if (isxdigit(string
[i
- 1])) string
[j
] = tolower(string
[i
- 1])-87;
1108 if (isdigit(string
[i
])) string
[j
] |= string
[i
] - 48;
1109 else if (isxdigit(string
[i
])) string
[j
] |= tolower(string
[i
])-87;
1116 if (i
>= strlen(string
))
1120 if (isdigit(string
[i
- 1])) unicodechar
= string
[i
- 1] - 48;
1121 else if (isxdigit(string
[i
- 1])) unicodechar
= tolower(string
[i
- 1])-87;
1124 if (isdigit(string
[i
])) unicodechar
|= string
[i
] - 48;
1125 else if (isxdigit(string
[i
])) unicodechar
|= tolower(string
[i
])-87;
1127 if (((i
+ 2) < strlen(string
)) && (isdigit(string
[i
+ 1]) || isxdigit(string
[i
+ 1]))
1128 && (isdigit(string
[i
+ 2]) || isxdigit(string
[i
+ 2])))
1132 if (isdigit(string
[i
- 1])) unicodechar
|= ((string
[i
- 1] - 48) << 4);
1133 else unicodechar
|= ((tolower(string
[i
- 1])-87) << 4);
1134 if (isdigit(string
[i
])) unicodechar
|= string
[i
] - 48;
1135 else unicodechar
|= tolower(string
[i
])-87;
1137 if (((i
+ 2) < strlen(string
)) && (isdigit(string
[i
+ 1]) || isxdigit(string
[i
+ 1]))
1138 && (isdigit(string
[i
+ 2]) || isxdigit(string
[i
+ 2])))
1142 if (isdigit(string
[i
- 1])) unicodechar
|= ((string
[i
- 1] - 48) << 4);
1143 else unicodechar
|= ((tolower(string
[i
- 1])-87) << 4);
1144 if (isdigit(string
[i
])) unicodechar
|= string
[i
] - 48;
1145 else unicodechar
|= tolower(string
[i
])-87;
1147 if (unicodechar
< 0x80)
1149 string
[j
] = unicodechar
;
1151 else if (unicodechar
< 0x800)
1153 string
[j
] = (unsigned char) ((unicodechar
>> 6) | 0xC0);
1155 string
[j
] = (unsigned char) ((unicodechar
& 0x3F) | 0x80);
1157 else if (unicodechar
< 0x10000)
1159 string
[j
] = (unsigned char) ((unicodechar
>> 12) | 0xE0);
1161 string
[j
] = (unsigned char) (((unicodechar
>> 6) & 0x3F) | 0x80);
1163 string
[j
] = (unsigned char) ((unicodechar
& 0x3F) | 0x80);
1165 else if (unicodechar
< 0x110000) /* more chars are not allowed in unicode */
1167 string
[j
] = (unsigned char) ((unicodechar
>> 18) | 0xF0);
1169 string
[j
] = (unsigned char) (((unicodechar
>> 12) & 0x3F) | 0x80);
1171 string
[j
] = (unsigned char) (((unicodechar
>> 6) & 0x3F) | 0x80);
1173 string
[j
] = (unsigned char) ((unicodechar
& 0x3F) | 0x80);
1182 /* unnecessary escapes are allowed */
1185 string
[j
] = string
[i
];
1190 string
[j
] = string
[i
];
1203 /* Wraps a string in place, replacing a space with a newline character.
1204 * wrapstart is the minimum position to start wrapping or -1 for default */
1205 gboolean
utils_wrap_string(gchar
*string
, gint wrapstart
)
1207 gchar
*pos
, *linestart
;
1208 gboolean ret
= FALSE
;
1213 for (pos
= linestart
= string
; *pos
!= '\0'; pos
++)
1215 if (pos
- linestart
>= wrapstart
&& *pos
== ' ')
1227 * Converts the given UTF-8 encoded string into locale encoding.
1228 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1230 * @param utf8_text UTF-8 encoded text.
1232 * @return The converted string in locale encoding, or a copy of the input string if conversion
1233 * failed. Should be freed with g_free(). If @a utf8_text is @c NULL, @c NULL is returned.
1235 gchar
*utils_get_locale_from_utf8(const gchar
*utf8_text
)
1238 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1239 * which would result in wrongly converted strings */
1240 return g_strdup(utf8_text
);
1246 locale_text
= g_locale_from_utf8(utf8_text
, -1, NULL
, NULL
, NULL
);
1247 if (locale_text
== NULL
)
1248 locale_text
= g_strdup(utf8_text
);
1255 * Converts the given string (in locale encoding) into UTF-8 encoding.
1256 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1258 * @param locale_text Text in locale encoding.
1260 * @return The converted string in UTF-8 encoding, or a copy of the input string if conversion
1261 * failed. Should be freed with g_free(). If @a locale_text is @c NULL, @c NULL is returned.
1263 gchar
*utils_get_utf8_from_locale(const gchar
*locale_text
)
1266 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1267 * which would result in wrongly converted strings */
1268 return g_strdup(locale_text
);
1274 utf8_text
= g_locale_to_utf8(locale_text
, -1, NULL
, NULL
, NULL
);
1275 if (utf8_text
== NULL
)
1276 utf8_text
= g_strdup(locale_text
);
1282 /* Pass pointers to free after arg_count.
1283 * The last argument must be NULL as an extra check that arg_count is correct. */
1284 void utils_free_pointers(gsize arg_count
, ...)
1290 va_start(a
, arg_count
);
1291 for (i
= 0; i
< arg_count
; i
++)
1293 ptr
= va_arg(a
, gpointer
);
1296 ptr
= va_arg(a
, gpointer
);
1298 g_warning("Wrong arg_count!");
1303 /* Creates a string array deep copy of a series of non-NULL strings.
1304 * The first argument is nothing special.
1305 * The list must be ended with NULL.
1306 * If first is NULL, NULL is returned. */
1307 gchar
**utils_strv_new(const gchar
*first
, ...)
1314 g_return_val_if_fail(first
!= NULL
, NULL
);
1316 strvlen
= 1; /* for first argument */
1318 /* count other arguments */
1319 va_start(args
, first
);
1320 for (; va_arg(args
, gchar
*) != NULL
; strvlen
++);
1323 strv
= g_new(gchar
*, strvlen
+ 1); /* +1 for NULL terminator */
1324 strv
[0] = g_strdup(first
);
1326 va_start(args
, first
);
1327 for (i
= 1; str
= va_arg(args
, gchar
*), str
!= NULL
; i
++)
1329 strv
[i
] = g_strdup(str
);
1339 * Creates a directory if it doesn't already exist.
1340 * Creates intermediate parent directories as needed, too.
1341 * The permissions of the created directory are set 0700.
1343 * @param path The path of the directory to create, in locale encoding.
1344 * @param create_parent_dirs Whether to create intermediate parent directories if necessary.
1346 * @return 0 if the directory was successfully created, otherwise the @c errno of the
1347 * failed operation is returned.
1349 gint
utils_mkdir(const gchar
*path
, gboolean create_parent_dirs
)
1354 if (path
== NULL
|| strlen(path
) == 0)
1357 result
= (create_parent_dirs
) ? g_mkdir_with_parents(path
, mode
) : g_mkdir(path
, mode
);
1365 * Gets a list of files from the specified directory.
1366 * Locale encoding is expected for @a path and used for the file list. The list and the data
1367 * in the list should be freed after use, e.g.:
1369 * g_slist_foreach(list, (GFunc) g_free, NULL);
1370 * g_slist_free(list); @endcode
1372 * @note If you don't need a list you should use the foreach_dir() macro instead -
1373 * it's more efficient.
1375 * @param path The path of the directory to scan, in locale encoding.
1376 * @param full_path Whether to include the full path for each filename in the list. Obviously this
1377 * will use more memory.
1378 * @param sort Whether to sort alphabetically (UTF-8 safe).
1379 * @param error The location for storing a possible error, or @c NULL.
1381 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1382 * freed when no longer needed.
1383 * @see utils_get_file_list().
1385 GSList
*utils_get_file_list_full(const gchar
*path
, gboolean full_path
, gboolean sort
, GError
**error
)
1387 GSList
*list
= NULL
;
1389 const gchar
*filename
;
1393 g_return_val_if_fail(path
!= NULL
, NULL
);
1395 dir
= g_dir_open(path
, 0, error
);
1399 foreach_dir(filename
, dir
)
1401 list
= g_slist_append(list
, full_path
?
1402 g_build_path(G_DIR_SEPARATOR_S
, path
, filename
, NULL
) : g_strdup(filename
));
1405 /* sorting last is quicker than on insertion */
1407 list
= g_slist_sort(list
, (GCompareFunc
) utils_str_casecmp
);
1413 * Gets a sorted list of files from the specified directory.
1414 * Locale encoding is expected for @a path and used for the file list. The list and the data
1415 * in the list should be freed after use, e.g.:
1417 * g_slist_foreach(list, (GFunc) g_free, NULL);
1418 * g_slist_free(list); @endcode
1420 * @param path The path of the directory to scan, in locale encoding.
1421 * @param length The location to store the number of non-@c NULL data items in the list,
1423 * @param error The location for storing a possible error, or @c NULL.
1425 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1426 * freed when no longer needed.
1427 * @see utils_get_file_list_full().
1429 GSList
*utils_get_file_list(const gchar
*path
, guint
*length
, GError
**error
)
1431 GSList
*list
= utils_get_file_list_full(path
, FALSE
, TRUE
, error
);
1434 *length
= g_slist_length(list
);
1439 /* returns TRUE if any letter in str is a capital, FALSE otherwise. Should be Unicode safe. */
1440 gboolean
utils_str_has_upper(const gchar
*str
)
1444 if (! NZV(str
) || ! g_utf8_validate(str
, -1, NULL
))
1447 while (*str
!= '\0')
1449 c
= g_utf8_get_char(str
);
1450 /* check only letters and stop once the first non-capital was found */
1451 if (g_unichar_isalpha(c
) && g_unichar_isupper(c
))
1453 /* FIXME don't write a const string */
1454 str
= g_utf8_next_char(str
);
1460 static guint
utils_string_replace_helper(GString
*haystack
, const gchar
*needle
,
1461 const gchar
*replace
, const guint max_replaces
)
1463 const gchar
*stack
, *match
;
1467 g_return_val_if_fail(haystack
!= NULL
, 0);
1468 if (haystack
->len
== 0)
1470 g_return_val_if_fail(NZV(needle
), 0);
1472 stack
= haystack
->str
;
1473 if (! (match
= strstr(stack
, needle
)))
1477 pos
= match
- haystack
->str
;
1478 g_string_erase(haystack
, pos
, strlen(needle
));
1480 /* make next search after removed matching text.
1481 * (we have to be careful to only use haystack->str as its address may change) */
1482 stack
= haystack
->str
+ pos
;
1484 if (G_LIKELY(replace
))
1486 g_string_insert(haystack
, pos
, replace
);
1487 stack
= haystack
->str
+ pos
+ strlen(replace
); /* skip past replacement */
1490 while (++ret
!= max_replaces
&& (match
= strstr(stack
, needle
)));
1497 * Replaces all occurrences of @a needle in @a haystack with @a replace.
1498 * As of Geany 0.16, @a replace can match @a needle, so the following will work:
1499 * @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
1501 * @param haystack The input string to operate on. This string is modified in place.
1502 * @param needle The string which should be replaced.
1503 * @param replace The replacement for @a needle.
1505 * @return Number of replacements made.
1507 guint
utils_string_replace_all(GString
*haystack
, const gchar
*needle
, const gchar
*replace
)
1509 return utils_string_replace_helper(haystack
, needle
, replace
, 0);
1514 * Replaces only the first occurrence of @a needle in @a haystack
1516 * For details, see utils_string_replace_all().
1518 * @param haystack The input string to operate on. This string is modified in place.
1519 * @param needle The string which should be replaced.
1520 * @param replace The replacement for @a needle.
1522 * @return Number of replacements made.
1526 guint
utils_string_replace_first(GString
*haystack
, const gchar
*needle
, const gchar
*replace
)
1528 return utils_string_replace_helper(haystack
, needle
, replace
, 1);
1532 /* Get project or default startup directory (if set), or NULL. */
1533 const gchar
*utils_get_default_dir_utf8(void)
1535 if (app
->project
&& NZV(app
->project
->base_path
))
1537 return app
->project
->base_path
;
1540 if (NZV(prefs
.default_open_path
))
1542 return prefs
.default_open_path
;
1548 static gboolean
check_error(GError
**error
)
1550 if (error
!= NULL
&& *error
!= NULL
)
1552 /* imitate the GLib warning */
1554 "GError set over the top of a previous GError or uninitialized memory.\n"
1555 "This indicates a bug in someone's code. You must ensure an error is NULL "
1556 "before it's set.");
1557 /* after returning the code may segfault, but we don't care because we should
1558 * make sure *error is NULL */
1566 * Wraps g_spawn_sync() and internally calls this function on Unix-like
1567 * systems. On Win32 platforms, it uses the Windows API.
1569 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1570 * @param argv The child's argument vector.
1571 * @param env The child's environment, or @a NULL to inherit parent's.
1572 * @param flags Flags from GSpawnFlags.
1573 * @param child_setup A function to run in the child just before exec().
1574 * @param user_data The user data for child_setup.
1575 * @param std_out The return location for child output.
1576 * @param std_err The return location for child error messages.
1577 * @param exit_status The child exit status, as returned by waitpid().
1578 * @param error The return location for error or @a NULL.
1580 * @return @c TRUE on success, @c FALSE if an error was set.
1582 gboolean
utils_spawn_sync(const gchar
*dir
, gchar
**argv
, gchar
**env
, GSpawnFlags flags
,
1583 GSpawnChildSetupFunc child_setup
, gpointer user_data
, gchar
**std_out
,
1584 gchar
**std_err
, gint
*exit_status
, GError
**error
)
1588 if (! check_error(error
))
1593 *error
= g_error_new(G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
, "argv must not be NULL");
1604 result
= win32_spawn(dir
, argv
, env
, flags
, std_out
, std_err
, exit_status
, error
);
1606 result
= g_spawn_sync(dir
, argv
, env
, flags
, NULL
, NULL
, std_out
, std_err
, exit_status
, error
);
1614 * Wraps g_spawn_async() and internally calls this function on Unix-like
1615 * systems. On Win32 platforms, it uses the Windows API.
1617 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1618 * @param argv The child's argument vector.
1619 * @param env The child's environment, or @a NULL to inherit parent's.
1620 * @param flags Flags from GSpawnFlags.
1621 * @param child_setup A function to run in the child just before exec().
1622 * @param user_data The user data for child_setup.
1623 * @param child_pid The return location for child process ID, or NULL.
1624 * @param error The return location for error or @a NULL.
1626 * @return @c TRUE on success, @c FALSE if an error was set.
1628 gboolean
utils_spawn_async(const gchar
*dir
, gchar
**argv
, gchar
**env
, GSpawnFlags flags
,
1629 GSpawnChildSetupFunc child_setup
, gpointer user_data
, GPid
*child_pid
,
1634 if (! check_error(error
))
1639 *error
= g_error_new(G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
, "argv must not be NULL");
1644 result
= win32_spawn(dir
, argv
, env
, flags
, NULL
, NULL
, NULL
, error
);
1646 result
= g_spawn_async(dir
, argv
, env
, flags
, NULL
, NULL
, child_pid
, error
);
1652 /* Similar to g_build_path() but (re)using a fixed buffer, so never free it.
1653 * This assumes a small enough resulting string length to be kept without freeing,
1654 * but this should be the case for filenames.
1655 * @warning As the buffer is reused, you can't call this recursively, e.g. for a
1656 * function argument and within the function called. */
1657 const gchar
*utils_build_path(const gchar
*first
, ...)
1659 static GString
*buffer
= NULL
;
1664 buffer
= g_string_new(first
);
1666 g_string_assign(buffer
, first
);
1668 va_start(args
, first
);
1671 str
= va_arg(args
, const gchar
*);
1675 g_string_append_c(buffer
, G_DIR_SEPARATOR
);
1676 g_string_append(buffer
, str
);
1684 /* Retrieves the path for the given URI.
1686 * - the path which was determined by g_filename_from_uri() or GIO
1687 * - NULL if the URI is non-local and gvfs-fuse is not installed
1688 * - a new copy of 'uri' if it is not an URI. */
1689 gchar
*utils_get_path_from_uri(const gchar
*uri
)
1691 gchar
*locale_filename
;
1693 g_return_val_if_fail(uri
!= NULL
, NULL
);
1695 if (! utils_is_uri(uri
))
1696 return g_strdup(uri
);
1698 /* this will work only for 'file://' URIs */
1699 locale_filename
= g_filename_from_uri(uri
, NULL
, NULL
);
1701 /* g_filename_from_uri() failed, so we probably have a non-local URI */
1702 if (locale_filename
== NULL
)
1704 GFile
*file
= g_file_new_for_uri(uri
);
1705 locale_filename
= g_file_get_path(file
);
1706 g_object_unref(file
);
1707 if (locale_filename
== NULL
)
1709 geany_debug("The URI '%s' could not be resolved to a local path. This means "
1710 "that the URI is invalid or that you don't have gvfs-fuse installed.", uri
);
1715 if (locale_filename
== NULL
)
1716 geany_debug("The URI '%s' could not be resolved to a local path. This means that the "
1717 "URI is invalid or that Geany can't use GVFS (maybe it is not installed).", uri
);
1719 return locale_filename
;
1723 gboolean
utils_is_uri(const gchar
*uri
)
1725 g_return_val_if_fail(uri
!= NULL
, FALSE
);
1727 return (strstr(uri
, "://") != NULL
);
1731 /* path should be in locale encoding */
1732 gboolean
utils_is_remote_path(const gchar
*path
)
1734 g_return_val_if_fail(path
!= NULL
, FALSE
);
1736 /* if path is an URI and it doesn't start "file://", we take it as remote */
1737 if (utils_is_uri(path
) && strncmp(path
, "file:", 5) != 0)
1741 if (glib_check_version(2, 16, 0) == NULL
) /* no need to check for this with GLib < 2.16 */
1743 static gchar
*fuse_path
= NULL
;
1744 static gsize len
= 0;
1746 if (G_UNLIKELY(fuse_path
== NULL
))
1748 fuse_path
= g_build_filename(g_get_home_dir(), ".gvfs", NULL
);
1749 len
= strlen(fuse_path
);
1751 /* Comparing the file path against a hardcoded path is not the most elegant solution
1752 * but for now it is better than nothing. Ideally, g_file_new_for_path() should create
1753 * proper GFile objects for Fuse paths, but it only does in future GVFS
1754 * versions (gvfs 1.1.1). */
1755 return (strncmp(path
, fuse_path
, len
) == 0);
1762 /* Remove all relative and untidy elements from the path of @a filename.
1763 * @param filename must be a valid absolute path.
1764 * @see tm_get_real_path() - also resolves links. */
1765 void utils_tidy_path(gchar
*filename
)
1767 GString
*str
= g_string_new(filename
);
1768 const gchar
*c
, *needle
;
1771 gboolean preserve_double_backslash
= FALSE
;
1773 g_return_if_fail(g_path_is_absolute(filename
));
1775 if (str
->len
>= 2 && strncmp(str
->str
, "\\\\", 2) == 0)
1776 preserve_double_backslash
= TRUE
;
1778 /* replace "/./" and "//" */
1779 utils_string_replace_all(str
, G_DIR_SEPARATOR_S
"." G_DIR_SEPARATOR_S
, G_DIR_SEPARATOR_S
);
1780 utils_string_replace_all(str
, G_DIR_SEPARATOR_S G_DIR_SEPARATOR_S
, G_DIR_SEPARATOR_S
);
1782 if (preserve_double_backslash
)
1783 g_string_prepend(str
, "\\");
1785 /* replace "/../" */
1786 needle
= G_DIR_SEPARATOR_S
".." G_DIR_SEPARATOR_S
;
1789 c
= strstr(str
->str
, needle
);
1796 break; /* bad path */
1798 /* replace "/../" */
1799 g_string_erase(str
, pos
, strlen(needle
));
1800 g_string_insert_c(str
, pos
, G_DIR_SEPARATOR
);
1802 tmp
= g_strndup(str
->str
, pos
); /* path up to "/../" */
1803 c
= g_strrstr(tmp
, G_DIR_SEPARATOR_S
);
1804 g_return_if_fail(c
);
1806 pos
= c
- tmp
; /* position of previous "/" */
1807 g_string_erase(str
, pos
, strlen(c
));
1811 g_return_if_fail(strlen(str
->str
) <= strlen(filename
));
1812 strcpy(filename
, str
->str
);
1813 g_string_free(str
, TRUE
);
1818 * Removes characters from a string, in place.
1820 * @param string String to search.
1821 * @param chars Characters to remove.
1823 * @return @a string - return value is only useful when nesting function calls, e.g.:
1824 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
1826 * @see @c g_strdelimit.
1828 gchar
*utils_str_remove_chars(gchar
*string
, const gchar
*chars
)
1833 g_return_val_if_fail(string
, NULL
);
1837 foreach_str(r
, string
)
1839 if (!strchr(chars
, *r
))
1847 static void utils_slist_remove_next(GSList
*node
)
1849 GSList
*old
= node
->next
;
1851 g_return_if_fail(old
);
1853 node
->next
= old
->next
;
1854 g_slist_free_1(old
);
1858 /* Gets list of sorted filenames with no path and no duplicates from user and system config */
1859 GSList
*utils_get_config_files(const gchar
*subdir
)
1861 gchar
*path
= g_build_path(G_DIR_SEPARATOR_S
, app
->configdir
, subdir
, NULL
);
1862 GSList
*list
= utils_get_file_list_full(path
, FALSE
, FALSE
, NULL
);
1863 GSList
*syslist
, *node
;
1867 utils_mkdir(path
, FALSE
);
1869 setptr(path
, g_build_path(G_DIR_SEPARATOR_S
, app
->datadir
, subdir
, NULL
));
1870 syslist
= utils_get_file_list_full(path
, FALSE
, FALSE
, NULL
);
1872 list
= g_slist_concat(list
, syslist
);
1874 list
= g_slist_sort(list
, (GCompareFunc
) utils_str_casecmp
);
1875 /* remove duplicates (next to each other after sorting) */
1876 foreach_slist(node
, list
)
1878 if (node
->next
&& utils_str_equal(node
->next
->data
, node
->data
))
1880 g_free(node
->next
->data
);
1881 utils_slist_remove_next(node
);
1889 /* Suffix can be NULL or a string which should be appended to the Help URL like
1890 * an anchor link, e.g. "#some_anchor". */
1891 gchar
*utils_get_help_url(const gchar
*suffix
)
1898 uri
= g_strconcat("file:///", app
->docdir
, "/Manual.html", NULL
);
1899 g_strdelimit(uri
, "\\", '/'); /* replace '\\' by '/' */
1902 uri
= g_strconcat("file://", app
->docdir
, "index.html", NULL
);
1905 if (! g_file_test(uri
+ skip
, G_FILE_TEST_IS_REGULAR
))
1906 { /* fall back to online documentation if it is not found on the hard disk */
1908 uri
= g_strconcat(GEANY_HOMEPAGE
, "manual/", VERSION
, "/index.html", NULL
);
1913 setptr(uri
, g_strconcat(uri
, suffix
, NULL
));
1920 static gboolean
str_in_array(const gchar
**haystack
, const gchar
*needle
)
1924 for (p
= haystack
; *p
!= NULL
; ++p
)
1926 if (utils_str_equal(*p
, needle
))
1934 * Copies the current environment into a new array.
1935 * @a exclude_vars is a @c NULL-terminated array of variable names which should be not copied.
1936 * All further arguments are key, value pairs of variables which should be added to
1939 * The argument list must be @c NULL-terminated.
1941 * @param exclude_vars @c NULL-terminated array of variable names to exclude.
1942 * @param first_varname Name of the first variable to copy into the new array.
1943 * @param ... Key-value pairs of variable names and values, @c NULL-terminated.
1945 * @return The new environment array.
1947 gchar
**utils_copy_environment(const gchar
**exclude_vars
, const gchar
*first_varname
, ...)
1953 const gchar
*key
, *value
;
1956 /* get all the environ variables */
1959 /* count the additional variables */
1960 va_start(args
, first_varname
);
1961 for (o
= 1; va_arg(args
, gchar
*) != NULL
; o
++);
1963 /* the passed arguments should be even (key, value pairs) */
1964 g_return_val_if_fail(o
% 2 == 0, NULL
);
1968 /* create an array large enough to hold the new environment */
1969 n
= g_strv_length(env
);
1970 /* 'n + o + 1' could leak a little bit when exclude_vars is set */
1971 result
= g_new(gchar
*, n
+ o
+ 1);
1973 /* copy the environment */
1974 for (n
= 0, p
= env
; *p
!= NULL
; ++p
)
1976 /* copy the variable */
1977 value
= g_getenv(*p
);
1978 if (G_LIKELY(value
!= NULL
))
1980 /* skip excluded variables */
1981 if (exclude_vars
!= NULL
&& str_in_array(exclude_vars
, *p
))
1984 result
[n
++] = g_strconcat(*p
, "=", value
, NULL
);
1989 /* now add additional variables */
1990 va_start(args
, first_varname
);
1991 key
= first_varname
;
1992 value
= va_arg(args
, gchar
*);
1995 result
[n
++] = g_strconcat(key
, "=", value
, NULL
);
1997 key
= va_arg(args
, gchar
*);
2000 value
= va_arg(args
, gchar
*);