Remove duplicate NEWS item
[geany-mirror.git] / src / utils.c
blob531a27f6b9c0b8e14352d0cf63bb3764fa3e186a
1 /*
2 * utils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 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.
23 * General utility functions, non-GTK related.
26 #include "geany.h"
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <math.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <stdarg.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
38 #endif
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
43 #include <glib/gstdio.h>
45 #include <gio/gio.h>
47 #include "prefs.h"
48 #include "support.h"
49 #include "document.h"
50 #include "filetypes.h"
51 #include "dialogs.h"
52 #include "win32.h"
53 #include "project.h"
54 #include "ui_utils.h"
56 #include "utils.h"
59 /**
60 * Tries to open the given URI in a browser.
61 * On Windows, the system's default browser is opened.
62 * On non-Windows systems, the browser command set in the preferences dialog is used. In case
63 * that fails or it is unset, the user is asked to correct or fill it.
65 * @param uri The URI to open in the web browser.
67 * @since 0.16
68 **/
69 void utils_open_browser(const gchar *uri)
71 #ifdef G_OS_WIN32
72 g_return_if_fail(uri != NULL);
73 win32_open_browser(uri);
74 #else
75 gboolean again = TRUE;
77 g_return_if_fail(uri != NULL);
79 while (again)
81 gchar *cmdline = g_strconcat(tool_prefs.browser_cmd, " \"", uri, "\"", NULL);
83 if (g_spawn_command_line_async(cmdline, NULL))
84 again = FALSE;
85 else
87 gchar *new_cmd = dialogs_show_input(_("Select Browser"), GTK_WINDOW(main_widgets.window),
88 _("Failed to spawn the configured browser command. "
89 "Please correct it or enter another one."),
90 tool_prefs.browser_cmd);
92 if (new_cmd == NULL) /* user canceled */
93 again = FALSE;
94 else
95 SETPTR(tool_prefs.browser_cmd, new_cmd);
97 g_free(cmdline);
99 #endif
103 /* taken from anjuta, to determine the EOL mode of the file */
104 gint utils_get_line_endings(const gchar* buffer, gsize size)
106 gsize i;
107 guint cr, lf, crlf, max_mode;
108 gint mode;
110 cr = lf = crlf = 0;
112 for (i = 0; i < size ; i++)
114 if (buffer[i] == 0x0a)
116 /* LF */
117 lf++;
119 else if (buffer[i] == 0x0d)
121 if (i >= (size - 1))
123 /* Last char, CR */
124 cr++;
126 else
128 if (buffer[i + 1] != 0x0a)
130 /* CR */
131 cr++;
133 else
135 /* CRLF */
136 crlf++;
138 i++;
143 /* Vote for the maximum */
144 mode = SC_EOL_LF;
145 max_mode = lf;
146 if (crlf > max_mode)
148 mode = SC_EOL_CRLF;
149 max_mode = crlf;
151 if (cr > max_mode)
153 mode = SC_EOL_CR;
154 max_mode = cr;
157 return mode;
161 gboolean utils_isbrace(gchar c, gboolean include_angles)
163 switch (c)
165 case '<':
166 case '>':
167 return include_angles;
169 case '(':
170 case ')':
171 case '{':
172 case '}':
173 case '[':
174 case ']': return TRUE;
175 default: return FALSE;
180 gboolean utils_is_opening_brace(gchar c, gboolean include_angles)
182 switch (c)
184 case '<':
185 return include_angles;
187 case '(':
188 case '{':
189 case '[': return TRUE;
190 default: return FALSE;
196 * Writes @a text into a file named @a filename.
197 * If the file doesn't exist, it will be created.
198 * If it already exists, it will be overwritten.
200 * @warning You should use @c g_file_set_contents() instead if you don't need
201 * file permissions and other metadata to be preserved, as that always handles
202 * disk exhaustion safely.
204 * @param filename The filename of the file to write, in locale encoding.
205 * @param text The text to write into the file.
207 * @return 0 if the file was successfully written, otherwise the @c errno of the
208 * failed operation is returned.
210 gint utils_write_file(const gchar *filename, const gchar *text)
212 g_return_val_if_fail(filename != NULL, ENOENT);
213 g_return_val_if_fail(text != NULL, EINVAL);
215 if (file_prefs.use_safe_file_saving)
217 GError *error = NULL;
218 if (! g_file_set_contents(filename, text, -1, &error))
220 geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message);
221 g_error_free(error);
222 return EIO;
225 else
227 FILE *fp;
228 gsize bytes_written, len;
229 gboolean fail = FALSE;
231 if (filename == NULL)
232 return ENOENT;
234 len = strlen(text);
235 errno = 0;
236 fp = g_fopen(filename, "w");
237 if (fp == NULL)
238 fail = TRUE;
239 else
241 bytes_written = fwrite(text, sizeof(gchar), len, fp);
243 if (len != bytes_written)
245 fail = TRUE;
246 geany_debug(
247 "utils_write_file(): written only %"G_GSIZE_FORMAT" bytes, had to write %"G_GSIZE_FORMAT" bytes to %s",
248 bytes_written, len, filename);
250 if (fclose(fp) != 0)
251 fail = TRUE;
253 if (fail)
255 geany_debug("utils_write_file(): could not write to file %s (%s)",
256 filename, g_strerror(errno));
257 return NVL(errno, EIO);
260 return 0;
264 /** Searches backward through @a size bytes looking for a '<'.
265 * @param sel .
266 * @param size .
267 * @return The tag name (newly allocated) or @c NULL if no opening tag was found.
269 gchar *utils_find_open_xml_tag(const gchar sel[], gint size)
271 const gchar *cur, *begin;
272 gsize len;
274 cur = utils_find_open_xml_tag_pos(sel, size);
275 if (cur == NULL)
276 return NULL;
278 cur++; /* skip the bracket */
279 begin = cur;
280 while (strchr(":_-.", *cur) || isalnum(*cur))
281 cur++;
283 len = (gsize)(cur - begin);
284 return len ? g_strndup(begin, len) : NULL;
288 /** Searches backward through @a size bytes looking for a '<'.
289 * @param sel .
290 * @param size .
291 * @return pointer to '<' of the found opening tag within @a sel, or @c NULL if no opening tag was found.
293 const gchar *utils_find_open_xml_tag_pos(const gchar sel[], gint size)
295 /* stolen from anjuta and modified */
296 const gchar *begin, *cur;
298 if (G_UNLIKELY(size < 3))
299 { /* Smallest tag is "<p>" which is 3 characters */
300 return NULL;
302 begin = &sel[0];
303 cur = &sel[size - 1];
305 /* Skip to the character before the closing brace */
306 while (cur > begin)
308 if (*cur == '>')
309 break;
310 --cur;
312 --cur;
313 /* skip whitespace */
314 while (cur > begin && isspace(*cur))
315 cur--;
316 if (*cur == '/')
317 return NULL; /* we found a short tag which doesn't need to be closed */
318 while (cur > begin)
320 if (*cur == '<')
321 break;
322 /* exit immediately if such non-valid XML/HTML is detected, e.g. "<script>if a >" */
323 else if (*cur == '>')
324 break;
325 --cur;
328 /* if the found tag is an opening, not a closing tag or empty <> */
329 if (*cur == '<' && *(cur + 1) != '/' && *(cur + 1) != '>')
330 return cur;
332 return NULL;
336 /* Returns true if tag_name is a self-closing tag */
337 gboolean utils_is_short_html_tag(const gchar *tag_name)
339 const gchar names[][20] = {
340 "area",
341 "base",
342 "basefont", /* < or not < */
343 "br",
344 "frame",
345 "hr",
346 "img",
347 "input",
348 "link",
349 "meta"
352 if (tag_name)
354 if (bsearch(tag_name, names, G_N_ELEMENTS(names), 20,
355 (GCompareFunc)g_ascii_strcasecmp))
356 return TRUE;
358 return FALSE;
362 const gchar *utils_get_eol_name(gint eol_mode)
364 switch (eol_mode)
366 case SC_EOL_CRLF: return _("Win (CRLF)"); break;
367 case SC_EOL_CR: return _("Mac (CR)"); break;
368 default: return _("Unix (LF)"); break;
373 const gchar *utils_get_eol_char(gint eol_mode)
375 switch (eol_mode)
377 case SC_EOL_CRLF: return "\r\n"; break;
378 case SC_EOL_CR: return "\r"; break;
379 default: return "\n"; break;
384 /* Converts line endings to @a target_eol_mode. */
385 void utils_ensure_same_eol_characters(GString *string, gint target_eol_mode)
387 const gchar *eol_str = utils_get_eol_char(target_eol_mode);
389 /* first convert data to LF only */
390 utils_string_replace_all(string, "\r\n", "\n");
391 utils_string_replace_all(string, "\r", "\n");
393 if (target_eol_mode == SC_EOL_LF)
394 return;
396 /* now convert to desired line endings */
397 utils_string_replace_all(string, "\n", eol_str);
401 gboolean utils_atob(const gchar *str)
403 if (G_UNLIKELY(str == NULL))
404 return FALSE;
405 else if (strcmp(str, "TRUE") == 0 || strcmp(str, "true") == 0)
406 return TRUE;
407 return FALSE;
411 /* NULL-safe version of g_path_is_absolute(). */
412 gboolean utils_is_absolute_path(const gchar *path)
414 if (G_UNLIKELY(! NZV(path)))
415 return FALSE;
417 return g_path_is_absolute(path);
421 /* Skips root if path is absolute, do nothing otherwise.
422 * This is a relative-safe version of g_path_skip_root().
424 const gchar *utils_path_skip_root(const gchar *path)
426 const gchar *path_relative;
428 path_relative = g_path_skip_root(path);
430 return (path_relative != NULL) ? path_relative : path;
434 gdouble utils_scale_round(gdouble val, gdouble factor)
436 /*val = floor(val * factor + 0.5);*/
437 val = floor(val);
438 val = MAX(val, 0);
439 val = MIN(val, factor);
441 return val;
445 /* like g_utf8_strdown() but if @str is not valid UTF8, convert it from locale first.
446 * returns NULL on charset conversion failure */
447 static gchar *utf8_strdown(const gchar *str)
449 gchar *down;
451 if (g_utf8_validate(str, -1, NULL))
452 down = g_utf8_strdown(str, -1);
453 else
455 down = g_locale_to_utf8(str, -1, NULL, NULL, NULL);
456 if (down)
457 SETPTR(down, g_utf8_strdown(down, -1));
460 return down;
465 * A replacement function for g_strncasecmp() to compare strings case-insensitive.
466 * It converts both strings into lowercase using g_utf8_strdown() and then compare
467 * both strings using strcmp().
468 * This is not completely accurate regarding locale-specific case sorting rules
469 * but seems to be a good compromise between correctness and performance.
471 * The input strings should be in UTF-8 or locale encoding.
473 * @param s1 Pointer to first string or @c NULL.
474 * @param s2 Pointer to second string or @c NULL.
476 * @return an integer less than, equal to, or greater than zero if @a s1 is found, respectively,
477 * to be less than, to match, or to be greater than @a s2.
479 * @since 0.16
481 gint utils_str_casecmp(const gchar *s1, const gchar *s2)
483 gchar *tmp1, *tmp2;
484 gint result;
486 g_return_val_if_fail(s1 != NULL, 1);
487 g_return_val_if_fail(s2 != NULL, -1);
489 /* ensure strings are UTF-8 and lowercase */
490 tmp1 = utf8_strdown(s1);
491 if (! tmp1)
492 return 1;
493 tmp2 = utf8_strdown(s2);
494 if (! tmp2)
496 g_free(tmp1);
497 return -1;
500 /* compare */
501 result = strcmp(tmp1, tmp2);
503 g_free(tmp1);
504 g_free(tmp2);
505 return result;
510 * Truncates the input string to a given length.
511 * Characters are removed from the middle of the string, so the start and the end of string
512 * won't change.
514 * @param string Input string.
515 * @param truncate_length The length in characters of the resulting string.
517 * @return A copy of @a string which is truncated to @a truncate_length characters,
518 * should be freed when no longer needed.
520 * @since 0.17
522 /* This following function is taken from Gedit. */
523 gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length)
525 GString *truncated;
526 guint length;
527 guint n_chars;
528 guint num_left_chars;
529 guint right_offset;
530 guint delimiter_length;
531 const gchar *delimiter = "\342\200\246";
533 g_return_val_if_fail(string != NULL, NULL);
535 length = strlen(string);
537 g_return_val_if_fail(g_utf8_validate(string, length, NULL), NULL);
539 /* It doesnt make sense to truncate strings to less than the size of the delimiter plus 2
540 * characters (one on each side) */
541 delimiter_length = g_utf8_strlen(delimiter, -1);
542 if (truncate_length < (delimiter_length + 2))
543 return g_strdup(string);
545 n_chars = g_utf8_strlen(string, length);
547 /* Make sure the string is not already small enough. */
548 if (n_chars <= truncate_length)
549 return g_strdup (string);
551 /* Find the 'middle' where the truncation will occur. */
552 num_left_chars = (truncate_length - delimiter_length) / 2;
553 right_offset = n_chars - truncate_length + num_left_chars + delimiter_length;
555 truncated = g_string_new_len(string, g_utf8_offset_to_pointer(string, num_left_chars) - string);
556 g_string_append(truncated, delimiter);
557 g_string_append(truncated, g_utf8_offset_to_pointer(string, right_offset));
559 return g_string_free(truncated, FALSE);
564 * @c NULL-safe string comparison. Returns @c TRUE if both @a a and @a b are @c NULL
565 * or if @a a and @a b refer to valid strings which are equal.
567 * @param a Pointer to first string or @c NULL.
568 * @param b Pointer to second string or @c NULL.
570 * @return @c TRUE if @a a equals @a b, else @c FALSE.
572 gboolean utils_str_equal(const gchar *a, const gchar *b)
574 /* (taken from libexo from os-cillation) */
575 if (a == NULL && b == NULL) return TRUE;
576 else if (a == NULL || b == NULL) return FALSE;
578 while (*a == *b++)
579 if (*a++ == '\0')
580 return TRUE;
582 return FALSE;
587 * Removes the extension from @a filename and return the result in a newly allocated string.
589 * @param filename The filename to operate on.
591 * @return A newly-allocated string, should be freed when no longer needed.
593 gchar *utils_remove_ext_from_filename(const gchar *filename)
595 gchar *last_dot;
596 gchar *result;
597 gsize len;
599 g_return_val_if_fail(filename != NULL, NULL);
601 last_dot = strrchr(filename, '.');
602 if (! last_dot)
603 return g_strdup(filename);
605 len = (gsize) (last_dot - filename);
606 result = g_malloc(len + 1);
607 memcpy(result, filename, len);
608 result[len] = 0;
610 return result;
614 gchar utils_brace_opposite(gchar ch)
616 switch (ch)
618 case '(': return ')';
619 case ')': return '(';
620 case '[': return ']';
621 case ']': return '[';
622 case '{': return '}';
623 case '}': return '{';
624 case '<': return '>';
625 case '>': return '<';
626 default: return '\0';
631 gchar *utils_get_hostname(void)
633 #ifdef G_OS_WIN32
634 return win32_get_hostname();
635 #elif defined(HAVE_GETHOSTNAME)
636 gchar hostname[100];
637 if (gethostname(hostname, sizeof(hostname)) == 0)
638 return g_strdup(hostname);
639 #endif
640 return g_strdup("localhost");
644 /* Checks whether the given file can be written. locale_filename is expected in locale encoding.
645 * Returns 0 if it can be written, otherwise it returns errno */
646 gint utils_is_file_writable(const gchar *locale_filename)
648 gchar *file;
649 gint ret;
651 if (! g_file_test(locale_filename, G_FILE_TEST_EXISTS) &&
652 ! g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
653 /* get the file's directory to check for write permission if it doesn't yet exist */
654 file = g_path_get_dirname(locale_filename);
655 else
656 file = g_strdup(locale_filename);
658 #ifdef G_OS_WIN32
659 /* use _waccess on Windows, access() doesn't accept special characters */
660 ret = win32_check_write_permission(file);
661 #else
663 /* access set also errno to "FILE NOT FOUND" even if locale_filename is writeable, so use
664 * errno only when access() explicitly returns an error */
665 if (access(file, R_OK | W_OK) != 0)
666 ret = errno;
667 else
668 ret = 0;
669 #endif
670 g_free(file);
671 return ret;
675 /* Replaces all occurrences of needle in haystack with replacement.
676 * Warning: *haystack must be a heap address; it may be freed and reassigned.
677 * Note: utils_string_replace_all() will always be faster when @a replacement is longer
678 * than @a needle.
679 * All strings have to be NULL-terminated.
680 * See utils_string_replace_all() for details. */
681 void utils_str_replace_all(gchar **haystack, const gchar *needle, const gchar *replacement)
683 GString *str;
685 g_return_if_fail(*haystack != NULL);
687 str = g_string_new(*haystack);
689 g_free(*haystack);
690 utils_string_replace_all(str, needle, replacement);
692 *haystack = g_string_free(str, FALSE);
696 gint utils_strpos(const gchar *haystack, const gchar *needle)
698 const gchar *sub;
700 if (! *needle)
701 return -1;
703 sub = strstr(haystack, needle);
704 if (! sub)
705 return -1;
707 return sub - haystack;
712 * Retrieves a formatted date/time string from strftime().
713 * This function should be preferred to directly calling strftime() since this function
714 * works on UTF-8 encoded strings.
716 * @param format The format string to pass to strftime(3). See the strftime(3)
717 * documentation for details, in UTF-8 encoding.
718 * @param time_to_use The date/time to use, in time_t format or NULL to use the current time.
720 * @return A newly-allocated string, should be freed when no longer needed.
722 * @since 0.16
724 gchar *utils_get_date_time(const gchar *format, time_t *time_to_use)
726 const struct tm *tm;
727 static gchar date[1024];
728 gchar *locale_format;
729 gsize len;
731 g_return_val_if_fail(format != NULL, NULL);
733 if (! g_utf8_validate(format, -1, NULL))
735 locale_format = g_locale_from_utf8(format, -1, NULL, NULL, NULL);
736 if (locale_format == NULL)
737 return NULL;
739 else
740 locale_format = g_strdup(format);
742 if (time_to_use != NULL)
743 tm = localtime(time_to_use);
744 else
746 time_t tp = time(NULL);
747 tm = localtime(&tp);
750 len = strftime(date, 1024, locale_format, tm);
751 g_free(locale_format);
752 if (len == 0)
753 return NULL;
755 if (! g_utf8_validate(date, len, NULL))
756 return g_locale_to_utf8(date, len, NULL, NULL, NULL);
757 else
758 return g_strdup(date);
762 gchar *utils_get_initials(const gchar *name)
764 gint i = 1, j = 1;
765 gchar *initials = g_malloc0(5);
767 initials[0] = name[0];
768 while (name[i] != '\0' && j < 4)
770 if (name[i] == ' ' && name[i + 1] != ' ')
772 initials[j++] = name[i + 1];
774 i++;
776 return initials;
781 * Wraps g_key_file_get_integer() to add a default value argument.
783 * @param config A GKeyFile object.
784 * @param section The group name to look in for the key.
785 * @param key The key to find.
786 * @param default_value The default value which will be returned when @a section or @a key
787 * don't exist.
789 * @return The value associated with @a key as an integer, or the given default value if the value
790 * could not be retrieved.
792 gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key,
793 const gint default_value)
795 gint tmp;
796 GError *error = NULL;
798 g_return_val_if_fail(config, default_value);
800 tmp = g_key_file_get_integer(config, section, key, &error);
801 if (error)
803 g_error_free(error);
804 return default_value;
806 return tmp;
811 * Wraps g_key_file_get_boolean() to add a default value argument.
813 * @param config A GKeyFile object.
814 * @param section The group name to look in for the key.
815 * @param key The key to find.
816 * @param default_value The default value which will be returned when @c section or @c key
817 * don't exist.
819 * @return The value associated with @a key as a boolean, or the given default value if the value
820 * could not be retrieved.
822 gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key,
823 const gboolean default_value)
825 gboolean tmp;
826 GError *error = NULL;
828 g_return_val_if_fail(config, default_value);
830 tmp = g_key_file_get_boolean(config, section, key, &error);
831 if (error)
833 g_error_free(error);
834 return default_value;
836 return tmp;
841 * Wraps g_key_file_get_string() to add a default value argument.
843 * @param config A GKeyFile object.
844 * @param section The group name to look in for the key.
845 * @param key The key to find.
846 * @param default_value The default value which will be returned when @a section or @a key
847 * don't exist.
849 * @return A newly allocated string, either the value for @a key or a copy of the given
850 * default value if it could not be retrieved.
852 gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key,
853 const gchar *default_value)
855 gchar *tmp;
857 g_return_val_if_fail(config, g_strdup(default_value));
859 tmp = g_key_file_get_string(config, section, key, NULL);
860 if (!tmp)
862 return g_strdup(default_value);
864 return tmp;
868 gchar *utils_get_hex_from_color(GdkColor *color)
870 gchar *buffer = g_malloc0(9);
872 g_return_val_if_fail(color != NULL, NULL);
874 g_snprintf(buffer, 8, "#%02X%02X%02X",
875 (guint) (utils_scale_round(color->red / 256, 255)),
876 (guint) (utils_scale_round(color->green / 256, 255)),
877 (guint) (utils_scale_round(color->blue / 256, 255)));
879 return buffer;
883 guint utils_invert_color(guint color)
885 guint r, g, b;
887 r = 0xffffff - color;
888 g = 0xffffff - (color >> 8);
889 b = 0xffffff - (color >> 16);
891 return (r | (g << 8) | (b << 16));
895 /* Get directory from current file in the notebook.
896 * Returns dir string that should be freed or NULL, depending on whether current file is valid.
897 * Returned string is in UTF-8 encoding */
898 gchar *utils_get_current_file_dir_utf8(void)
900 GeanyDocument *doc = document_get_current();
902 if (doc != NULL)
904 /* get current filename */
905 const gchar *cur_fname = doc->file_name;
907 if (cur_fname != NULL)
909 /* get folder part from current filename */
910 return g_path_get_dirname(cur_fname); /* returns "." if no path */
914 return NULL; /* no file open */
918 /* very simple convenience function */
919 void utils_beep(void)
921 if (prefs.beep_on_errors)
922 gdk_beep();
926 /* taken from busybox, thanks */
927 gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
928 gulong display_unit)
930 /* The code will adjust for additional (appended) units. */
931 static const gchar zero_and_units[] = { '0', 0, 'K', 'M', 'G', 'T' };
932 static const gchar fmt[] = "%Lu %c%c";
933 static const gchar fmt_tenths[] = "%Lu.%d %c%c";
935 guint64 val;
936 gint frac;
937 const gchar *u;
938 const gchar *f;
940 u = zero_and_units;
941 f = fmt;
942 frac = 0;
944 val = size * block_size;
945 if (val == 0)
946 return g_strdup(u);
948 if (display_unit)
950 val += display_unit/2; /* Deal with rounding. */
951 val /= display_unit; /* Don't combine with the line above!!! */
953 else
955 ++u;
956 while ((val >= 1024) && (u < zero_and_units + sizeof(zero_and_units) - 1))
958 f = fmt_tenths;
959 ++u;
960 frac = ((((gint)(val % 1024)) * 10) + (1024 / 2)) / 1024;
961 val /= 1024;
963 if (frac >= 10)
964 { /* We need to round up here. */
965 ++val;
966 frac = 0;
970 /* If f==fmt then 'frac' and 'u' are ignored. */
971 return g_strdup_printf(f, val, frac, *u, 'b');
975 static guint utils_get_value_of_hex(const gchar ch)
977 if (ch >= '0' && ch <= '9')
978 return ch - '0';
979 else if (ch >= 'A' && ch <= 'F')
980 return ch - 'A' + 10;
981 else if (ch >= 'a' && ch <= 'f')
982 return ch - 'a' + 10;
983 else
984 return 0;
988 /* utils_strtod() converts a string containing a hex colour ("0x00ff00") into an integer.
989 * Basically, it is the same as strtod() would do, but it does not understand hex colour values,
990 * before ANSI-C99. With with_route set, it takes strings of the format "#00ff00".
991 * Returns -1 on failure. */
992 gint utils_strtod(const gchar *source, gchar **end, gboolean with_route)
994 guint red, green, blue, offset = 0;
996 g_return_val_if_fail(source != NULL, -1);
998 if (with_route && (strlen(source) != 7 || source[0] != '#'))
999 return -1;
1000 else if (! with_route && (strlen(source) != 8 || source[0] != '0' ||
1001 (source[1] != 'x' && source[1] != 'X')))
1003 return -1;
1006 /* offset is set to 1 when the string starts with 0x, otherwise it starts with #
1007 * and we don't need to increase the index */
1008 if (! with_route)
1009 offset = 1;
1011 red = utils_get_value_of_hex(
1012 source[1 + offset]) * 16 + utils_get_value_of_hex(source[2 + offset]);
1013 green = utils_get_value_of_hex(
1014 source[3 + offset]) * 16 + utils_get_value_of_hex(source[4 + offset]);
1015 blue = utils_get_value_of_hex(
1016 source[5 + offset]) * 16 + utils_get_value_of_hex(source[6 + offset]);
1018 return (red | (green << 8) | (blue << 16));
1022 /* Returns: newly allocated string with the current time formatted HH:MM:SS. */
1023 gchar *utils_get_current_time_string(void)
1025 const time_t tp = time(NULL);
1026 const struct tm *tmval = localtime(&tp);
1027 gchar *result = g_malloc0(9);
1029 strftime(result, 9, "%H:%M:%S", tmval);
1030 result[8] = '\0';
1031 return result;
1035 GIOChannel *utils_set_up_io_channel(
1036 gint fd, GIOCondition cond, gboolean nblock, GIOFunc func, gpointer data)
1038 GIOChannel *ioc;
1039 /*const gchar *encoding;*/
1041 #ifdef G_OS_WIN32
1042 ioc = g_io_channel_win32_new_fd(fd);
1043 #else
1044 ioc = g_io_channel_unix_new(fd);
1045 #endif
1047 if (nblock)
1048 g_io_channel_set_flags(ioc, G_IO_FLAG_NONBLOCK, NULL);
1050 g_io_channel_set_encoding(ioc, NULL, NULL);
1052 if (! g_get_charset(&encoding))
1053 { // hope this works reliably
1054 GError *error = NULL;
1055 g_io_channel_set_encoding(ioc, encoding, &error);
1056 if (error)
1058 geany_debug("%s: %s", G_STRFUNC, error->message);
1059 g_error_free(error);
1060 return ioc;
1064 /* "auto-close" ;-) */
1065 g_io_channel_set_close_on_unref(ioc, TRUE);
1067 g_io_add_watch(ioc, cond, func, data);
1068 g_io_channel_unref(ioc);
1070 return ioc;
1074 gchar **utils_read_file_in_array(const gchar *filename)
1076 gchar **result = NULL;
1077 gchar *data;
1079 g_return_val_if_fail(filename != NULL, NULL);
1081 g_file_get_contents(filename, &data, NULL, NULL);
1083 if (data != NULL)
1085 result = g_strsplit_set(data, "\r\n", -1);
1086 g_free(data);
1089 return result;
1093 /* Contributed by Stefan Oltmanns, thanks.
1094 * Replaces \\, \r, \n, \t and \uXXX by their real counterparts.
1095 * keep_backslash is used for regex strings to leave '\\' and '\?' in place */
1096 gboolean utils_str_replace_escape(gchar *string, gboolean keep_backslash)
1098 gsize i, j, len;
1099 guint unicodechar;
1101 g_return_val_if_fail(string != NULL, FALSE);
1103 j = 0;
1104 len = strlen(string);
1105 for (i = 0; i < len; i++)
1107 if (string[i]=='\\')
1109 if (i++ >= strlen(string))
1111 return FALSE;
1113 switch (string[i])
1115 case '\\':
1116 if (keep_backslash)
1117 string[j++] = '\\';
1118 string[j] = '\\';
1119 break;
1120 case 'n':
1121 string[j] = '\n';
1122 break;
1123 case 'r':
1124 string[j] = '\r';
1125 break;
1126 case 't':
1127 string[j] = '\t';
1128 break;
1129 #if 0
1130 case 'x': /* Warning: May produce illegal utf-8 string! */
1131 i += 2;
1132 if (i >= strlen(string))
1134 return FALSE;
1136 if (isdigit(string[i - 1])) string[j] = string[i - 1] - 48;
1137 else if (isxdigit(string[i - 1])) string[j] = tolower(string[i - 1])-87;
1138 else return FALSE;
1139 string[j] <<= 4;
1140 if (isdigit(string[i])) string[j] |= string[i] - 48;
1141 else if (isxdigit(string[i])) string[j] |= tolower(string[i])-87;
1142 else return FALSE;
1143 break;
1144 #endif
1145 case 'u':
1147 i += 2;
1148 if (i >= strlen(string))
1150 return FALSE;
1152 if (isdigit(string[i - 1])) unicodechar = string[i - 1] - 48;
1153 else if (isxdigit(string[i - 1])) unicodechar = tolower(string[i - 1])-87;
1154 else return FALSE;
1155 unicodechar <<= 4;
1156 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1157 else if (isxdigit(string[i])) unicodechar |= tolower(string[i])-87;
1158 else return FALSE;
1159 if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
1160 && (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
1162 i += 2;
1163 unicodechar <<= 8;
1164 if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
1165 else unicodechar |= ((tolower(string[i - 1])-87) << 4);
1166 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1167 else unicodechar |= tolower(string[i])-87;
1169 if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
1170 && (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
1172 i += 2;
1173 unicodechar <<= 8;
1174 if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
1175 else unicodechar |= ((tolower(string[i - 1])-87) << 4);
1176 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1177 else unicodechar |= tolower(string[i])-87;
1179 if (unicodechar < 0x80)
1181 string[j] = unicodechar;
1183 else if (unicodechar < 0x800)
1185 string[j] = (unsigned char) ((unicodechar >> 6) | 0xC0);
1186 j++;
1187 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1189 else if (unicodechar < 0x10000)
1191 string[j] = (unsigned char) ((unicodechar >> 12) | 0xE0);
1192 j++;
1193 string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
1194 j++;
1195 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1197 else if (unicodechar < 0x110000) /* more chars are not allowed in unicode */
1199 string[j] = (unsigned char) ((unicodechar >> 18) | 0xF0);
1200 j++;
1201 string[j] = (unsigned char) (((unicodechar >> 12) & 0x3F) | 0x80);
1202 j++;
1203 string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
1204 j++;
1205 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1207 else
1209 return FALSE;
1211 break;
1213 default:
1214 /* unnecessary escapes are allowed */
1215 if (keep_backslash)
1216 string[j++] = '\\';
1217 string[j] = string[i];
1220 else
1222 string[j] = string[i];
1224 j++;
1226 while (j < i)
1228 string[j] = 0;
1229 j++;
1231 return TRUE;
1235 /* Wraps a string in place, replacing a space with a newline character.
1236 * wrapstart is the minimum position to start wrapping or -1 for default */
1237 gboolean utils_wrap_string(gchar *string, gint wrapstart)
1239 gchar *pos, *linestart;
1240 gboolean ret = FALSE;
1242 if (wrapstart < 0)
1243 wrapstart = 80;
1245 for (pos = linestart = string; *pos != '\0'; pos++)
1247 if (pos - linestart >= wrapstart && *pos == ' ')
1249 *pos = '\n';
1250 linestart = pos;
1251 ret = TRUE;
1254 return ret;
1259 * Converts the given UTF-8 encoded string into locale encoding.
1260 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1262 * @param utf8_text UTF-8 encoded text.
1264 * @return The converted string in locale encoding, or a copy of the input string if conversion
1265 * failed. Should be freed with g_free(). If @a utf8_text is @c NULL, @c NULL is returned.
1267 gchar *utils_get_locale_from_utf8(const gchar *utf8_text)
1269 #ifdef G_OS_WIN32
1270 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1271 * which would result in wrongly converted strings */
1272 return g_strdup(utf8_text);
1273 #else
1274 gchar *locale_text;
1276 if (! utf8_text)
1277 return NULL;
1278 locale_text = g_locale_from_utf8(utf8_text, -1, NULL, NULL, NULL);
1279 if (locale_text == NULL)
1280 locale_text = g_strdup(utf8_text);
1281 return locale_text;
1282 #endif
1287 * Converts the given string (in locale encoding) into UTF-8 encoding.
1288 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1290 * @param locale_text Text in locale encoding.
1292 * @return The converted string in UTF-8 encoding, or a copy of the input string if conversion
1293 * failed. Should be freed with g_free(). If @a locale_text is @c NULL, @c NULL is returned.
1295 gchar *utils_get_utf8_from_locale(const gchar *locale_text)
1297 #ifdef G_OS_WIN32
1298 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1299 * which would result in wrongly converted strings */
1300 return g_strdup(locale_text);
1301 #else
1302 gchar *utf8_text;
1304 if (! locale_text)
1305 return NULL;
1306 utf8_text = g_locale_to_utf8(locale_text, -1, NULL, NULL, NULL);
1307 if (utf8_text == NULL)
1308 utf8_text = g_strdup(locale_text);
1309 return utf8_text;
1310 #endif
1314 /* Pass pointers to free after arg_count.
1315 * The last argument must be NULL as an extra check that arg_count is correct. */
1316 void utils_free_pointers(gsize arg_count, ...)
1318 va_list a;
1319 gsize i;
1320 gpointer ptr;
1322 va_start(a, arg_count);
1323 for (i = 0; i < arg_count; i++)
1325 ptr = va_arg(a, gpointer);
1326 g_free(ptr);
1328 ptr = va_arg(a, gpointer);
1329 if (ptr)
1330 g_warning("Wrong arg_count!");
1331 va_end(a);
1335 /* currently unused */
1336 #if 0
1337 /* Creates a string array deep copy of a series of non-NULL strings.
1338 * The first argument is nothing special.
1339 * The list must be ended with NULL.
1340 * If first is NULL, NULL is returned. */
1341 gchar **utils_strv_new(const gchar *first, ...)
1343 gsize strvlen, i;
1344 va_list args;
1345 gchar *str;
1346 gchar **strv;
1348 g_return_val_if_fail(first != NULL, NULL);
1350 strvlen = 1; /* for first argument */
1352 /* count other arguments */
1353 va_start(args, first);
1354 for (; va_arg(args, gchar*) != NULL; strvlen++);
1355 va_end(args);
1357 strv = g_new(gchar*, strvlen + 1); /* +1 for NULL terminator */
1358 strv[0] = g_strdup(first);
1360 va_start(args, first);
1361 for (i = 1; str = va_arg(args, gchar*), str != NULL; i++)
1363 strv[i] = g_strdup(str);
1365 va_end(args);
1367 strv[i] = NULL;
1368 return strv;
1370 #endif
1374 * Creates a directory if it doesn't already exist.
1375 * Creates intermediate parent directories as needed, too.
1376 * The permissions of the created directory are set 0700.
1378 * @param path The path of the directory to create, in locale encoding.
1379 * @param create_parent_dirs Whether to create intermediate parent directories if necessary.
1381 * @return 0 if the directory was successfully created, otherwise the @c errno of the
1382 * failed operation is returned.
1384 gint utils_mkdir(const gchar *path, gboolean create_parent_dirs)
1386 gint mode = 0700;
1387 gint result;
1389 if (path == NULL || strlen(path) == 0)
1390 return EFAULT;
1392 result = (create_parent_dirs) ? g_mkdir_with_parents(path, mode) : g_mkdir(path, mode);
1393 if (result != 0)
1394 return errno;
1395 return 0;
1400 * Gets a list of files from the specified directory.
1401 * Locale encoding is expected for @a path and used for the file list. The list and the data
1402 * in the list should be freed after use, e.g.:
1403 * @code
1404 * g_slist_foreach(list, (GFunc) g_free, NULL);
1405 * g_slist_free(list); @endcode
1407 * @note If you don't need a list you should use the foreach_dir() macro instead -
1408 * it's more efficient.
1410 * @param path The path of the directory to scan, in locale encoding.
1411 * @param full_path Whether to include the full path for each filename in the list. Obviously this
1412 * will use more memory.
1413 * @param sort Whether to sort alphabetically (UTF-8 safe).
1414 * @param error The location for storing a possible error, or @c NULL.
1416 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1417 * freed when no longer needed.
1418 * @see utils_get_file_list().
1420 GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error)
1422 GSList *list = NULL;
1423 GDir *dir;
1424 const gchar *filename;
1426 if (error)
1427 *error = NULL;
1428 g_return_val_if_fail(path != NULL, NULL);
1430 dir = g_dir_open(path, 0, error);
1431 if (dir == NULL)
1432 return NULL;
1434 foreach_dir(filename, dir)
1436 list = g_slist_prepend(list, full_path ?
1437 g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL) : g_strdup(filename));
1439 g_dir_close(dir);
1440 /* sorting last is quicker than on insertion */
1441 if (sort)
1442 list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
1443 else
1444 list = g_slist_reverse(list);
1445 return list;
1450 * Gets a sorted list of files from the specified directory.
1451 * Locale encoding is expected for @a path and used for the file list. The list and the data
1452 * in the list should be freed after use, e.g.:
1453 * @code
1454 * g_slist_foreach(list, (GFunc) g_free, NULL);
1455 * g_slist_free(list); @endcode
1457 * @param path The path of the directory to scan, in locale encoding.
1458 * @param length The location to store the number of non-@c NULL data items in the list,
1459 * unless @c NULL.
1460 * @param error The location for storing a possible error, or @c NULL.
1462 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1463 * freed when no longer needed.
1464 * @see utils_get_file_list_full().
1466 GSList *utils_get_file_list(const gchar *path, guint *length, GError **error)
1468 GSList *list = utils_get_file_list_full(path, FALSE, TRUE, error);
1470 if (length)
1471 *length = g_slist_length(list);
1472 return list;
1476 /* returns TRUE if any letter in str is a capital, FALSE otherwise. Should be Unicode safe. */
1477 gboolean utils_str_has_upper(const gchar *str)
1479 gunichar c;
1481 if (! NZV(str) || ! g_utf8_validate(str, -1, NULL))
1482 return FALSE;
1484 while (*str != '\0')
1486 c = g_utf8_get_char(str);
1487 /* check only letters and stop once the first non-capital was found */
1488 if (g_unichar_isalpha(c) && g_unichar_isupper(c))
1489 return TRUE;
1490 /* FIXME don't write a const string */
1491 str = g_utf8_next_char(str);
1493 return FALSE;
1497 /* end can be -1 for haystack->len.
1498 * returns: position of found text or -1. */
1499 gint utils_string_find(GString *haystack, gint start, gint end, const gchar *needle)
1501 gint pos;
1503 g_return_val_if_fail(haystack != NULL, -1);
1504 if (haystack->len == 0)
1505 return -1;
1507 g_return_val_if_fail(start >= 0, -1);
1508 if (start >= (gint)haystack->len)
1509 return -1;
1511 g_return_val_if_fail(NZV(needle), -1);
1513 if (end < 0)
1514 end = haystack->len;
1516 pos = utils_strpos(haystack->str + start, needle);
1517 if (pos == -1)
1518 return -1;
1520 pos += start;
1521 if (pos >= end)
1522 return -1;
1523 return pos;
1527 /* Replaces @len characters from offset @a pos.
1528 * len can be -1 to replace the remainder of @a str.
1529 * returns: pos + strlen(replace). */
1530 gint utils_string_replace(GString *str, gint pos, gint len, const gchar *replace)
1532 g_string_erase(str, pos, len);
1533 if (replace)
1535 g_string_insert(str, pos, replace);
1536 pos += strlen(replace);
1538 return pos;
1543 * Replaces all occurrences of @a needle in @a haystack with @a replace.
1544 * As of Geany 0.16, @a replace can match @a needle, so the following will work:
1545 * @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
1547 * @param haystack The input string to operate on. This string is modified in place.
1548 * @param needle The string which should be replaced.
1549 * @param replace The replacement for @a needle.
1551 * @return Number of replacements made.
1553 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
1555 guint count = 0;
1556 gint pos = 0;
1557 gsize needle_length = strlen(needle);
1559 while (1)
1561 pos = utils_string_find(haystack, pos, -1, needle);
1563 if (pos == -1)
1564 break;
1566 pos = utils_string_replace(haystack, pos, needle_length, replace);
1567 count++;
1569 return count;
1574 * Replaces only the first occurrence of @a needle in @a haystack
1575 * with @a replace.
1576 * For details, see utils_string_replace_all().
1578 * @param haystack The input string to operate on. This string is modified in place.
1579 * @param needle The string which should be replaced.
1580 * @param replace The replacement for @a needle.
1582 * @return Number of replacements made.
1584 * @since 0.16
1586 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace)
1588 gint pos = utils_string_find(haystack, 0, -1, needle);
1590 if (pos == -1)
1591 return 0;
1593 utils_string_replace(haystack, pos, strlen(needle), replace);
1594 return 1;
1598 /* Similar to g_regex_replace but allows matching a subgroup.
1599 * match_num: which match to replace, 0 for whole match.
1600 * literal: FALSE to interpret escape sequences in @a replace.
1601 * returns: number of replacements.
1602 * bug: replaced text can affect matching of ^ or \b */
1603 guint utils_string_regex_replace_all(GString *haystack, GRegex *regex,
1604 guint match_num, const gchar *replace, gboolean literal)
1606 GMatchInfo *minfo;
1607 guint ret = 0;
1608 gint start = 0;
1610 g_assert(literal); /* escapes not implemented yet */
1611 g_return_val_if_fail(replace, 0);
1613 /* ensure haystack->str is not null */
1614 if (haystack->len == 0)
1615 return 0;
1617 /* passing a start position makes G_REGEX_MATCH_NOTBOL automatic */
1618 while (g_regex_match_full(regex, haystack->str, -1, start, 0, &minfo, NULL))
1620 gint end, len;
1622 g_match_info_fetch_pos(minfo, match_num, &start, &end);
1623 len = end - start;
1624 utils_string_replace(haystack, start, len, replace);
1625 ret++;
1627 /* skip past whole match */
1628 g_match_info_fetch_pos(minfo, 0, NULL, &end);
1629 start = end - len + strlen(replace);
1630 g_match_info_free(minfo);
1632 g_match_info_free(minfo);
1633 return ret;
1637 /* Get project or default startup directory (if set), or NULL. */
1638 const gchar *utils_get_default_dir_utf8(void)
1640 if (app->project && NZV(app->project->base_path))
1642 return app->project->base_path;
1645 if (NZV(prefs.default_open_path))
1647 return prefs.default_open_path;
1649 return NULL;
1653 static gboolean check_error(GError **error)
1655 if (error != NULL && *error != NULL)
1657 /* imitate the GLib warning */
1658 g_warning(
1659 "GError set over the top of a previous GError or uninitialized memory.\n"
1660 "This indicates a bug in someone's code. You must ensure an error is NULL "
1661 "before it's set.");
1662 /* after returning the code may segfault, but we don't care because we should
1663 * make sure *error is NULL */
1664 return FALSE;
1666 return TRUE;
1671 * Wraps g_spawn_sync() and internally calls this function on Unix-like
1672 * systems. On Win32 platforms, it uses the Windows API.
1674 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1675 * @param argv The child's argument vector.
1676 * @param env The child's environment, or @a NULL to inherit parent's.
1677 * @param flags Flags from GSpawnFlags.
1678 * @param child_setup A function to run in the child just before exec().
1679 * @param user_data The user data for child_setup.
1680 * @param std_out The return location for child output.
1681 * @param std_err The return location for child error messages.
1682 * @param exit_status The child exit status, as returned by waitpid().
1683 * @param error The return location for error or @a NULL.
1685 * @return @c TRUE on success, @c FALSE if an error was set.
1687 gboolean utils_spawn_sync(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
1688 GSpawnChildSetupFunc child_setup, gpointer user_data, gchar **std_out,
1689 gchar **std_err, gint *exit_status, GError **error)
1691 gboolean result;
1693 if (! check_error(error))
1694 return FALSE;
1696 if (argv == NULL)
1698 *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "argv must not be NULL");
1699 return FALSE;
1702 if (std_out)
1703 *std_out = NULL;
1705 if (std_err)
1706 *std_err = NULL;
1708 #ifdef G_OS_WIN32
1709 result = win32_spawn(dir, argv, env, flags, std_out, std_err, exit_status, error);
1710 #else
1711 result = g_spawn_sync(dir, argv, env, flags, NULL, NULL, std_out, std_err, exit_status, error);
1712 #endif
1714 return result;
1719 * Wraps g_spawn_async() and internally calls this function on Unix-like
1720 * systems. On Win32 platforms, it uses the Windows API.
1722 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1723 * @param argv The child's argument vector.
1724 * @param env The child's environment, or @a NULL to inherit parent's.
1725 * @param flags Flags from GSpawnFlags.
1726 * @param child_setup A function to run in the child just before exec().
1727 * @param user_data The user data for child_setup.
1728 * @param child_pid The return location for child process ID, or NULL.
1729 * @param error The return location for error or @a NULL.
1731 * @return @c TRUE on success, @c FALSE if an error was set.
1733 gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
1734 GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
1735 GError **error)
1737 gboolean result;
1739 if (! check_error(error))
1740 return FALSE;
1742 if (argv == NULL)
1744 *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "argv must not be NULL");
1745 return FALSE;
1748 #ifdef G_OS_WIN32
1749 result = win32_spawn(dir, argv, env, flags, NULL, NULL, NULL, error);
1750 #else
1751 result = g_spawn_async(dir, argv, env, flags, NULL, NULL, child_pid, error);
1752 #endif
1753 return result;
1757 /* Retrieves the path for the given URI.
1758 * It returns:
1759 * - the path which was determined by g_filename_from_uri() or GIO
1760 * - NULL if the URI is non-local and gvfs-fuse is not installed
1761 * - a new copy of 'uri' if it is not an URI. */
1762 gchar *utils_get_path_from_uri(const gchar *uri)
1764 gchar *locale_filename;
1766 g_return_val_if_fail(uri != NULL, NULL);
1768 if (! utils_is_uri(uri))
1769 return g_strdup(uri);
1771 /* this will work only for 'file://' URIs */
1772 locale_filename = g_filename_from_uri(uri, NULL, NULL);
1773 /* g_filename_from_uri() failed, so we probably have a non-local URI */
1774 if (locale_filename == NULL)
1776 GFile *file = g_file_new_for_uri(uri);
1777 locale_filename = g_file_get_path(file);
1778 g_object_unref(file);
1779 if (locale_filename == NULL)
1781 geany_debug("The URI '%s' could not be resolved to a local path. This means "
1782 "that the URI is invalid or that you don't have gvfs-fuse installed.", uri);
1786 return locale_filename;
1790 gboolean utils_is_uri(const gchar *uri)
1792 g_return_val_if_fail(uri != NULL, FALSE);
1794 return (strstr(uri, "://") != NULL);
1798 /* path should be in locale encoding */
1799 gboolean utils_is_remote_path(const gchar *path)
1801 g_return_val_if_fail(path != NULL, FALSE);
1803 /* if path is an URI and it doesn't start "file://", we take it as remote */
1804 if (utils_is_uri(path) && strncmp(path, "file:", 5) != 0)
1805 return TRUE;
1807 #ifndef G_OS_WIN32
1809 static gchar *fuse_path = NULL;
1810 static gsize len = 0;
1812 if (G_UNLIKELY(fuse_path == NULL))
1814 fuse_path = g_build_filename(g_get_home_dir(), ".gvfs", NULL);
1815 len = strlen(fuse_path);
1817 /* Comparing the file path against a hardcoded path is not the most elegant solution
1818 * but for now it is better than nothing. Ideally, g_file_new_for_path() should create
1819 * proper GFile objects for Fuse paths, but it only does in future GVFS
1820 * versions (gvfs 1.1.1). */
1821 return (strncmp(path, fuse_path, len) == 0);
1823 #endif
1825 return FALSE;
1829 /* Remove all relative and untidy elements from the path of @a filename.
1830 * @param filename must be a valid absolute path.
1831 * @see tm_get_real_path() - also resolves links. */
1832 void utils_tidy_path(gchar *filename)
1834 GString *str = g_string_new(filename);
1835 const gchar *c, *needle;
1836 gchar *tmp;
1837 gssize pos;
1838 gboolean preserve_double_backslash = FALSE;
1840 g_return_if_fail(g_path_is_absolute(filename));
1842 if (str->len >= 2 && strncmp(str->str, "\\\\", 2) == 0)
1843 preserve_double_backslash = TRUE;
1845 #ifdef G_OS_WIN32
1846 /* using MSYS we can get Unix-style separators */
1847 utils_string_replace_all(str, "/", G_DIR_SEPARATOR_S);
1848 #endif
1849 /* replace "/./" and "//" */
1850 utils_string_replace_all(str, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
1851 utils_string_replace_all(str, G_DIR_SEPARATOR_S G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
1853 if (preserve_double_backslash)
1854 g_string_prepend(str, "\\");
1856 /* replace "/../" */
1857 needle = G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S;
1858 while (1)
1860 c = strstr(str->str, needle);
1861 if (c == NULL)
1862 break;
1863 else
1865 pos = c - str->str;
1866 if (pos <= 3)
1867 break; /* bad path */
1869 /* replace "/../" */
1870 g_string_erase(str, pos, strlen(needle));
1871 g_string_insert_c(str, pos, G_DIR_SEPARATOR);
1873 tmp = g_strndup(str->str, pos); /* path up to "/../" */
1874 c = g_strrstr(tmp, G_DIR_SEPARATOR_S);
1875 g_return_if_fail(c);
1877 pos = c - tmp; /* position of previous "/" */
1878 g_string_erase(str, pos, strlen(c));
1879 g_free(tmp);
1882 g_return_if_fail(strlen(str->str) <= strlen(filename));
1883 strcpy(filename, str->str);
1884 g_string_free(str, TRUE);
1889 * Removes characters from a string, in place.
1891 * @param string String to search.
1892 * @param chars Characters to remove.
1894 * @return @a string - return value is only useful when nesting function calls, e.g.:
1895 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
1897 * @see @c g_strdelimit.
1899 gchar *utils_str_remove_chars(gchar *string, const gchar *chars)
1901 const gchar *r;
1902 gchar *w = string;
1904 g_return_val_if_fail(string, NULL);
1905 if (G_UNLIKELY(! NZV(chars)))
1906 return string;
1908 foreach_str(r, string)
1910 if (!strchr(chars, *r))
1911 *w++ = *r;
1913 *w = 0x0;
1914 return string;
1918 /* Gets list of sorted filenames with no path and no duplicates from user and system config */
1919 GSList *utils_get_config_files(const gchar *subdir)
1921 gchar *path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, subdir, NULL);
1922 GSList *list = utils_get_file_list_full(path, FALSE, FALSE, NULL);
1923 GSList *syslist, *node;
1925 if (!list)
1927 utils_mkdir(path, FALSE);
1929 SETPTR(path, g_build_path(G_DIR_SEPARATOR_S, app->datadir, subdir, NULL));
1930 syslist = utils_get_file_list_full(path, FALSE, FALSE, NULL);
1931 /* merge lists */
1932 list = g_slist_concat(list, syslist);
1934 list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
1935 /* remove duplicates (next to each other after sorting) */
1936 foreach_slist(node, list)
1938 if (node->next && utils_str_equal(node->next->data, node->data))
1940 GSList *old = node->next;
1942 g_free(old->data);
1943 node->next = old->next;
1944 g_slist_free1(old);
1947 g_free(path);
1948 return list;
1952 /* Suffix can be NULL or a string which should be appended to the Help URL like
1953 * an anchor link, e.g. "#some_anchor". */
1954 gchar *utils_get_help_url(const gchar *suffix)
1956 gint skip;
1957 gchar *uri;
1959 #ifdef G_OS_WIN32
1960 skip = 8;
1961 uri = g_strconcat("file:///", app->docdir, "/Manual.html", NULL);
1962 g_strdelimit(uri, "\\", '/'); /* replace '\\' by '/' */
1963 #else
1964 skip = 7;
1965 uri = g_strconcat("file://", app->docdir, "/index.html", NULL);
1966 #endif
1968 if (! g_file_test(uri + skip, G_FILE_TEST_IS_REGULAR))
1969 { /* fall back to online documentation if it is not found on the hard disk */
1970 g_free(uri);
1971 uri = g_strconcat(GEANY_HOMEPAGE, "manual/", VERSION, "/index.html", NULL);
1974 if (suffix != NULL)
1976 SETPTR(uri, g_strconcat(uri, suffix, NULL));
1979 return uri;
1983 static gboolean str_in_array(const gchar **haystack, const gchar *needle)
1985 const gchar **p;
1987 for (p = haystack; *p != NULL; ++p)
1989 if (utils_str_equal(*p, needle))
1990 return TRUE;
1992 return FALSE;
1997 * Copies the current environment into a new array.
1998 * @a exclude_vars is a @c NULL-terminated array of variable names which should be not copied.
1999 * All further arguments are key, value pairs of variables which should be added to
2000 * the environment.
2002 * The argument list must be @c NULL-terminated.
2004 * @param exclude_vars @c NULL-terminated array of variable names to exclude.
2005 * @param first_varname Name of the first variable to copy into the new array.
2006 * @param ... Key-value pairs of variable names and values, @c NULL-terminated.
2008 * @return The new environment array.
2010 gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...)
2012 gchar **result;
2013 gchar **p;
2014 gchar **env;
2015 va_list args;
2016 const gchar *key, *value;
2017 guint n, o;
2019 /* get all the environ variables */
2020 env = g_listenv();
2022 /* count the additional variables */
2023 va_start(args, first_varname);
2024 for (o = 1; va_arg(args, gchar*) != NULL; o++);
2025 va_end(args);
2026 /* the passed arguments should be even (key, value pairs) */
2027 g_return_val_if_fail(o % 2 == 0, NULL);
2029 o /= 2;
2031 /* create an array large enough to hold the new environment */
2032 n = g_strv_length(env);
2033 /* 'n + o + 1' could leak a little bit when exclude_vars is set */
2034 result = g_new(gchar *, n + o + 1);
2036 /* copy the environment */
2037 for (n = 0, p = env; *p != NULL; ++p)
2039 /* copy the variable */
2040 value = g_getenv(*p);
2041 if (G_LIKELY(value != NULL))
2043 /* skip excluded variables */
2044 if (exclude_vars != NULL && str_in_array(exclude_vars, *p))
2045 continue;
2047 result[n++] = g_strconcat(*p, "=", value, NULL);
2050 g_strfreev(env);
2052 /* now add additional variables */
2053 va_start(args, first_varname);
2054 key = first_varname;
2055 value = va_arg(args, gchar*);
2056 while (key != NULL)
2058 result[n++] = g_strconcat(key, "=", value, NULL);
2060 key = va_arg(args, gchar*);
2061 if (key == NULL)
2062 break;
2063 value = va_arg(args, gchar*);
2065 va_end(args);
2067 result[n] = NULL;
2069 return result;
2073 /* Joins @a first and @a second into a new string vector, freeing the originals.
2074 * The original contents are reused. */
2075 gchar **utils_strv_join(gchar **first, gchar **second)
2077 gchar **strv;
2078 gchar **rptr, **wptr;
2080 if (!first)
2081 return second;
2082 if (!second)
2083 return first;
2085 strv = g_new0(gchar*, g_strv_length(first) + g_strv_length(second) + 1);
2086 wptr = strv;
2088 foreach_strv(rptr, first)
2089 *wptr++ = *rptr;
2090 foreach_strv(rptr, second)
2091 *wptr++ = *rptr;
2093 g_free(first);
2094 g_free(second);
2095 return strv;