geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

utils.h File Reference

General utility functions, non-GTK related. More...

#include <string.h>
#include <time.h>
#include <glib.h>
#include <gdk/gdk.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define EMPTY(ptr)    (!(ptr) || !*(ptr))
 Returns TRUE if ptr is NULL or *ptr is FALSE. More...
 
#define NZV(ptr)   (!EMPTY(ptr))
 
#define SETPTR(ptr, result)
 Assigns result to ptr, then frees the old value. More...
 
#define setptr(ptr, result)
 
#define utils_strdupa(str)    strcpy(g_alloca(strlen(str) + 1), str)
 Duplicates a string on the stack using g_alloca(). More...
 
#define utils_get_setting(type, home, sys, group, key, default_val)
 
#define utils_filenamecmp(a, b)   strcmp((a), (b))
 
#define foreach_c_array(item, array, len)    for (item = array; item < &array[len]; item++)
 Iterates all the items in array using pointers. More...
 
#define foreach_array(type, item, array)    foreach_c_array(item, ((type*)(gpointer)array->data), array->len)
 Iterates all items in array. More...
 
#define foreach_ptr_array(item, idx, ptr_array)
 Iterates all the pointers in ptr_array. More...
 
#define foreach_list(node, list)    for (node = list; node != NULL; node = node->next)
 Iterates all the nodes in list. More...
 
#define foreach_slist(node, list)    foreach_list(node, list)
 Iterates all the nodes in list. More...
 
#define foreach_list_safe(node, list)
 
#define foreach_dir(filename, dir)    for ((filename) = g_dir_read_name(dir); (filename) != NULL; (filename) = g_dir_read_name(dir))
 Iterates through each unsorted filename in a GDir. More...
 
#define foreach_str(char_ptr, string)    for (char_ptr = string; *char_ptr; char_ptr++)
 Iterates through each character in string. More...
 
#define foreach_strv(str_ptr, strv)    if (!(strv)) {} else foreach_str(str_ptr, strv)
 Iterates a null-terminated string vector. More...
 
#define foreach_range(i, size)    for (i = 0; i < size; i++)
 Iterates from 0 to size. More...
 

Functions

gboolean utils_str_equal (const gchar *a, const gchar *b)
 NULL-safe string comparison. More...
 
guint utils_string_replace_all (GString *haystack, const gchar *needle, const gchar *replace)
 Replaces all occurrences of needle in haystack with replace. More...
 
GSList * utils_get_file_list (const gchar *path, guint *length, GError **error)
 Gets a sorted list of files from the specified directory. More...
 
GSList * utils_get_file_list_full (const gchar *path, gboolean full_path, gboolean sort, GError **error)
 Gets a list of files from the specified directory. More...
 
gint utils_write_file (const gchar *filename, const gchar *text)
 Writes text into a file named filename. More...
 
gchar * utils_get_locale_from_utf8 (const gchar *utf8_text)
 Converts the given UTF-8 encoded string into locale encoding. More...
 
gchar * utils_get_utf8_from_locale (const gchar *locale_text)
 Converts the given string (in locale encoding) into UTF-8 encoding. More...
 
gchar * utils_remove_ext_from_filename (const gchar *filename)
 Removes the extension from filename and return the result in a newly allocated string. More...
 
gint utils_mkdir (const gchar *path, gboolean create_parent_dirs)
 Creates a directory if it doesn't already exist. More...
 
gboolean utils_get_setting_boolean (GKeyFile *config, const gchar *section, const gchar *key, const gboolean default_value)
 Wraps g_key_file_get_boolean() to add a default value argument. More...
 
gint utils_get_setting_integer (GKeyFile *config, const gchar *section, const gchar *key, const gint default_value)
 Wraps g_key_file_get_integer() to add a default value argument. More...
 
gchar * utils_get_setting_string (GKeyFile *config, const gchar *section, const gchar *key, const gchar *default_value)
 Wraps g_key_file_get_string() to add a default value argument. More...
 
gboolean utils_spawn_sync (const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags, GSpawnChildSetupFunc child_setup, gpointer user_data, gchar **std_out, gchar **std_err, gint *exit_status, GError **error)
 Wraps spawn_sync(), which see. More...
 
gboolean utils_spawn_async (const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags, GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid, GError **error)
 Wraps spawn_async(), which see. More...
 
gint utils_str_casecmp (const gchar *s1, const gchar *s2)
 A replacement function for g_strncasecmp() to compare strings case-insensitive. More...
 
gchar * utils_get_date_time (const gchar *format, time_t *time_to_use)
 Retrieves a formatted date/time string from strftime(). More...
 
void utils_open_browser (const gchar *uri)
 Tries to open the given URI in a browser. More...
 
guint utils_string_replace_first (GString *haystack, const gchar *needle, const gchar *replace)
 Replaces only the first occurrence of needle in haystack with replace. More...
 
gchar * utils_str_middle_truncate (const gchar *string, guint truncate_length)
 Truncates the input string to a given length. More...
 
gchar * utils_str_remove_chars (gchar *string, const gchar *chars)
 Removes characters from a string, in place. More...
 
gchar ** utils_copy_environment (const gchar **exclude_vars, const gchar *first_varname,...) G_GNUC_NULL_TERMINATED
 Copies the current environment into a new array. More...
 
gchar * utils_find_open_xml_tag (const gchar sel[], gint size)
 Searches backward through size bytes looking for a '<'. More...
 
const gchar * utils_find_open_xml_tag_pos (const gchar sel[], gint size)
 Searches backward through size bytes looking for a '<'. More...
 
gchar * utils_get_real_path (const gchar *file_name)
 Get a link-dereferenced, absolute version of a file name. More...
 
gchar ** utils_strv_shorten_file_list (gchar **file_names, gssize file_names_len)
 Transform file names in a list to be shorter. More...
 

Detailed Description

General utility functions, non-GTK related.

Definition in file utils.h.

Macro Definition Documentation

◆ EMPTY

#define EMPTY (   ptr)     (!(ptr) || !*(ptr))

Returns TRUE if ptr is NULL or *ptr is FALSE.

Definition at line 38 of file utils.h.

◆ foreach_array

#define foreach_array (   type,
  item,
  array 
)     foreach_c_array(item, ((type*)(gpointer)array->data), array->len)

Iterates all items in array.

Parameters
typeType of item.
itempointer to item in array.
arrayGArray to traverse.

Definition at line 101 of file utils.h.

◆ foreach_c_array

#define foreach_c_array (   item,
  array,
  len 
)     for (item = array; item < &array[len]; item++)

Iterates all the items in array using pointers.

Parameters
itempointer to an item in array.
arrayC array to traverse.
lenLength of the array.

Definition at line 94 of file utils.h.

◆ foreach_dir

#define foreach_dir (   filename,
  dir 
)     for ((filename) = g_dir_read_name(dir); (filename) != NULL; (filename) = g_dir_read_name(dir))

Iterates through each unsorted filename in a GDir.

Parameters
filename(const gchar*) locale-encoded filename, without path. Do not modify or free.
dirGDir created with g_dir_open(). Call g_dir_close() afterwards.
See also
utils_get_file_list() if you want a sorted list.
Since
Geany 0.19.

Definition at line 137 of file utils.h.

◆ foreach_list

#define foreach_list (   node,
  list 
)     for (node = list; node != NULL; node = node->next)

Iterates all the nodes in list.

Parameters
nodeshould be a (GList*).
listGList to traverse.

Definition at line 115 of file utils.h.

◆ foreach_list_safe

#define foreach_list_safe (   node,
  list 
)
Value:
for (GList *_node = (list), *_next = (list) ? (list)->next : NULL; \
(node = _node) != NULL; \
_node = _next, _next = _next ? _next->next : NULL)
tokenInfo * list
#define NULL
Definition: rbtree.h:150

Definition at line 127 of file utils.h.

◆ foreach_ptr_array

#define foreach_ptr_array (   item,
  idx,
  ptr_array 
)
Value:
for (idx = 0, item = ((ptr_array)->len > 0 ? g_ptr_array_index((ptr_array), 0) : NULL); \
idx < (ptr_array)->len; ++idx, item = g_ptr_array_index((ptr_array), idx))

Iterates all the pointers in ptr_array.

Parameters
itempointer in ptr_array.
idxguint index into ptr_array.
ptr_arrayGPtrArray to traverse.

Definition at line 108 of file utils.h.

◆ foreach_range

#define foreach_range (   i,
  size 
)     for (i = 0; i < size; i++)

Iterates from 0 to size.

Parameters
iInteger.
sizeNumber of iterations.
Since
Geany 0.20.

Definition at line 159 of file utils.h.

◆ foreach_slist

#define foreach_slist (   node,
  list 
)     foreach_list(node, list)

Iterates all the nodes in list.

Parameters
nodeshould be a (GSList*).
listGSList to traverse.

Definition at line 121 of file utils.h.

◆ foreach_str

#define foreach_str (   char_ptr,
  string 
)     for (char_ptr = string; *char_ptr; char_ptr++)

Iterates through each character in string.

Parameters
char_ptrPointer to character.
stringString to traverse.
Warning
Doesn't include null terminating character.
Since
Geany 0.19.

Definition at line 145 of file utils.h.

◆ foreach_strv

#define foreach_strv (   str_ptr,
  strv 
)     if (!(strv)) {} else foreach_str(str_ptr, strv)

Iterates a null-terminated string vector.

Parameters
str_ptrgchar** pointer to string element.
strvCan be NULL.
Since
Geany 0.20

Definition at line 152 of file utils.h.

◆ NZV

#define NZV (   ptr)    (!EMPTY(ptr))
Deprecated:
2013/08 - use !EMPTY() instead.

Definition at line 43 of file utils.h.

◆ SETPTR

#define SETPTR (   ptr,
  result 
)
Value:
do {\
gpointer setptr_tmp = ptr;\
ptr = result;\
g_free(setptr_tmp);\
} while (0)
GeanyBuildCommand ** ptr
Definition: build.c:2679

Assigns result to ptr, then frees the old value.

result can be an expression using the 'old' value of ptr. E.g.

SETPTR(str, g_strndup(str, 5));
#define SETPTR(ptr, result)
Assigns result to ptr, then frees the old value.
Definition: utils.h:50

Definition at line 50 of file utils.h.

◆ setptr

#define setptr (   ptr,
  result 
)
Value:
{\
gpointer setptr_tmp = ptr;\
ptr = result;\
g_free(setptr_tmp);\
}
Deprecated:
2011/11/15 - use SETPTR() instead.

Definition at line 59 of file utils.h.

◆ utils_filenamecmp

#define utils_filenamecmp (   a,
 
)    strcmp((a), (b))

Definition at line 86 of file utils.h.

◆ utils_get_setting

#define utils_get_setting (   type,
  home,
  sys,
  group,
  key,
  default_val 
)
Value:
(g_key_file_has_key(home, group, key, NULL)) ?\
utils_get_setting_##type(home, group, key, default_val) :\
utils_get_setting_##type(sys, group, key, default_val)
StashGroup * group
Definition: stash-example.c:1

Definition at line 77 of file utils.h.

◆ utils_strdupa

#define utils_strdupa (   str)     strcpy(g_alloca(strlen(str) + 1), str)

Duplicates a string on the stack using g_alloca().

Like glibc's strdupa(), but portable.

Note
You must include string.h yourself.
Warning
Don't use excessively or for long strings otherwise there may be stack exhaustion - see the GLib docs for g_alloca().

Definition at line 72 of file utils.h.

Function Documentation

◆ utils_copy_environment()

gchar ** utils_copy_environment ( const gchar **  exclude_vars,
const gchar *  first_varname,
  ... 
)

Copies the current environment into a new array.

exclude_vars is a NULL-terminated array of variable names which should be not copied. All further arguments are key, value pairs of variables which should be added to the environment.

The argument list must be NULL-terminated.

Parameters
exclude_varsNULL-terminated array of variable names to exclude.
first_varnameName of the first variable to copy into the new array.
...Key-value pairs of variable names and values, NULL-terminated.
Returns
(transfer: full) The new environment array. Use g_strfreev() to free it.

Definition at line 1956 of file utils.c.

References NULL, and str_in_array().

Referenced by run_command().

◆ utils_find_open_xml_tag()

gchar * utils_find_open_xml_tag ( const gchar  sel[],
gint  size 
)

Searches backward through size bytes looking for a '<'.

Parameters
sel.
size.
Returns
The tag name (newly allocated) or NULL if no opening tag was found.

Definition at line 269 of file utils.c.

References NULL, and utils_find_open_xml_tag_pos().

Referenced by get_xml_indent(), and handle_xml().

◆ utils_find_open_xml_tag_pos()

const gchar * utils_find_open_xml_tag_pos ( const gchar  sel[],
gint  size 
)

Searches backward through size bytes looking for a '<'.

Parameters
sel.
size.
Returns
pointer to '<' of the found opening tag within sel, or NULL if no opening tag was found.

Definition at line 294 of file utils.c.

References NULL.

Referenced by utils_find_open_xml_tag().

◆ utils_get_date_time()

gchar * utils_get_date_time ( const gchar *  format,
time_t *  time_to_use 
)

Retrieves a formatted date/time string from strftime().

This function should be preferred to directly calling strftime() since this function works on UTF-8 encoded strings.

Parameters
formatThe format string to pass to strftime(3). See the strftime(3) documentation for details, in UTF-8 encoding.
time_to_use The date/time to use, in time_t format or NULL to use the current time.
Returns
A newly-allocated string, should be freed when no longer needed.
Since
0.16

Definition at line 736 of file utils.c.

References format, and NULL.

Referenced by add_page_header(), backupcopy_document_save_cb(), get_date(), insert_date(), and templates_replace_default_dates().

◆ utils_get_file_list()

GSList * utils_get_file_list ( const gchar *  path,
guint *  length,
GError **  error 
)

Gets a sorted list of files from the specified directory.

Locale encoding is expected for path and used for the file list. The list and the data in the list should be freed after use, e.g.:

g_slist_foreach(list, (GFunc) g_free, NULL);
g_slist_free(list);
Parameters
pathThe path of the directory to scan, in locale encoding.
lengthThe location to store the number of non-NULL data items in the list, unless NULL.
errorThe location for storing a possible error, or NULL.
Returns
(element-type: filename) (transfer: full) A newly allocated list or NULL if no files were found. The list and its data should be freed when no longer needed.
See also
utils_get_file_list_full().

Definition at line 1441 of file utils.c.

References error(), list, and utils_get_file_list_full().

Referenced by load_plugins_from_path(), refresh(), and search_get_argv().

◆ utils_get_file_list_full()

GSList * utils_get_file_list_full ( const gchar *  path,
gboolean  full_path,
gboolean  sort,
GError **  error 
)

Gets a list of files from the specified directory.

Locale encoding is expected for path and used for the file list. The list and the data in the list should be freed after use, e.g.:

g_slist_foreach(list, (GFunc) g_free, NULL);
g_slist_free(list);
Note
If you don't need a list you should use the foreach_dir() macro instead - it's more efficient.
Parameters
pathThe path of the directory to scan, in locale encoding.
full_pathWhether to include the full path for each filename in the list. Obviously this will use more memory.
sortWhether to sort alphabetically (UTF-8 safe).
errorThe location for storing a possible error, or NULL.
Returns
(element-type: filename) (transfer: full) A newly allocated list or NULL if no files were found. The list and its data should be freed when no longer needed.
See also
utils_get_file_list().

Definition at line 1396 of file utils.c.

References error(), filename, foreach_dir, list, NULL, and utils_str_casecmp().

Referenced by init_user_tags(), utils_get_config_files(), and utils_get_file_list().

◆ utils_get_locale_from_utf8()

gchar * utils_get_locale_from_utf8 ( const gchar *  utf8_text)

Converts the given UTF-8 encoded string into locale encoding.

On Windows platforms, it does nothing and instead it just returns a copy of the input string.

Parameters
utf8_textUTF-8 encoded text.
Returns
The converted string in locale encoding, or a copy of the input string if conversion failed. Should be freed with g_free(). If utf8_text is NULL, NULL is returned.

Definition at line 1243 of file utils.c.

References NULL.

Referenced by backupcopy_document_save_cb(), build_run_cmd(), build_spawn_cmd(), cb_func_file_action(), create_file_save_as_dialog(), create_save_file_dialog(), detect_filetype_conf_file(), dialogs_show_file_properties(), dialogs_show_open_file(), document_activate_cb(), document_check_disk_status(), document_open_file_full(), document_rename_file(), document_save_file(), document_update_tags(), filetypes_detect_from_file(), get_custom_plugin_path(), get_default_dir(), get_real_path_from_utf8(), get_session_file_string(), get_tree_path_filename(), goto_compiler_file_line(), load_session_project_file(), monitor_file_setup(), msgwin_parse_generic_line(), on_color_scheme_changed(), on_current_path(), on_menu_open_selected_file1_activate(), on_new_with_file_template(), on_open_in_new_window_activate(), on_path_entry_activate(), open_external(), open_session_file(), prepare_run_cmd(), project_open(), project_open_cb(), recent_file_activate_cb(), recent_project_activate_cb(), run_dialog(), run_file_chooser(), search_find_in_files(), setup_paths(), show_save_as_gtk(), store_target_directory(), target_directory_button_clicked_cb(), update_config(), and write_config().

◆ utils_get_real_path()

gchar * utils_get_real_path ( const gchar *  file_name)

Get a link-dereferenced, absolute version of a file name.

This is similar to the POSIX realpath function when passed a NULL argument.

Warning
This function suffers the same problems as the POSIX function realpath(), namely that it's impossible to determine a suitable size for the returned buffer, and so it's limited to a maximum of PATH_MAX.
Parameters
file_nameThe file name to get the real path of.
Returns
A newly-allocated string containing the real path which should be freed with g_free() when no longer needed, or NULL if the real path cannot be obtained.
Since
1.32 (API 235)

Definition at line 2406 of file utils.c.

References tm_get_real_path().

Referenced by document_open_file_full(), get_real_path_from_utf8(), save_doc(), and setup_config_dir().

◆ utils_get_setting_boolean()

gboolean utils_get_setting_boolean ( GKeyFile *  config,
const gchar *  section,
const gchar *  key,
const gboolean  default_value 
)

Wraps g_key_file_get_boolean() to add a default value argument.

Parameters
configA GKeyFile object.
sectionThe group name to look in for the key.
keyThe key to find.
default_valueThe default value which will be returned when section or key don't exist.
Returns
The value associated with key as a boolean, or the given default value if the value could not be retrieved.

Definition at line 836 of file utils.c.

References error(), and NULL.

Referenced by handle_boolean_setting(), init_configuration(), load_dialog_prefs(), load_settings(), load_ui_prefs(), and plugin_init().

◆ utils_get_setting_integer()

gint utils_get_setting_integer ( GKeyFile *  config,
const gchar *  section,
const gchar *  key,
const gint  default_value 
)

Wraps g_key_file_get_integer() to add a default value argument.

Parameters
configA GKeyFile object.
sectionThe group name to look in for the key.
keyThe key to find.
default_valueThe default value which will be returned when section or key don't exist.
Returns
The value associated with key as an integer, or the given default value if the value could not be retrieved.

Definition at line 805 of file utils.c.

References error(), and NULL.

Referenced by configuration_load_session_files(), handle_integer_setting(), load_config(), load_dialog_prefs(), load_ui_prefs(), and plugin_init().

◆ utils_get_setting_string()

gchar * utils_get_setting_string ( GKeyFile *  config,
const gchar *  section,
const gchar *  key,
const gchar *  default_value 
)

Wraps g_key_file_get_string() to add a default value argument.

Parameters
configA GKeyFile object.
sectionThe group name to look in for the key.
keyThe key to find.
default_valueThe default value which will be returned when section or key don't exist.
Returns
A newly allocated string, either the value for key or a copy of the given default value if it could not be retrieved.

Definition at line 867 of file utils.c.

References NULL.

Referenced by configuration_load_session_files(), handle_string_setting(), load_config(), load_dialog_prefs(), load_settings(), load_ui_prefs(), plugin_init(), project_load_prefs(), and snippets_load().

◆ utils_get_utf8_from_locale()

gchar * utils_get_utf8_from_locale ( const gchar *  locale_text)

Converts the given string (in locale encoding) into UTF-8 encoding.

On Windows platforms, it does nothing and instead it just returns a copy of the input string.

Parameters
locale_textText in locale encoding.
Returns
The converted string in UTF-8 encoding, or a copy of the input string if conversion failed. Should be freed with g_free(). If locale_text is NULL, NULL is returned.

Definition at line 1272 of file utils.c.

References NULL.

Referenced by add_color_scheme_file(), add_file_item(), add_item(), add_top_level_entry(), build_run_cmd(), document_open_file_full(), get_mtime(), goto_compiler_file_line(), init_user_tags(), load_config(), main_handle_filename(), main_lib(), msgwin_compiler_add_string(), msgwin_msg_add_string(), msgwin_parse_compiler_error_line(), on_color_scheme_changed(), on_config_file_clicked(), on_file_save_dialog_response(), on_find_in_files(), on_menu_open_selected_file1_activate(), on_new_with_file_template(), project_load_file(), read_file(), refresh(), run_dialog(), run_file_chooser(), run_open_dialog(), save_as_dialog_handle_response(), symbols_generate_global_tags(), symbols_show_load_tags_dialog(), target_directory_button_clicked_cb(), and write_data().

◆ utils_mkdir()

gint utils_mkdir ( const gchar *  path,
gboolean  create_parent_dirs 
)

Creates a directory if it doesn't already exist.

Creates intermediate parent directories as needed, too. The permissions of the created directory are set 0700.

Parameters
pathThe path of the directory to create, in locale encoding.
create_parent_dirsWhether to create intermediate parent directories if necessary.
Returns
0 if the directory was successfully created, otherwise the errno of the failed operation is returned.

Definition at line 1359 of file utils.c.

References errno, and NULL.

Referenced by backupcopy_create_dir_parts(), configure_response_cb(), create_config_dir(), init_user_tags(), save_settings(), set_status(), update_config(), and utils_get_config_files().

◆ utils_open_browser()

void utils_open_browser ( const gchar *  uri)

Tries to open the given URI in a browser.

On Windows, the system's default browser is opened. On non-Windows systems, the browser command set in the preferences dialog is used. In case that fails or it is unset, the user is asked to correct or fill it.

Parameters
uriThe URI to open in the web browser.
Since
0.16

Definition at line 75 of file utils.c.

References _, GeanyToolPrefs::browser_cmd, dialogs_show_input(), main_widgets, NULL, SETPTR, spawn_async(), tool_prefs, and GeanyMainWidgets::window.

Referenced by homepage_clicked(), on_build_menu_item(), on_help1_activate(), on_help_menu_item_bug_report_activate(), on_help_menu_item_donate_activate(), on_help_menu_item_wiki_activate(), on_website1_activate(), and open_preferences_help().

◆ utils_remove_ext_from_filename()

gchar * utils_remove_ext_from_filename ( const gchar *  filename)

Removes the extension from filename and return the result in a newly allocated string.

Parameters
filenameThe filename to operate on.
Returns
A newly-allocated string, should be freed when no longer needed.

Definition at line 617 of file utils.c.

References filename, and NULL.

Referenced by build_replace_placeholder().

◆ utils_spawn_async()

gboolean utils_spawn_async ( const gchar *  dir,
gchar **  argv,
gchar **  env,
GSpawnFlags  flags,
GSpawnChildSetupFunc  child_setup,
gpointer  user_data,
GPid *  child_pid,
GError **  error 
)

Wraps spawn_async(), which see.

Parameters
dir The child's current working directory, or NULL to inherit parent's.
argvThe child's argument vector.
env The child's environment, or NULL to inherit parent's.
flagsIgnored.
child_setup Ignored.
user_dataIgnored.
child_pid (out) The return location for child process ID, or NULL.
errorThe return location for error or NULL.
Returns
TRUE on success, FALSE if an error was set.

Definition at line 1680 of file utils.c.

References error(), NULL, and spawn_async().

Referenced by utils_start_new_geany_instance().

◆ utils_spawn_sync()

gboolean utils_spawn_sync ( const gchar *  dir,
gchar **  argv,
gchar **  env,
GSpawnFlags  flags,
GSpawnChildSetupFunc  child_setup,
gpointer  user_data,
gchar **  std_out,
gchar **  std_err,
gint *  exit_status,
GError **  error 
)

Wraps spawn_sync(), which see.

Parameters
dir The child's current working directory, or NULL to inherit parent's.
argvThe child's argument vector.
env The child's environment, or NULL to inherit parent's.
flagsIgnored.
child_setup Ignored.
user_data Ignored.
std_out (out) The return location for child output, or NULL.
std_err (out) The return location for child error messages, or NULL.
exit_status (out) The child exit status, as returned by waitpid(), or NULL.
errorThe return location for error or NULL.
Returns
TRUE on success, FALSE if an error was set.

Definition at line 1647 of file utils.c.

References error(), NULL, and spawn_sync().

◆ utils_str_casecmp()

gint utils_str_casecmp ( const gchar *  s1,
const gchar *  s2 
)

A replacement function for g_strncasecmp() to compare strings case-insensitive.

It converts both strings into lowercase using g_utf8_strdown() and then compare both strings using strcmp(). This is not completely accurate regarding locale-specific case sorting rules but seems to be a good compromise between correctness and performance.

The input strings should be in UTF-8 or locale encoding.

Parameters
s1 Pointer to first string or NULL.
s2 Pointer to second string or NULL.
Returns
an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or to be greater than s2.
Since
0.16

Definition at line 506 of file utils.c.

References NULL, and utf8_strdown().

Referenced by cmp_filetype(), compare_menu_item_labels(), get_doc_words(), is_plugin(), utils_get_config_files(), and utils_get_file_list_full().

◆ utils_str_equal()

gboolean utils_str_equal ( const gchar *  a,
const gchar *  b 
)

NULL-safe string comparison.

Returns TRUE if both a and b are NULL or if a and b refer to valid strings which are equal.

Parameters
a Pointer to first string or NULL.
b Pointer to second string or NULL.
Returns
TRUE if a equals b, else FALSE.

Definition at line 599 of file utils.c.

References NULL.

Referenced by add_color_scheme_item(), add_item(), check_filtered(), compare_top_level_names(), demoproxy_probe(), document_activate_cb(), document_open_file_full(), document_save_file(), document_set_encoding(), encodings_radio_item_change_cb(), encodings_select_radio_item(), find_active_plugin_by_name(), find_by_filename(), find_calltip(), find_recent_file_item(), find_toplevel_iter(), find_tree_iter_dir(), gb_on_key_pressed(), get_custom_plugin_path(), get_entity(), get_symbol_name(), get_symbol_tooltip(), handle_buffer(), handle_forced_encoding(), insert_date(), load_dialog_prefs(), main_get_version_string(), on_document_save(), open_preferences_help(), plugin_configure(), plugin_loaded(), project_get_base_path(), project_open_cb(), queue_pos_matches(), search_show_find_in_files_dialog_full(), set_stop_button(), show_prompt(), sidebar_openfiles_update(), str_in_array(), tag_equal(), tb_editor_btn_add_clicked_cb(), tb_editor_btn_remove_clicked_cb(), tb_editor_drag_data_rcvd_cb(), tb_editor_foreach_used(), tb_editor_handler_start_element(), tb_editor_set_item_values(), toolbar_notify_style_cb(), tree_model_find_text(), tree_sort_func(), update_changed_state(), update_parents_table(), and utils_get_config_files().

◆ utils_str_middle_truncate()

gchar * utils_str_middle_truncate ( const gchar *  string,
guint  truncate_length 
)

Truncates the input string to a given length.

Characters are removed from the middle of the string, so the start and the end of string won't change.

Parameters
stringInput string.
truncate_lengthThe length in characters of the resulting string.
Returns
A copy of string which is truncated to truncate_length characters, should be freed when no longer needed.
Since
0.17

Definition at line 549 of file utils.c.

References NULL.

Referenced by dialogs_show_file_properties(), document_get_basename_for_display(), and document_open_file_full().

◆ utils_str_remove_chars()

gchar * utils_str_remove_chars ( gchar *  string,
const gchar *  chars 
)

Removes characters from a string, in place.

Parameters
stringString to search.
charsCharacters to remove.
Returns
string - return value is only useful when nesting function calls, e.g.:
str = utils_str_remove_chars(g_strdup("f_o_o"), "_");
gchar * utils_str_remove_chars(gchar *string, const gchar *chars)
Removes characters from a string, in place.
Definition: utils.c:1847
See also
g_strdelimit.

Definition at line 1847 of file utils.c.

References chars, EMPTY, foreach_str, and NULL.

Referenced by keybindings_get_label(), tb_editor_set_item_values(), and ui_tool_button_new().

◆ utils_string_replace_all()

guint utils_string_replace_all ( GString *  haystack,
const gchar *  needle,
const gchar *  replace 
)

Replaces all occurrences of needle in haystack with replace.

As of Geany 0.16, replace can match needle, so the following will work:

gchar * text
Definition: editor.c:83
guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
Replaces all occurrences of needle in haystack with replace.
Definition: utils.c:1529
Parameters
haystackThe input string to operate on. This string is modified in place.
needleThe string which should be replaced.
replaceThe replacement for needle.
Returns
Number of replacements made.

Definition at line 1529 of file utils.c.

References count, pos, utils_string_find(), and utils_string_replace().

Referenced by build_replace_placeholder(), editor_insert_text_block(), fix_indentation(), get_grep_options(), open_external(), pm_tree_search(), replace_static_values(), snippets_make_replacements(), snippets_replace_specials(), templates_replace_valist(), update_config(), utils_ensure_same_eol_characters(), utils_str_replace_all(), utils_tidy_path(), write_html_file(), and write_latex_file().

◆ utils_string_replace_first()

guint utils_string_replace_first ( GString *  haystack,
const gchar *  needle,
const gchar *  replace 
)

Replaces only the first occurrence of needle in haystack with replace.

For details, see utils_string_replace_all().

Parameters
haystackThe input string to operate on. This string is modified in place.
needleThe string which should be replaced.
replaceThe replacement for needle.
Returns
Number of replacements made.
Since
0.16

Definition at line 1563 of file utils.c.

References pos, utils_string_find(), and utils_string_replace().

Referenced by on_path_entry_activate(), and templates_replace_command().

◆ utils_strv_shorten_file_list()

gchar ** utils_strv_shorten_file_list ( gchar **  file_names,
gssize  file_names_len 
)

Transform file names in a list to be shorter.

This function takes a list of file names (probably with absolute paths), and transforms the paths such that they are short but still unique. This is intended for dialogs which present the file list to the user, where the base name may result in duplicates (showing the full path might be inappropriate).

The algorthm strips the common prefix (e-g. the user's home directory) and replaces the longest common substring with an ellipsis ("...").

Parameters
file_names The list of strings to process.
file_names_lenThe number of strings contained in file_names. Can be -1 if it's terminated by NULL.
Returns
(transfer: full) A newly-allocated array of transformed paths strings, terminated by NULL. Use g_strfreev() to free it.
Since
1.34 (API 239)

Definition at line 2169 of file utils.c.

References NULL, strstr(), utils_strv_find_common_prefix(), and utils_strv_find_lcs().

Referenced by show_goto_popup().

◆ utils_write_file()

gint utils_write_file ( const gchar *  filename,
const gchar *  text 
)

Writes text into a file named filename.

If the file doesn't exist, it will be created. If it already exists, it will be overwritten.

Warning
You should use g_file_set_contents() instead if you don't need file permissions and other metadata to be preserved, as that always handles disk exhaustion safely.
Parameters
filenameThe filename of the file to write, in locale encoding.
textThe text to write into the file.
Returns
0 if the file was successfully written, otherwise the errno of the failed operation is returned.

Definition at line 209 of file utils.c.

References errno, error(), FALLBACK, file_prefs, filename, geany_debug(), NULL, text, and GeanyFilePrefs::use_safe_file_saving.

Referenced by configuration_clear_default_session(), configuration_save(), configuration_save_default_session(), configure_response_cb(), create_config_dir(), filetypes_save_commands(), keybindings_write_to_file(), load_user_kb(), save_settings(), set_status(), stash_group_save_to_file(), tb_editor_write_markup(), write_config(), and write_data().