2005-11-22 12:26:26 +00:00
|
|
|
/*
|
|
|
|
* utils.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2012-06-18 01:13:05 +02:00
|
|
|
* Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2005-11-22 12:26:26 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2012-08-24 19:25:57 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-11-22 12:26:26 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-24 11:41:56 +00:00
|
|
|
/*
|
|
|
|
* General utility functions, non-GTK related.
|
|
|
|
*/
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2014-05-18 17:31:51 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#include "dialogs.h"
|
|
|
|
#include "document.h"
|
|
|
|
#include "prefs.h"
|
2015-06-26 22:33:16 +02:00
|
|
|
#include "prefix.h"
|
2014-05-18 17:31:51 -07:00
|
|
|
#include "sciwrappers.h"
|
2015-03-29 13:47:13 +03:00
|
|
|
#include "spawn.h"
|
2014-05-18 17:31:51 -07:00
|
|
|
#include "support.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "ui_utils.h"
|
2014-08-01 12:00:20 +01:00
|
|
|
#include "win32.h"
|
2015-02-04 00:27:34 +01:00
|
|
|
#include "osx.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <unistd.h>
|
2005-12-29 20:01:18 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2007-01-14 17:09:17 +00:00
|
|
|
#include <stdarg.h>
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2005-12-29 20:01:18 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
2005-11-22 12:26:26 +00:00
|
|
|
#endif
|
|
|
|
|
2007-03-10 17:26:03 +00:00
|
|
|
#include <glib/gstdio.h>
|
2011-06-17 22:51:17 +00:00
|
|
|
#include <gio/gio.h>
|
2007-03-10 17:26:03 +00:00
|
|
|
|
2006-08-07 14:53:58 +00:00
|
|
|
|
2009-01-28 19:30:18 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2011-09-15 02:01:38 +00:00
|
|
|
* that fails or it is unset, the user is asked to correct or fill it.
|
2009-01-28 19:30:18 +00:00
|
|
|
*
|
|
|
|
* @param uri The URI to open in the web browser.
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.16
|
2009-01-28 19:30:18 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-01-28 19:30:18 +00:00
|
|
|
void utils_open_browser(const gchar *uri)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-07-26 17:02:16 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(uri != NULL);
|
2006-07-25 12:44:42 +00:00
|
|
|
win32_open_browser(uri);
|
2005-11-22 12:26:26 +00:00
|
|
|
#else
|
2015-03-29 13:47:13 +03:00
|
|
|
gchar *argv[2] = { (gchar *) uri, NULL };
|
2009-01-28 19:30:18 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(uri != NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2015-03-29 13:47:13 +03:00
|
|
|
while (!spawn_async(NULL, tool_prefs.browser_cmd, argv, NULL, NULL, NULL))
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2015-03-29 13:47:13 +03:00
|
|
|
gchar *new_cmd = dialogs_show_input(_("Select Browser"), GTK_WINDOW(main_widgets.window),
|
|
|
|
_("Failed to spawn the configured browser command. "
|
|
|
|
"Please correct it or enter another one."),
|
|
|
|
tool_prefs.browser_cmd);
|
2011-09-15 02:01:38 +00:00
|
|
|
|
2015-03-29 13:47:13 +03:00
|
|
|
if (new_cmd == NULL) /* user canceled */
|
|
|
|
break;
|
2011-09-15 02:01:38 +00:00
|
|
|
|
2015-03-29 13:47:13 +03:00
|
|
|
SETPTR(tool_prefs.browser_cmd, new_cmd);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* taken from anjuta, to determine the EOL mode of the file */
|
2011-06-17 22:52:17 +00:00
|
|
|
gint utils_get_line_endings(const gchar* buffer, gsize size)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-06-17 22:52:17 +00:00
|
|
|
gsize i;
|
2005-11-22 12:26:26 +00:00
|
|
|
guint cr, lf, crlf, max_mode;
|
|
|
|
gint mode;
|
|
|
|
|
|
|
|
cr = lf = crlf = 0;
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
for (i = 0; i < size ; i++)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
if (buffer[i] == 0x0a)
|
2007-08-24 11:31:27 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* LF */
|
2005-11-22 12:26:26 +00:00
|
|
|
lf++;
|
|
|
|
}
|
2009-04-05 21:07:40 +00:00
|
|
|
else if (buffer[i] == 0x0d)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-09-16 14:13:38 +00:00
|
|
|
if (i >= (size - 1))
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Last char, CR */
|
2005-11-22 12:26:26 +00:00
|
|
|
cr++;
|
2007-08-24 11:31:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 14:13:38 +00:00
|
|
|
if (buffer[i + 1] != 0x0a)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* CR */
|
2005-11-22 12:26:26 +00:00
|
|
|
cr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* CRLF */
|
2005-11-22 12:26:26 +00:00
|
|
|
crlf++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Vote for the maximum */
|
|
|
|
mode = SC_EOL_LF;
|
|
|
|
max_mode = lf;
|
2007-08-24 11:31:27 +00:00
|
|
|
if (crlf > max_mode)
|
|
|
|
{
|
2005-11-22 12:26:26 +00:00
|
|
|
mode = SC_EOL_CRLF;
|
|
|
|
max_mode = crlf;
|
|
|
|
}
|
2007-08-24 11:31:27 +00:00
|
|
|
if (cr > max_mode)
|
|
|
|
{
|
2005-11-22 12:26:26 +00:00
|
|
|
mode = SC_EOL_CR;
|
|
|
|
max_mode = cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
gboolean utils_isbrace(gchar c, gboolean include_angles)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-06-24 14:36:35 +00:00
|
|
|
switch (c)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-12-24 12:07:35 +00:00
|
|
|
case '<':
|
|
|
|
case '>':
|
2007-08-23 11:34:06 +00:00
|
|
|
return include_angles;
|
2006-12-24 12:07:35 +00:00
|
|
|
|
2006-06-24 14:36:35 +00:00
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
case '[':
|
2006-06-29 18:22:11 +00:00
|
|
|
case ']': return TRUE;
|
|
|
|
default: return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
gboolean utils_is_opening_brace(gchar c, gboolean include_angles)
|
2006-06-29 18:22:11 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2006-12-24 12:07:35 +00:00
|
|
|
case '<':
|
2007-08-23 11:34:06 +00:00
|
|
|
return include_angles;
|
2006-12-24 12:07:35 +00:00
|
|
|
|
2006-06-29 18:22:11 +00:00
|
|
|
case '(':
|
|
|
|
case '{':
|
|
|
|
case '[': return TRUE;
|
|
|
|
default: return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-11-30 18:25:16 +00:00
|
|
|
* Writes @a text into a file named @a filename.
|
|
|
|
* If the file doesn't exist, it will be created.
|
|
|
|
* If it already exists, it will be overwritten.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2010-11-30 18:25:16 +00:00
|
|
|
* @warning You should use @c 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.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2010-11-30 18:25:16 +00:00
|
|
|
* @param filename The filename of the file to write, in locale encoding.
|
|
|
|
* @param text The text to write into the file.
|
|
|
|
*
|
|
|
|
* @return 0 if the file was successfully written, otherwise the @c errno of the
|
|
|
|
* failed operation is returned.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2005-12-12 16:19:30 +00:00
|
|
|
gint utils_write_file(const gchar *filename, const gchar *text)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(filename != NULL, ENOENT);
|
|
|
|
g_return_val_if_fail(text != NULL, EINVAL);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-05-11 15:56:33 +00:00
|
|
|
if (file_prefs.use_safe_file_saving)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
if (! g_file_set_contents(filename, text, -1, &error))
|
|
|
|
{
|
|
|
|
geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-05-11 15:56:33 +00:00
|
|
|
FILE *fp;
|
2011-06-17 22:52:17 +00:00
|
|
|
gsize bytes_written, len;
|
2010-11-30 18:18:24 +00:00
|
|
|
gboolean fail = FALSE;
|
2009-05-11 15:56:33 +00:00
|
|
|
|
|
|
|
if (filename == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
len = strlen(text);
|
2010-11-30 18:18:24 +00:00
|
|
|
errno = 0;
|
2009-05-11 15:56:33 +00:00
|
|
|
fp = g_fopen(filename, "w");
|
2010-11-30 18:18:24 +00:00
|
|
|
if (fp == NULL)
|
|
|
|
fail = TRUE;
|
|
|
|
else
|
2009-05-11 15:56:33 +00:00
|
|
|
{
|
2010-11-30 18:18:24 +00:00
|
|
|
bytes_written = fwrite(text, sizeof(gchar), len, fp);
|
2009-05-11 15:56:33 +00:00
|
|
|
|
|
|
|
if (len != bytes_written)
|
|
|
|
{
|
2010-11-30 18:18:24 +00:00
|
|
|
fail = TRUE;
|
2009-05-11 15:56:33 +00:00
|
|
|
geany_debug(
|
2011-06-17 22:52:17 +00:00
|
|
|
"utils_write_file(): written only %"G_GSIZE_FORMAT" bytes, had to write %"G_GSIZE_FORMAT" bytes to %s",
|
2009-05-11 15:56:33 +00:00
|
|
|
bytes_written, len, filename);
|
|
|
|
}
|
2010-11-30 18:18:24 +00:00
|
|
|
if (fclose(fp) != 0)
|
|
|
|
fail = TRUE;
|
2009-05-11 15:56:33 +00:00
|
|
|
}
|
2010-11-30 18:18:24 +00:00
|
|
|
if (fail)
|
2009-05-11 15:56:33 +00:00
|
|
|
{
|
|
|
|
geany_debug("utils_write_file(): could not write to file %s (%s)",
|
|
|
|
filename, g_strerror(errno));
|
2013-08-11 14:10:57 +01:00
|
|
|
return FALLBACK(errno, EIO);
|
2009-05-11 15:56:33 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2005-12-12 16:19:30 +00:00
|
|
|
return 0;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-23 11:44:31 +00:00
|
|
|
/** Searches backward through @a size bytes looking for a '<'.
|
2010-11-16 15:07:40 +00:00
|
|
|
* @param sel .
|
|
|
|
* @param size .
|
2011-04-23 11:44:31 +00:00
|
|
|
* @return The tag name (newly allocated) or @c NULL if no opening tag was found.
|
2005-11-22 12:26:26 +00:00
|
|
|
*/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2010-10-25 16:58:13 +00:00
|
|
|
gchar *utils_find_open_xml_tag(const gchar sel[], gint size)
|
2011-04-23 11:44:31 +00:00
|
|
|
{
|
|
|
|
const gchar *cur, *begin;
|
2011-06-17 22:52:17 +00:00
|
|
|
gsize len;
|
2011-04-23 11:44:31 +00:00
|
|
|
|
|
|
|
cur = utils_find_open_xml_tag_pos(sel, size);
|
|
|
|
if (cur == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cur++; /* skip the bracket */
|
|
|
|
begin = cur;
|
|
|
|
while (strchr(":_-.", *cur) || isalnum(*cur))
|
|
|
|
cur++;
|
|
|
|
|
2011-06-17 22:52:17 +00:00
|
|
|
len = (gsize)(cur - begin);
|
2011-04-25 16:23:58 +00:00
|
|
|
return len ? g_strndup(begin, len) : NULL;
|
2011-04-23 11:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Searches backward through @a size bytes looking for a '<'.
|
|
|
|
* @param sel .
|
|
|
|
* @param size .
|
|
|
|
* @return pointer to '<' of the found opening tag within @a sel, or @c NULL if no opening tag was found.
|
|
|
|
*/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2011-04-23 11:44:31 +00:00
|
|
|
const gchar *utils_find_open_xml_tag_pos(const gchar sel[], gint size)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2010-11-16 15:07:40 +00:00
|
|
|
/* stolen from anjuta and modified */
|
2005-11-22 12:26:26 +00:00
|
|
|
const gchar *begin, *cur;
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(size < 3))
|
2008-07-03 12:32:55 +00:00
|
|
|
{ /* Smallest tag is "<p>" which is 3 characters */
|
|
|
|
return NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
begin = &sel[0];
|
2010-10-27 16:12:05 +00:00
|
|
|
cur = &sel[size - 1];
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2010-05-08 13:10:24 +00:00
|
|
|
/* Skip to the character before the closing brace */
|
|
|
|
while (cur > begin)
|
|
|
|
{
|
|
|
|
if (*cur == '>')
|
|
|
|
break;
|
|
|
|
--cur;
|
|
|
|
}
|
|
|
|
--cur;
|
|
|
|
/* skip whitespace */
|
|
|
|
while (cur > begin && isspace(*cur))
|
|
|
|
cur--;
|
|
|
|
if (*cur == '/')
|
|
|
|
return NULL; /* we found a short tag which doesn't need to be closed */
|
2005-11-22 12:26:26 +00:00
|
|
|
while (cur > begin)
|
|
|
|
{
|
2010-05-08 13:10:33 +00:00
|
|
|
if (*cur == '<')
|
|
|
|
break;
|
2011-04-23 11:44:31 +00:00
|
|
|
/* exit immediately if such non-valid XML/HTML is detected, e.g. "<script>if a >" */
|
2010-10-27 16:12:05 +00:00
|
|
|
else if (*cur == '>')
|
|
|
|
break;
|
2005-11-22 12:26:26 +00:00
|
|
|
--cur;
|
|
|
|
}
|
|
|
|
|
2011-04-23 11:44:31 +00:00
|
|
|
/* if the found tag is an opening, not a closing tag or empty <> */
|
|
|
|
if (*cur == '<' && *(cur + 1) != '/' && *(cur + 1) != '>')
|
|
|
|
return cur;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-07-03 14:39:49 +00:00
|
|
|
return NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-22 13:18:26 +00:00
|
|
|
/* Returns true if tag_name is a self-closing tag */
|
2010-10-25 16:58:13 +00:00
|
|
|
gboolean utils_is_short_html_tag(const gchar *tag_name)
|
|
|
|
{
|
2010-12-22 13:18:26 +00:00
|
|
|
const gchar names[][20] = {
|
|
|
|
"area",
|
|
|
|
"base",
|
|
|
|
"basefont", /* < or not < */
|
|
|
|
"br",
|
2013-01-14 09:26:51 +13:00
|
|
|
"col",
|
|
|
|
"command",
|
|
|
|
"embed",
|
2010-12-22 13:18:26 +00:00
|
|
|
"frame",
|
|
|
|
"hr",
|
|
|
|
"img",
|
|
|
|
"input",
|
2013-01-14 09:26:51 +13:00
|
|
|
"keygen",
|
2010-12-22 13:18:26 +00:00
|
|
|
"link",
|
2013-01-14 09:26:51 +13:00
|
|
|
"meta",
|
|
|
|
"param",
|
|
|
|
"source",
|
|
|
|
"track",
|
|
|
|
"wbr"
|
2010-12-22 13:18:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (tag_name)
|
|
|
|
{
|
|
|
|
if (bsearch(tag_name, names, G_N_ELEMENTS(names), 20,
|
|
|
|
(GCompareFunc)g_ascii_strcasecmp))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2010-10-25 16:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-14 15:36:27 +00:00
|
|
|
const gchar *utils_get_eol_name(gint eol_mode)
|
|
|
|
{
|
|
|
|
switch (eol_mode)
|
|
|
|
{
|
2015-02-10 17:15:38 +01:00
|
|
|
case SC_EOL_CRLF: return _("Windows (CRLF)"); break;
|
2015-02-06 11:23:10 +01:00
|
|
|
case SC_EOL_CR: return _("Classic Mac (CR)"); break;
|
2008-05-14 15:36:27 +00:00
|
|
|
default: return _("Unix (LF)"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-04 15:57:26 +01:00
|
|
|
const gchar *utils_get_eol_short_name(gint eol_mode)
|
|
|
|
{
|
|
|
|
switch (eol_mode)
|
|
|
|
{
|
|
|
|
case SC_EOL_CRLF: return _("CRLF"); break;
|
|
|
|
case SC_EOL_CR: return _("CR"); break;
|
|
|
|
default: return _("LF"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 10:23:19 +00:00
|
|
|
const gchar *utils_get_eol_char(gint eol_mode)
|
|
|
|
{
|
|
|
|
switch (eol_mode)
|
|
|
|
{
|
|
|
|
case SC_EOL_CRLF: return "\r\n"; break;
|
|
|
|
case SC_EOL_CR: return "\r"; break;
|
|
|
|
default: return "\n"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 18:17:57 +00:00
|
|
|
/* Converts line endings to @a target_eol_mode. */
|
|
|
|
void utils_ensure_same_eol_characters(GString *string, gint target_eol_mode)
|
2010-08-01 17:20:50 +00:00
|
|
|
{
|
2010-11-09 18:17:57 +00:00
|
|
|
const gchar *eol_str = utils_get_eol_char(target_eol_mode);
|
2010-08-01 17:20:50 +00:00
|
|
|
|
2010-11-09 18:17:57 +00:00
|
|
|
/* first convert data to LF only */
|
|
|
|
utils_string_replace_all(string, "\r\n", "\n");
|
|
|
|
utils_string_replace_all(string, "\r", "\n");
|
2010-08-01 17:20:50 +00:00
|
|
|
|
2010-11-09 18:17:57 +00:00
|
|
|
if (target_eol_mode == SC_EOL_LF)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* now convert to desired line endings */
|
|
|
|
utils_string_replace_all(string, "\n", eol_str);
|
2010-08-01 17:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
gboolean utils_atob(const gchar *str)
|
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(str == NULL))
|
2008-11-11 18:15:14 +00:00
|
|
|
return FALSE;
|
|
|
|
else if (strcmp(str, "TRUE") == 0 || strcmp(str, "true") == 0)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-25 11:50:48 +00:00
|
|
|
/* NULL-safe version of g_path_is_absolute(). */
|
2005-11-22 12:26:26 +00:00
|
|
|
gboolean utils_is_absolute_path(const gchar *path)
|
|
|
|
{
|
2013-08-11 14:10:57 +01:00
|
|
|
if (G_UNLIKELY(EMPTY(path)))
|
2005-11-22 12:26:26 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2008-04-25 11:50:48 +00:00
|
|
|
return g_path_is_absolute(path);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 15:40:19 +00:00
|
|
|
/* Skips root if path is absolute, do nothing otherwise.
|
|
|
|
* This is a relative-safe version of g_path_skip_root().
|
|
|
|
*/
|
|
|
|
const gchar *utils_path_skip_root(const gchar *path)
|
|
|
|
{
|
|
|
|
const gchar *path_relative;
|
|
|
|
|
|
|
|
path_relative = g_path_skip_root(path);
|
|
|
|
|
|
|
|
return (path_relative != NULL) ? path_relative : path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 14:37:57 +00:00
|
|
|
gdouble utils_scale_round(gdouble val, gdouble factor)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/*val = floor(val * factor + 0.5);*/
|
2005-11-22 12:26:26 +00:00
|
|
|
val = floor(val);
|
|
|
|
val = MAX(val, 0);
|
|
|
|
val = MIN(val, factor);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-03 17:05:41 +00:00
|
|
|
/* like g_utf8_strdown() but if @str is not valid UTF8, convert it from locale first.
|
|
|
|
* returns NULL on charset conversion failure */
|
|
|
|
static gchar *utf8_strdown(const gchar *str)
|
|
|
|
{
|
|
|
|
gchar *down;
|
|
|
|
|
|
|
|
if (g_utf8_validate(str, -1, NULL))
|
|
|
|
down = g_utf8_strdown(str, -1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
down = g_locale_to_utf8(str, -1, NULL, NULL, NULL);
|
|
|
|
if (down)
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(down, g_utf8_strdown(down, -1));
|
2011-10-03 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return down;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-11 19:18:51 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @param s1 Pointer to first string or @c NULL.
|
|
|
|
* @param s2 Pointer to second string or @c NULL.
|
2008-11-11 19:18:51 +00:00
|
|
|
*
|
|
|
|
* @return an integer less than, equal to, or greater than zero if @a s1 is found, respectively,
|
|
|
|
* to be less than, to match, or to be greater than @a s2.
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.16
|
|
|
|
*/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2008-11-11 19:18:51 +00:00
|
|
|
gint utils_str_casecmp(const gchar *s1, const gchar *s2)
|
|
|
|
{
|
2008-11-12 13:05:33 +00:00
|
|
|
gchar *tmp1, *tmp2;
|
2008-11-11 19:18:51 +00:00
|
|
|
gint result;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(s1 != NULL, 1);
|
|
|
|
g_return_val_if_fail(s2 != NULL, -1);
|
2008-11-11 19:18:51 +00:00
|
|
|
|
2011-10-03 17:05:41 +00:00
|
|
|
/* ensure strings are UTF-8 and lowercase */
|
|
|
|
tmp1 = utf8_strdown(s1);
|
|
|
|
if (! tmp1)
|
2008-11-11 19:18:51 +00:00
|
|
|
return 1;
|
2011-10-03 17:05:41 +00:00
|
|
|
tmp2 = utf8_strdown(s2);
|
|
|
|
if (! tmp2)
|
2008-11-11 19:18:51 +00:00
|
|
|
{
|
2008-11-12 13:05:33 +00:00
|
|
|
g_free(tmp1);
|
2008-11-11 19:18:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compare */
|
|
|
|
result = strcmp(tmp1, tmp2);
|
|
|
|
|
2008-11-12 13:05:33 +00:00
|
|
|
g_free(tmp1);
|
|
|
|
g_free(tmp2);
|
2008-11-11 19:18:51 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param string Input string.
|
|
|
|
* @param truncate_length The length in characters of the resulting string.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return A copy of @a string which is truncated to @a truncate_length characters,
|
2009-04-21 20:52:51 +00:00
|
|
|
* should be freed when no longer needed.
|
|
|
|
*
|
|
|
|
* @since 0.17
|
|
|
|
*/
|
|
|
|
/* This following function is taken from Gedit. */
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-04-21 20:52:51 +00:00
|
|
|
gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length)
|
|
|
|
{
|
|
|
|
GString *truncated;
|
|
|
|
guint length;
|
|
|
|
guint n_chars;
|
|
|
|
guint num_left_chars;
|
|
|
|
guint right_offset;
|
|
|
|
guint delimiter_length;
|
|
|
|
const gchar *delimiter = "\342\200\246";
|
|
|
|
|
|
|
|
g_return_val_if_fail(string != NULL, NULL);
|
|
|
|
|
|
|
|
length = strlen(string);
|
|
|
|
|
|
|
|
g_return_val_if_fail(g_utf8_validate(string, length, NULL), NULL);
|
|
|
|
|
2015-04-18 08:25:59 +02:00
|
|
|
/* It doesn't make sense to truncate strings to less than the size of the delimiter plus 2
|
2009-04-21 20:52:51 +00:00
|
|
|
* characters (one on each side) */
|
|
|
|
delimiter_length = g_utf8_strlen(delimiter, -1);
|
|
|
|
if (truncate_length < (delimiter_length + 2))
|
|
|
|
return g_strdup(string);
|
|
|
|
|
|
|
|
n_chars = g_utf8_strlen(string, length);
|
|
|
|
|
|
|
|
/* Make sure the string is not already small enough. */
|
|
|
|
if (n_chars <= truncate_length)
|
|
|
|
return g_strdup (string);
|
|
|
|
|
|
|
|
/* Find the 'middle' where the truncation will occur. */
|
|
|
|
num_left_chars = (truncate_length - delimiter_length) / 2;
|
|
|
|
right_offset = n_chars - truncate_length + num_left_chars + delimiter_length;
|
|
|
|
|
|
|
|
truncated = g_string_new_len(string, g_utf8_offset_to_pointer(string, num_left_chars) - string);
|
|
|
|
g_string_append(truncated, delimiter);
|
|
|
|
g_string_append(truncated, g_utf8_offset_to_pointer(string, right_offset));
|
|
|
|
|
|
|
|
return g_string_free(truncated, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2009-07-21 22:09:43 +00:00
|
|
|
* @c NULL-safe string comparison. Returns @c TRUE if both @a a and @a b are @c NULL
|
|
|
|
* or if @a a and @a b refer to valid strings which are equal.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @param a Pointer to first string or @c NULL.
|
|
|
|
* @param b Pointer to second string or @c NULL.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return @c TRUE if @a a equals @a b, else @c FALSE.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2006-12-07 16:09:45 +00:00
|
|
|
gboolean utils_str_equal(const gchar *a, const gchar *b)
|
|
|
|
{
|
2008-02-17 18:00:42 +00:00
|
|
|
/* (taken from libexo from os-cillation) */
|
2009-04-15 22:47:33 +00:00
|
|
|
if (a == NULL && b == NULL) return TRUE;
|
|
|
|
else if (a == NULL || b == NULL) return FALSE;
|
2006-12-07 16:09:45 +00:00
|
|
|
|
2014-04-27 22:36:25 +02:00
|
|
|
return strcmp(a, b) == 0;
|
2006-12-07 16:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Removes the extension from @a filename and return the result in a newly allocated string.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @param filename The filename to operate on.
|
|
|
|
*
|
|
|
|
* @return A newly-allocated string, should be freed when no longer needed.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2005-11-22 12:26:26 +00:00
|
|
|
gchar *utils_remove_ext_from_filename(const gchar *filename)
|
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
gchar *last_dot;
|
2007-03-03 13:34:15 +00:00
|
|
|
gchar *result;
|
2011-10-03 17:05:57 +00:00
|
|
|
gsize len;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(filename != NULL, NULL);
|
2006-05-12 16:11:14 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
last_dot = strrchr(filename, '.');
|
|
|
|
if (! last_dot)
|
|
|
|
return g_strdup(filename);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-10-03 17:05:57 +00:00
|
|
|
len = (gsize) (last_dot - filename);
|
|
|
|
result = g_malloc(len + 1);
|
|
|
|
memcpy(result, filename, len);
|
|
|
|
result[len] = 0;
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar utils_brace_opposite(gchar ch)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '(': return ')';
|
|
|
|
case ')': return '(';
|
|
|
|
case '[': return ']';
|
|
|
|
case ']': return '[';
|
|
|
|
case '{': return '}';
|
|
|
|
case '}': return '{';
|
|
|
|
case '<': return '>';
|
|
|
|
case '>': return '<';
|
|
|
|
default: return '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:59:01 +00:00
|
|
|
/* Checks whether the given file can be written. locale_filename is expected in locale encoding.
|
|
|
|
* Returns 0 if it can be written, otherwise it returns errno */
|
2011-02-06 16:47:18 +00:00
|
|
|
gint utils_is_file_writable(const gchar *locale_filename)
|
2008-01-06 19:59:01 +00:00
|
|
|
{
|
|
|
|
gchar *file;
|
|
|
|
gint ret;
|
|
|
|
|
|
|
|
if (! g_file_test(locale_filename, G_FILE_TEST_EXISTS) &&
|
|
|
|
! g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get the file's directory to check for write permission if it doesn't yet exist */
|
2008-01-06 19:59:01 +00:00
|
|
|
file = g_path_get_dirname(locale_filename);
|
|
|
|
else
|
|
|
|
file = g_strdup(locale_filename);
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
2008-02-27 13:17:29 +00:00
|
|
|
/* use _waccess on Windows, access() doesn't accept special characters */
|
2008-01-06 19:59:01 +00:00
|
|
|
ret = win32_check_write_permission(file);
|
|
|
|
#else
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* access set also errno to "FILE NOT FOUND" even if locale_filename is writeable, so use
|
|
|
|
* errno only when access() explicitly returns an error */
|
2008-01-06 19:59:01 +00:00
|
|
|
if (access(file, R_OK | W_OK) != 0)
|
|
|
|
ret = errno;
|
|
|
|
else
|
|
|
|
ret = 0;
|
2008-01-09 15:40:22 +00:00
|
|
|
#endif
|
2008-01-06 19:59:01 +00:00
|
|
|
g_free(file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-11 15:21:11 +00:00
|
|
|
/* Replaces all occurrences of needle in haystack with replacement.
|
2009-07-23 16:01:45 +00:00
|
|
|
* Warning: *haystack must be a heap address; it may be freed and reassigned.
|
|
|
|
* Note: utils_string_replace_all() will always be faster when @a replacement is longer
|
|
|
|
* than @a needle.
|
2008-11-14 14:15:32 +00:00
|
|
|
* All strings have to be NULL-terminated.
|
|
|
|
* See utils_string_replace_all() for details. */
|
2009-07-23 16:01:45 +00:00
|
|
|
void utils_str_replace_all(gchar **haystack, const gchar *needle, const gchar *replacement)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-11-16 17:55:23 +00:00
|
|
|
GString *str;
|
2008-11-18 17:14:00 +00:00
|
|
|
|
2009-07-23 16:01:45 +00:00
|
|
|
g_return_if_fail(*haystack != NULL);
|
2008-11-18 17:14:00 +00:00
|
|
|
|
2009-07-23 16:01:45 +00:00
|
|
|
str = g_string_new(*haystack);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-07-23 16:01:45 +00:00
|
|
|
g_free(*haystack);
|
2008-11-14 14:15:32 +00:00
|
|
|
utils_string_replace_all(str, needle, replacement);
|
|
|
|
|
2009-07-23 16:01:45 +00:00
|
|
|
*haystack = g_string_free(str, FALSE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-28 15:41:44 +00:00
|
|
|
gint utils_strpos(const gchar *haystack, const gchar *needle)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-10-03 17:06:12 +00:00
|
|
|
const gchar *sub;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-10-03 17:06:12 +00:00
|
|
|
if (! *needle)
|
2005-11-22 12:26:26 +00:00
|
|
|
return -1;
|
2011-10-03 17:06:12 +00:00
|
|
|
|
|
|
|
sub = strstr(haystack, needle);
|
|
|
|
if (! sub)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return sub - haystack;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-21 17:40:04 +00:00
|
|
|
/**
|
2010-03-12 18:15:48 +00:00
|
|
|
* Retrieves a formatted date/time string from strftime().
|
2008-11-21 17:40:04 +00:00
|
|
|
* This function should be preferred to directly calling strftime() since this function
|
|
|
|
* works on UTF-8 encoded strings.
|
|
|
|
*
|
|
|
|
* @param format The format string to pass to strftime(3). See the strftime(3)
|
|
|
|
* documentation for details, in UTF-8 encoding.
|
|
|
|
* @param time_to_use The date/time to use, in time_t format or NULL to use the current time.
|
|
|
|
*
|
|
|
|
* @return A newly-allocated string, should be freed when no longer needed.
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.16
|
|
|
|
*/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2007-11-17 13:09:19 +00:00
|
|
|
gchar *utils_get_date_time(const gchar *format, time_t *time_to_use)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-11-17 13:09:19 +00:00
|
|
|
const struct tm *tm;
|
2008-11-21 17:40:04 +00:00
|
|
|
static gchar date[1024];
|
|
|
|
gchar *locale_format;
|
|
|
|
gsize len;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(format != NULL, NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-11-21 18:32:41 +00:00
|
|
|
if (! g_utf8_validate(format, -1, NULL))
|
|
|
|
{
|
|
|
|
locale_format = g_locale_from_utf8(format, -1, NULL, NULL, NULL);
|
|
|
|
if (locale_format == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
locale_format = g_strdup(format);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2007-11-17 13:09:19 +00:00
|
|
|
if (time_to_use != NULL)
|
2008-11-21 17:40:04 +00:00
|
|
|
tm = localtime(time_to_use);
|
2007-11-17 13:09:19 +00:00
|
|
|
else
|
2008-11-21 17:40:04 +00:00
|
|
|
{
|
|
|
|
time_t tp = time(NULL);
|
|
|
|
tm = localtime(&tp);
|
|
|
|
}
|
2007-11-17 13:09:19 +00:00
|
|
|
|
2008-11-21 17:40:04 +00:00
|
|
|
len = strftime(date, 1024, locale_format, tm);
|
|
|
|
g_free(locale_format);
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (! g_utf8_validate(date, len, NULL))
|
|
|
|
return g_locale_to_utf8(date, len, NULL, NULL, NULL);
|
|
|
|
else
|
|
|
|
return g_strdup(date);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 14:23:13 +00:00
|
|
|
gchar *utils_get_initials(const gchar *name)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint i = 1, j = 1;
|
|
|
|
gchar *initials = g_malloc0(5);
|
|
|
|
|
|
|
|
initials[0] = name[0];
|
|
|
|
while (name[i] != '\0' && j < 4)
|
|
|
|
{
|
|
|
|
if (name[i] == ' ' && name[i + 1] != ' ')
|
|
|
|
{
|
|
|
|
initials[j++] = name[i + 1];
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return initials;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-12 18:15:48 +00:00
|
|
|
* Wraps g_key_file_get_integer() to add a default value argument.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @param config A GKeyFile object.
|
|
|
|
* @param section The group name to look in for the key.
|
|
|
|
* @param key The key to find.
|
2009-07-21 22:09:43 +00:00
|
|
|
* @param default_value The default value which will be returned when @a section or @a key
|
2008-02-17 18:00:42 +00:00
|
|
|
* don't exist.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return The value associated with @a key as an integer, or the given default value if the value
|
2008-02-17 18:00:42 +00:00
|
|
|
* could not be retrieved.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2008-02-17 18:00:42 +00:00
|
|
|
gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key,
|
|
|
|
const gint default_value)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint tmp;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2011-10-31 17:22:39 +00:00
|
|
|
g_return_val_if_fail(config, default_value);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
tmp = g_key_file_get_integer(config, section, key, &error);
|
2011-03-24 22:00:18 +00:00
|
|
|
if (error)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
g_error_free(error);
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-12 18:15:48 +00:00
|
|
|
* Wraps g_key_file_get_boolean() to add a default value argument.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @param config A GKeyFile object.
|
|
|
|
* @param section The group name to look in for the key.
|
|
|
|
* @param key The key to find.
|
|
|
|
* @param default_value The default value which will be returned when @c section or @c key
|
|
|
|
* don't exist.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return The value associated with @a key as a boolean, or the given default value if the value
|
2008-02-17 18:00:42 +00:00
|
|
|
* could not be retrieved.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2008-02-17 18:00:42 +00:00
|
|
|
gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key,
|
|
|
|
const gboolean default_value)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gboolean tmp;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2011-10-31 17:22:39 +00:00
|
|
|
g_return_val_if_fail(config, default_value);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
tmp = g_key_file_get_boolean(config, section, key, &error);
|
2011-03-24 22:00:18 +00:00
|
|
|
if (error)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
g_error_free(error);
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-12 18:15:48 +00:00
|
|
|
* Wraps g_key_file_get_string() to add a default value argument.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @param config A GKeyFile object.
|
|
|
|
* @param section The group name to look in for the key.
|
|
|
|
* @param key The key to find.
|
2009-07-21 22:09:43 +00:00
|
|
|
* @param default_value The default value which will be returned when @a section or @a key
|
2008-02-17 18:00:42 +00:00
|
|
|
* don't exist.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return A newly allocated string, either the value for @a key or a copy of the given
|
2008-12-01 13:06:25 +00:00
|
|
|
* default value if it could not be retrieved.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2008-02-17 18:00:42 +00:00
|
|
|
gchar *utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key,
|
|
|
|
const gchar *default_value)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gchar *tmp;
|
|
|
|
|
2011-10-31 17:22:39 +00:00
|
|
|
g_return_val_if_fail(config, g_strdup(default_value));
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-10-19 15:19:09 +00:00
|
|
|
tmp = g_key_file_get_string(config, section, key, NULL);
|
2011-03-24 22:00:18 +00:00
|
|
|
if (!tmp)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-12-01 13:06:25 +00:00
|
|
|
return g_strdup(default_value);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-11 02:16:02 +00:00
|
|
|
gchar *utils_get_hex_from_color(GdkColor *color)
|
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(color != NULL, NULL);
|
2006-08-22 19:03:38 +00:00
|
|
|
|
2013-11-03 01:51:03 +01:00
|
|
|
return g_strdup_printf("#%02X%02X%02X",
|
|
|
|
(guint) (utils_scale_round(color->red / 256, 255)),
|
|
|
|
(guint) (utils_scale_round(color->green / 256, 255)),
|
|
|
|
(guint) (utils_scale_round(color->blue / 256, 255)));
|
2005-12-11 02:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-26 21:32:46 +00:00
|
|
|
/* Get directory from current file in the notebook.
|
|
|
|
* Returns dir string that should be freed or NULL, depending on whether current file is valid.
|
2007-12-02 10:52:19 +00:00
|
|
|
* Returned string is in UTF-8 encoding */
|
2008-02-20 11:24:23 +00:00
|
|
|
gchar *utils_get_current_file_dir_utf8(void)
|
2006-01-26 21:32:46 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2006-01-26 21:32:46 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (doc != NULL)
|
2006-01-26 21:32:46 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get current filename */
|
2008-06-15 13:35:48 +00:00
|
|
|
const gchar *cur_fname = doc->file_name;
|
2006-01-26 21:32:46 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (cur_fname != NULL)
|
2006-01-26 21:32:46 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get folder part from current filename */
|
|
|
|
return g_path_get_dirname(cur_fname); /* returns "." if no path */
|
2006-01-26 21:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
return NULL; /* no file open */
|
2006-01-26 21:32:46 +00:00
|
|
|
}
|
2006-02-06 06:34:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* very simple convenience function */
|
2008-02-20 11:24:23 +00:00
|
|
|
void utils_beep(void)
|
2006-02-06 06:34:06 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
if (prefs.beep_on_errors)
|
|
|
|
gdk_beep();
|
2006-02-06 06:34:06 +00:00
|
|
|
}
|
2006-02-22 13:41:50 +00:00
|
|
|
|
2006-02-25 22:26:43 +00:00
|
|
|
|
2006-02-22 13:41:50 +00:00
|
|
|
/* taken from busybox, thanks */
|
2008-02-28 16:06:04 +00:00
|
|
|
gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
|
2008-02-17 18:00:42 +00:00
|
|
|
gulong display_unit)
|
2006-02-22 13:41:50 +00:00
|
|
|
{
|
|
|
|
/* The code will adjust for additional (appended) units. */
|
2006-04-27 17:58:52 +00:00
|
|
|
static const gchar zero_and_units[] = { '0', 0, 'K', 'M', 'G', 'T' };
|
|
|
|
static const gchar fmt[] = "%Lu %c%c";
|
|
|
|
static const gchar fmt_tenths[] = "%Lu.%d %c%c";
|
2006-02-22 13:41:50 +00:00
|
|
|
|
2008-02-28 16:06:04 +00:00
|
|
|
guint64 val;
|
2006-04-27 17:58:52 +00:00
|
|
|
gint frac;
|
|
|
|
const gchar *u;
|
|
|
|
const gchar *f;
|
2006-02-22 13:41:50 +00:00
|
|
|
|
|
|
|
u = zero_and_units;
|
|
|
|
f = fmt;
|
|
|
|
frac = 0;
|
|
|
|
|
|
|
|
val = size * block_size;
|
2009-04-15 22:47:33 +00:00
|
|
|
if (val == 0)
|
2009-04-05 21:07:40 +00:00
|
|
|
return g_strdup(u);
|
2006-02-22 13:41:50 +00:00
|
|
|
|
|
|
|
if (display_unit)
|
|
|
|
{
|
|
|
|
val += display_unit/2; /* Deal with rounding. */
|
|
|
|
val /= display_unit; /* Don't combine with the line above!!! */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++u;
|
2009-07-04 09:18:01 +00:00
|
|
|
while ((val >= 1024) && (u < zero_and_units + sizeof(zero_and_units) - 1))
|
2006-02-22 13:41:50 +00:00
|
|
|
{
|
|
|
|
f = fmt_tenths;
|
|
|
|
++u;
|
2009-09-16 14:13:38 +00:00
|
|
|
frac = ((((gint)(val % 1024)) * 10) + (1024 / 2)) / 1024;
|
2009-07-04 09:18:01 +00:00
|
|
|
val /= 1024;
|
2006-02-22 13:41:50 +00:00
|
|
|
}
|
|
|
|
if (frac >= 10)
|
|
|
|
{ /* We need to round up here. */
|
|
|
|
++val;
|
|
|
|
frac = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If f==fmt then 'frac' and 'u' are ignored. */
|
|
|
|
return g_strdup_printf(f, val, frac, *u, 'b');
|
|
|
|
}
|
2006-02-26 18:19:28 +00:00
|
|
|
|
2006-05-05 16:07:40 +00:00
|
|
|
|
2013-12-04 15:31:17 +01:00
|
|
|
/* converts a color representation using gdk_color_parse(), with additional
|
|
|
|
* support of the "0x" prefix as a synonym for "#" */
|
|
|
|
gboolean utils_parse_color(const gchar *spec, GdkColor *color)
|
2006-05-22 11:55:07 +00:00
|
|
|
{
|
2013-12-04 15:31:17 +01:00
|
|
|
gchar buf[64] = {0};
|
|
|
|
|
|
|
|
g_return_val_if_fail(spec != NULL, -1);
|
|
|
|
|
|
|
|
if (spec[0] == '0' && (spec[1] == 'x' || spec[1] == 'X'))
|
2013-12-02 22:44:40 +01:00
|
|
|
{
|
2013-12-04 15:31:17 +01:00
|
|
|
/* convert to # format for GDK to understand it */
|
|
|
|
buf[0] = '#';
|
|
|
|
strncpy(buf + 1, spec + 2, sizeof(buf) - 2);
|
|
|
|
spec = buf;
|
2013-12-02 22:44:40 +01:00
|
|
|
}
|
2013-12-04 15:31:17 +01:00
|
|
|
|
|
|
|
return gdk_color_parse(spec, color);
|
2006-05-22 11:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 15:31:17 +01:00
|
|
|
/* converts a GdkColor to the packed 24 bits BGR format, as understood by Scintilla
|
|
|
|
* returns a 24 bits BGR color, or -1 on failure */
|
|
|
|
gint utils_color_to_bgr(const GdkColor *c)
|
2006-05-22 11:55:07 +00:00
|
|
|
{
|
2013-12-04 15:31:17 +01:00
|
|
|
g_return_val_if_fail(c != NULL, -1);
|
|
|
|
return (c->red / 256) | ((c->green / 256) << 8) | ((c->blue / 256) << 16);
|
|
|
|
}
|
2006-05-22 11:55:07 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
|
2013-12-04 15:31:17 +01:00
|
|
|
/* parses @p spec using utils_parse_color() and convert it to 24 bits BGR using
|
|
|
|
* utils_color_to_bgr() */
|
|
|
|
gint utils_parse_color_to_bgr(const gchar *spec)
|
|
|
|
{
|
|
|
|
GdkColor color;
|
|
|
|
if (utils_parse_color(spec, &color))
|
|
|
|
return utils_color_to_bgr(&color);
|
2013-12-02 22:38:11 +01:00
|
|
|
else
|
2006-07-27 20:57:13 +00:00
|
|
|
return -1;
|
2006-05-22 11:55:07 +00:00
|
|
|
}
|
|
|
|
|
2006-05-12 16:11:14 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Returns: newly allocated string with the current time formatted HH:MM:SS. */
|
2008-02-20 11:24:23 +00:00
|
|
|
gchar *utils_get_current_time_string(void)
|
2006-07-01 12:02:30 +00:00
|
|
|
{
|
2006-09-17 20:40:15 +00:00
|
|
|
const time_t tp = time(NULL);
|
|
|
|
const struct tm *tmval = localtime(&tp);
|
|
|
|
gchar *result = g_malloc0(9);
|
|
|
|
|
2006-09-26 16:54:20 +00:00
|
|
|
strftime(result, 9, "%H:%M:%S", tmval);
|
2006-09-17 20:40:15 +00:00
|
|
|
result[8] = '\0';
|
2006-07-01 12:02:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-01 21:45:43 +00:00
|
|
|
GIOChannel *utils_set_up_io_channel(
|
|
|
|
gint fd, GIOCondition cond, gboolean nblock, GIOFunc func, gpointer data)
|
2006-07-13 14:30:44 +00:00
|
|
|
{
|
|
|
|
GIOChannel *ioc;
|
2008-02-27 13:17:29 +00:00
|
|
|
/*const gchar *encoding;*/
|
2006-07-13 14:30:44 +00:00
|
|
|
|
2007-06-13 12:00:23 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
ioc = g_io_channel_win32_new_fd(fd);
|
|
|
|
#else
|
2006-07-13 14:30:44 +00:00
|
|
|
ioc = g_io_channel_unix_new(fd);
|
2007-06-13 12:00:23 +00:00
|
|
|
#endif
|
2006-07-13 14:30:44 +00:00
|
|
|
|
2007-03-01 21:45:43 +00:00
|
|
|
if (nblock)
|
|
|
|
g_io_channel_set_flags(ioc, G_IO_FLAG_NONBLOCK, NULL);
|
2007-03-03 13:34:15 +00:00
|
|
|
|
2007-03-01 10:54:37 +00:00
|
|
|
g_io_channel_set_encoding(ioc, NULL, NULL);
|
|
|
|
/*
|
2006-07-13 14:30:44 +00:00
|
|
|
if (! g_get_charset(&encoding))
|
|
|
|
{ // hope this works reliably
|
2007-03-01 10:54:37 +00:00
|
|
|
GError *error = NULL;
|
2006-07-13 14:30:44 +00:00
|
|
|
g_io_channel_set_encoding(ioc, encoding, &error);
|
|
|
|
if (error)
|
|
|
|
{
|
2009-01-21 22:49:21 +00:00
|
|
|
geany_debug("%s: %s", G_STRFUNC, error->message);
|
2006-07-13 14:30:44 +00:00
|
|
|
g_error_free(error);
|
|
|
|
return ioc;
|
|
|
|
}
|
|
|
|
}
|
2007-03-01 10:54:37 +00:00
|
|
|
*/
|
2008-02-27 13:17:29 +00:00
|
|
|
/* "auto-close" ;-) */
|
2006-07-13 14:30:44 +00:00
|
|
|
g_io_channel_set_close_on_unref(ioc, TRUE);
|
|
|
|
|
|
|
|
g_io_add_watch(ioc, cond, func, data);
|
|
|
|
g_io_channel_unref(ioc);
|
|
|
|
|
|
|
|
return ioc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-16 22:02:31 +00:00
|
|
|
gchar **utils_read_file_in_array(const gchar *filename)
|
|
|
|
{
|
|
|
|
gchar **result = NULL;
|
|
|
|
gchar *data;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(filename != NULL, NULL);
|
2006-07-16 22:02:31 +00:00
|
|
|
|
|
|
|
g_file_get_contents(filename, &data, NULL, NULL);
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (data != NULL)
|
2006-07-16 22:02:31 +00:00
|
|
|
{
|
|
|
|
result = g_strsplit_set(data, "\r\n", -1);
|
2009-01-18 23:05:54 +00:00
|
|
|
g_free(data);
|
2006-07-16 22:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-07-19 11:30:59 +00:00
|
|
|
|
|
|
|
/* Contributed by Stefan Oltmanns, thanks.
|
2010-03-01 18:28:57 +00:00
|
|
|
* Replaces \\, \r, \n, \t and \uXXX by their real counterparts.
|
|
|
|
* keep_backslash is used for regex strings to leave '\\' and '\?' in place */
|
2010-03-01 18:16:28 +00:00
|
|
|
gboolean utils_str_replace_escape(gchar *string, gboolean keep_backslash)
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
2009-02-10 21:11:25 +00:00
|
|
|
gsize i, j, len;
|
2006-07-19 11:30:59 +00:00
|
|
|
guint unicodechar;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(string != NULL, FALSE);
|
|
|
|
|
2006-07-19 11:30:59 +00:00
|
|
|
j = 0;
|
2009-02-10 21:11:25 +00:00
|
|
|
len = strlen(string);
|
|
|
|
for (i = 0; i < len; i++)
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
if (string[i]=='\\')
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
|
|
|
if (i++ >= strlen(string))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
switch (string[i])
|
|
|
|
{
|
|
|
|
case '\\':
|
2010-03-01 18:16:28 +00:00
|
|
|
if (keep_backslash)
|
|
|
|
string[j++] = '\\';
|
2006-07-19 11:30:59 +00:00
|
|
|
string[j] = '\\';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
string[j] = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
string[j] = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
string[j] = '\t';
|
|
|
|
break;
|
|
|
|
#if 0
|
2008-02-27 13:17:29 +00:00
|
|
|
case 'x': /* Warning: May produce illegal utf-8 string! */
|
2006-07-19 11:30:59 +00:00
|
|
|
i += 2;
|
|
|
|
if (i >= strlen(string))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-09-16 14:13:38 +00:00
|
|
|
if (isdigit(string[i - 1])) string[j] = string[i - 1] - 48;
|
|
|
|
else if (isxdigit(string[i - 1])) string[j] = tolower(string[i - 1])-87;
|
2006-07-19 11:30:59 +00:00
|
|
|
else return FALSE;
|
|
|
|
string[j] <<= 4;
|
2009-09-16 14:13:38 +00:00
|
|
|
if (isdigit(string[i])) string[j] |= string[i] - 48;
|
2006-07-19 11:30:59 +00:00
|
|
|
else if (isxdigit(string[i])) string[j] |= tolower(string[i])-87;
|
|
|
|
else return FALSE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'u':
|
2010-02-25 18:30:43 +00:00
|
|
|
{
|
2006-07-19 11:30:59 +00:00
|
|
|
i += 2;
|
|
|
|
if (i >= strlen(string))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-09-16 14:13:38 +00:00
|
|
|
if (isdigit(string[i - 1])) unicodechar = string[i - 1] - 48;
|
|
|
|
else if (isxdigit(string[i - 1])) unicodechar = tolower(string[i - 1])-87;
|
2006-07-19 11:30:59 +00:00
|
|
|
else return FALSE;
|
|
|
|
unicodechar <<= 4;
|
2009-09-16 14:13:38 +00:00
|
|
|
if (isdigit(string[i])) unicodechar |= string[i] - 48;
|
2006-07-19 11:30:59 +00:00
|
|
|
else if (isxdigit(string[i])) unicodechar |= tolower(string[i])-87;
|
|
|
|
else return FALSE;
|
2009-09-16 14:13:38 +00:00
|
|
|
if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
|
|
|
|
&& (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
|
|
|
i += 2;
|
|
|
|
unicodechar <<= 8;
|
2009-09-16 14:13:38 +00:00
|
|
|
if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
|
|
|
|
else unicodechar |= ((tolower(string[i - 1])-87) << 4);
|
|
|
|
if (isdigit(string[i])) unicodechar |= string[i] - 48;
|
2006-07-19 11:30:59 +00:00
|
|
|
else unicodechar |= tolower(string[i])-87;
|
|
|
|
}
|
2009-09-16 14:13:38 +00:00
|
|
|
if (((i + 2) < strlen(string)) && (isdigit(string[i + 1]) || isxdigit(string[i + 1]))
|
|
|
|
&& (isdigit(string[i + 2]) || isxdigit(string[i + 2])))
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
|
|
|
i += 2;
|
|
|
|
unicodechar <<= 8;
|
2009-09-16 14:13:38 +00:00
|
|
|
if (isdigit(string[i - 1])) unicodechar |= ((string[i - 1] - 48) << 4);
|
|
|
|
else unicodechar |= ((tolower(string[i - 1])-87) << 4);
|
|
|
|
if (isdigit(string[i])) unicodechar |= string[i] - 48;
|
2006-07-19 11:30:59 +00:00
|
|
|
else unicodechar |= tolower(string[i])-87;
|
|
|
|
}
|
2009-09-16 14:13:38 +00:00
|
|
|
if (unicodechar < 0x80)
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
|
|
|
string[j] = unicodechar;
|
|
|
|
}
|
|
|
|
else if (unicodechar < 0x800)
|
|
|
|
{
|
2009-09-16 14:13:38 +00:00
|
|
|
string[j] = (unsigned char) ((unicodechar >> 6) | 0xC0);
|
2006-07-19 11:30:59 +00:00
|
|
|
j++;
|
2009-09-16 14:13:38 +00:00
|
|
|
string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
|
2006-07-19 11:30:59 +00:00
|
|
|
}
|
|
|
|
else if (unicodechar < 0x10000)
|
|
|
|
{
|
|
|
|
string[j] = (unsigned char) ((unicodechar >> 12) | 0xE0);
|
|
|
|
j++;
|
|
|
|
string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
|
|
|
|
j++;
|
|
|
|
string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
else if (unicodechar < 0x110000) /* more chars are not allowed in unicode */
|
2006-07-19 11:30:59 +00:00
|
|
|
{
|
|
|
|
string[j] = (unsigned char) ((unicodechar >> 18) | 0xF0);
|
|
|
|
j++;
|
|
|
|
string[j] = (unsigned char) (((unicodechar >> 12) & 0x3F) | 0x80);
|
|
|
|
j++;
|
|
|
|
string[j] = (unsigned char) (((unicodechar >> 6) & 0x3F) | 0x80);
|
|
|
|
j++;
|
|
|
|
string[j] = (unsigned char) ((unicodechar & 0x3F) | 0x80);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
break;
|
2010-02-25 18:30:43 +00:00
|
|
|
}
|
2006-07-19 11:30:59 +00:00
|
|
|
default:
|
2010-02-25 18:30:43 +00:00
|
|
|
/* unnecessary escapes are allowed */
|
2010-03-01 18:28:57 +00:00
|
|
|
if (keep_backslash)
|
|
|
|
string[j++] = '\\';
|
2010-02-25 18:30:43 +00:00
|
|
|
string[j] = string[i];
|
2006-07-19 11:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string[j] = string[i];
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
while (j < i)
|
|
|
|
{
|
|
|
|
string[j] = 0;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2006-08-03 13:27:30 +00:00
|
|
|
/* Wraps a string in place, replacing a space with a newline character.
|
|
|
|
* wrapstart is the minimum position to start wrapping or -1 for default */
|
|
|
|
gboolean utils_wrap_string(gchar *string, gint wrapstart)
|
|
|
|
{
|
|
|
|
gchar *pos, *linestart;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (wrapstart < 0)
|
|
|
|
wrapstart = 80;
|
2006-08-03 13:27:30 +00:00
|
|
|
|
|
|
|
for (pos = linestart = string; *pos != '\0'; pos++)
|
|
|
|
{
|
|
|
|
if (pos - linestart >= wrapstart && *pos == ' ')
|
|
|
|
{
|
|
|
|
*pos = '\n';
|
|
|
|
linestart = pos;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-08-12 21:36:55 +00:00
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param utf8_text UTF-8 encoded text.
|
|
|
|
*
|
|
|
|
* @return The converted string in locale encoding, or a copy of the input string if conversion
|
|
|
|
* failed. Should be freed with g_free(). If @a utf8_text is @c NULL, @c NULL is returned.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2006-08-12 21:36:55 +00:00
|
|
|
gchar *utils_get_locale_from_utf8(const gchar *utf8_text)
|
|
|
|
{
|
2007-04-16 15:58:34 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2008-02-27 13:17:29 +00:00
|
|
|
/* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
|
|
|
|
* which would result in wrongly converted strings */
|
2007-04-16 15:58:34 +00:00
|
|
|
return g_strdup(utf8_text);
|
|
|
|
#else
|
2007-12-21 16:34:07 +00:00
|
|
|
gchar *locale_text;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! utf8_text)
|
2007-12-21 16:34:07 +00:00
|
|
|
return NULL;
|
|
|
|
locale_text = g_locale_from_utf8(utf8_text, -1, NULL, NULL, NULL);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (locale_text == NULL)
|
2007-12-21 16:34:07 +00:00
|
|
|
locale_text = g_strdup(utf8_text);
|
2006-08-12 21:36:55 +00:00
|
|
|
return locale_text;
|
2007-04-16 15:58:34 +00:00
|
|
|
#endif
|
2006-08-12 21:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param locale_text Text in locale encoding.
|
|
|
|
*
|
|
|
|
* @return The converted string in UTF-8 encoding, or a copy of the input string if conversion
|
|
|
|
* failed. Should be freed with g_free(). If @a locale_text is @c NULL, @c NULL is returned.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2006-08-12 21:36:55 +00:00
|
|
|
gchar *utils_get_utf8_from_locale(const gchar *locale_text)
|
|
|
|
{
|
2007-04-16 15:58:34 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2008-02-27 13:17:29 +00:00
|
|
|
/* just do nothing on Windows platforms, this ifdef is just to prevent unwanted conversions
|
|
|
|
* which would result in wrongly converted strings */
|
2007-04-16 15:58:34 +00:00
|
|
|
return g_strdup(locale_text);
|
|
|
|
#else
|
2007-12-21 16:34:07 +00:00
|
|
|
gchar *utf8_text;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! locale_text)
|
2007-12-21 16:34:07 +00:00
|
|
|
return NULL;
|
|
|
|
utf8_text = g_locale_to_utf8(locale_text, -1, NULL, NULL, NULL);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (utf8_text == NULL)
|
2007-12-21 16:34:07 +00:00
|
|
|
utf8_text = g_strdup(locale_text);
|
2006-08-12 21:36:55 +00:00
|
|
|
return utf8_text;
|
2007-04-16 15:58:34 +00:00
|
|
|
#endif
|
2006-08-12 21:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-15 16:15:28 +00:00
|
|
|
/* Pass pointers to free after arg_count.
|
|
|
|
* The last argument must be NULL as an extra check that arg_count is correct. */
|
|
|
|
void utils_free_pointers(gsize arg_count, ...)
|
|
|
|
{
|
|
|
|
va_list a;
|
|
|
|
gsize i;
|
|
|
|
gpointer ptr;
|
|
|
|
|
|
|
|
va_start(a, arg_count);
|
|
|
|
for (i = 0; i < arg_count; i++)
|
|
|
|
{
|
|
|
|
ptr = va_arg(a, gpointer);
|
|
|
|
g_free(ptr);
|
|
|
|
}
|
|
|
|
ptr = va_arg(a, gpointer);
|
|
|
|
if (ptr)
|
|
|
|
g_warning("Wrong arg_count!");
|
|
|
|
va_end(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 15:41:24 +00:00
|
|
|
/* currently unused */
|
|
|
|
#if 0
|
2007-04-23 15:52:44 +00:00
|
|
|
/* Creates a string array deep copy of a series of non-NULL strings.
|
|
|
|
* The first argument is nothing special.
|
|
|
|
* The list must be ended with NULL.
|
|
|
|
* If first is NULL, NULL is returned. */
|
2008-02-27 14:23:13 +00:00
|
|
|
gchar **utils_strv_new(const gchar *first, ...)
|
2007-02-08 16:26:45 +00:00
|
|
|
{
|
|
|
|
gsize strvlen, i;
|
|
|
|
va_list args;
|
|
|
|
gchar *str;
|
|
|
|
gchar **strv;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(first != NULL, NULL);
|
2007-02-08 16:26:45 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
strvlen = 1; /* for first argument */
|
2007-02-08 16:26:45 +00:00
|
|
|
|
2011-08-06 18:34:14 +00:00
|
|
|
/* count other arguments */
|
|
|
|
va_start(args, first);
|
|
|
|
for (; va_arg(args, gchar*) != NULL; strvlen++);
|
2007-02-08 16:26:45 +00:00
|
|
|
va_end(args);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
strv = g_new(gchar*, strvlen + 1); /* +1 for NULL terminator */
|
2007-02-08 16:26:45 +00:00
|
|
|
strv[0] = g_strdup(first);
|
|
|
|
|
2011-08-06 18:34:14 +00:00
|
|
|
va_start(args, first);
|
|
|
|
for (i = 1; str = va_arg(args, gchar*), str != NULL; i++)
|
|
|
|
{
|
2007-02-08 16:26:45 +00:00
|
|
|
strv[i] = g_strdup(str);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
strv[i] = NULL;
|
|
|
|
return strv;
|
|
|
|
}
|
2010-10-21 15:41:24 +00:00
|
|
|
#endif
|
2007-02-08 16:26:45 +00:00
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Creates a directory if it doesn't already exist.
|
|
|
|
* Creates intermediate parent directories as needed, too.
|
2008-11-16 17:52:38 +00:00
|
|
|
* The permissions of the created directory are set 0700.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @param path The path of the directory to create, in locale encoding.
|
|
|
|
* @param create_parent_dirs Whether to create intermediate parent directories if necessary.
|
|
|
|
*
|
|
|
|
* @return 0 if the directory was successfully created, otherwise the @c errno of the
|
|
|
|
* failed operation is returned.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2007-03-10 17:26:03 +00:00
|
|
|
gint utils_mkdir(const gchar *path, gboolean create_parent_dirs)
|
2007-01-17 23:44:08 +00:00
|
|
|
{
|
2007-03-10 17:26:03 +00:00
|
|
|
gint mode = 0700;
|
|
|
|
gint result;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (path == NULL || strlen(path) == 0)
|
2007-01-17 23:44:08 +00:00
|
|
|
return EFAULT;
|
|
|
|
|
2007-03-10 17:26:03 +00:00
|
|
|
result = (create_parent_dirs) ? g_mkdir_with_parents(path, mode) : g_mkdir(path, mode);
|
|
|
|
if (result != 0)
|
|
|
|
return errno;
|
2007-01-17 23:44:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-03-10 17:26:03 +00:00
|
|
|
|
|
|
|
|
2009-09-29 13:51:54 +00:00
|
|
|
/**
|
2009-09-29 12:10:17 +00:00
|
|
|
* Gets a list of files from the specified directory.
|
2009-09-17 16:47:45 +00:00
|
|
|
* Locale encoding is expected for @a path and used for the file list. The list and the data
|
|
|
|
* in the list should be freed after use, e.g.:
|
|
|
|
* @code
|
|
|
|
* g_slist_foreach(list, (GFunc) g_free, NULL);
|
|
|
|
* g_slist_free(list); @endcode
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-09-29 12:10:17 +00:00
|
|
|
* @note If you don't need a list you should use the foreach_dir() macro instead -
|
|
|
|
* it's more efficient.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-09-17 16:47:45 +00:00
|
|
|
* @param path The path of the directory to scan, in locale encoding.
|
2009-09-29 12:10:17 +00:00
|
|
|
* @param full_path Whether to include the full path for each filename in the list. Obviously this
|
|
|
|
* will use more memory.
|
|
|
|
* @param sort Whether to sort alphabetically (UTF-8 safe).
|
2009-09-17 16:47:45 +00:00
|
|
|
* @param error The location for storing a possible error, or @c NULL.
|
|
|
|
*
|
2009-09-29 12:10:17 +00:00
|
|
|
* @return A newly allocated list or @c NULL if no files were found. The list and its data should be
|
|
|
|
* freed when no longer needed.
|
|
|
|
* @see utils_get_file_list().
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-09-29 12:10:17 +00:00
|
|
|
GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error)
|
2007-05-11 11:42:00 +00:00
|
|
|
{
|
|
|
|
GSList *list = NULL;
|
|
|
|
GDir *dir;
|
2009-09-17 16:47:45 +00:00
|
|
|
const gchar *filename;
|
2007-05-11 11:42:00 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = NULL;
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(path != NULL, NULL);
|
2007-05-11 11:42:00 +00:00
|
|
|
|
|
|
|
dir = g_dir_open(path, 0, error);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (dir == NULL)
|
2007-05-11 11:42:00 +00:00
|
|
|
return NULL;
|
|
|
|
|
2009-09-17 16:47:45 +00:00
|
|
|
foreach_dir(filename, dir)
|
2007-05-11 11:42:00 +00:00
|
|
|
{
|
2011-03-10 22:27:04 +00:00
|
|
|
list = g_slist_prepend(list, full_path ?
|
2009-09-29 12:10:17 +00:00
|
|
|
g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL) : g_strdup(filename));
|
2007-05-11 11:42:00 +00:00
|
|
|
}
|
|
|
|
g_dir_close(dir);
|
2009-09-21 12:34:16 +00:00
|
|
|
/* sorting last is quicker than on insertion */
|
2009-09-29 12:10:17 +00:00
|
|
|
if (sort)
|
|
|
|
list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a sorted list of files from the specified directory.
|
|
|
|
* Locale encoding is expected for @a path and used for the file list. The list and the data
|
|
|
|
* in the list should be freed after use, e.g.:
|
|
|
|
* @code
|
|
|
|
* g_slist_foreach(list, (GFunc) g_free, NULL);
|
|
|
|
* g_slist_free(list); @endcode
|
|
|
|
*
|
|
|
|
* @param path The path of the directory to scan, in locale encoding.
|
|
|
|
* @param length The location to store the number of non-@c NULL data items in the list,
|
|
|
|
* unless @c NULL.
|
|
|
|
* @param error The location for storing a possible error, or @c NULL.
|
|
|
|
*
|
|
|
|
* @return A newly allocated list or @c NULL if no files were found. The list and its data should be
|
|
|
|
* freed when no longer needed.
|
|
|
|
* @see utils_get_file_list_full().
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-09-29 12:10:17 +00:00
|
|
|
GSList *utils_get_file_list(const gchar *path, guint *length, GError **error)
|
|
|
|
{
|
|
|
|
GSList *list = utils_get_file_list_full(path, FALSE, TRUE, error);
|
2007-05-11 11:42:00 +00:00
|
|
|
|
|
|
|
if (length)
|
2009-09-29 12:10:17 +00:00
|
|
|
*length = g_slist_length(list);
|
2007-05-11 11:42:00 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-07 19:01:55 +00:00
|
|
|
/* returns TRUE if any letter in str is a capital, FALSE otherwise. Should be Unicode safe. */
|
|
|
|
gboolean utils_str_has_upper(const gchar *str)
|
|
|
|
{
|
|
|
|
gunichar c;
|
|
|
|
|
2013-08-11 14:10:57 +01:00
|
|
|
if (EMPTY(str) || ! g_utf8_validate(str, -1, NULL))
|
2007-09-07 19:01:55 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while (*str != '\0')
|
|
|
|
{
|
|
|
|
c = g_utf8_get_char(str);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* check only letters and stop once the first non-capital was found */
|
2007-09-07 19:01:55 +00:00
|
|
|
if (g_unichar_isalpha(c) && g_unichar_isupper(c))
|
|
|
|
return TRUE;
|
2008-03-13 17:53:03 +00:00
|
|
|
/* FIXME don't write a const string */
|
2007-09-07 19:01:55 +00:00
|
|
|
str = g_utf8_next_char(str);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-09-11 15:21:11 +00:00
|
|
|
|
|
|
|
|
2011-04-19 16:36:29 +00:00
|
|
|
/* end can be -1 for haystack->len.
|
|
|
|
* returns: position of found text or -1. */
|
|
|
|
gint utils_string_find(GString *haystack, gint start, gint end, const gchar *needle)
|
2009-01-11 21:05:03 +00:00
|
|
|
{
|
2011-04-15 16:59:15 +00:00
|
|
|
gint pos;
|
2009-01-11 21:05:03 +00:00
|
|
|
|
2011-04-15 16:59:15 +00:00
|
|
|
g_return_val_if_fail(haystack != NULL, -1);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (haystack->len == 0)
|
2011-04-15 16:59:15 +00:00
|
|
|
return -1;
|
2009-01-11 21:05:03 +00:00
|
|
|
|
2011-04-15 16:59:15 +00:00
|
|
|
g_return_val_if_fail(start >= 0, -1);
|
|
|
|
if (start >= (gint)haystack->len)
|
|
|
|
return -1;
|
2009-01-11 21:05:03 +00:00
|
|
|
|
2013-08-11 14:10:57 +01:00
|
|
|
g_return_val_if_fail(!EMPTY(needle), -1);
|
2009-01-11 21:05:03 +00:00
|
|
|
|
2011-04-15 16:59:15 +00:00
|
|
|
if (end < 0)
|
|
|
|
end = haystack->len;
|
2009-01-11 21:05:03 +00:00
|
|
|
|
2011-04-15 16:59:15 +00:00
|
|
|
pos = utils_strpos(haystack->str + start, needle);
|
|
|
|
if (pos == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pos += start;
|
|
|
|
if (pos >= end)
|
|
|
|
return -1;
|
|
|
|
return pos;
|
2009-01-11 21:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-19 16:36:29 +00:00
|
|
|
/* Replaces @len characters from offset @a pos.
|
2011-10-05 15:33:40 +00:00
|
|
|
* len can be -1 to replace the remainder of @a str.
|
2011-04-19 16:36:29 +00:00
|
|
|
* returns: pos + strlen(replace). */
|
|
|
|
gint utils_string_replace(GString *str, gint pos, gint len, const gchar *replace)
|
|
|
|
{
|
|
|
|
g_string_erase(str, pos, len);
|
2011-09-28 19:49:28 +00:00
|
|
|
if (replace)
|
|
|
|
{
|
|
|
|
g_string_insert(str, pos, replace);
|
|
|
|
pos += strlen(replace);
|
|
|
|
}
|
|
|
|
return pos;
|
2011-04-19 16:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2009-07-21 22:09:43 +00:00
|
|
|
* Replaces all occurrences of @a needle in @a haystack with @a replace.
|
2008-11-13 17:19:52 +00:00
|
|
|
* As of Geany 0.16, @a replace can match @a needle, so the following will work:
|
|
|
|
* @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2008-11-13 17:19:52 +00:00
|
|
|
* @param haystack The input string to operate on. This string is modified in place.
|
|
|
|
* @param needle The string which should be replaced.
|
2009-07-21 22:09:43 +00:00
|
|
|
* @param replace The replacement for @a needle.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2010-03-12 18:15:48 +00:00
|
|
|
* @return Number of replacements made.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-01-11 21:05:03 +00:00
|
|
|
guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
|
2007-09-11 15:21:11 +00:00
|
|
|
{
|
2011-04-15 16:59:15 +00:00
|
|
|
guint count = 0;
|
|
|
|
gint pos = 0;
|
2011-09-28 19:49:45 +00:00
|
|
|
gsize needle_length = strlen(needle);
|
2011-04-15 16:59:15 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2011-04-19 16:36:29 +00:00
|
|
|
pos = utils_string_find(haystack, pos, -1, needle);
|
2011-04-15 16:59:15 +00:00
|
|
|
|
|
|
|
if (pos == -1)
|
|
|
|
break;
|
|
|
|
|
2011-09-28 19:49:45 +00:00
|
|
|
pos = utils_string_replace(haystack, pos, needle_length, replace);
|
2011-04-15 16:59:15 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
2009-01-11 21:05:03 +00:00
|
|
|
}
|
2008-11-13 17:19:52 +00:00
|
|
|
|
|
|
|
|
2009-01-30 16:12:25 +00:00
|
|
|
/**
|
2010-03-12 18:15:48 +00:00
|
|
|
* Replaces only the first occurrence of @a needle in @a haystack
|
2009-07-21 22:09:43 +00:00
|
|
|
* with @a replace.
|
2009-01-30 16:12:25 +00:00
|
|
|
* For details, see utils_string_replace_all().
|
2009-01-11 21:05:03 +00:00
|
|
|
*
|
|
|
|
* @param haystack The input string to operate on. This string is modified in place.
|
|
|
|
* @param needle The string which should be replaced.
|
2009-07-21 22:09:43 +00:00
|
|
|
* @param replace The replacement for @a needle.
|
2009-01-11 21:05:03 +00:00
|
|
|
*
|
2010-03-12 18:15:48 +00:00
|
|
|
* @return Number of replacements made.
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.16
|
2009-01-11 21:05:03 +00:00
|
|
|
*/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-01-11 21:05:03 +00:00
|
|
|
guint utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace)
|
|
|
|
{
|
2011-04-19 16:36:29 +00:00
|
|
|
gint pos = utils_string_find(haystack, 0, -1, needle);
|
|
|
|
|
|
|
|
if (pos == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
utils_string_replace(haystack, pos, strlen(needle), replace);
|
|
|
|
return 1;
|
2007-09-11 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-30 11:10:22 +00:00
|
|
|
/* Similar to g_regex_replace but allows matching a subgroup.
|
|
|
|
* match_num: which match to replace, 0 for whole match.
|
|
|
|
* literal: FALSE to interpret escape sequences in @a replace.
|
|
|
|
* returns: number of replacements.
|
|
|
|
* bug: replaced text can affect matching of ^ or \b */
|
|
|
|
guint utils_string_regex_replace_all(GString *haystack, GRegex *regex,
|
|
|
|
guint match_num, const gchar *replace, gboolean literal)
|
|
|
|
{
|
|
|
|
GMatchInfo *minfo;
|
|
|
|
guint ret = 0;
|
|
|
|
gint start = 0;
|
|
|
|
|
|
|
|
g_assert(literal); /* escapes not implemented yet */
|
|
|
|
g_return_val_if_fail(replace, 0);
|
|
|
|
|
|
|
|
/* ensure haystack->str is not null */
|
|
|
|
if (haystack->len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* passing a start position makes G_REGEX_MATCH_NOTBOL automatic */
|
|
|
|
while (g_regex_match_full(regex, haystack->str, -1, start, 0, &minfo, NULL))
|
|
|
|
{
|
|
|
|
gint end, len;
|
2011-10-11 21:52:58 -07:00
|
|
|
|
2011-09-30 11:10:22 +00:00
|
|
|
g_match_info_fetch_pos(minfo, match_num, &start, &end);
|
|
|
|
len = end - start;
|
|
|
|
utils_string_replace(haystack, start, len, replace);
|
|
|
|
ret++;
|
2011-10-11 21:52:58 -07:00
|
|
|
|
2011-09-30 11:10:22 +00:00
|
|
|
/* skip past whole match */
|
|
|
|
g_match_info_fetch_pos(minfo, 0, NULL, &end);
|
|
|
|
start = end - len + strlen(replace);
|
|
|
|
g_match_info_free(minfo);
|
|
|
|
}
|
|
|
|
g_match_info_free(minfo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-21 17:25:58 +00:00
|
|
|
/* Get project or default startup directory (if set), or NULL. */
|
2008-02-20 11:24:23 +00:00
|
|
|
const gchar *utils_get_default_dir_utf8(void)
|
2007-12-21 17:25:58 +00:00
|
|
|
{
|
2013-08-11 14:10:57 +01:00
|
|
|
if (app->project && !EMPTY(app->project->base_path))
|
2007-12-21 17:25:58 +00:00
|
|
|
{
|
|
|
|
return app->project->base_path;
|
|
|
|
}
|
|
|
|
|
2013-08-11 14:10:57 +01:00
|
|
|
if (!EMPTY(prefs.default_open_path))
|
2007-12-21 17:25:58 +00:00
|
|
|
{
|
|
|
|
return prefs.default_open_path;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-24 10:27:32 +00:00
|
|
|
/**
|
2015-03-29 13:47:13 +03:00
|
|
|
* Wraps @c spawn_sync(), which see.
|
2008-02-24 10:27:32 +00:00
|
|
|
*
|
|
|
|
* @param dir The child's current working directory, or @a NULL to inherit parent's.
|
|
|
|
* @param argv The child's argument vector.
|
|
|
|
* @param env The child's environment, or @a NULL to inherit parent's.
|
2015-03-29 13:47:13 +03:00
|
|
|
* @param flags Ignored.
|
|
|
|
* @param child_setup Ignored.
|
|
|
|
* @param user_data Ignored.
|
2014-10-14 23:18:38 +02:00
|
|
|
* @param std_out The return location for child output, or @a NULL.
|
|
|
|
* @param std_err The return location for child error messages, or @a NULL.
|
|
|
|
* @param exit_status The child exit status, as returned by waitpid(), or @a NULL.
|
2008-02-24 10:27:32 +00:00
|
|
|
* @param error The return location for error or @a NULL.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return @c TRUE on success, @c FALSE if an error was set.
|
2008-02-24 10:27:32 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2008-02-24 10:27:32 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-03-29 13:47:13 +03:00
|
|
|
GString *output = std_out ? g_string_new(NULL) : NULL;
|
|
|
|
GString *errors = std_err ? g_string_new(NULL) : NULL;
|
|
|
|
gboolean result = spawn_sync(dir, NULL, argv, env, NULL, output, errors, exit_status, error);
|
2008-02-24 10:27:32 +00:00
|
|
|
|
|
|
|
if (std_out)
|
2015-03-29 13:47:13 +03:00
|
|
|
*std_out = g_string_free(output, !result);
|
2008-02-24 10:27:32 +00:00
|
|
|
|
|
|
|
if (std_err)
|
2015-03-29 13:47:13 +03:00
|
|
|
*std_err = g_string_free(errors, !result);
|
2008-02-24 10:27:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-29 13:47:13 +03:00
|
|
|
* Wraps @c spawn_async(), which see.
|
2008-02-24 10:27:32 +00:00
|
|
|
*
|
|
|
|
* @param dir The child's current working directory, or @a NULL to inherit parent's.
|
|
|
|
* @param argv The child's argument vector.
|
|
|
|
* @param env The child's environment, or @a NULL to inherit parent's.
|
2015-03-29 13:47:13 +03:00
|
|
|
* @param flags Ignored.
|
|
|
|
* @param child_setup Ignored.
|
|
|
|
* @param user_data Ignored.
|
2008-02-24 10:27:32 +00:00
|
|
|
* @param child_pid The return location for child process ID, or NULL.
|
|
|
|
* @param error The return location for error or @a NULL.
|
|
|
|
*
|
2009-07-21 22:09:43 +00:00
|
|
|
* @return @c TRUE on success, @c FALSE if an error was set.
|
2008-02-24 10:27:32 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2008-02-24 10:27:32 +00:00
|
|
|
gboolean utils_spawn_async(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
|
|
|
|
GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
|
|
|
|
GError **error)
|
|
|
|
{
|
2015-03-29 13:47:13 +03:00
|
|
|
return spawn_async(dir, NULL, argv, env, child_pid, error);
|
2008-02-24 10:27:32 +00:00
|
|
|
}
|
2008-11-26 13:15:53 +00:00
|
|
|
|
|
|
|
|
2009-01-09 18:21:12 +00:00
|
|
|
/* Retrieves the path for the given URI.
|
|
|
|
* It returns:
|
|
|
|
* - the path which was determined by g_filename_from_uri() or GIO
|
|
|
|
* - NULL if the URI is non-local and gvfs-fuse is not installed
|
|
|
|
* - a new copy of 'uri' if it is not an URI. */
|
|
|
|
gchar *utils_get_path_from_uri(const gchar *uri)
|
|
|
|
{
|
|
|
|
gchar *locale_filename;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(uri != NULL, NULL);
|
2009-01-09 18:21:12 +00:00
|
|
|
|
|
|
|
if (! utils_is_uri(uri))
|
|
|
|
return g_strdup(uri);
|
|
|
|
|
|
|
|
/* this will work only for 'file://' URIs */
|
|
|
|
locale_filename = g_filename_from_uri(uri, NULL, NULL);
|
|
|
|
/* g_filename_from_uri() failed, so we probably have a non-local URI */
|
|
|
|
if (locale_filename == NULL)
|
|
|
|
{
|
|
|
|
GFile *file = g_file_new_for_uri(uri);
|
|
|
|
locale_filename = g_file_get_path(file);
|
|
|
|
g_object_unref(file);
|
|
|
|
if (locale_filename == NULL)
|
|
|
|
{
|
|
|
|
geany_debug("The URI '%s' could not be resolved to a local path. This means "
|
|
|
|
"that the URI is invalid or that you don't have gvfs-fuse installed.", uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return locale_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean utils_is_uri(const gchar *uri)
|
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(uri != NULL, FALSE);
|
2009-01-09 18:21:12 +00:00
|
|
|
|
|
|
|
return (strstr(uri, "://") != NULL);
|
|
|
|
}
|
2009-01-09 18:22:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* path should be in locale encoding */
|
|
|
|
gboolean utils_is_remote_path(const gchar *path)
|
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(path != NULL, FALSE);
|
2009-01-09 18:22:07 +00:00
|
|
|
|
|
|
|
/* if path is an URI and it doesn't start "file://", we take it as remote */
|
|
|
|
if (utils_is_uri(path) && strncmp(path, "file:", 5) != 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
#ifndef G_OS_WIN32
|
2011-10-28 10:25:58 -07:00
|
|
|
{
|
2011-12-17 23:10:03 +01:00
|
|
|
static gchar *fuse_path = NULL;
|
|
|
|
static gsize len = 0;
|
|
|
|
|
|
|
|
if (G_UNLIKELY(fuse_path == NULL))
|
|
|
|
{
|
|
|
|
fuse_path = g_build_filename(g_get_home_dir(), ".gvfs", NULL);
|
|
|
|
len = strlen(fuse_path);
|
|
|
|
}
|
|
|
|
/* Comparing the file path against a hardcoded path is not the most elegant solution
|
|
|
|
* but for now it is better than nothing. Ideally, g_file_new_for_path() should create
|
|
|
|
* proper GFile objects for Fuse paths, but it only does in future GVFS
|
|
|
|
* versions (gvfs 1.1.1). */
|
|
|
|
return (strncmp(path, fuse_path, len) == 0);
|
2009-01-09 18:22:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-10-28 10:25:58 -07:00
|
|
|
|
2009-01-09 18:22:07 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-20 15:37:02 +00:00
|
|
|
/* Remove all relative and untidy elements from the path of @a filename.
|
|
|
|
* @param filename must be a valid absolute path.
|
|
|
|
* @see tm_get_real_path() - also resolves links. */
|
|
|
|
void utils_tidy_path(gchar *filename)
|
|
|
|
{
|
2013-11-03 01:52:12 +01:00
|
|
|
GString *str;
|
2013-11-07 23:07:33 +01:00
|
|
|
const gchar *needle;
|
2009-08-29 19:06:19 +00:00
|
|
|
gboolean preserve_double_backslash = FALSE;
|
2009-07-20 15:37:02 +00:00
|
|
|
|
|
|
|
g_return_if_fail(g_path_is_absolute(filename));
|
|
|
|
|
2013-11-03 01:52:12 +01:00
|
|
|
str = g_string_new(filename);
|
|
|
|
|
2009-08-29 19:06:19 +00:00
|
|
|
if (str->len >= 2 && strncmp(str->str, "\\\\", 2) == 0)
|
|
|
|
preserve_double_backslash = TRUE;
|
|
|
|
|
2011-11-01 15:46:11 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* using MSYS we can get Unix-style separators */
|
|
|
|
utils_string_replace_all(str, "/", G_DIR_SEPARATOR_S);
|
|
|
|
#endif
|
2009-07-20 15:37:02 +00:00
|
|
|
/* replace "/./" and "//" */
|
|
|
|
utils_string_replace_all(str, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
|
|
|
|
utils_string_replace_all(str, G_DIR_SEPARATOR_S G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
|
|
|
|
|
2009-08-29 19:06:19 +00:00
|
|
|
if (preserve_double_backslash)
|
|
|
|
g_string_prepend(str, "\\");
|
|
|
|
|
2009-09-14 11:14:23 +00:00
|
|
|
/* replace "/../" */
|
|
|
|
needle = G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S;
|
2009-07-20 15:37:02 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2013-11-07 23:07:33 +01:00
|
|
|
const gchar *c = strstr(str->str, needle);
|
2009-07-20 15:37:02 +00:00
|
|
|
if (c == NULL)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
2013-11-07 23:07:33 +01:00
|
|
|
gssize pos, sub_len;
|
|
|
|
|
2009-07-20 15:37:02 +00:00
|
|
|
pos = c - str->str;
|
|
|
|
if (pos <= 3)
|
|
|
|
break; /* bad path */
|
|
|
|
|
2009-09-14 11:14:23 +00:00
|
|
|
/* replace "/../" */
|
|
|
|
g_string_erase(str, pos, strlen(needle));
|
|
|
|
g_string_insert_c(str, pos, G_DIR_SEPARATOR);
|
2009-07-20 15:37:02 +00:00
|
|
|
|
2013-11-07 23:07:33 +01:00
|
|
|
/* search for last "/" before found "/../" */
|
|
|
|
c = g_strrstr_len(str->str, pos, G_DIR_SEPARATOR_S);
|
|
|
|
sub_len = pos - (c - str->str);
|
|
|
|
if (! c)
|
|
|
|
break; /* bad path */
|
2009-07-20 15:37:02 +00:00
|
|
|
|
2013-11-07 23:07:33 +01:00
|
|
|
pos = c - str->str; /* position of previous "/" */
|
|
|
|
g_string_erase(str, pos, sub_len);
|
2009-07-20 15:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-07 23:07:33 +01:00
|
|
|
if (str->len <= strlen(filename))
|
|
|
|
memcpy(filename, str->str, str->len + 1);
|
|
|
|
else
|
|
|
|
g_warn_if_reached();
|
2009-07-20 15:37:02 +00:00
|
|
|
g_string_free(str, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-20 23:10:46 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Removes characters from a string, in place.
|
2009-07-20 23:10:46 +00:00
|
|
|
*
|
2009-07-23 11:00:32 +00:00
|
|
|
* @param string String to search.
|
2009-07-20 23:10:46 +00:00
|
|
|
* @param chars Characters to remove.
|
|
|
|
*
|
2009-07-23 11:00:32 +00:00
|
|
|
* @return @a string - return value is only useful when nesting function calls, e.g.:
|
|
|
|
* @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
|
|
|
|
*
|
|
|
|
* @see @c g_strdelimit.
|
2009-07-20 23:10:46 +00:00
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2009-07-23 11:00:32 +00:00
|
|
|
gchar *utils_str_remove_chars(gchar *string, const gchar *chars)
|
2009-07-20 23:10:46 +00:00
|
|
|
{
|
2009-07-23 11:00:32 +00:00
|
|
|
const gchar *r;
|
|
|
|
gchar *w = string;
|
2009-07-20 23:10:46 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail(string, NULL);
|
2013-08-11 14:10:57 +01:00
|
|
|
if (G_UNLIKELY(EMPTY(chars)))
|
2009-07-23 11:00:32 +00:00
|
|
|
return string;
|
2009-07-20 23:10:46 +00:00
|
|
|
|
2009-07-23 11:00:32 +00:00
|
|
|
foreach_str(r, string)
|
2009-07-20 23:10:46 +00:00
|
|
|
{
|
2009-07-23 11:00:32 +00:00
|
|
|
if (!strchr(chars, *r))
|
|
|
|
*w++ = *r;
|
2009-07-20 23:10:46 +00:00
|
|
|
}
|
2009-07-23 11:00:32 +00:00
|
|
|
*w = 0x0;
|
|
|
|
return string;
|
2009-07-20 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 14:47:07 +00:00
|
|
|
/* Gets list of sorted filenames with no path and no duplicates from user and system config */
|
|
|
|
GSList *utils_get_config_files(const gchar *subdir)
|
|
|
|
{
|
|
|
|
gchar *path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, subdir, NULL);
|
|
|
|
GSList *list = utils_get_file_list_full(path, FALSE, FALSE, NULL);
|
|
|
|
GSList *syslist, *node;
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
{
|
|
|
|
utils_mkdir(path, FALSE);
|
|
|
|
}
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(path, g_build_path(G_DIR_SEPARATOR_S, app->datadir, subdir, NULL));
|
2009-11-04 14:47:07 +00:00
|
|
|
syslist = utils_get_file_list_full(path, FALSE, FALSE, NULL);
|
|
|
|
/* merge lists */
|
|
|
|
list = g_slist_concat(list, syslist);
|
|
|
|
|
|
|
|
list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp);
|
|
|
|
/* remove duplicates (next to each other after sorting) */
|
|
|
|
foreach_slist(node, list)
|
|
|
|
{
|
|
|
|
if (node->next && utils_str_equal(node->next->data, node->data))
|
|
|
|
{
|
2012-01-26 16:10:53 +00:00
|
|
|
GSList *old = node->next;
|
|
|
|
|
|
|
|
g_free(old->data);
|
|
|
|
node->next = old->next;
|
|
|
|
g_slist_free1(old);
|
2009-11-04 14:47:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-23 22:59:36 +00:00
|
|
|
/* Suffix can be NULL or a string which should be appended to the Help URL like
|
|
|
|
* an anchor link, e.g. "#some_anchor". */
|
|
|
|
gchar *utils_get_help_url(const gchar *suffix)
|
|
|
|
{
|
|
|
|
gint skip;
|
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
skip = 8;
|
|
|
|
uri = g_strconcat("file:///", app->docdir, "/Manual.html", NULL);
|
|
|
|
g_strdelimit(uri, "\\", '/'); /* replace '\\' by '/' */
|
|
|
|
#else
|
|
|
|
skip = 7;
|
2010-11-01 15:17:22 +00:00
|
|
|
uri = g_strconcat("file://", app->docdir, "/index.html", NULL);
|
2009-11-23 22:59:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (! g_file_test(uri + skip, G_FILE_TEST_IS_REGULAR))
|
|
|
|
{ /* fall back to online documentation if it is not found on the hard disk */
|
|
|
|
g_free(uri);
|
|
|
|
uri = g_strconcat(GEANY_HOMEPAGE, "manual/", VERSION, "/index.html", NULL);
|
|
|
|
}
|
2010-01-01 22:55:18 +00:00
|
|
|
|
2009-11-23 22:59:36 +00:00
|
|
|
if (suffix != NULL)
|
|
|
|
{
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(uri, g_strconcat(uri, suffix, NULL));
|
2009-11-23 22:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2010-01-24 14:18:00 +00:00
|
|
|
|
|
|
|
static gboolean str_in_array(const gchar **haystack, const gchar *needle)
|
|
|
|
{
|
|
|
|
const gchar **p;
|
|
|
|
|
|
|
|
for (p = haystack; *p != NULL; ++p)
|
|
|
|
{
|
|
|
|
if (utils_str_equal(*p, needle))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 14:48:43 +00:00
|
|
|
/**
|
|
|
|
* Copies the current environment into a new array.
|
|
|
|
* @a exclude_vars is a @c NULL-terminated array of variable names which should be not copied.
|
2010-01-24 14:18:00 +00:00
|
|
|
* All further arguments are key, value pairs of variables which should be added to
|
|
|
|
* the environment.
|
|
|
|
*
|
2010-03-15 14:48:43 +00:00
|
|
|
* The argument list must be @c NULL-terminated.
|
|
|
|
*
|
|
|
|
* @param exclude_vars @c NULL-terminated array of variable names to exclude.
|
|
|
|
* @param first_varname Name of the first variable to copy into the new array.
|
|
|
|
* @param ... Key-value pairs of variable names and values, @c NULL-terminated.
|
|
|
|
*
|
|
|
|
* @return The new environment array.
|
|
|
|
**/
|
2014-10-13 16:36:36 -07:00
|
|
|
GEANY_API_SYMBOL
|
2010-01-24 14:18:00 +00:00
|
|
|
gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...)
|
|
|
|
{
|
|
|
|
gchar **result;
|
|
|
|
gchar **p;
|
|
|
|
gchar **env;
|
|
|
|
va_list args;
|
|
|
|
const gchar *key, *value;
|
|
|
|
guint n, o;
|
|
|
|
|
|
|
|
/* count the additional variables */
|
|
|
|
va_start(args, first_varname);
|
|
|
|
for (o = 1; va_arg(args, gchar*) != NULL; o++);
|
|
|
|
va_end(args);
|
|
|
|
/* the passed arguments should be even (key, value pairs) */
|
|
|
|
g_return_val_if_fail(o % 2 == 0, NULL);
|
|
|
|
|
|
|
|
o /= 2;
|
|
|
|
|
2014-05-05 16:03:25 +02:00
|
|
|
/* get all the environ variables */
|
|
|
|
env = g_listenv();
|
|
|
|
|
2010-01-24 14:18:00 +00:00
|
|
|
/* create an array large enough to hold the new environment */
|
|
|
|
n = g_strv_length(env);
|
|
|
|
/* 'n + o + 1' could leak a little bit when exclude_vars is set */
|
|
|
|
result = g_new(gchar *, n + o + 1);
|
|
|
|
|
|
|
|
/* copy the environment */
|
|
|
|
for (n = 0, p = env; *p != NULL; ++p)
|
|
|
|
{
|
|
|
|
/* copy the variable */
|
|
|
|
value = g_getenv(*p);
|
|
|
|
if (G_LIKELY(value != NULL))
|
|
|
|
{
|
|
|
|
/* skip excluded variables */
|
|
|
|
if (exclude_vars != NULL && str_in_array(exclude_vars, *p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result[n++] = g_strconcat(*p, "=", value, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(env);
|
|
|
|
|
|
|
|
/* now add additional variables */
|
|
|
|
va_start(args, first_varname);
|
|
|
|
key = first_varname;
|
|
|
|
value = va_arg(args, gchar*);
|
|
|
|
while (key != NULL)
|
|
|
|
{
|
|
|
|
result[n++] = g_strconcat(key, "=", value, NULL);
|
|
|
|
|
|
|
|
key = va_arg(args, gchar*);
|
|
|
|
if (key == NULL)
|
|
|
|
break;
|
|
|
|
value = va_arg(args, gchar*);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
result[n] = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 11:07:10 +00:00
|
|
|
/* Joins @a first and @a second into a new string vector, freeing the originals.
|
|
|
|
* The original contents are reused. */
|
|
|
|
gchar **utils_strv_join(gchar **first, gchar **second)
|
|
|
|
{
|
|
|
|
gchar **strv;
|
|
|
|
gchar **rptr, **wptr;
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
return second;
|
|
|
|
if (!second)
|
|
|
|
return first;
|
|
|
|
|
|
|
|
strv = g_new0(gchar*, g_strv_length(first) + g_strv_length(second) + 1);
|
|
|
|
wptr = strv;
|
|
|
|
|
|
|
|
foreach_strv(rptr, first)
|
|
|
|
*wptr++ = *rptr;
|
|
|
|
foreach_strv(rptr, second)
|
|
|
|
*wptr++ = *rptr;
|
|
|
|
|
|
|
|
g_free(first);
|
|
|
|
g_free(second);
|
|
|
|
return strv;
|
|
|
|
}
|
2013-03-17 17:16:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Try to parse a date using g_date_set_parse(). It doesn't take any format hint,
|
|
|
|
* obviously g_date_set_parse() uses some magic.
|
|
|
|
* The returned GDate object must be freed. */
|
|
|
|
GDate *utils_parse_date(const gchar *input)
|
|
|
|
{
|
|
|
|
GDate *date = g_date_new();
|
|
|
|
|
|
|
|
g_date_set_parse(date, input);
|
|
|
|
|
|
|
|
if (g_date_valid(date))
|
|
|
|
return date;
|
|
|
|
|
|
|
|
g_date_free(date);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar *utils_parse_and_format_build_date(const gchar *input)
|
|
|
|
{
|
|
|
|
gchar date_buf[255];
|
|
|
|
GDate *date = utils_parse_date(input);
|
|
|
|
|
|
|
|
if (date != NULL)
|
2013-03-17 19:36:17 +01:00
|
|
|
{
|
2013-03-17 17:16:32 +01:00
|
|
|
g_date_strftime(date_buf, sizeof(date_buf), GEANY_TEMPLATES_FORMAT_DATE, date);
|
2013-03-17 19:36:17 +01:00
|
|
|
g_date_free(date);
|
2013-03-17 17:16:32 +01:00
|
|
|
return g_strdup(date_buf);
|
2013-03-17 19:36:17 +01:00
|
|
|
}
|
2013-03-17 17:16:32 +01:00
|
|
|
|
|
|
|
return g_strdup(input);
|
|
|
|
}
|
2014-10-05 12:55:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
gchar *utils_get_user_config_dir(void)
|
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return win32_get_user_config_dir();
|
|
|
|
#else
|
2014-10-06 16:24:26 +02:00
|
|
|
return g_build_filename(g_get_user_config_dir(), "geany", NULL);
|
2014-10-05 12:55:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
2015-02-04 00:27:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
static gboolean is_osx_bundle(void)
|
|
|
|
{
|
|
|
|
#ifdef MAC_INTEGRATION
|
|
|
|
gchar *bundle_id = gtkosx_application_get_bundle_id();
|
|
|
|
if (bundle_id)
|
|
|
|
{
|
|
|
|
g_free(bundle_id);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const gchar *utils_resource_dir(GeanyResourceDirType type)
|
|
|
|
{
|
|
|
|
static const gchar *resdirs[RESOURCE_DIR_COUNT] = {NULL};
|
|
|
|
|
|
|
|
if (!resdirs[RESOURCE_DIR_DATA])
|
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
gchar *prefix = win32_get_installation_dir();
|
|
|
|
|
|
|
|
resdirs[RESOURCE_DIR_DATA] = g_build_filename(prefix, "data", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_ICON] = g_build_filename(prefix, "share", "icons", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_DOC] = g_build_filename(prefix, "doc", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_LOCALE] = g_build_filename(prefix, "share", "locale", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_PLUGIN] = g_build_filename(prefix, "lib", NULL);
|
|
|
|
g_free(prefix);
|
|
|
|
#else
|
|
|
|
if (is_osx_bundle())
|
|
|
|
{
|
|
|
|
# ifdef MAC_INTEGRATION
|
|
|
|
gchar *prefix = gtkosx_application_get_resource_path();
|
|
|
|
|
|
|
|
resdirs[RESOURCE_DIR_DATA] = g_build_filename(prefix, "share", "geany", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_ICON] = g_build_filename(prefix, "share", "icons", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_DOC] = g_build_filename(prefix, "share", "doc", "geany", "html", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_LOCALE] = g_build_filename(prefix, "share", "locale", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_PLUGIN] = g_build_filename(prefix, "lib", "geany", NULL);
|
|
|
|
g_free(prefix);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resdirs[RESOURCE_DIR_DATA] = g_build_filename(GEANY_DATADIR, "geany", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_ICON] = g_build_filename(GEANY_DATADIR, "icons", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_DOC] = g_build_filename(GEANY_DOCDIR, "html", NULL);
|
|
|
|
resdirs[RESOURCE_DIR_LOCALE] = g_build_filename(GEANY_LOCALEDIR, NULL);
|
|
|
|
resdirs[RESOURCE_DIR_PLUGIN] = g_build_filename(GEANY_LIBDIR, "geany", NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return resdirs[type];
|
|
|
|
}
|
2015-02-18 17:19:57 +01:00
|
|
|
|
|
|
|
|
2015-03-17 14:09:31 +01:00
|
|
|
void utils_start_new_geany_instance(const gchar *doc_path)
|
2015-02-18 17:19:57 +01:00
|
|
|
{
|
2015-04-10 18:52:46 -07:00
|
|
|
const gchar *const *argv;
|
2015-02-18 17:19:57 +01:00
|
|
|
const gchar *command = is_osx_bundle() ? "open" : "geany";
|
|
|
|
gchar *exec_path = g_find_program_in_path(command);
|
|
|
|
|
|
|
|
if (exec_path)
|
|
|
|
{
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
if (is_osx_bundle())
|
|
|
|
{
|
2015-04-10 18:52:46 -07:00
|
|
|
const gchar *const osx_argv[] = {exec_path, "-n", "-a", "Geany", doc_path, NULL};
|
2015-02-18 17:19:57 +01:00
|
|
|
argv = osx_argv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-10 18:52:46 -07:00
|
|
|
const gchar *const unix_argv[] = {exec_path, "-i", doc_path, NULL};
|
2015-02-18 17:19:57 +01:00
|
|
|
argv = unix_argv;
|
|
|
|
}
|
|
|
|
|
2015-04-10 18:52:46 -07:00
|
|
|
if (!utils_spawn_async(NULL, (gchar**) argv, NULL, 0, NULL, NULL, NULL, &err))
|
2015-02-18 17:19:57 +01:00
|
|
|
{
|
|
|
|
g_printerr("Unable to open new window: %s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
g_free(exec_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_printerr("Unable to find 'geany'");
|
|
|
|
}
|