Use a program run helper rather than a script
[geany-mirror.git] / src / utils.h
blobb1430fefaf3eb566de41103acb7f75e139f938df
1 /*
2 * utils.h - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /**
23 * @file: utils.h
24 * General utility functions, non-GTK related.
27 #ifndef GEANY_UTILS_H
28 #define GEANY_UTILS_H 1
30 #include <string.h>
31 #include <time.h>
33 #include <glib.h>
34 #include <gdk/gdk.h> /* for GdkColor */
36 G_BEGIN_DECLS
38 /** Returns @c TRUE if @a ptr is @c NULL or @c *ptr is @c FALSE. */
39 #define EMPTY(ptr) \
40 (!(ptr) || !*(ptr))
42 /** @deprecated 2013/08 - use @c !EMPTY() instead. */
43 #ifndef GEANY_DISABLE_DEPRECATED
44 #define NZV(ptr) (!EMPTY(ptr))
45 #endif
47 /** Assigns @a result to @a ptr, then frees the old value.
48 * @a result can be an expression using the 'old' value of @a ptr.
49 * E.g. @code SETPTR(str, g_strndup(str, 5)); @endcode
50 **/
51 #define SETPTR(ptr, result) \
52 do {\
53 gpointer setptr_tmp = ptr;\
54 ptr = result;\
55 g_free(setptr_tmp);\
56 } while (0)
58 #ifndef GEANY_DISABLE_DEPRECATED
59 /** @deprecated 2011/11/15 - use SETPTR() instead. */
60 #define setptr(ptr, result) \
62 gpointer setptr_tmp = ptr;\
63 ptr = result;\
64 g_free(setptr_tmp);\
66 #endif
68 /** Duplicates a string on the stack using @c g_alloca().
69 * Like glibc's @c strdupa(), but portable.
70 * @note You must include @c string.h yourself.
71 * @warning Don't use excessively or for long strings otherwise there may be stack exhaustion -
72 * see the GLib docs for @c g_alloca(). */
73 #define utils_strdupa(str) \
74 strcpy(g_alloca(strlen(str) + 1), str)
76 /* Get a keyfile setting, using the home keyfile if the key exists,
77 * otherwise system keyfile. */
78 #define utils_get_setting(type, home, sys, group, key, default_val)\
79 (g_key_file_has_key(home, group, key, NULL)) ?\
80 utils_get_setting_##type(home, group, key, default_val) :\
81 utils_get_setting_##type(sys, group, key, default_val)
83 /* ignore the case of filenames and paths under WIN32, causes errors if not */
84 #ifdef G_OS_WIN32
85 #define utils_filenamecmp(a, b) utils_str_casecmp((a), (b))
86 #else
87 #define utils_filenamecmp(a, b) strcmp((a), (b))
88 #endif
91 /** Iterates all the items in @a array using pointers.
92 * @param item pointer to an item in @a array.
93 * @param array C array to traverse.
94 * @param len Length of the array. */
95 #define foreach_c_array(item, array, len) \
96 for (item = array; item < &array[len]; item++)
98 /** Iterates all items in @a array.
99 * @param type Type of @a item.
100 * @param item pointer to item in @a array.
101 * @param array @c GArray to traverse. */
102 #define foreach_array(type, item, array) \
103 foreach_c_array(item, ((type*)(gpointer)array->data), array->len)
105 /** Iterates all the pointers in @a ptr_array.
106 * @param item pointer in @a ptr_array.
107 * @param idx @c guint index into @a ptr_array.
108 * @param ptr_array @c GPtrArray to traverse. */
109 #define foreach_ptr_array(item, idx, ptr_array) \
110 for (idx = 0, item = ((ptr_array)->len > 0 ? g_ptr_array_index((ptr_array), 0) : NULL); \
111 idx < (ptr_array)->len; ++idx, item = g_ptr_array_index((ptr_array), idx))
113 /** Iterates all the nodes in @a list.
114 * @param node should be a (@c GList*).
115 * @param list @c GList to traverse. */
116 #define foreach_list(node, list) \
117 for (node = list; node != NULL; node = node->next)
119 /** Iterates all the nodes in @a list.
120 * @param node should be a (@c GSList*).
121 * @param list @c GSList to traverse. */
122 #define foreach_slist(node, list) \
123 foreach_list(node, list)
125 /* Iterates all the nodes in @a list. Safe against removal during iteration
126 * @param node should be a (@c GList*).
127 * @param list @c GList to traverse. */
128 #define foreach_list_safe(node, list) \
129 for (GList *_node = (list), *_next = (list) ? (list)->next : NULL; \
130 (node = _node) != NULL; \
131 _node = _next, _next = _next ? _next->next : NULL)
133 /** Iterates through each unsorted filename in a @c GDir.
134 * @param filename (@c const @c gchar*) locale-encoded filename, without path. Do not modify or free.
135 * @param dir @c GDir created with @c g_dir_open(). Call @c g_dir_close() afterwards.
136 * @see utils_get_file_list() if you want a sorted list.
137 * @since Geany 0.19. */
138 #define foreach_dir(filename, dir)\
139 for ((filename) = g_dir_read_name(dir); (filename) != NULL; (filename) = g_dir_read_name(dir))
141 /** Iterates through each character in @a string.
142 * @param char_ptr Pointer to character.
143 * @param string String to traverse.
144 * @warning Doesn't include null terminating character.
145 * @since Geany 0.19. */
146 #define foreach_str(char_ptr, string) \
147 for (char_ptr = string; *char_ptr; char_ptr++)
149 /** Iterates a null-terminated string vector.
150 * @param str_ptr @c gchar** pointer to string element.
151 * @param strv Can be @c NULL.
152 * @since Geany 0.20 */
153 #define foreach_strv(str_ptr, strv)\
154 if (!(strv)) {} else foreach_str(str_ptr, strv)
156 /** Iterates from 0 to @a size.
157 * @param i Integer.
158 * @param size Number of iterations.
159 * @since Geany 0.20. */
160 #define foreach_range(i, size) \
161 for (i = 0; i < size; i++)
164 gboolean utils_str_equal(const gchar *a, const gchar *b);
166 guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace);
168 GSList *utils_get_file_list(const gchar *path, guint *length, GError **error);
170 GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error);
172 gint utils_write_file(const gchar *filename, const gchar *text);
174 gchar *utils_get_locale_from_utf8(const gchar *utf8_text);
176 gchar *utils_get_utf8_from_locale(const gchar *locale_text);
178 gchar *utils_remove_ext_from_filename(const gchar *filename);
180 gint utils_mkdir(const gchar *path, gboolean create_parent_dirs);
182 gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key, const gboolean default_value);
184 gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key, const gint default_value);
186 gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key, const gchar *default_value);
188 gboolean utils_spawn_sync(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
189 GSpawnChildSetupFunc child_setup, gpointer user_data, gchar **std_out,
190 gchar **std_err, gint *exit_status, GError **error);
192 gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
193 GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
194 GError **error);
196 gint utils_str_casecmp(const gchar *s1, const gchar *s2);
198 gchar *utils_get_date_time(const gchar *format, time_t *time_to_use);
200 void utils_open_browser(const gchar *uri);
202 guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace);
204 gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length);
206 gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
208 gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...) G_GNUC_NULL_TERMINATED;
210 gchar *utils_find_open_xml_tag(const gchar sel[], gint size);
212 const gchar *utils_find_open_xml_tag_pos(const gchar sel[], gint size);
215 #ifdef GEANY_PRIVATE
217 typedef enum
219 RESOURCE_DIR_DATA,
220 RESOURCE_DIR_ICON,
221 RESOURCE_DIR_DOC,
222 RESOURCE_DIR_LOCALE,
223 RESOURCE_DIR_PLUGIN,
224 RESOURCE_DIR_LIBEXEC,
226 RESOURCE_DIR_COUNT
227 } GeanyResourceDirType;
230 gint utils_get_line_endings(const gchar* buffer, gsize size);
232 gboolean utils_isbrace(gchar c, gboolean include_angles);
234 gboolean utils_is_opening_brace(gchar c, gboolean include_angles);
236 gboolean utils_is_short_html_tag(const gchar *tag_name);
238 void utils_ensure_same_eol_characters(GString *string, gint target_eol_mode);
240 const gchar *utils_get_eol_char(gint eol_mode);
242 const gchar *utils_get_eol_name(gint eol_mode);
244 const gchar *utils_get_eol_short_name(gint eol_mode);
246 gboolean utils_atob(const gchar *str);
248 void utils_tidy_path(gchar *filename);
250 gboolean utils_is_absolute_path(const gchar *path);
252 const gchar *utils_path_skip_root(const gchar *path);
254 gdouble utils_scale_round(gdouble val, gdouble factor);
256 gchar utils_brace_opposite(gchar ch);
258 gint utils_string_find(GString *haystack, gint start, gint end, const gchar *needle);
260 gint utils_string_replace(GString *str, gint pos, gint len, const gchar *replace);
262 guint utils_string_regex_replace_all(GString *haystack, GRegex *regex,
263 guint match_num, const gchar *replace, gboolean literal);
265 void utils_str_replace_all(gchar **haystack, const gchar *needle, const gchar *replacement);
267 gint utils_strpos(const gchar* haystack, const gchar *needle);
269 gchar *utils_get_initials(const gchar *name);
271 gchar *utils_get_hex_from_color(GdkColor *color);
273 const gchar *utils_get_default_dir_utf8(void);
275 gchar *utils_get_current_file_dir_utf8(void);
277 void utils_beep(void);
279 gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
280 gulong display_unit);
282 gboolean utils_parse_color(const gchar *spec, GdkColor *color);
284 gint utils_color_to_bgr(const GdkColor *color);
286 gint utils_parse_color_to_bgr(const gchar *spec);
288 gchar *utils_get_current_time_string(void);
290 GIOChannel *utils_set_up_io_channel(gint fd, GIOCondition cond, gboolean nblock,
291 GIOFunc func, gpointer data);
293 gboolean utils_str_replace_escape(gchar *string, gboolean keep_backslash);
295 gboolean utils_wrap_string(gchar *string, gint wrapstart);
297 void utils_free_pointers(gsize arg_count, ...) G_GNUC_NULL_TERMINATED;
299 gchar **utils_strv_new(const gchar *first, ...) G_GNUC_NULL_TERMINATED;
301 gchar **utils_strv_join(gchar **first, gchar **second) G_GNUC_WARN_UNUSED_RESULT;
303 GSList *utils_get_config_files(const gchar *subdir);
305 gchar *utils_get_help_url(const gchar *suffix);
307 gboolean utils_str_has_upper(const gchar *str);
309 gint utils_is_file_writable(const gchar *locale_filename);
312 gchar *utils_get_path_from_uri(const gchar *uri);
314 gboolean utils_is_uri(const gchar *uri);
316 gboolean utils_is_remote_path(const gchar *path);
318 GDate *utils_parse_date(const gchar *input);
320 gchar *utils_parse_and_format_build_date(const gchar *input);
322 gchar *utils_get_user_config_dir(void);
324 const gchar *utils_resource_dir(GeanyResourceDirType type);
326 void utils_start_new_geany_instance(const gchar *doc_path);
328 #endif /* GEANY_PRIVATE */
330 G_END_DECLS
332 #endif /* GEANY_UTILS_H */