Add 'Refresh' popup menu item (part of geany-plugins #2999858).
[geany-mirror.git] / src / utils.h
blob0e53d56eda111530cc560b45e9c11fb5b97a6542
1 /*
2 * utils.h - 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$
24 /**
25 * @file: utils.h
26 * General utility functions, non-GTK related.
29 #ifndef GEANY_UTILS_H
30 #define GEANY_UTILS_H 1
33 #include <time.h>
36 /** Returns TRUE if @a ptr points to a non-zero value. */
37 #define NZV(ptr) \
38 (G_LIKELY((ptr)) && G_LIKELY((ptr)[0]))
40 /**
41 * Frees @a ptr (if not @c NULL), then assigns @a result to it.
42 * @a result can be an expression using the 'old' value of @a ptr.
43 * It prevents a memory leak compared with: @code ptr = func(ptr); @endcode
44 **/
45 #define setptr(ptr, result) \
47 gpointer setptr_tmp = ptr;\
48 ptr = result;\
49 g_free(setptr_tmp);\
52 /** Duplicates a string on the stack using @c g_alloca().
53 * Like glibc's @c strdupa(), but portable.
54 * @note You must include @c string.h yourself.
55 * @warning Don't use excessively or for long strings otherwise there may be stack exhaustion -
56 * see the GLib docs for @c g_alloca(). */
57 #define utils_strdupa(str) \
58 strcpy(g_alloca(strlen(str) + 1), str)
60 /** Iterates all the items in @a array using pointers.
61 * @param item pointer to an item in @a array.
62 * @param array C array to traverse.
63 * @param len Length of the array. */
64 #define foreach_c_array(item, array, len) \
65 for (item = array; item < &array[len]; item++)
67 /** Iterates all items in @a array.
68 * @param type Type of @a item.
69 * @param item pointer to item in @a array.
70 * @param array @c GArray to traverse. */
71 #define foreach_array(type, item, array) \
72 foreach_c_array(item, ((type*)(gpointer)array->data), array->len)
74 /** Iterates all the pointers in @a ptr_array.
75 * @param item pointer in @a ptr_array.
76 * @param idx @c guint index into @a ptr_array.
77 * @param ptr_array @c GPtrArray to traverse. */
78 #define foreach_ptr_array(item, idx, ptr_array) \
79 for (idx = 0, item = g_ptr_array_index(ptr_array, 0); \
80 idx < ptr_array->len; ++idx, item = g_ptr_array_index(ptr_array, idx))
82 /** Iterates all the nodes in @a list.
83 * @param node should be a (@c GList*).
84 * @param list @c GList to traverse. */
85 #define foreach_list(node, list) \
86 for (node = list; node != NULL; node = node->next)
88 /** Iterates all the nodes in @a list.
89 * @param node should be a (@c GSList*).
90 * @param list @c GSList to traverse. */
91 #define foreach_slist(node, list) \
92 foreach_list(node, list)
94 /** Iterates through each unsorted filename in a @c GDir.
95 * @param filename (@c const @c gchar*) locale-encoded filename, without path. Do not modify or free.
96 * @param dir @c GDir created with @c g_dir_open(). Call @c g_dir_close() afterwards.
97 * @see utils_get_file_list() if you want a sorted list.
98 * @since Geany 0.19. */
99 #define foreach_dir(filename, dir)\
100 for ((filename) = g_dir_read_name(dir); (filename) != NULL; (filename) = g_dir_read_name(dir))
102 /** Iterates through each character in @a string.
103 * @param char_ptr Pointer to character.
104 * @param string String to traverse.
105 * @warning Doesn't include null terminating character.
106 * @since Geany 0.19. */
107 #define foreach_str(char_ptr, string) \
108 for (char_ptr = string; *char_ptr; char_ptr++)
111 void utils_open_browser(const gchar *uri);
113 gint utils_get_line_endings(const gchar* buffer, glong size);
115 gboolean utils_isbrace(gchar c, gboolean include_angles);
117 gboolean utils_is_opening_brace(gchar c, gboolean include_angles);
119 gint utils_write_file(const gchar *filename, const gchar *text);
121 gchar *utils_find_open_xml_tag(const gchar sel[], gint size, gboolean check_tag);
123 const gchar *utils_get_eol_name(gint eol_mode);
125 gboolean utils_atob(const gchar *str);
127 void utils_tidy_path(gchar *filename);
129 gboolean utils_is_absolute_path(const gchar *path);
131 const gchar *utils_path_skip_root(const gchar *path);
133 gdouble utils_scale_round(gdouble val, gdouble factor);
135 gboolean utils_str_equal(const gchar *a, const gchar *b);
137 gchar *utils_remove_ext_from_filename(const gchar *filename);
139 gchar utils_brace_opposite(gchar ch);
141 gchar *utils_get_hostname(void);
143 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace);
145 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace);
147 void utils_str_replace_all(gchar **haystack, const gchar *needle, const gchar *replacement);
149 gint utils_strpos(const gchar* haystack, const gchar *needle);
151 gchar *utils_get_date_time(const gchar *format, time_t *time_to_use);
153 gchar *utils_get_initials(const gchar *name);
155 gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key, const gboolean default_value);
157 gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key, const gint default_value);
159 gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key, const gchar *default_value);
161 gchar *utils_get_hex_from_color(GdkColor *color);
163 guint utils_invert_color(guint color);
165 const gchar *utils_get_default_dir_utf8(void);
167 gchar *utils_get_current_file_dir_utf8(void);
169 void utils_beep(void);
171 gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
172 gulong display_unit);
174 gint utils_strtod(const gchar *source, gchar **end, gboolean with_route);
176 gchar *utils_get_current_time_string(void);
178 GIOChannel *utils_set_up_io_channel(gint fd, GIOCondition cond, gboolean nblock,
179 GIOFunc func, gpointer data);
181 gchar **utils_read_file_in_array(const gchar *filename);
183 gboolean utils_str_replace_escape(gchar *string, gboolean keep_backslash);
185 gboolean utils_wrap_string(gchar *string, gint wrapstart);
187 gchar *utils_get_locale_from_utf8(const gchar *utf8_text);
189 gchar *utils_get_utf8_from_locale(const gchar *locale_text);
191 void utils_free_pointers(gsize arg_count, ...) G_GNUC_NULL_TERMINATED;
193 gchar **utils_strv_new(const gchar *first, ...) G_GNUC_NULL_TERMINATED;
195 gint utils_mkdir(const gchar *path, gboolean create_parent_dirs);
197 GSList *utils_get_file_list(const gchar *path, guint *length, GError **error);
199 GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error);
201 GSList *utils_get_config_files(const gchar *subdir);
203 gchar *utils_get_help_url(const gchar *suffix);
205 gboolean utils_str_has_upper(const gchar *str);
207 gint utils_is_file_writeable(const gchar *locale_filename);
210 gboolean utils_spawn_sync(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
211 GSpawnChildSetupFunc child_setup, gpointer user_data, gchar **std_out,
212 gchar **std_err, gint *exit_status, GError **error);
214 gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
215 GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
216 GError **error);
218 gint utils_str_casecmp(const gchar *s1, const gchar *s2);
220 const gchar *utils_build_path(const gchar *first, ...) G_GNUC_NULL_TERMINATED;
222 gchar *utils_get_path_from_uri(const gchar *uri);
224 gboolean utils_is_uri(const gchar *uri);
226 gboolean utils_is_remote_path(const gchar *path);
228 gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length);
230 gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
232 gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...) G_GNUC_NULL_TERMINATED;
234 #endif