Don't use 'Enable' in pref labels when unnecessary.
[geany-mirror.git] / src / utils.c
blob46e9207e04538765ee0058adeaf980ba3e653aa1
1 /*
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.
21 * $Id$
25 * General utility functions, non-GTK related.
28 #include "geany.h"
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <math.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <stdarg.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
45 #include <glib/gstdio.h>
47 #ifdef HAVE_GIO
48 # include <gio/gio.h>
49 #endif
51 #include "prefs.h"
52 #include "support.h"
53 #include "document.h"
54 #include "filetypes.h"
55 #include "dialogs.h"
56 #include "win32.h"
57 #include "project.h"
59 #include "utils.h"
62 /**
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
67 * browsers.
69 * @param uri The URI to open in the web browser.
71 * @since 0.16
72 **/
73 void utils_open_browser(const gchar *uri)
75 #ifdef G_OS_WIN32
76 g_return_if_fail(uri != NULL);
77 win32_open_browser(uri);
78 #else
79 gchar *cmdline;
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))
86 const gchar *argv[3];
88 argv[0] = "xdg-open";
89 argv[1] = uri;
90 argv[2] = NULL;
91 if (! g_spawn_async(NULL, (gchar**)argv, NULL, G_SPAWN_SEARCH_PATH,
92 NULL, NULL, NULL, NULL))
94 argv[0] = "firefox";
95 if (! g_spawn_async(NULL, (gchar**)argv, NULL, G_SPAWN_SEARCH_PATH,
96 NULL, NULL, NULL, NULL))
98 argv[0] = "mozilla";
99 if (! g_spawn_async(NULL, (gchar**)argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
100 NULL, NULL, NULL))
102 argv[0] = "opera";
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);
119 g_free(cmdline);
120 #endif
124 /* taken from anjuta, to determine the EOL mode of the file */
125 gint utils_get_line_endings(const gchar* buffer, glong size)
127 gint i;
128 guint cr, lf, crlf, max_mode;
129 gint mode;
131 if (size == -1)
132 size = strlen(buffer);
134 cr = lf = crlf = 0;
136 for (i = 0; i < size ; i++)
138 if (buffer[i] == 0x0a)
140 /* LF */
141 lf++;
143 else if (buffer[i] == 0x0d)
145 if (i >= (size - 1))
147 /* Last char, CR */
148 cr++;
150 else
152 if (buffer[i + 1] != 0x0a)
154 /* CR */
155 cr++;
157 else
159 /* CRLF */
160 crlf++;
162 i++;
167 /* Vote for the maximum */
168 mode = SC_EOL_LF;
169 max_mode = lf;
170 if (crlf > max_mode)
172 mode = SC_EOL_CRLF;
173 max_mode = crlf;
175 if (cr > max_mode)
177 mode = SC_EOL_CR;
178 max_mode = cr;
181 return mode;
185 gboolean utils_isbrace(gchar c, gboolean include_angles)
187 switch (c)
189 case '<':
190 case '>':
191 return include_angles;
193 case '(':
194 case ')':
195 case '{':
196 case '}':
197 case '[':
198 case ']': return TRUE;
199 default: return FALSE;
204 gboolean utils_is_opening_brace(gchar c, gboolean include_angles)
206 switch (c)
208 case '<':
209 return include_angles;
211 case '(':
212 case '{':
213 case '[': return TRUE;
214 default: return FALSE;
220 * Writes the given @a text into a file with @a filename.
221 * If the file doesn't exist, it will be created.
222 * If it already exists, it will be overwritten.
224 * @param filename The filename of the file to write, in locale encoding.
225 * @param text The text to write into the file.
227 * @return 0 if the file was successfully written, otherwise the @c errno of the
228 * failed operation is returned.
230 gint utils_write_file(const gchar *filename, const gchar *text)
232 g_return_val_if_fail(filename != NULL, ENOENT);
233 g_return_val_if_fail(text != NULL, EINVAL);
235 if (file_prefs.use_safe_file_saving)
237 GError *error = NULL;
238 if (! g_file_set_contents(filename, text, -1, &error))
240 geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message);
241 g_error_free(error);
242 return EIO;
245 else
247 FILE *fp;
248 gint bytes_written, len;
250 if (filename == NULL)
251 return ENOENT;
253 len = strlen(text);
254 fp = g_fopen(filename, "w");
255 if (fp != NULL)
257 bytes_written = fwrite(text, sizeof (gchar), len, fp);
258 fclose(fp);
260 if (len != bytes_written)
262 geany_debug(
263 "utils_write_file(): written only %d bytes, had to write %d bytes to %s",
264 bytes_written, len, filename);
265 return EIO;
268 else
270 geany_debug("utils_write_file(): could not write to file %s (%s)",
271 filename, g_strerror(errno));
272 return errno;
275 return 0;
279 /** Searches backward through @a size bytes looking for a '<', then returns the tag, if any.
280 * @param sel .
281 * @param size .
282 * @return The tag name.
284 gchar *utils_find_open_xml_tag(const gchar sel[], gint size)
286 /* stolen from anjuta and modified */
287 const gchar *begin, *cur;
289 if (G_UNLIKELY(size < 3))
290 { /* Smallest tag is "<p>" which is 3 characters */
291 return NULL;
293 begin = &sel[0];
294 cur = &sel[size - 1];
296 /* Skip to the character before the closing brace */
297 while (cur > begin)
299 if (*cur == '>')
300 break;
301 --cur;
303 --cur;
304 /* skip whitespace */
305 while (cur > begin && isspace(*cur))
306 cur--;
307 if (*cur == '/')
308 return NULL; /* we found a short tag which doesn't need to be closed */
309 while (cur > begin)
311 if (*cur == '<')
312 break;
313 else if (*cur == '>')
314 break;
315 --cur;
318 if (*cur == '<')
320 GString *result;
322 cur++;
323 if (*cur == '/')
324 return NULL; /* we found a closing tag */
326 result = g_string_sized_new(64);
327 while (strchr(":_-.", *cur) || isalnum(*cur))
329 g_string_append_c(result, *cur);
330 cur++;
332 return g_string_free(result, FALSE);
335 return NULL;
339 /* Returns true if specified tag doesn't usually contain any content and can be left unclosed */
340 gboolean utils_is_short_html_tag(const gchar *tag_name)
342 return utils_str_equal(tag_name, "br")
343 || utils_str_equal(tag_name, "hr")
344 || utils_str_equal(tag_name, "img")
345 || utils_str_equal(tag_name, "base")
346 || utils_str_equal(tag_name, "basefont") /* < or not < */
347 || utils_str_equal(tag_name, "frame")
348 || utils_str_equal(tag_name, "input")
349 || utils_str_equal(tag_name, "link")
350 || utils_str_equal(tag_name, "area")
351 || utils_str_equal(tag_name, "meta");
355 const gchar *utils_get_eol_name(gint eol_mode)
357 switch (eol_mode)
359 case SC_EOL_CRLF: return _("Win (CRLF)"); break;
360 case SC_EOL_CR: return _("Mac (CR)"); break;
361 default: return _("Unix (LF)"); break;
366 const gchar *utils_get_eol_char(gint eol_mode)
368 switch (eol_mode)
370 case SC_EOL_CRLF: return "\r\n"; break;
371 case SC_EOL_CR: return "\r"; break;
372 default: return "\n"; break;
377 /* Converts line endings to @a target_eol_mode. */
378 void utils_ensure_same_eol_characters(GString *string, gint target_eol_mode)
380 const gchar *eol_str = utils_get_eol_char(target_eol_mode);
382 /* first convert data to LF only */
383 utils_string_replace_all(string, "\r\n", "\n");
384 utils_string_replace_all(string, "\r", "\n");
386 if (target_eol_mode == SC_EOL_LF)
387 return;
389 /* now convert to desired line endings */
390 utils_string_replace_all(string, "\n", eol_str);
394 gboolean utils_atob(const gchar *str)
396 if (G_UNLIKELY(str == NULL))
397 return FALSE;
398 else if (strcmp(str, "TRUE") == 0 || strcmp(str, "true") == 0)
399 return TRUE;
400 return FALSE;
404 /* NULL-safe version of g_path_is_absolute(). */
405 gboolean utils_is_absolute_path(const gchar *path)
407 if (! NZV(path))
408 return FALSE;
410 return g_path_is_absolute(path);
414 /* Skips root if path is absolute, do nothing otherwise.
415 * This is a relative-safe version of g_path_skip_root().
417 const gchar *utils_path_skip_root(const gchar *path)
419 const gchar *path_relative;
421 path_relative = g_path_skip_root(path);
423 return (path_relative != NULL) ? path_relative : path;
427 gdouble utils_scale_round(gdouble val, gdouble factor)
429 /*val = floor(val * factor + 0.5);*/
430 val = floor(val);
431 val = MAX(val, 0);
432 val = MIN(val, factor);
434 return val;
439 * A replacement function for g_strncasecmp() to compare strings case-insensitive.
440 * It converts both strings into lowercase using g_utf8_strdown() and then compare
441 * both strings using strcmp().
442 * This is not completely accurate regarding locale-specific case sorting rules
443 * but seems to be a good compromise between correctness and performance.
445 * The input strings should be in UTF-8 or locale encoding.
447 * @param s1 Pointer to first string or @c NULL.
448 * @param s2 Pointer to second string or @c NULL.
450 * @return an integer less than, equal to, or greater than zero if @a s1 is found, respectively,
451 * to be less than, to match, or to be greater than @a s2.
453 * @since 0.16
455 gint utils_str_casecmp(const gchar *s1, const gchar *s2)
457 gchar *tmp1, *tmp2;
458 gint result;
460 g_return_val_if_fail(s1 != NULL, 1);
461 g_return_val_if_fail(s2 != NULL, -1);
463 tmp1 = g_strdup(s1);
464 tmp2 = g_strdup(s2);
466 /* first ensure strings are UTF-8 */
467 if (! g_utf8_validate(s1, -1, NULL))
468 setptr(tmp1, g_locale_to_utf8(s1, -1, NULL, NULL, NULL));
469 if (! g_utf8_validate(s2, -1, NULL))
470 setptr(tmp2, g_locale_to_utf8(s2, -1, NULL, NULL, NULL));
472 if (tmp1 == NULL)
474 g_free(tmp2);
475 return 1;
477 if (tmp2 == NULL)
479 g_free(tmp1);
480 return -1;
483 /* then convert the strings into a case-insensitive form */
484 setptr(tmp1, g_utf8_strdown(tmp1, -1));
485 setptr(tmp2, g_utf8_strdown(tmp2, -1));
487 /* compare */
488 result = strcmp(tmp1, tmp2);
490 g_free(tmp1);
491 g_free(tmp2);
492 return result;
497 * Truncates the input string to a given length.
498 * Characters are removed from the middle of the string, so the start and the end of string
499 * won't change.
501 * @param string Input string.
502 * @param truncate_length The length in characters of the resulting string.
504 * @return A copy of @a string which is truncated to @a truncate_length characters,
505 * should be freed when no longer needed.
507 * @since 0.17
509 /* This following function is taken from Gedit. */
510 gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length)
512 GString *truncated;
513 guint length;
514 guint n_chars;
515 guint num_left_chars;
516 guint right_offset;
517 guint delimiter_length;
518 const gchar *delimiter = "\342\200\246";
520 g_return_val_if_fail(string != NULL, NULL);
522 length = strlen(string);
524 g_return_val_if_fail(g_utf8_validate(string, length, NULL), NULL);
526 /* It doesnt make sense to truncate strings to less than the size of the delimiter plus 2
527 * characters (one on each side) */
528 delimiter_length = g_utf8_strlen(delimiter, -1);
529 if (truncate_length < (delimiter_length + 2))
530 return g_strdup(string);
532 n_chars = g_utf8_strlen(string, length);
534 /* Make sure the string is not already small enough. */
535 if (n_chars <= truncate_length)
536 return g_strdup (string);
538 /* Find the 'middle' where the truncation will occur. */
539 num_left_chars = (truncate_length - delimiter_length) / 2;
540 right_offset = n_chars - truncate_length + num_left_chars + delimiter_length;
542 truncated = g_string_new_len(string, g_utf8_offset_to_pointer(string, num_left_chars) - string);
543 g_string_append(truncated, delimiter);
544 g_string_append(truncated, g_utf8_offset_to_pointer(string, right_offset));
546 return g_string_free(truncated, FALSE);
551 * @c NULL-safe string comparison. Returns @c TRUE if both @a a and @a b are @c NULL
552 * or if @a a and @a b refer to valid strings which are equal.
554 * @param a Pointer to first string or @c NULL.
555 * @param b Pointer to second string or @c NULL.
557 * @return @c TRUE if @a a equals @a b, else @c FALSE.
559 gboolean utils_str_equal(const gchar *a, const gchar *b)
561 /* (taken from libexo from os-cillation) */
562 if (a == NULL && b == NULL) return TRUE;
563 else if (a == NULL || b == NULL) return FALSE;
565 while (*a == *b++)
566 if (*a++ == '\0')
567 return TRUE;
569 return FALSE;
574 * Removes the extension from @a filename and return the result in a newly allocated string.
576 * @param filename The filename to operate on.
578 * @return A newly-allocated string, should be freed when no longer needed.
580 gchar *utils_remove_ext_from_filename(const gchar *filename)
582 gchar *last_dot;
583 gchar *result;
584 gint i;
586 g_return_val_if_fail(filename != NULL, NULL);
588 last_dot = strrchr(filename, '.');
589 if (! last_dot)
590 return g_strdup(filename);
592 /* assumes extension is small, so extra bytes don't matter */
593 result = g_malloc(strlen(filename));
594 i = 0;
595 while ((filename + i) != last_dot)
597 result[i] = filename[i];
598 i++;
600 result[i] = 0;
601 return result;
605 gchar utils_brace_opposite(gchar ch)
607 switch (ch)
609 case '(': return ')';
610 case ')': return '(';
611 case '[': return ']';
612 case ']': return '[';
613 case '{': return '}';
614 case '}': return '{';
615 case '<': return '>';
616 case '>': return '<';
617 default: return '\0';
622 gchar *utils_get_hostname(void)
624 #ifdef G_OS_WIN32
625 return win32_get_hostname();
626 #elif defined(HAVE_GETHOSTNAME)
627 gchar hostname[100];
628 if (gethostname(hostname, sizeof(hostname)) == 0)
629 return g_strdup(hostname);
630 #endif
631 return g_strdup("localhost");
635 /* Checks whether the given file can be written. locale_filename is expected in locale encoding.
636 * Returns 0 if it can be written, otherwise it returns errno */
637 gint utils_is_file_writeable(const gchar *locale_filename)
639 gchar *file;
640 gint ret;
642 if (! g_file_test(locale_filename, G_FILE_TEST_EXISTS) &&
643 ! g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
644 /* get the file's directory to check for write permission if it doesn't yet exist */
645 file = g_path_get_dirname(locale_filename);
646 else
647 file = g_strdup(locale_filename);
649 #ifdef G_OS_WIN32
650 /* use _waccess on Windows, access() doesn't accept special characters */
651 ret = win32_check_write_permission(file);
652 #else
654 /* access set also errno to "FILE NOT FOUND" even if locale_filename is writeable, so use
655 * errno only when access() explicitly returns an error */
656 if (access(file, R_OK | W_OK) != 0)
657 ret = errno;
658 else
659 ret = 0;
660 #endif
661 g_free(file);
662 return ret;
666 /* Replaces all occurrences of needle in haystack with replacement.
667 * Warning: *haystack must be a heap address; it may be freed and reassigned.
668 * Note: utils_string_replace_all() will always be faster when @a replacement is longer
669 * than @a needle.
670 * All strings have to be NULL-terminated.
671 * See utils_string_replace_all() for details. */
672 void utils_str_replace_all(gchar **haystack, const gchar *needle, const gchar *replacement)
674 GString *str;
676 g_return_if_fail(*haystack != NULL);
678 str = g_string_new(*haystack);
680 g_free(*haystack);
681 utils_string_replace_all(str, needle, replacement);
683 *haystack = g_string_free(str, FALSE);
687 gint utils_strpos(const gchar *haystack, const gchar *needle)
689 gint haystack_length = strlen(haystack);
690 gint needle_length = strlen(needle);
691 gint i, j, pos = -1;
693 if (needle_length > haystack_length)
695 return -1;
697 else
699 for (i = 0; (i < haystack_length) && pos == -1; i++)
701 if (haystack[i] == needle[0] && needle_length == 1)
702 return i;
703 else if (haystack[i] == needle[0])
705 for (j = 1; (j < needle_length); j++)
707 if (haystack[i + j] == needle[j])
709 if (pos == -1)
710 pos = i;
712 else
714 pos = -1;
715 break;
720 return pos;
726 * Retrieves a formatted date/time string from strftime().
727 * This function should be preferred to directly calling strftime() since this function
728 * works on UTF-8 encoded strings.
730 * @param format The format string to pass to strftime(3). See the strftime(3)
731 * documentation for details, in UTF-8 encoding.
732 * @param time_to_use The date/time to use, in time_t format or NULL to use the current time.
734 * @return A newly-allocated string, should be freed when no longer needed.
736 * @since 0.16
738 gchar *utils_get_date_time(const gchar *format, time_t *time_to_use)
740 const struct tm *tm;
741 static gchar date[1024];
742 gchar *locale_format;
743 gsize len;
745 g_return_val_if_fail(format != NULL, NULL);
747 if (! g_utf8_validate(format, -1, NULL))
749 locale_format = g_locale_from_utf8(format, -1, NULL, NULL, NULL);
750 if (locale_format == NULL)
751 return NULL;
753 else
754 locale_format = g_strdup(format);
756 if (time_to_use != NULL)
757 tm = localtime(time_to_use);
758 else
760 time_t tp = time(NULL);
761 tm = localtime(&tp);
764 len = strftime(date, 1024, locale_format, tm);
765 g_free(locale_format);
766 if (len == 0)
767 return NULL;
769 if (! g_utf8_validate(date, len, NULL))
770 return g_locale_to_utf8(date, len, NULL, NULL, NULL);
771 else
772 return g_strdup(date);
776 gchar *utils_get_initials(const gchar *name)
778 gint i = 1, j = 1;
779 gchar *initials = g_malloc0(5);
781 initials[0] = name[0];
782 while (name[i] != '\0' && j < 4)
784 if (name[i] == ' ' && name[i + 1] != ' ')
786 initials[j++] = name[i + 1];
788 i++;
790 return initials;
795 * Wraps g_key_file_get_integer() to add a default value argument.
797 * @param config A GKeyFile object.
798 * @param section The group name to look in for the key.
799 * @param key The key to find.
800 * @param default_value The default value which will be returned when @a section or @a key
801 * don't exist.
803 * @return The value associated with @a key as an integer, or the given default value if the value
804 * could not be retrieved.
806 gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key,
807 const gint default_value)
809 gint tmp;
810 GError *error = NULL;
812 if (G_UNLIKELY(config == NULL))
813 return default_value;
815 tmp = g_key_file_get_integer(config, section, key, &error);
816 if (G_UNLIKELY(error))
818 g_error_free(error);
819 return default_value;
821 return tmp;
826 * Wraps g_key_file_get_boolean() to add a default value argument.
828 * @param config A GKeyFile object.
829 * @param section The group name to look in for the key.
830 * @param key The key to find.
831 * @param default_value The default value which will be returned when @c section or @c key
832 * don't exist.
834 * @return The value associated with @a key as a boolean, or the given default value if the value
835 * could not be retrieved.
837 gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key,
838 const gboolean default_value)
840 gboolean tmp;
841 GError *error = NULL;
843 if (G_UNLIKELY(config == NULL))
844 return default_value;
846 tmp = g_key_file_get_boolean(config, section, key, &error);
847 if (G_UNLIKELY(error))
849 g_error_free(error);
850 return default_value;
852 return tmp;
857 * Wraps g_key_file_get_string() to add a default value argument.
859 * @param config A GKeyFile object.
860 * @param section The group name to look in for the key.
861 * @param key The key to find.
862 * @param default_value The default value which will be returned when @a section or @a key
863 * don't exist.
865 * @return A newly allocated string, either the value for @a key or a copy of the given
866 * default value if it could not be retrieved.
868 gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key,
869 const gchar *default_value)
871 gchar *tmp;
873 if (G_UNLIKELY(config == NULL))
874 return g_strdup(default_value);
876 tmp = g_key_file_get_string(config, section, key, NULL);
877 if (G_UNLIKELY(!tmp))
879 return g_strdup(default_value);
881 return tmp;
885 gchar *utils_get_hex_from_color(GdkColor *color)
887 gchar *buffer = g_malloc0(9);
889 g_return_val_if_fail(color != NULL, NULL);
891 g_snprintf(buffer, 8, "#%02X%02X%02X",
892 (guint) (utils_scale_round(color->red / 256, 255)),
893 (guint) (utils_scale_round(color->green / 256, 255)),
894 (guint) (utils_scale_round(color->blue / 256, 255)));
896 return buffer;
900 guint utils_invert_color(guint color)
902 guint r, g, b;
904 r = 0xffffff - color;
905 g = 0xffffff - (color >> 8);
906 b = 0xffffff - (color >> 16);
908 return (r | (g << 8) | (b << 16));
912 /* Get directory from current file in the notebook.
913 * Returns dir string that should be freed or NULL, depending on whether current file is valid.
914 * Returned string is in UTF-8 encoding */
915 gchar *utils_get_current_file_dir_utf8(void)
917 GeanyDocument *doc = document_get_current();
919 if (doc != NULL)
921 /* get current filename */
922 const gchar *cur_fname = doc->file_name;
924 if (cur_fname != NULL)
926 /* get folder part from current filename */
927 return g_path_get_dirname(cur_fname); /* returns "." if no path */
931 return NULL; /* no file open */
935 /* very simple convenience function */
936 void utils_beep(void)
938 if (prefs.beep_on_errors)
939 gdk_beep();
943 /* taken from busybox, thanks */
944 gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
945 gulong display_unit)
947 /* The code will adjust for additional (appended) units. */
948 static const gchar zero_and_units[] = { '0', 0, 'K', 'M', 'G', 'T' };
949 static const gchar fmt[] = "%Lu %c%c";
950 static const gchar fmt_tenths[] = "%Lu.%d %c%c";
952 guint64 val;
953 gint frac;
954 const gchar *u;
955 const gchar *f;
957 u = zero_and_units;
958 f = fmt;
959 frac = 0;
961 val = size * block_size;
962 if (val == 0)
963 return g_strdup(u);
965 if (display_unit)
967 val += display_unit/2; /* Deal with rounding. */
968 val /= display_unit; /* Don't combine with the line above!!! */
970 else
972 ++u;
973 while ((val >= 1024) && (u < zero_and_units + sizeof(zero_and_units) - 1))
975 f = fmt_tenths;
976 ++u;
977 frac = ((((gint)(val % 1024)) * 10) + (1024 / 2)) / 1024;
978 val /= 1024;
980 if (frac >= 10)
981 { /* We need to round up here. */
982 ++val;
983 frac = 0;
987 /* If f==fmt then 'frac' and 'u' are ignored. */
988 return g_strdup_printf(f, val, frac, *u, 'b');
992 static guint utils_get_value_of_hex(const gchar ch)
994 if (ch >= '0' && ch <= '9')
995 return ch - '0';
996 else if (ch >= 'A' && ch <= 'F')
997 return ch - 'A' + 10;
998 else if (ch >= 'a' && ch <= 'f')
999 return ch - 'a' + 10;
1000 else
1001 return 0;
1005 /* utils_strtod() converts a string containing a hex colour ("0x00ff00") into an integer.
1006 * Basically, it is the same as strtod() would do, but it does not understand hex colour values,
1007 * before ANSI-C99. With with_route set, it takes strings of the format "#00ff00".
1008 * Returns -1 on failure. */
1009 gint utils_strtod(const gchar *source, gchar **end, gboolean with_route)
1011 guint red, green, blue, offset = 0;
1013 g_return_val_if_fail(source != NULL, -1);
1015 if (with_route && (strlen(source) != 7 || source[0] != '#'))
1016 return -1;
1017 else if (! with_route && (strlen(source) != 8 || source[0] != '0' ||
1018 (source[1] != 'x' && source[1] != 'X')))
1020 return -1;
1023 /* offset is set to 1 when the string starts with 0x, otherwise it starts with #
1024 * and we don't need to increase the index */
1025 if (! with_route)
1026 offset = 1;
1028 red = utils_get_value_of_hex(
1029 source[1 + offset]) * 16 + utils_get_value_of_hex(source[2 + offset]);
1030 green = utils_get_value_of_hex(
1031 source[3 + offset]) * 16 + utils_get_value_of_hex(source[4 + offset]);
1032 blue = utils_get_value_of_hex(
1033 source[5 + offset]) * 16 + utils_get_value_of_hex(source[6 + offset]);
1035 return (red | (green << 8) | (blue << 16));
1039 /* Returns: newly allocated string with the current time formatted HH:MM:SS. */
1040 gchar *utils_get_current_time_string(void)
1042 const time_t tp = time(NULL);
1043 const struct tm *tmval = localtime(&tp);
1044 gchar *result = g_malloc0(9);
1046 strftime(result, 9, "%H:%M:%S", tmval);
1047 result[8] = '\0';
1048 return result;
1052 GIOChannel *utils_set_up_io_channel(
1053 gint fd, GIOCondition cond, gboolean nblock, GIOFunc func, gpointer data)
1055 GIOChannel *ioc;
1056 /*const gchar *encoding;*/
1058 #ifdef G_OS_WIN32
1059 ioc = g_io_channel_win32_new_fd(fd);
1060 #else
1061 ioc = g_io_channel_unix_new(fd);
1062 #endif
1064 if (nblock)
1065 g_io_channel_set_flags(ioc, G_IO_FLAG_NONBLOCK, NULL);
1067 g_io_channel_set_encoding(ioc, NULL, NULL);
1069 if (! g_get_charset(&encoding))
1070 { // hope this works reliably
1071 GError *error = NULL;
1072 g_io_channel_set_encoding(ioc, encoding, &error);
1073 if (error)
1075 geany_debug("%s: %s", G_STRFUNC, error->message);
1076 g_error_free(error);
1077 return ioc;
1081 /* "auto-close" ;-) */
1082 g_io_channel_set_close_on_unref(ioc, TRUE);
1084 g_io_add_watch(ioc, cond, func, data);
1085 g_io_channel_unref(ioc);
1087 return ioc;
1091 gchar **utils_read_file_in_array(const gchar *filename)
1093 gchar **result = NULL;
1094 gchar *data;
1096 g_return_val_if_fail(filename != NULL, NULL);
1098 g_file_get_contents(filename, &data, NULL, NULL);
1100 if (data != NULL)
1102 result = g_strsplit_set(data, "\r\n", -1);
1103 g_free(data);
1106 return result;
1110 /* Contributed by Stefan Oltmanns, thanks.
1111 * Replaces \\, \r, \n, \t and \uXXX by their real counterparts.
1112 * keep_backslash is used for regex strings to leave '\\' and '\?' in place */
1113 gboolean utils_str_replace_escape(gchar *string, gboolean keep_backslash)
1115 gsize i, j, len;
1116 guint unicodechar;
1118 g_return_val_if_fail(string != NULL, FALSE);
1120 j = 0;
1121 len = strlen(string);
1122 for (i = 0; i < len; i++)
1124 if (string[i]=='\\')
1126 if (i++ >= strlen(string))
1128 return FALSE;
1130 switch (string[i])
1132 case '\\':
1133 if (keep_backslash)
1134 string[j++] = '\\';
1135 string[j] = '\\';
1136 break;
1137 case 'n':
1138 string[j] = '\n';
1139 break;
1140 case 'r':
1141 string[j] = '\r';
1142 break;
1143 case 't':
1144 string[j] = '\t';
1145 break;
1146 #if 0
1147 case 'x': /* Warning: May produce illegal utf-8 string! */
1148 i += 2;
1149 if (i >= strlen(string))
1151 return FALSE;
1153 if (isdigit(string[i - 1])) string[j] = string[i - 1] - 48;
1154 else if (isxdigit(string[i - 1])) string[j] = tolower(string[i - 1])-87;
1155 else return FALSE;
1156 string[j] <<= 4;
1157 if (isdigit(string[i])) string[j] |= string[i] - 48;
1158 else if (isxdigit(string[i])) string[j] |= tolower(string[i])-87;
1159 else return FALSE;
1160 break;
1161 #endif
1162 case 'u':
1164 i += 2;
1165 if (i >= strlen(string))
1167 return FALSE;
1169 if (isdigit(string[i - 1])) unicodechar = string[i - 1] - 48;
1170 else if (isxdigit(string[i - 1])) unicodechar = tolower(string[i - 1])-87;
1171 else return FALSE;
1172 unicodechar <<= 4;
1173 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1174 else if (isxdigit(string[i])) unicodechar |= tolower(string[i])-87;
1175 else return FALSE;
1176 if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
1177 && (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
1179 i += 2;
1180 unicodechar <<= 8;
1181 if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
1182 else unicodechar |= ((tolower(string[i - 1])-87) << 4);
1183 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1184 else unicodechar |= tolower(string[i])-87;
1186 if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
1187 && (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
1189 i += 2;
1190 unicodechar <<= 8;
1191 if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
1192 else unicodechar |= ((tolower(string[i - 1])-87) << 4);
1193 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1194 else unicodechar |= tolower(string[i])-87;
1196 if (unicodechar < 0x80)
1198 string[j] = unicodechar;
1200 else if (unicodechar < 0x800)
1202 string[j] = (unsigned char) ((unicodechar >> 6) | 0xC0);
1203 j++;
1204 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1206 else if (unicodechar < 0x10000)
1208 string[j] = (unsigned char) ((unicodechar >> 12) | 0xE0);
1209 j++;
1210 string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
1211 j++;
1212 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1214 else if (unicodechar < 0x110000) /* more chars are not allowed in unicode */
1216 string[j] = (unsigned char) ((unicodechar >> 18) | 0xF0);
1217 j++;
1218 string[j] = (unsigned char) (((unicodechar >> 12) & 0x3F) | 0x80);
1219 j++;
1220 string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
1221 j++;
1222 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1224 else
1226 return FALSE;
1228 break;
1230 default:
1231 /* unnecessary escapes are allowed */
1232 if (keep_backslash)
1233 string[j++] = '\\';
1234 string[j] = string[i];
1237 else
1239 string[j] = string[i];
1241 j++;
1243 while (j < i)
1245 string[j] = 0;
1246 j++;
1248 return TRUE;
1252 /* Wraps a string in place, replacing a space with a newline character.
1253 * wrapstart is the minimum position to start wrapping or -1 for default */
1254 gboolean utils_wrap_string(gchar *string, gint wrapstart)
1256 gchar *pos, *linestart;
1257 gboolean ret = FALSE;
1259 if (wrapstart < 0)
1260 wrapstart = 80;
1262 for (pos = linestart = string; *pos != '\0'; pos++)
1264 if (pos - linestart >= wrapstart && *pos == ' ')
1266 *pos = '\n';
1267 linestart = pos;
1268 ret = TRUE;
1271 return ret;
1276 * Converts the given UTF-8 encoded string into locale encoding.
1277 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1279 * @param utf8_text UTF-8 encoded text.
1281 * @return The converted string in locale encoding, or a copy of the input string if conversion
1282 * failed. Should be freed with g_free(). If @a utf8_text is @c NULL, @c NULL is returned.
1284 gchar *utils_get_locale_from_utf8(const gchar *utf8_text)
1286 #ifdef G_OS_WIN32
1287 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1288 * which would result in wrongly converted strings */
1289 return g_strdup(utf8_text);
1290 #else
1291 gchar *locale_text;
1293 if (! utf8_text)
1294 return NULL;
1295 locale_text = g_locale_from_utf8(utf8_text, -1, NULL, NULL, NULL);
1296 if (locale_text == NULL)
1297 locale_text = g_strdup(utf8_text);
1298 return locale_text;
1299 #endif
1304 * Converts the given string (in locale encoding) into UTF-8 encoding.
1305 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1307 * @param locale_text Text in locale encoding.
1309 * @return The converted string in UTF-8 encoding, or a copy of the input string if conversion
1310 * failed. Should be freed with g_free(). If @a locale_text is @c NULL, @c NULL is returned.
1312 gchar *utils_get_utf8_from_locale(const gchar *locale_text)
1314 #ifdef G_OS_WIN32
1315 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1316 * which would result in wrongly converted strings */
1317 return g_strdup(locale_text);
1318 #else
1319 gchar *utf8_text;
1321 if (! locale_text)
1322 return NULL;
1323 utf8_text = g_locale_to_utf8(locale_text, -1, NULL, NULL, NULL);
1324 if (utf8_text == NULL)
1325 utf8_text = g_strdup(locale_text);
1326 return utf8_text;
1327 #endif
1331 /* Pass pointers to free after arg_count.
1332 * The last argument must be NULL as an extra check that arg_count is correct. */
1333 void utils_free_pointers(gsize arg_count, ...)
1335 va_list a;
1336 gsize i;
1337 gpointer ptr;
1339 va_start(a, arg_count);
1340 for (i = 0; i < arg_count; i++)
1342 ptr = va_arg(a, gpointer);
1343 g_free(ptr);
1345 ptr = va_arg(a, gpointer);
1346 if (ptr)
1347 g_warning("Wrong arg_count!");
1348 va_end(a);
1352 /* currently unused */
1353 #if 0
1354 /* Creates a string array deep copy of a series of non-NULL strings.
1355 * The first argument is nothing special.
1356 * The list must be ended with NULL.
1357 * If first is NULL, NULL is returned. */
1358 gchar **utils_strv_new(const gchar *first, ...)
1360 gsize strvlen, i;
1361 va_list args;
1362 gchar *str;
1363 gchar **strv;
1365 g_return_val_if_fail(first != NULL, NULL);
1367 strvlen = 1; /* for first argument */
1369 /* count other arguments */
1370 va_start(args, first);
1371 for (; va_arg(args, gchar*) != NULL; strvlen++);
1372 va_end(args);
1374 strv = g_new(gchar*, strvlen + 1); /* +1 for NULL terminator */
1375 strv[0] = g_strdup(first);
1377 va_start(args, first);
1378 for (i = 1; str = va_arg(args, gchar*), str != NULL; i++)
1380 strv[i] = g_strdup(str);
1382 va_end(args);
1384 strv[i] = NULL;
1385 return strv;
1387 #endif
1391 * Creates a directory if it doesn't already exist.
1392 * Creates intermediate parent directories as needed, too.
1393 * The permissions of the created directory are set 0700.
1395 * @param path The path of the directory to create, in locale encoding.
1396 * @param create_parent_dirs Whether to create intermediate parent directories if necessary.
1398 * @return 0 if the directory was successfully created, otherwise the @c errno of the
1399 * failed operation is returned.
1401 gint utils_mkdir(const gchar *path, gboolean create_parent_dirs)
1403 gint mode = 0700;
1404 gint result;
1406 if (path == NULL || strlen(path) == 0)
1407 return EFAULT;
1409 result = (create_parent_dirs) ? g_mkdir_with_parents(path, mode) : g_mkdir(path, mode);
1410 if (result != 0)
1411 return errno;
1412 return 0;
1417 * Gets a list of files from the specified directory.
1418 * Locale encoding is expected for @a path and used for the file list. The list and the data
1419 * in the list should be freed after use, e.g.:
1420 * @code
1421 * g_slist_foreach(list, (GFunc) g_free, NULL);
1422 * g_slist_free(list); @endcode
1424 * @note If you don't need a list you should use the foreach_dir() macro instead -
1425 * it's more efficient.
1427 * @param path The path of the directory to scan, in locale encoding.
1428 * @param full_path Whether to include the full path for each filename in the list. Obviously this
1429 * will use more memory.
1430 * @param sort Whether to sort alphabetically (UTF-8 safe).
1431 * @param error The location for storing a possible error, or @c NULL.
1433 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1434 * freed when no longer needed.
1435 * @see utils_get_file_list().
1437 GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error)
1439 GSList *list = NULL;
1440 GDir *dir;
1441 const gchar *filename;
1443 if (error)
1444 *error = NULL;
1445 g_return_val_if_fail(path != NULL, NULL);
1447 dir = g_dir_open(path, 0, error);
1448 if (dir == NULL)
1449 return NULL;
1451 foreach_dir(filename, dir)
1453 list = g_slist_append(list, full_path ?
1454 g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL) : g_strdup(filename));
1456 g_dir_close(dir);
1457 /* sorting last is quicker than on insertion */
1458 if (sort)
1459 list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
1460 return list;
1465 * Gets a sorted list of files from the specified directory.
1466 * Locale encoding is expected for @a path and used for the file list. The list and the data
1467 * in the list should be freed after use, e.g.:
1468 * @code
1469 * g_slist_foreach(list, (GFunc) g_free, NULL);
1470 * g_slist_free(list); @endcode
1472 * @param path The path of the directory to scan, in locale encoding.
1473 * @param length The location to store the number of non-@c NULL data items in the list,
1474 * unless @c NULL.
1475 * @param error The location for storing a possible error, or @c NULL.
1477 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1478 * freed when no longer needed.
1479 * @see utils_get_file_list_full().
1481 GSList *utils_get_file_list(const gchar *path, guint *length, GError **error)
1483 GSList *list = utils_get_file_list_full(path, FALSE, TRUE, error);
1485 if (length)
1486 *length = g_slist_length(list);
1487 return list;
1491 /* returns TRUE if any letter in str is a capital, FALSE otherwise. Should be Unicode safe. */
1492 gboolean utils_str_has_upper(const gchar *str)
1494 gunichar c;
1496 if (! NZV(str) || ! g_utf8_validate(str, -1, NULL))
1497 return FALSE;
1499 while (*str != '\0')
1501 c = g_utf8_get_char(str);
1502 /* check only letters and stop once the first non-capital was found */
1503 if (g_unichar_isalpha(c) && g_unichar_isupper(c))
1504 return TRUE;
1505 /* FIXME don't write a const string */
1506 str = g_utf8_next_char(str);
1508 return FALSE;
1512 static guint utils_string_replace_helper(GString *haystack, const gchar *needle,
1513 const gchar *replace, const guint max_replaces)
1515 const gchar *stack, *match;
1516 guint ret = 0;
1517 gssize pos;
1519 g_return_val_if_fail(haystack != NULL, 0);
1520 if (haystack->len == 0)
1521 return FALSE;
1522 g_return_val_if_fail(NZV(needle), 0);
1524 stack = haystack->str;
1525 if (! (match = strstr(stack, needle)))
1526 return 0;
1529 pos = match - haystack->str;
1530 g_string_erase(haystack, pos, strlen(needle));
1532 /* make next search after removed matching text.
1533 * (we have to be careful to only use haystack->str as its address may change) */
1534 stack = haystack->str + pos;
1536 if (G_LIKELY(replace))
1538 g_string_insert(haystack, pos, replace);
1539 stack = haystack->str + pos + strlen(replace); /* skip past replacement */
1542 while (++ret != max_replaces && (match = strstr(stack, needle)));
1544 return ret;
1549 * Replaces all occurrences of @a needle in @a haystack with @a replace.
1550 * As of Geany 0.16, @a replace can match @a needle, so the following will work:
1551 * @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
1553 * @param haystack The input string to operate on. This string is modified in place.
1554 * @param needle The string which should be replaced.
1555 * @param replace The replacement for @a needle.
1557 * @return Number of replacements made.
1559 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
1561 return utils_string_replace_helper(haystack, needle, replace, 0);
1566 * Replaces only the first occurrence of @a needle in @a haystack
1567 * with @a replace.
1568 * For details, see utils_string_replace_all().
1570 * @param haystack The input string to operate on. This string is modified in place.
1571 * @param needle The string which should be replaced.
1572 * @param replace The replacement for @a needle.
1574 * @return Number of replacements made.
1576 * @since 0.16
1578 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace)
1580 return utils_string_replace_helper(haystack, needle, replace, 1);
1584 /* Get project or default startup directory (if set), or NULL. */
1585 const gchar *utils_get_default_dir_utf8(void)
1587 if (app->project && NZV(app->project->base_path))
1589 return app->project->base_path;
1592 if (NZV(prefs.default_open_path))
1594 return prefs.default_open_path;
1596 return NULL;
1600 static gboolean check_error(GError **error)
1602 if (error != NULL && *error != NULL)
1604 /* imitate the GLib warning */
1605 g_warning(
1606 "GError set over the top of a previous GError or uninitialized memory.\n"
1607 "This indicates a bug in someone's code. You must ensure an error is NULL "
1608 "before it's set.");
1609 /* after returning the code may segfault, but we don't care because we should
1610 * make sure *error is NULL */
1611 return FALSE;
1613 return TRUE;
1618 * Wraps g_spawn_sync() and internally calls this function on Unix-like
1619 * systems. On Win32 platforms, it uses the Windows API.
1621 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1622 * @param argv The child's argument vector.
1623 * @param env The child's environment, or @a NULL to inherit parent's.
1624 * @param flags Flags from GSpawnFlags.
1625 * @param child_setup A function to run in the child just before exec().
1626 * @param user_data The user data for child_setup.
1627 * @param std_out The return location for child output.
1628 * @param std_err The return location for child error messages.
1629 * @param exit_status The child exit status, as returned by waitpid().
1630 * @param error The return location for error or @a NULL.
1632 * @return @c TRUE on success, @c FALSE if an error was set.
1634 gboolean utils_spawn_sync(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
1635 GSpawnChildSetupFunc child_setup, gpointer user_data, gchar **std_out,
1636 gchar **std_err, gint *exit_status, GError **error)
1638 gboolean result;
1640 if (! check_error(error))
1641 return FALSE;
1643 if (argv == NULL)
1645 *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "argv must not be NULL");
1646 return FALSE;
1649 if (std_out)
1650 *std_out = NULL;
1652 if (std_err)
1653 *std_err = NULL;
1655 #ifdef G_OS_WIN32
1656 result = win32_spawn(dir, argv, env, flags, std_out, std_err, exit_status, error);
1657 #else
1658 result = g_spawn_sync(dir, argv, env, flags, NULL, NULL, std_out, std_err, exit_status, error);
1659 #endif
1661 return result;
1666 * Wraps g_spawn_async() and internally calls this function on Unix-like
1667 * systems. On Win32 platforms, it uses the Windows API.
1669 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1670 * @param argv The child's argument vector.
1671 * @param env The child's environment, or @a NULL to inherit parent's.
1672 * @param flags Flags from GSpawnFlags.
1673 * @param child_setup A function to run in the child just before exec().
1674 * @param user_data The user data for child_setup.
1675 * @param child_pid The return location for child process ID, or NULL.
1676 * @param error The return location for error or @a NULL.
1678 * @return @c TRUE on success, @c FALSE if an error was set.
1680 gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
1681 GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
1682 GError **error)
1684 gboolean result;
1686 if (! check_error(error))
1687 return FALSE;
1689 if (argv == NULL)
1691 *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "argv must not be NULL");
1692 return FALSE;
1695 #ifdef G_OS_WIN32
1696 result = win32_spawn(dir, argv, env, flags, NULL, NULL, NULL, error);
1697 #else
1698 result = g_spawn_async(dir, argv, env, flags, NULL, NULL, child_pid, error);
1699 #endif
1700 return result;
1704 /* Similar to g_build_path() but (re)using a fixed buffer, so never free it.
1705 * This assumes a small enough resulting string length to be kept without freeing,
1706 * but this should be the case for filenames.
1707 * @warning As the buffer is reused, you can't call this recursively, e.g. for a
1708 * function argument and within the function called. */
1709 const gchar *utils_build_path(const gchar *first, ...)
1711 static GString *buffer = NULL;
1712 const gchar *str;
1713 va_list args;
1715 if (! buffer)
1716 buffer = g_string_new(first);
1717 else
1718 g_string_assign(buffer, first);
1720 va_start(args, first);
1721 while (1)
1723 str = va_arg(args, const gchar *);
1724 if (!str)
1725 break;
1727 g_string_append_c(buffer, G_DIR_SEPARATOR);
1728 g_string_append(buffer, str);
1730 va_end(args);
1732 return buffer->str;
1736 /* Retrieves the path for the given URI.
1737 * It returns:
1738 * - the path which was determined by g_filename_from_uri() or GIO
1739 * - NULL if the URI is non-local and gvfs-fuse is not installed
1740 * - a new copy of 'uri' if it is not an URI. */
1741 gchar *utils_get_path_from_uri(const gchar *uri)
1743 gchar *locale_filename;
1745 g_return_val_if_fail(uri != NULL, NULL);
1747 if (! utils_is_uri(uri))
1748 return g_strdup(uri);
1750 /* this will work only for 'file://' URIs */
1751 locale_filename = g_filename_from_uri(uri, NULL, NULL);
1752 #ifdef HAVE_GIO
1753 /* g_filename_from_uri() failed, so we probably have a non-local URI */
1754 if (locale_filename == NULL)
1756 GFile *file = g_file_new_for_uri(uri);
1757 locale_filename = g_file_get_path(file);
1758 g_object_unref(file);
1759 if (locale_filename == NULL)
1761 geany_debug("The URI '%s' could not be resolved to a local path. This means "
1762 "that the URI is invalid or that you don't have gvfs-fuse installed.", uri);
1763 return NULL;
1766 #endif
1767 if (locale_filename == NULL)
1768 geany_debug("The URI '%s' could not be resolved to a local path. This means that the "
1769 "URI is invalid or that Geany can't use GVFS (maybe it is not installed).", uri);
1771 return locale_filename;
1775 gboolean utils_is_uri(const gchar *uri)
1777 g_return_val_if_fail(uri != NULL, FALSE);
1779 return (strstr(uri, "://") != NULL);
1783 /* path should be in locale encoding */
1784 gboolean utils_is_remote_path(const gchar *path)
1786 g_return_val_if_fail(path != NULL, FALSE);
1788 /* if path is an URI and it doesn't start "file://", we take it as remote */
1789 if (utils_is_uri(path) && strncmp(path, "file:", 5) != 0)
1790 return TRUE;
1792 #ifndef G_OS_WIN32
1793 if (glib_check_version(2, 16, 0) == NULL) /* no need to check for this with GLib < 2.16 */
1795 static gchar *fuse_path = NULL;
1796 static gsize len = 0;
1798 if (G_UNLIKELY(fuse_path == NULL))
1800 fuse_path = g_build_filename(g_get_home_dir(), ".gvfs", NULL);
1801 len = strlen(fuse_path);
1803 /* Comparing the file path against a hardcoded path is not the most elegant solution
1804 * but for now it is better than nothing. Ideally, g_file_new_for_path() should create
1805 * proper GFile objects for Fuse paths, but it only does in future GVFS
1806 * versions (gvfs 1.1.1). */
1807 return (strncmp(path, fuse_path, len) == 0);
1809 #endif
1810 return FALSE;
1814 /* Remove all relative and untidy elements from the path of @a filename.
1815 * @param filename must be a valid absolute path.
1816 * @see tm_get_real_path() - also resolves links. */
1817 void utils_tidy_path(gchar *filename)
1819 GString *str = g_string_new(filename);
1820 const gchar *c, *needle;
1821 gchar *tmp;
1822 gssize pos;
1823 gboolean preserve_double_backslash = FALSE;
1825 g_return_if_fail(g_path_is_absolute(filename));
1827 if (str->len >= 2 && strncmp(str->str, "\\\\", 2) == 0)
1828 preserve_double_backslash = TRUE;
1830 /* replace "/./" and "//" */
1831 utils_string_replace_all(str, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
1832 utils_string_replace_all(str, G_DIR_SEPARATOR_S G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
1834 if (preserve_double_backslash)
1835 g_string_prepend(str, "\\");
1837 /* replace "/../" */
1838 needle = G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S;
1839 while (1)
1841 c = strstr(str->str, needle);
1842 if (c == NULL)
1843 break;
1844 else
1846 pos = c - str->str;
1847 if (pos <= 3)
1848 break; /* bad path */
1850 /* replace "/../" */
1851 g_string_erase(str, pos, strlen(needle));
1852 g_string_insert_c(str, pos, G_DIR_SEPARATOR);
1854 tmp = g_strndup(str->str, pos); /* path up to "/../" */
1855 c = g_strrstr(tmp, G_DIR_SEPARATOR_S);
1856 g_return_if_fail(c);
1858 pos = c - tmp; /* position of previous "/" */
1859 g_string_erase(str, pos, strlen(c));
1860 g_free(tmp);
1863 g_return_if_fail(strlen(str->str) <= strlen(filename));
1864 strcpy(filename, str->str);
1865 g_string_free(str, TRUE);
1870 * Removes characters from a string, in place.
1872 * @param string String to search.
1873 * @param chars Characters to remove.
1875 * @return @a string - return value is only useful when nesting function calls, e.g.:
1876 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
1878 * @see @c g_strdelimit.
1880 gchar *utils_str_remove_chars(gchar *string, const gchar *chars)
1882 const gchar *r;
1883 gchar *w = string;
1885 g_return_val_if_fail(string, NULL);
1886 if (!NZV(chars))
1887 return string;
1889 foreach_str(r, string)
1891 if (!strchr(chars, *r))
1892 *w++ = *r;
1894 *w = 0x0;
1895 return string;
1899 static void utils_slist_remove_next(GSList *node)
1901 GSList *old = node->next;
1903 g_return_if_fail(old);
1905 node->next = old->next;
1906 g_slist_free_1(old);
1910 /* Gets list of sorted filenames with no path and no duplicates from user and system config */
1911 GSList *utils_get_config_files(const gchar *subdir)
1913 gchar *path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, subdir, NULL);
1914 GSList *list = utils_get_file_list_full(path, FALSE, FALSE, NULL);
1915 GSList *syslist, *node;
1917 if (!list)
1919 utils_mkdir(path, FALSE);
1921 setptr(path, g_build_path(G_DIR_SEPARATOR_S, app->datadir, subdir, NULL));
1922 syslist = utils_get_file_list_full(path, FALSE, FALSE, NULL);
1923 /* merge lists */
1924 list = g_slist_concat(list, syslist);
1926 list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
1927 /* remove duplicates (next to each other after sorting) */
1928 foreach_slist(node, list)
1930 if (node->next && utils_str_equal(node->next->data, node->data))
1932 g_free(node->next->data);
1933 utils_slist_remove_next(node);
1936 g_free(path);
1937 return list;
1941 /* Suffix can be NULL or a string which should be appended to the Help URL like
1942 * an anchor link, e.g. "#some_anchor". */
1943 gchar *utils_get_help_url(const gchar *suffix)
1945 gint skip;
1946 gchar *uri;
1948 #ifdef G_OS_WIN32
1949 skip = 8;
1950 uri = g_strconcat("file:///", app->docdir, "/Manual.html", NULL);
1951 g_strdelimit(uri, "\\", '/'); /* replace '\\' by '/' */
1952 #else
1953 skip = 7;
1954 uri = g_strconcat("file://", app->docdir, "/index.html", NULL);
1955 #endif
1957 if (! g_file_test(uri + skip, G_FILE_TEST_IS_REGULAR))
1958 { /* fall back to online documentation if it is not found on the hard disk */
1959 g_free(uri);
1960 uri = g_strconcat(GEANY_HOMEPAGE, "manual/", VERSION, "/index.html", NULL);
1963 if (suffix != NULL)
1965 setptr(uri, g_strconcat(uri, suffix, NULL));
1968 return uri;
1972 static gboolean str_in_array(const gchar **haystack, const gchar *needle)
1974 const gchar **p;
1976 for (p = haystack; *p != NULL; ++p)
1978 if (utils_str_equal(*p, needle))
1979 return TRUE;
1981 return FALSE;
1986 * Copies the current environment into a new array.
1987 * @a exclude_vars is a @c NULL-terminated array of variable names which should be not copied.
1988 * All further arguments are key, value pairs of variables which should be added to
1989 * the environment.
1991 * The argument list must be @c NULL-terminated.
1993 * @param exclude_vars @c NULL-terminated array of variable names to exclude.
1994 * @param first_varname Name of the first variable to copy into the new array.
1995 * @param ... Key-value pairs of variable names and values, @c NULL-terminated.
1997 * @return The new environment array.
1999 gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...)
2001 gchar **result;
2002 gchar **p;
2003 gchar **env;
2004 va_list args;
2005 const gchar *key, *value;
2006 guint n, o;
2008 /* get all the environ variables */
2009 env = g_listenv();
2011 /* count the additional variables */
2012 va_start(args, first_varname);
2013 for (o = 1; va_arg(args, gchar*) != NULL; o++);
2014 va_end(args);
2015 /* the passed arguments should be even (key, value pairs) */
2016 g_return_val_if_fail(o % 2 == 0, NULL);
2018 o /= 2;
2020 /* create an array large enough to hold the new environment */
2021 n = g_strv_length(env);
2022 /* 'n + o + 1' could leak a little bit when exclude_vars is set */
2023 result = g_new(gchar *, n + o + 1);
2025 /* copy the environment */
2026 for (n = 0, p = env; *p != NULL; ++p)
2028 /* copy the variable */
2029 value = g_getenv(*p);
2030 if (G_LIKELY(value != NULL))
2032 /* skip excluded variables */
2033 if (exclude_vars != NULL && str_in_array(exclude_vars, *p))
2034 continue;
2036 result[n++] = g_strconcat(*p, "=", value, NULL);
2039 g_strfreev(env);
2041 /* now add additional variables */
2042 va_start(args, first_varname);
2043 key = first_varname;
2044 value = va_arg(args, gchar*);
2045 while (key != NULL)
2047 result[n++] = g_strconcat(key, "=", value, NULL);
2049 key = va_arg(args, gchar*);
2050 if (key == NULL)
2051 break;
2052 value = va_arg(args, gchar*);
2054 va_end(args);
2056 result[n] = NULL;
2058 return result;
2062 /* Joins @a first and @a second into a new string vector, freeing the originals.
2063 * The original contents are reused. */
2064 gchar **utils_strv_join(gchar **first, gchar **second)
2066 gchar **strv;
2067 gchar **rptr, **wptr;
2069 if (!first)
2070 return second;
2071 if (!second)
2072 return first;
2074 strv = g_new0(gchar*, g_strv_length(first) + g_strv_length(second) + 1);
2075 wptr = strv;
2077 foreach_strv(rptr, first)
2078 *wptr++ = *rptr;
2079 foreach_strv(rptr, second)
2080 *wptr++ = *rptr;
2082 g_free(first);
2083 g_free(second);
2084 return strv;