r5162 | eht16 | 2010-08-15 13:53:09 +0100 (Sun, 15 Aug 2010) | 1 line
[geany-mirror.git] / src / utils.c
blob1d1161f5365e90168cf0f28d3e26400c5051a62b
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 cr = lf = crlf = 0;
133 for (i = 0; i < size ; i++)
135 if (buffer[i] == 0x0a)
137 /* LF */
138 lf++;
140 else if (buffer[i] == 0x0d)
142 if (i >= (size - 1))
144 /* Last char, CR */
145 cr++;
147 else
149 if (buffer[i + 1] != 0x0a)
151 /* CR */
152 cr++;
154 else
156 /* CRLF */
157 crlf++;
159 i++;
164 /* Vote for the maximum */
165 mode = SC_EOL_LF;
166 max_mode = lf;
167 if (crlf > max_mode)
169 mode = SC_EOL_CRLF;
170 max_mode = crlf;
172 if (cr > max_mode)
174 mode = SC_EOL_CR;
175 max_mode = cr;
178 return mode;
182 gboolean utils_isbrace(gchar c, gboolean include_angles)
184 switch (c)
186 case '<':
187 case '>':
188 return include_angles;
190 case '(':
191 case ')':
192 case '{':
193 case '}':
194 case '[':
195 case ']': return TRUE;
196 default: return FALSE;
201 gboolean utils_is_opening_brace(gchar c, gboolean include_angles)
203 switch (c)
205 case '<':
206 return include_angles;
208 case '(':
209 case '{':
210 case '[': return TRUE;
211 default: return FALSE;
217 * Writes the given @a text into a file with @a filename.
218 * If the file doesn't exist, it will be created.
219 * If it already exists, it will be overwritten.
221 * @param filename The filename of the file to write, in locale encoding.
222 * @param text The text to write into the file.
224 * @return 0 if the file was successfully written, otherwise the @c errno of the
225 * failed operation is returned.
227 gint utils_write_file(const gchar *filename, const gchar *text)
229 g_return_val_if_fail(filename != NULL, ENOENT);
230 g_return_val_if_fail(text != NULL, EINVAL);
232 if (file_prefs.use_safe_file_saving)
234 GError *error = NULL;
235 if (! g_file_set_contents(filename, text, -1, &error))
237 geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message);
238 g_error_free(error);
239 return EIO;
242 else
244 FILE *fp;
245 gint bytes_written, len;
247 if (filename == NULL)
248 return ENOENT;
250 len = strlen(text);
251 fp = g_fopen(filename, "w");
252 if (fp != NULL)
254 bytes_written = fwrite(text, sizeof (gchar), len, fp);
255 fclose(fp);
257 if (len != bytes_written)
259 geany_debug(
260 "utils_write_file(): written only %d bytes, had to write %d bytes to %s",
261 bytes_written, len, filename);
262 return EIO;
265 else
267 geany_debug("utils_write_file(): could not write to file %s (%s)",
268 filename, g_strerror(errno));
269 return errno;
272 return 0;
277 * (stolen from anjuta and modified)
278 * Search backward through size bytes looking for a '<', then return the tag, if any.
279 * @return The tag name
281 gchar *utils_find_open_xml_tag(const gchar sel[], gint size, gboolean check_tag)
283 const gchar *begin, *cur;
285 if (G_UNLIKELY(size < 3))
286 { /* Smallest tag is "<p>" which is 3 characters */
287 return NULL;
289 begin = &sel[0];
290 if (check_tag)
291 cur = &sel[size - 3];
292 else
293 cur = &sel[size - 1];
295 /* Skip to the character before the closing brace */
296 while (cur > begin)
298 if (*cur == '>')
299 break;
300 --cur;
302 --cur;
303 /* skip whitespace */
304 while (cur > begin && isspace(*cur))
305 cur--;
306 if (*cur == '/')
307 return NULL; /* we found a short tag which doesn't need to be closed */
308 while (cur > begin)
310 if (*cur == '<')
311 break;
312 else if (! check_tag && *cur == '>')
313 break;
314 --cur;
317 if (*cur == '<')
319 GString *result = g_string_sized_new(64);
321 cur++;
322 while (strchr(":_-.", *cur) || isalnum(*cur))
324 g_string_append_c(result, *cur);
325 cur++;
327 return g_string_free(result, FALSE);
330 return NULL;
334 const gchar *utils_get_eol_name(gint eol_mode)
336 switch (eol_mode)
338 case SC_EOL_CRLF: return _("Win (CRLF)"); break;
339 case SC_EOL_CR: return _("Mac (CR)"); break;
340 default: return _("Unix (LF)"); break;
345 gboolean utils_atob(const gchar *str)
347 if (G_UNLIKELY(str == NULL))
348 return FALSE;
349 else if (strcmp(str, "TRUE") == 0 || strcmp(str, "true") == 0)
350 return TRUE;
351 return FALSE;
355 /* NULL-safe version of g_path_is_absolute(). */
356 gboolean utils_is_absolute_path(const gchar *path)
358 if (! NZV(path))
359 return FALSE;
361 return g_path_is_absolute(path);
365 /* Skips root if path is absolute, do nothing otherwise.
366 * This is a relative-safe version of g_path_skip_root().
368 const gchar *utils_path_skip_root(const gchar *path)
370 const gchar *path_relative;
372 path_relative = g_path_skip_root(path);
374 return (path_relative != NULL) ? path_relative : path;
378 gdouble utils_scale_round(gdouble val, gdouble factor)
380 /*val = floor(val * factor + 0.5);*/
381 val = floor(val);
382 val = MAX(val, 0);
383 val = MIN(val, factor);
385 return val;
390 * A replacement function for g_strncasecmp() to compare strings case-insensitive.
391 * It converts both strings into lowercase using g_utf8_strdown() and then compare
392 * both strings using strcmp().
393 * This is not completely accurate regarding locale-specific case sorting rules
394 * but seems to be a good compromise between correctness and performance.
396 * The input strings should be in UTF-8 or locale encoding.
398 * @param s1 Pointer to first string or @c NULL.
399 * @param s2 Pointer to second string or @c NULL.
401 * @return an integer less than, equal to, or greater than zero if @a s1 is found, respectively,
402 * to be less than, to match, or to be greater than @a s2.
404 * @since 0.16
406 gint utils_str_casecmp(const gchar *s1, const gchar *s2)
408 gchar *tmp1, *tmp2;
409 gint result;
411 g_return_val_if_fail(s1 != NULL, 1);
412 g_return_val_if_fail(s2 != NULL, -1);
414 tmp1 = g_strdup(s1);
415 tmp2 = g_strdup(s2);
417 /* first ensure strings are UTF-8 */
418 if (! g_utf8_validate(s1, -1, NULL))
419 setptr(tmp1, g_locale_to_utf8(s1, -1, NULL, NULL, NULL));
420 if (! g_utf8_validate(s2, -1, NULL))
421 setptr(tmp2, g_locale_to_utf8(s2, -1, NULL, NULL, NULL));
423 if (tmp1 == NULL)
425 g_free(tmp2);
426 return 1;
428 if (tmp2 == NULL)
430 g_free(tmp1);
431 return -1;
434 /* then convert the strings into a case-insensitive form */
435 setptr(tmp1, g_utf8_strdown(tmp1, -1));
436 setptr(tmp2, g_utf8_strdown(tmp2, -1));
438 /* compare */
439 result = strcmp(tmp1, tmp2);
441 g_free(tmp1);
442 g_free(tmp2);
443 return result;
448 * Truncates the input string to a given length.
449 * Characters are removed from the middle of the string, so the start and the end of string
450 * won't change.
452 * @param string Input string.
453 * @param truncate_length The length in characters of the resulting string.
455 * @return A copy of @a string which is truncated to @a truncate_length characters,
456 * should be freed when no longer needed.
458 * @since 0.17
460 /* This following function is taken from Gedit. */
461 gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length)
463 GString *truncated;
464 guint length;
465 guint n_chars;
466 guint num_left_chars;
467 guint right_offset;
468 guint delimiter_length;
469 const gchar *delimiter = "\342\200\246";
471 g_return_val_if_fail(string != NULL, NULL);
473 length = strlen(string);
475 g_return_val_if_fail(g_utf8_validate(string, length, NULL), NULL);
477 /* It doesnt make sense to truncate strings to less than the size of the delimiter plus 2
478 * characters (one on each side) */
479 delimiter_length = g_utf8_strlen(delimiter, -1);
480 if (truncate_length < (delimiter_length + 2))
481 return g_strdup(string);
483 n_chars = g_utf8_strlen(string, length);
485 /* Make sure the string is not already small enough. */
486 if (n_chars <= truncate_length)
487 return g_strdup (string);
489 /* Find the 'middle' where the truncation will occur. */
490 num_left_chars = (truncate_length - delimiter_length) / 2;
491 right_offset = n_chars - truncate_length + num_left_chars + delimiter_length;
493 truncated = g_string_new_len(string, g_utf8_offset_to_pointer(string, num_left_chars) - string);
494 g_string_append(truncated, delimiter);
495 g_string_append(truncated, g_utf8_offset_to_pointer(string, right_offset));
497 return g_string_free(truncated, FALSE);
502 * @c NULL-safe string comparison. Returns @c TRUE if both @a a and @a b are @c NULL
503 * or if @a a and @a b refer to valid strings which are equal.
505 * @param a Pointer to first string or @c NULL.
506 * @param b Pointer to second string or @c NULL.
508 * @return @c TRUE if @a a equals @a b, else @c FALSE.
510 gboolean utils_str_equal(const gchar *a, const gchar *b)
512 /* (taken from libexo from os-cillation) */
513 if (a == NULL && b == NULL) return TRUE;
514 else if (a == NULL || b == NULL) return FALSE;
516 while (*a == *b++)
517 if (*a++ == '\0')
518 return TRUE;
520 return FALSE;
525 * Removes the extension from @a filename and return the result in a newly allocated string.
527 * @param filename The filename to operate on.
529 * @return A newly-allocated string, should be freed when no longer needed.
531 gchar *utils_remove_ext_from_filename(const gchar *filename)
533 gchar *last_dot;
534 gchar *result;
535 gint i;
537 g_return_val_if_fail(filename != NULL, NULL);
539 last_dot = strrchr(filename, '.');
540 if (! last_dot)
541 return g_strdup(filename);
543 /* assumes extension is small, so extra bytes don't matter */
544 result = g_malloc(strlen(filename));
545 i = 0;
546 while ((filename + i) != last_dot)
548 result[i] = filename[i];
549 i++;
551 result[i] = 0;
552 return result;
556 gchar utils_brace_opposite(gchar ch)
558 switch (ch)
560 case '(': return ')';
561 case ')': return '(';
562 case '[': return ']';
563 case ']': return '[';
564 case '{': return '}';
565 case '}': return '{';
566 case '<': return '>';
567 case '>': return '<';
568 default: return '\0';
573 gchar *utils_get_hostname(void)
575 #ifdef G_OS_WIN32
576 return win32_get_hostname();
577 #elif defined(HAVE_GETHOSTNAME)
578 gchar hostname[100];
579 if (gethostname(hostname, sizeof(hostname)) == 0)
580 return g_strdup(hostname);
581 #endif
582 return g_strdup("localhost");
586 /* Checks whether the given file can be written. locale_filename is expected in locale encoding.
587 * Returns 0 if it can be written, otherwise it returns errno */
588 gint utils_is_file_writeable(const gchar *locale_filename)
590 gchar *file;
591 gint ret;
593 if (! g_file_test(locale_filename, G_FILE_TEST_EXISTS) &&
594 ! g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
595 /* get the file's directory to check for write permission if it doesn't yet exist */
596 file = g_path_get_dirname(locale_filename);
597 else
598 file = g_strdup(locale_filename);
600 #ifdef G_OS_WIN32
601 /* use _waccess on Windows, access() doesn't accept special characters */
602 ret = win32_check_write_permission(file);
603 #else
605 /* access set also errno to "FILE NOT FOUND" even if locale_filename is writeable, so use
606 * errno only when access() explicitly returns an error */
607 if (access(file, R_OK | W_OK) != 0)
608 ret = errno;
609 else
610 ret = 0;
611 #endif
612 g_free(file);
613 return ret;
617 /* Replaces all occurrences of needle in haystack with replacement.
618 * Warning: *haystack must be a heap address; it may be freed and reassigned.
619 * Note: utils_string_replace_all() will always be faster when @a replacement is longer
620 * than @a needle.
621 * All strings have to be NULL-terminated.
622 * See utils_string_replace_all() for details. */
623 void utils_str_replace_all(gchar **haystack, const gchar *needle, const gchar *replacement)
625 GString *str;
627 g_return_if_fail(*haystack != NULL);
629 str = g_string_new(*haystack);
631 g_free(*haystack);
632 utils_string_replace_all(str, needle, replacement);
634 *haystack = g_string_free(str, FALSE);
638 gint utils_strpos(const gchar *haystack, const gchar *needle)
640 gint haystack_length = strlen(haystack);
641 gint needle_length = strlen(needle);
642 gint i, j, pos = -1;
644 if (needle_length > haystack_length)
646 return -1;
648 else
650 for (i = 0; (i < haystack_length) && pos == -1; i++)
652 if (haystack[i] == needle[0] && needle_length == 1)
653 return i;
654 else if (haystack[i] == needle[0])
656 for (j = 1; (j < needle_length); j++)
658 if (haystack[i + j] == needle[j])
660 if (pos == -1)
661 pos = i;
663 else
665 pos = -1;
666 break;
671 return pos;
677 * Retrieves a formatted date/time string from strftime().
678 * This function should be preferred to directly calling strftime() since this function
679 * works on UTF-8 encoded strings.
681 * @param format The format string to pass to strftime(3). See the strftime(3)
682 * documentation for details, in UTF-8 encoding.
683 * @param time_to_use The date/time to use, in time_t format or NULL to use the current time.
685 * @return A newly-allocated string, should be freed when no longer needed.
687 * @since 0.16
689 gchar *utils_get_date_time(const gchar *format, time_t *time_to_use)
691 const struct tm *tm;
692 static gchar date[1024];
693 gchar *locale_format;
694 gsize len;
696 g_return_val_if_fail(format != NULL, NULL);
698 if (! g_utf8_validate(format, -1, NULL))
700 locale_format = g_locale_from_utf8(format, -1, NULL, NULL, NULL);
701 if (locale_format == NULL)
702 return NULL;
704 else
705 locale_format = g_strdup(format);
707 if (time_to_use != NULL)
708 tm = localtime(time_to_use);
709 else
711 time_t tp = time(NULL);
712 tm = localtime(&tp);
715 len = strftime(date, 1024, locale_format, tm);
716 g_free(locale_format);
717 if (len == 0)
718 return NULL;
720 if (! g_utf8_validate(date, len, NULL))
721 return g_locale_to_utf8(date, len, NULL, NULL, NULL);
722 else
723 return g_strdup(date);
727 gchar *utils_get_initials(const gchar *name)
729 gint i = 1, j = 1;
730 gchar *initials = g_malloc0(5);
732 initials[0] = name[0];
733 while (name[i] != '\0' && j < 4)
735 if (name[i] == ' ' && name[i + 1] != ' ')
737 initials[j++] = name[i + 1];
739 i++;
741 return initials;
746 * Wraps g_key_file_get_integer() to add a default value argument.
748 * @param config A GKeyFile object.
749 * @param section The group name to look in for the key.
750 * @param key The key to find.
751 * @param default_value The default value which will be returned when @a section or @a key
752 * don't exist.
754 * @return The value associated with @a key as an integer, or the given default value if the value
755 * could not be retrieved.
757 gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key,
758 const gint default_value)
760 gint tmp;
761 GError *error = NULL;
763 if (G_UNLIKELY(config == NULL))
764 return default_value;
766 tmp = g_key_file_get_integer(config, section, key, &error);
767 if (G_UNLIKELY(error))
769 g_error_free(error);
770 return default_value;
772 return tmp;
777 * Wraps g_key_file_get_boolean() to add a default value argument.
779 * @param config A GKeyFile object.
780 * @param section The group name to look in for the key.
781 * @param key The key to find.
782 * @param default_value The default value which will be returned when @c section or @c key
783 * don't exist.
785 * @return The value associated with @a key as a boolean, or the given default value if the value
786 * could not be retrieved.
788 gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key,
789 const gboolean default_value)
791 gboolean tmp;
792 GError *error = NULL;
794 if (G_UNLIKELY(config == NULL))
795 return default_value;
797 tmp = g_key_file_get_boolean(config, section, key, &error);
798 if (G_UNLIKELY(error))
800 g_error_free(error);
801 return default_value;
803 return tmp;
808 * Wraps g_key_file_get_string() to add a default value argument.
810 * @param config A GKeyFile object.
811 * @param section The group name to look in for the key.
812 * @param key The key to find.
813 * @param default_value The default value which will be returned when @a section or @a key
814 * don't exist.
816 * @return A newly allocated string, either the value for @a key or a copy of the given
817 * default value if it could not be retrieved.
819 gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key,
820 const gchar *default_value)
822 gchar *tmp;
824 if (G_UNLIKELY(config == NULL))
825 return g_strdup(default_value);
827 tmp = g_key_file_get_string(config, section, key, NULL);
828 if (G_UNLIKELY(!tmp))
830 return g_strdup(default_value);
832 return tmp;
836 gchar *utils_get_hex_from_color(GdkColor *color)
838 gchar *buffer = g_malloc0(9);
840 g_return_val_if_fail(color != NULL, NULL);
842 g_snprintf(buffer, 8, "#%02X%02X%02X",
843 (guint) (utils_scale_round(color->red / 256, 255)),
844 (guint) (utils_scale_round(color->green / 256, 255)),
845 (guint) (utils_scale_round(color->blue / 256, 255)));
847 return buffer;
851 guint utils_invert_color(guint color)
853 guint r, g, b;
855 r = 0xffffff - color;
856 g = 0xffffff - (color >> 8);
857 b = 0xffffff - (color >> 16);
859 return (r | (g << 8) | (b << 16));
863 /* Get directory from current file in the notebook.
864 * Returns dir string that should be freed or NULL, depending on whether current file is valid.
865 * Returned string is in UTF-8 encoding */
866 gchar *utils_get_current_file_dir_utf8(void)
868 GeanyDocument *doc = document_get_current();
870 if (doc != NULL)
872 /* get current filename */
873 const gchar *cur_fname = doc->file_name;
875 if (cur_fname != NULL)
877 /* get folder part from current filename */
878 return g_path_get_dirname(cur_fname); /* returns "." if no path */
882 return NULL; /* no file open */
886 /* very simple convenience function */
887 void utils_beep(void)
889 if (prefs.beep_on_errors)
890 gdk_beep();
894 /* taken from busybox, thanks */
895 gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
896 gulong display_unit)
898 /* The code will adjust for additional (appended) units. */
899 static const gchar zero_and_units[] = { '0', 0, 'K', 'M', 'G', 'T' };
900 static const gchar fmt[] = "%Lu %c%c";
901 static const gchar fmt_tenths[] = "%Lu.%d %c%c";
903 guint64 val;
904 gint frac;
905 const gchar *u;
906 const gchar *f;
908 u = zero_and_units;
909 f = fmt;
910 frac = 0;
912 val = size * block_size;
913 if (val == 0)
914 return g_strdup(u);
916 if (display_unit)
918 val += display_unit/2; /* Deal with rounding. */
919 val /= display_unit; /* Don't combine with the line above!!! */
921 else
923 ++u;
924 while ((val >= 1024) && (u < zero_and_units + sizeof(zero_and_units) - 1))
926 f = fmt_tenths;
927 ++u;
928 frac = ((((gint)(val % 1024)) * 10) + (1024 / 2)) / 1024;
929 val /= 1024;
931 if (frac >= 10)
932 { /* We need to round up here. */
933 ++val;
934 frac = 0;
938 /* If f==fmt then 'frac' and 'u' are ignored. */
939 return g_strdup_printf(f, val, frac, *u, 'b');
943 static guint utils_get_value_of_hex(const gchar ch)
945 if (ch >= '0' && ch <= '9')
946 return ch - '0';
947 else if (ch >= 'A' && ch <= 'F')
948 return ch - 'A' + 10;
949 else if (ch >= 'a' && ch <= 'f')
950 return ch - 'a' + 10;
951 else
952 return 0;
956 /* utils_strtod() converts a string containing a hex colour ("0x00ff00") into an integer.
957 * Basically, it is the same as strtod() would do, but it does not understand hex colour values,
958 * before ANSI-C99. With with_route set, it takes strings of the format "#00ff00".
959 * Returns -1 on failure. */
960 gint utils_strtod(const gchar *source, gchar **end, gboolean with_route)
962 guint red, green, blue, offset = 0;
964 g_return_val_if_fail(source != NULL, -1);
966 if (with_route && (strlen(source) != 7 || source[0] != '#'))
967 return -1;
968 else if (! with_route && (strlen(source) != 8 || source[0] != '0' ||
969 (source[1] != 'x' && source[1] != 'X')))
971 return -1;
974 /* offset is set to 1 when the string starts with 0x, otherwise it starts with #
975 * and we don't need to increase the index */
976 if (! with_route)
977 offset = 1;
979 red = utils_get_value_of_hex(
980 source[1 + offset]) * 16 + utils_get_value_of_hex(source[2 + offset]);
981 green = utils_get_value_of_hex(
982 source[3 + offset]) * 16 + utils_get_value_of_hex(source[4 + offset]);
983 blue = utils_get_value_of_hex(
984 source[5 + offset]) * 16 + utils_get_value_of_hex(source[6 + offset]);
986 return (red | (green << 8) | (blue << 16));
990 /* Returns: newly allocated string with the current time formatted HH:MM:SS. */
991 gchar *utils_get_current_time_string(void)
993 const time_t tp = time(NULL);
994 const struct tm *tmval = localtime(&tp);
995 gchar *result = g_malloc0(9);
997 strftime(result, 9, "%H:%M:%S", tmval);
998 result[8] = '\0';
999 return result;
1003 GIOChannel *utils_set_up_io_channel(
1004 gint fd, GIOCondition cond, gboolean nblock, GIOFunc func, gpointer data)
1006 GIOChannel *ioc;
1007 /*const gchar *encoding;*/
1009 #ifdef G_OS_WIN32
1010 ioc = g_io_channel_win32_new_fd(fd);
1011 #else
1012 ioc = g_io_channel_unix_new(fd);
1013 #endif
1015 if (nblock)
1016 g_io_channel_set_flags(ioc, G_IO_FLAG_NONBLOCK, NULL);
1018 g_io_channel_set_encoding(ioc, NULL, NULL);
1020 if (! g_get_charset(&encoding))
1021 { // hope this works reliably
1022 GError *error = NULL;
1023 g_io_channel_set_encoding(ioc, encoding, &error);
1024 if (error)
1026 geany_debug("%s: %s", G_STRFUNC, error->message);
1027 g_error_free(error);
1028 return ioc;
1032 /* "auto-close" ;-) */
1033 g_io_channel_set_close_on_unref(ioc, TRUE);
1035 g_io_add_watch(ioc, cond, func, data);
1036 g_io_channel_unref(ioc);
1038 return ioc;
1042 gchar **utils_read_file_in_array(const gchar *filename)
1044 gchar **result = NULL;
1045 gchar *data;
1047 g_return_val_if_fail(filename != NULL, NULL);
1049 g_file_get_contents(filename, &data, NULL, NULL);
1051 if (data != NULL)
1053 result = g_strsplit_set(data, "\r\n", -1);
1054 g_free(data);
1057 return result;
1061 /* Contributed by Stefan Oltmanns, thanks.
1062 * Replaces \\, \r, \n, \t and \uXXX by their real counterparts.
1063 * keep_backslash is used for regex strings to leave '\\' and '\?' in place */
1064 gboolean utils_str_replace_escape(gchar *string, gboolean keep_backslash)
1066 gsize i, j, len;
1067 guint unicodechar;
1069 g_return_val_if_fail(string != NULL, FALSE);
1071 j = 0;
1072 len = strlen(string);
1073 for (i = 0; i < len; i++)
1075 if (string[i]=='\\')
1077 if (i++ >= strlen(string))
1079 return FALSE;
1081 switch (string[i])
1083 case '\\':
1084 if (keep_backslash)
1085 string[j++] = '\\';
1086 string[j] = '\\';
1087 break;
1088 case 'n':
1089 string[j] = '\n';
1090 break;
1091 case 'r':
1092 string[j] = '\r';
1093 break;
1094 case 't':
1095 string[j] = '\t';
1096 break;
1097 #if 0
1098 case 'x': /* Warning: May produce illegal utf-8 string! */
1099 i += 2;
1100 if (i >= strlen(string))
1102 return FALSE;
1104 if (isdigit(string[i - 1])) string[j] = string[i - 1] - 48;
1105 else if (isxdigit(string[i - 1])) string[j] = tolower(string[i - 1])-87;
1106 else return FALSE;
1107 string[j] <<= 4;
1108 if (isdigit(string[i])) string[j] |= string[i] - 48;
1109 else if (isxdigit(string[i])) string[j] |= tolower(string[i])-87;
1110 else return FALSE;
1111 break;
1112 #endif
1113 case 'u':
1115 i += 2;
1116 if (i >= strlen(string))
1118 return FALSE;
1120 if (isdigit(string[i - 1])) unicodechar = string[i - 1] - 48;
1121 else if (isxdigit(string[i - 1])) unicodechar = tolower(string[i - 1])-87;
1122 else return FALSE;
1123 unicodechar <<= 4;
1124 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1125 else if (isxdigit(string[i])) unicodechar |= tolower(string[i])-87;
1126 else return FALSE;
1127 if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
1128 && (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
1130 i += 2;
1131 unicodechar <<= 8;
1132 if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
1133 else unicodechar |= ((tolower(string[i - 1])-87) << 4);
1134 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1135 else unicodechar |= tolower(string[i])-87;
1137 if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
1138 && (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
1140 i += 2;
1141 unicodechar <<= 8;
1142 if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
1143 else unicodechar |= ((tolower(string[i - 1])-87) << 4);
1144 if (isdigit(string[i])) unicodechar |= string[i] - 48;
1145 else unicodechar |= tolower(string[i])-87;
1147 if (unicodechar < 0x80)
1149 string[j] = unicodechar;
1151 else if (unicodechar < 0x800)
1153 string[j] = (unsigned char) ((unicodechar >> 6) | 0xC0);
1154 j++;
1155 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1157 else if (unicodechar < 0x10000)
1159 string[j] = (unsigned char) ((unicodechar >> 12) | 0xE0);
1160 j++;
1161 string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
1162 j++;
1163 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1165 else if (unicodechar < 0x110000) /* more chars are not allowed in unicode */
1167 string[j] = (unsigned char) ((unicodechar >> 18) | 0xF0);
1168 j++;
1169 string[j] = (unsigned char) (((unicodechar >> 12) & 0x3F) | 0x80);
1170 j++;
1171 string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
1172 j++;
1173 string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
1175 else
1177 return FALSE;
1179 break;
1181 default:
1182 /* unnecessary escapes are allowed */
1183 if (keep_backslash)
1184 string[j++] = '\\';
1185 string[j] = string[i];
1188 else
1190 string[j] = string[i];
1192 j++;
1194 while (j < i)
1196 string[j] = 0;
1197 j++;
1199 return TRUE;
1203 /* Wraps a string in place, replacing a space with a newline character.
1204 * wrapstart is the minimum position to start wrapping or -1 for default */
1205 gboolean utils_wrap_string(gchar *string, gint wrapstart)
1207 gchar *pos, *linestart;
1208 gboolean ret = FALSE;
1210 if (wrapstart < 0)
1211 wrapstart = 80;
1213 for (pos = linestart = string; *pos != '\0'; pos++)
1215 if (pos - linestart >= wrapstart && *pos == ' ')
1217 *pos = '\n';
1218 linestart = pos;
1219 ret = TRUE;
1222 return ret;
1227 * Converts the given UTF-8 encoded string into locale encoding.
1228 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1230 * @param utf8_text UTF-8 encoded text.
1232 * @return The converted string in locale encoding, or a copy of the input string if conversion
1233 * failed. Should be freed with g_free(). If @a utf8_text is @c NULL, @c NULL is returned.
1235 gchar *utils_get_locale_from_utf8(const gchar *utf8_text)
1237 #ifdef G_OS_WIN32
1238 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1239 * which would result in wrongly converted strings */
1240 return g_strdup(utf8_text);
1241 #else
1242 gchar *locale_text;
1244 if (! utf8_text)
1245 return NULL;
1246 locale_text = g_locale_from_utf8(utf8_text, -1, NULL, NULL, NULL);
1247 if (locale_text == NULL)
1248 locale_text = g_strdup(utf8_text);
1249 return locale_text;
1250 #endif
1255 * Converts the given string (in locale encoding) into UTF-8 encoding.
1256 * On Windows platforms, it does nothing and instead it just returns a copy of the input string.
1258 * @param locale_text Text in locale encoding.
1260 * @return The converted string in UTF-8 encoding, or a copy of the input string if conversion
1261 * failed. Should be freed with g_free(). If @a locale_text is @c NULL, @c NULL is returned.
1263 gchar *utils_get_utf8_from_locale(const gchar *locale_text)
1265 #ifdef G_OS_WIN32
1266 /* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
1267 * which would result in wrongly converted strings */
1268 return g_strdup(locale_text);
1269 #else
1270 gchar *utf8_text;
1272 if (! locale_text)
1273 return NULL;
1274 utf8_text = g_locale_to_utf8(locale_text, -1, NULL, NULL, NULL);
1275 if (utf8_text == NULL)
1276 utf8_text = g_strdup(locale_text);
1277 return utf8_text;
1278 #endif
1282 /* Pass pointers to free after arg_count.
1283 * The last argument must be NULL as an extra check that arg_count is correct. */
1284 void utils_free_pointers(gsize arg_count, ...)
1286 va_list a;
1287 gsize i;
1288 gpointer ptr;
1290 va_start(a, arg_count);
1291 for (i = 0; i < arg_count; i++)
1293 ptr = va_arg(a, gpointer);
1294 g_free(ptr);
1296 ptr = va_arg(a, gpointer);
1297 if (ptr)
1298 g_warning("Wrong arg_count!");
1299 va_end(a);
1303 /* Creates a string array deep copy of a series of non-NULL strings.
1304 * The first argument is nothing special.
1305 * The list must be ended with NULL.
1306 * If first is NULL, NULL is returned. */
1307 gchar **utils_strv_new(const gchar *first, ...)
1309 gsize strvlen, i;
1310 va_list args;
1311 gchar *str;
1312 gchar **strv;
1314 g_return_val_if_fail(first != NULL, NULL);
1316 strvlen = 1; /* for first argument */
1318 /* count other arguments */
1319 va_start(args, first);
1320 for (; va_arg(args, gchar*) != NULL; strvlen++);
1321 va_end(args);
1323 strv = g_new(gchar*, strvlen + 1); /* +1 for NULL terminator */
1324 strv[0] = g_strdup(first);
1326 va_start(args, first);
1327 for (i = 1; str = va_arg(args, gchar*), str != NULL; i++)
1329 strv[i] = g_strdup(str);
1331 va_end(args);
1333 strv[i] = NULL;
1334 return strv;
1339 * Creates a directory if it doesn't already exist.
1340 * Creates intermediate parent directories as needed, too.
1341 * The permissions of the created directory are set 0700.
1343 * @param path The path of the directory to create, in locale encoding.
1344 * @param create_parent_dirs Whether to create intermediate parent directories if necessary.
1346 * @return 0 if the directory was successfully created, otherwise the @c errno of the
1347 * failed operation is returned.
1349 gint utils_mkdir(const gchar *path, gboolean create_parent_dirs)
1351 gint mode = 0700;
1352 gint result;
1354 if (path == NULL || strlen(path) == 0)
1355 return EFAULT;
1357 result = (create_parent_dirs) ? g_mkdir_with_parents(path, mode) : g_mkdir(path, mode);
1358 if (result != 0)
1359 return errno;
1360 return 0;
1365 * Gets a list of files from the specified directory.
1366 * Locale encoding is expected for @a path and used for the file list. The list and the data
1367 * in the list should be freed after use, e.g.:
1368 * @code
1369 * g_slist_foreach(list, (GFunc) g_free, NULL);
1370 * g_slist_free(list); @endcode
1372 * @note If you don't need a list you should use the foreach_dir() macro instead -
1373 * it's more efficient.
1375 * @param path The path of the directory to scan, in locale encoding.
1376 * @param full_path Whether to include the full path for each filename in the list. Obviously this
1377 * will use more memory.
1378 * @param sort Whether to sort alphabetically (UTF-8 safe).
1379 * @param error The location for storing a possible error, or @c NULL.
1381 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1382 * freed when no longer needed.
1383 * @see utils_get_file_list().
1385 GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error)
1387 GSList *list = NULL;
1388 GDir *dir;
1389 const gchar *filename;
1391 if (error)
1392 *error = NULL;
1393 g_return_val_if_fail(path != NULL, NULL);
1395 dir = g_dir_open(path, 0, error);
1396 if (dir == NULL)
1397 return NULL;
1399 foreach_dir(filename, dir)
1401 list = g_slist_append(list, full_path ?
1402 g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL) : g_strdup(filename));
1404 g_dir_close(dir);
1405 /* sorting last is quicker than on insertion */
1406 if (sort)
1407 list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
1408 return list;
1413 * Gets a sorted list of files from the specified directory.
1414 * Locale encoding is expected for @a path and used for the file list. The list and the data
1415 * in the list should be freed after use, e.g.:
1416 * @code
1417 * g_slist_foreach(list, (GFunc) g_free, NULL);
1418 * g_slist_free(list); @endcode
1420 * @param path The path of the directory to scan, in locale encoding.
1421 * @param length The location to store the number of non-@c NULL data items in the list,
1422 * unless @c NULL.
1423 * @param error The location for storing a possible error, or @c NULL.
1425 * @return A newly allocated list or @c NULL if no files were found. The list and its data should be
1426 * freed when no longer needed.
1427 * @see utils_get_file_list_full().
1429 GSList *utils_get_file_list(const gchar *path, guint *length, GError **error)
1431 GSList *list = utils_get_file_list_full(path, FALSE, TRUE, error);
1433 if (length)
1434 *length = g_slist_length(list);
1435 return list;
1439 /* returns TRUE if any letter in str is a capital, FALSE otherwise. Should be Unicode safe. */
1440 gboolean utils_str_has_upper(const gchar *str)
1442 gunichar c;
1444 if (! NZV(str) || ! g_utf8_validate(str, -1, NULL))
1445 return FALSE;
1447 while (*str != '\0')
1449 c = g_utf8_get_char(str);
1450 /* check only letters and stop once the first non-capital was found */
1451 if (g_unichar_isalpha(c) && g_unichar_isupper(c))
1452 return TRUE;
1453 /* FIXME don't write a const string */
1454 str = g_utf8_next_char(str);
1456 return FALSE;
1460 static guint utils_string_replace_helper(GString *haystack, const gchar *needle,
1461 const gchar *replace, const guint max_replaces)
1463 const gchar *stack, *match;
1464 guint ret = 0;
1465 gssize pos;
1467 g_return_val_if_fail(haystack != NULL, 0);
1468 if (haystack->len == 0)
1469 return FALSE;
1470 g_return_val_if_fail(NZV(needle), 0);
1472 stack = haystack->str;
1473 if (! (match = strstr(stack, needle)))
1474 return 0;
1477 pos = match - haystack->str;
1478 g_string_erase(haystack, pos, strlen(needle));
1480 /* make next search after removed matching text.
1481 * (we have to be careful to only use haystack->str as its address may change) */
1482 stack = haystack->str + pos;
1484 if (G_LIKELY(replace))
1486 g_string_insert(haystack, pos, replace);
1487 stack = haystack->str + pos + strlen(replace); /* skip past replacement */
1490 while (++ret != max_replaces && (match = strstr(stack, needle)));
1492 return ret;
1497 * Replaces all occurrences of @a needle in @a haystack with @a replace.
1498 * As of Geany 0.16, @a replace can match @a needle, so the following will work:
1499 * @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
1501 * @param haystack The input string to operate on. This string is modified in place.
1502 * @param needle The string which should be replaced.
1503 * @param replace The replacement for @a needle.
1505 * @return Number of replacements made.
1507 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
1509 return utils_string_replace_helper(haystack, needle, replace, 0);
1514 * Replaces only the first occurrence of @a needle in @a haystack
1515 * with @a replace.
1516 * For details, see utils_string_replace_all().
1518 * @param haystack The input string to operate on. This string is modified in place.
1519 * @param needle The string which should be replaced.
1520 * @param replace The replacement for @a needle.
1522 * @return Number of replacements made.
1524 * @since 0.16
1526 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace)
1528 return utils_string_replace_helper(haystack, needle, replace, 1);
1532 /* Get project or default startup directory (if set), or NULL. */
1533 const gchar *utils_get_default_dir_utf8(void)
1535 if (app->project && NZV(app->project->base_path))
1537 return app->project->base_path;
1540 if (NZV(prefs.default_open_path))
1542 return prefs.default_open_path;
1544 return NULL;
1548 static gboolean check_error(GError **error)
1550 if (error != NULL && *error != NULL)
1552 /* imitate the GLib warning */
1553 g_warning(
1554 "GError set over the top of a previous GError or uninitialized memory.\n"
1555 "This indicates a bug in someone's code. You must ensure an error is NULL "
1556 "before it's set.");
1557 /* after returning the code may segfault, but we don't care because we should
1558 * make sure *error is NULL */
1559 return FALSE;
1561 return TRUE;
1566 * Wraps g_spawn_sync() and internally calls this function on Unix-like
1567 * systems. On Win32 platforms, it uses the Windows API.
1569 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1570 * @param argv The child's argument vector.
1571 * @param env The child's environment, or @a NULL to inherit parent's.
1572 * @param flags Flags from GSpawnFlags.
1573 * @param child_setup A function to run in the child just before exec().
1574 * @param user_data The user data for child_setup.
1575 * @param std_out The return location for child output.
1576 * @param std_err The return location for child error messages.
1577 * @param exit_status The child exit status, as returned by waitpid().
1578 * @param error The return location for error or @a NULL.
1580 * @return @c TRUE on success, @c FALSE if an error was set.
1582 gboolean utils_spawn_sync(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
1583 GSpawnChildSetupFunc child_setup, gpointer user_data, gchar **std_out,
1584 gchar **std_err, gint *exit_status, GError **error)
1586 gboolean result;
1588 if (! check_error(error))
1589 return FALSE;
1591 if (argv == NULL)
1593 *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "argv must not be NULL");
1594 return FALSE;
1597 if (std_out)
1598 *std_out = NULL;
1600 if (std_err)
1601 *std_err = NULL;
1603 #ifdef G_OS_WIN32
1604 result = win32_spawn(dir, argv, env, flags, std_out, std_err, exit_status, error);
1605 #else
1606 result = g_spawn_sync(dir, argv, env, flags, NULL, NULL, std_out, std_err, exit_status, error);
1607 #endif
1609 return result;
1614 * Wraps g_spawn_async() and internally calls this function on Unix-like
1615 * systems. On Win32 platforms, it uses the Windows API.
1617 * @param dir The child's current working directory, or @a NULL to inherit parent's.
1618 * @param argv The child's argument vector.
1619 * @param env The child's environment, or @a NULL to inherit parent's.
1620 * @param flags Flags from GSpawnFlags.
1621 * @param child_setup A function to run in the child just before exec().
1622 * @param user_data The user data for child_setup.
1623 * @param child_pid The return location for child process ID, or NULL.
1624 * @param error The return location for error or @a NULL.
1626 * @return @c TRUE on success, @c FALSE if an error was set.
1628 gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
1629 GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
1630 GError **error)
1632 gboolean result;
1634 if (! check_error(error))
1635 return FALSE;
1637 if (argv == NULL)
1639 *error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "argv must not be NULL");
1640 return FALSE;
1643 #ifdef G_OS_WIN32
1644 result = win32_spawn(dir, argv, env, flags, NULL, NULL, NULL, error);
1645 #else
1646 result = g_spawn_async(dir, argv, env, flags, NULL, NULL, child_pid, error);
1647 #endif
1648 return result;
1652 /* Similar to g_build_path() but (re)using a fixed buffer, so never free it.
1653 * This assumes a small enough resulting string length to be kept without freeing,
1654 * but this should be the case for filenames.
1655 * @warning As the buffer is reused, you can't call this recursively, e.g. for a
1656 * function argument and within the function called. */
1657 const gchar *utils_build_path(const gchar *first, ...)
1659 static GString *buffer = NULL;
1660 const gchar *str;
1661 va_list args;
1663 if (! buffer)
1664 buffer = g_string_new(first);
1665 else
1666 g_string_assign(buffer, first);
1668 va_start(args, first);
1669 while (1)
1671 str = va_arg(args, const gchar *);
1672 if (!str)
1673 break;
1675 g_string_append_c(buffer, G_DIR_SEPARATOR);
1676 g_string_append(buffer, str);
1678 va_end(args);
1680 return buffer->str;
1684 /* Retrieves the path for the given URI.
1685 * It returns:
1686 * - the path which was determined by g_filename_from_uri() or GIO
1687 * - NULL if the URI is non-local and gvfs-fuse is not installed
1688 * - a new copy of 'uri' if it is not an URI. */
1689 gchar *utils_get_path_from_uri(const gchar *uri)
1691 gchar *locale_filename;
1693 g_return_val_if_fail(uri != NULL, NULL);
1695 if (! utils_is_uri(uri))
1696 return g_strdup(uri);
1698 /* this will work only for 'file://' URIs */
1699 locale_filename = g_filename_from_uri(uri, NULL, NULL);
1700 #ifdef HAVE_GIO
1701 /* g_filename_from_uri() failed, so we probably have a non-local URI */
1702 if (locale_filename == NULL)
1704 GFile *file = g_file_new_for_uri(uri);
1705 locale_filename = g_file_get_path(file);
1706 g_object_unref(file);
1707 if (locale_filename == NULL)
1709 geany_debug("The URI '%s' could not be resolved to a local path. This means "
1710 "that the URI is invalid or that you don't have gvfs-fuse installed.", uri);
1711 return NULL;
1714 #endif
1715 if (locale_filename == NULL)
1716 geany_debug("The URI '%s' could not be resolved to a local path. This means that the "
1717 "URI is invalid or that Geany can't use GVFS (maybe it is not installed).", uri);
1719 return locale_filename;
1723 gboolean utils_is_uri(const gchar *uri)
1725 g_return_val_if_fail(uri != NULL, FALSE);
1727 return (strstr(uri, "://") != NULL);
1731 /* path should be in locale encoding */
1732 gboolean utils_is_remote_path(const gchar *path)
1734 g_return_val_if_fail(path != NULL, FALSE);
1736 /* if path is an URI and it doesn't start "file://", we take it as remote */
1737 if (utils_is_uri(path) && strncmp(path, "file:", 5) != 0)
1738 return TRUE;
1740 #ifndef G_OS_WIN32
1741 if (glib_check_version(2, 16, 0) == NULL) /* no need to check for this with GLib < 2.16 */
1743 static gchar *fuse_path = NULL;
1744 static gsize len = 0;
1746 if (G_UNLIKELY(fuse_path == NULL))
1748 fuse_path = g_build_filename(g_get_home_dir(), ".gvfs", NULL);
1749 len = strlen(fuse_path);
1751 /* Comparing the file path against a hardcoded path is not the most elegant solution
1752 * but for now it is better than nothing. Ideally, g_file_new_for_path() should create
1753 * proper GFile objects for Fuse paths, but it only does in future GVFS
1754 * versions (gvfs 1.1.1). */
1755 return (strncmp(path, fuse_path, len) == 0);
1757 #endif
1758 return FALSE;
1762 /* Remove all relative and untidy elements from the path of @a filename.
1763 * @param filename must be a valid absolute path.
1764 * @see tm_get_real_path() - also resolves links. */
1765 void utils_tidy_path(gchar *filename)
1767 GString *str = g_string_new(filename);
1768 const gchar *c, *needle;
1769 gchar *tmp;
1770 gssize pos;
1771 gboolean preserve_double_backslash = FALSE;
1773 g_return_if_fail(g_path_is_absolute(filename));
1775 if (str->len >= 2 && strncmp(str->str, "\\\\", 2) == 0)
1776 preserve_double_backslash = TRUE;
1778 /* replace "/./" and "//" */
1779 utils_string_replace_all(str, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
1780 utils_string_replace_all(str, G_DIR_SEPARATOR_S G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
1782 if (preserve_double_backslash)
1783 g_string_prepend(str, "\\");
1785 /* replace "/../" */
1786 needle = G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S;
1787 while (1)
1789 c = strstr(str->str, needle);
1790 if (c == NULL)
1791 break;
1792 else
1794 pos = c - str->str;
1795 if (pos <= 3)
1796 break; /* bad path */
1798 /* replace "/../" */
1799 g_string_erase(str, pos, strlen(needle));
1800 g_string_insert_c(str, pos, G_DIR_SEPARATOR);
1802 tmp = g_strndup(str->str, pos); /* path up to "/../" */
1803 c = g_strrstr(tmp, G_DIR_SEPARATOR_S);
1804 g_return_if_fail(c);
1806 pos = c - tmp; /* position of previous "/" */
1807 g_string_erase(str, pos, strlen(c));
1808 g_free(tmp);
1811 g_return_if_fail(strlen(str->str) <= strlen(filename));
1812 strcpy(filename, str->str);
1813 g_string_free(str, TRUE);
1818 * Removes characters from a string, in place.
1820 * @param string String to search.
1821 * @param chars Characters to remove.
1823 * @return @a string - return value is only useful when nesting function calls, e.g.:
1824 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
1826 * @see @c g_strdelimit.
1828 gchar *utils_str_remove_chars(gchar *string, const gchar *chars)
1830 const gchar *r;
1831 gchar *w = string;
1833 g_return_val_if_fail(string, NULL);
1834 if (!NZV(chars))
1835 return string;
1837 foreach_str(r, string)
1839 if (!strchr(chars, *r))
1840 *w++ = *r;
1842 *w = 0x0;
1843 return string;
1847 static void utils_slist_remove_next(GSList *node)
1849 GSList *old = node->next;
1851 g_return_if_fail(old);
1853 node->next = old->next;
1854 g_slist_free_1(old);
1858 /* Gets list of sorted filenames with no path and no duplicates from user and system config */
1859 GSList *utils_get_config_files(const gchar *subdir)
1861 gchar *path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, subdir, NULL);
1862 GSList *list = utils_get_file_list_full(path, FALSE, FALSE, NULL);
1863 GSList *syslist, *node;
1865 if (!list)
1867 utils_mkdir(path, FALSE);
1869 setptr(path, g_build_path(G_DIR_SEPARATOR_S, app->datadir, subdir, NULL));
1870 syslist = utils_get_file_list_full(path, FALSE, FALSE, NULL);
1871 /* merge lists */
1872 list = g_slist_concat(list, syslist);
1874 list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
1875 /* remove duplicates (next to each other after sorting) */
1876 foreach_slist(node, list)
1878 if (node->next && utils_str_equal(node->next->data, node->data))
1880 g_free(node->next->data);
1881 utils_slist_remove_next(node);
1884 g_free(path);
1885 return list;
1889 /* Suffix can be NULL or a string which should be appended to the Help URL like
1890 * an anchor link, e.g. "#some_anchor". */
1891 gchar *utils_get_help_url(const gchar *suffix)
1893 gint skip;
1894 gchar *uri;
1896 #ifdef G_OS_WIN32
1897 skip = 8;
1898 uri = g_strconcat("file:///", app->docdir, "/Manual.html", NULL);
1899 g_strdelimit(uri, "\\", '/'); /* replace '\\' by '/' */
1900 #else
1901 skip = 7;
1902 uri = g_strconcat("file://", app->docdir, "index.html", NULL);
1903 #endif
1905 if (! g_file_test(uri + skip, G_FILE_TEST_IS_REGULAR))
1906 { /* fall back to online documentation if it is not found on the hard disk */
1907 g_free(uri);
1908 uri = g_strconcat(GEANY_HOMEPAGE, "manual/", VERSION, "/index.html", NULL);
1911 if (suffix != NULL)
1913 setptr(uri, g_strconcat(uri, suffix, NULL));
1916 return uri;
1920 static gboolean str_in_array(const gchar **haystack, const gchar *needle)
1922 const gchar **p;
1924 for (p = haystack; *p != NULL; ++p)
1926 if (utils_str_equal(*p, needle))
1927 return TRUE;
1929 return FALSE;
1934 * Copies the current environment into a new array.
1935 * @a exclude_vars is a @c NULL-terminated array of variable names which should be not copied.
1936 * All further arguments are key, value pairs of variables which should be added to
1937 * the environment.
1939 * The argument list must be @c NULL-terminated.
1941 * @param exclude_vars @c NULL-terminated array of variable names to exclude.
1942 * @param first_varname Name of the first variable to copy into the new array.
1943 * @param ... Key-value pairs of variable names and values, @c NULL-terminated.
1945 * @return The new environment array.
1947 gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...)
1949 gchar **result;
1950 gchar **p;
1951 gchar **env;
1952 va_list args;
1953 const gchar *key, *value;
1954 guint n, o;
1956 /* get all the environ variables */
1957 env = g_listenv();
1959 /* count the additional variables */
1960 va_start(args, first_varname);
1961 for (o = 1; va_arg(args, gchar*) != NULL; o++);
1962 va_end(args);
1963 /* the passed arguments should be even (key, value pairs) */
1964 g_return_val_if_fail(o % 2 == 0, NULL);
1966 o /= 2;
1968 /* create an array large enough to hold the new environment */
1969 n = g_strv_length(env);
1970 /* 'n + o + 1' could leak a little bit when exclude_vars is set */
1971 result = g_new(gchar *, n + o + 1);
1973 /* copy the environment */
1974 for (n = 0, p = env; *p != NULL; ++p)
1976 /* copy the variable */
1977 value = g_getenv(*p);
1978 if (G_LIKELY(value != NULL))
1980 /* skip excluded variables */
1981 if (exclude_vars != NULL && str_in_array(exclude_vars, *p))
1982 continue;
1984 result[n++] = g_strconcat(*p, "=", value, NULL);
1987 g_strfreev(env);
1989 /* now add additional variables */
1990 va_start(args, first_varname);
1991 key = first_varname;
1992 value = va_arg(args, gchar*);
1993 while (key != NULL)
1995 result[n++] = g_strconcat(key, "=", value, NULL);
1997 key = va_arg(args, gchar*);
1998 if (key == NULL)
1999 break;
2000 value = va_arg(args, gchar*);
2002 va_end(args);
2004 result[n] = NULL;
2006 return result;