Kaydet (Commit) 160e5e84 authored tarafından Enrico Tröger's avatar Enrico Tröger

Attempt to make utils_get_date_time() UTF-8 safe and add it to the plugin API.

Fix misnamed str_casecmp() function in the plugin API, sorry.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3267 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst ffffc759
......@@ -3,6 +3,11 @@
* src/main.c:
Try to fix some problems when opening files with non-Ascii characters
on Windows from the command line.
* plugins/export.c, plugins/saveactions.c, src/callbacks.c,
src/plugindata.h, src/plugins.c, src/utils.c:
Attempt to make utils_get_date_time() UTF-8 safe and add it to the
plugin API.
Fix misnamed str_casecmp() function in the plugin API, sorry.
2008-11-21 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
......
......@@ -281,20 +281,12 @@ static void write_data(const gchar *filename, const gchar *data)
}
static const gchar *get_date(gint type)
static gchar *get_date(gint type)
{
static gchar str[128];
gchar *format;
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp == NULL)
return "";
if (type == DATE_TYPE_HTML)
/** needs testing */
/* needs testing */
#ifdef _GNU_SOURCE
format = "%Y-%m-%dT%H:%M:%S%z";
#else
......@@ -303,9 +295,7 @@ static const gchar *get_date(gint type)
else
format = "%c";
strftime(str, sizeof str, format, tmp);
return str;
return p_utils->get_date_time(format, NULL);
}
......@@ -350,7 +340,7 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean
{
GeanyEditor *editor = doc->editor;
gint i, style = -1, old_style = 0, column = 0;
gchar c, c_next, *tmp;
gchar c, c_next, *tmp, *date;
/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
gint styles[STYLE_MAX + 1][MAX_TYPES];
gboolean block_open = FALSE;
......@@ -533,11 +523,12 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean
}
}
date = get_date(DATE_TYPE_DEFAULT);
/* write all */
latex = g_string_new(TEMPLATE_LATEX);
p_utils->string_replace_all(latex, "{export_content}", body->str);
p_utils->string_replace_all(latex, "{export_styles}", cmds->str);
p_utils->string_replace_all(latex, "{export_date}", get_date(DATE_TYPE_DEFAULT));
p_utils->string_replace_all(latex, "{export_date}", date);
if (doc->file_name == NULL)
p_utils->string_replace_all(latex, "{export_filename}", GEANY_STRING_UNTITLED);
else
......@@ -548,6 +539,7 @@ static void write_latex_file(GeanyDocument *doc, const gchar *filename, gboolean
g_string_free(body, TRUE);
g_string_free(cmds, TRUE);
g_string_free(latex, TRUE);
g_free(date);
}
......@@ -555,7 +547,7 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean
{
GeanyEditor *editor = doc->editor;
gint i, style = -1, old_style = 0, column = 0;
gchar c, c_next;
gchar c, c_next, *date;
/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
gint styles[STYLE_MAX + 1][MAX_TYPES];
gboolean span_open = FALSE;
......@@ -686,9 +678,10 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean
}
}
date = get_date(DATE_TYPE_HTML);
/* write all */
html = g_string_new(TEMPLATE_HTML);
p_utils->string_replace_all(html, "{export_date}", get_date(DATE_TYPE_HTML));
p_utils->string_replace_all(html, "{export_date}", date);
p_utils->string_replace_all(html, "{export_content}", body->str);
p_utils->string_replace_all(html, "{export_styles}", css->str);
if (doc->file_name == NULL)
......@@ -702,6 +695,7 @@ static void write_html_file(GeanyDocument *doc, const gchar *filename, gboolean
g_string_free(body, TRUE);
g_string_free(css, TRUE);
g_string_free(html, TRUE);
g_free(date);
}
......
......@@ -196,14 +196,11 @@ static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpoint
gchar *locale_filename_dst;
gchar *basename_src;
gchar *dir_parts_src;
gchar stamp[512];
time_t t = time(NULL);
struct tm *now;
gchar *stamp;
if (! enable_backupcopy)
return;
now = localtime(&t);
locale_filename_src = p_utils->get_locale_from_utf8(doc->file_name);
if ((src = g_fopen(locale_filename_src, "r")) == NULL)
......@@ -215,7 +212,7 @@ static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpoint
return;
}
strftime(stamp, sizeof(stamp), backupcopy_time_fmt, now);
stamp = p_utils->get_date_time(backupcopy_time_fmt, NULL);
basename_src = g_path_get_basename(locale_filename_src);
dir_parts_src = backupcopy_create_dir_parts(locale_filename_src);
locale_filename_dst = g_strconcat(
......@@ -231,6 +228,7 @@ static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpoint
g_strerror(errno));
g_free(locale_filename_src);
g_free(locale_filename_dst);
g_free(stamp);
fclose(src);
return;
}
......@@ -244,6 +242,7 @@ static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpoint
fclose(dst);
g_free(locale_filename_src);
g_free(locale_filename_dst);
g_free(stamp);
}
......
......@@ -1418,10 +1418,7 @@ on_insert_date_activate (GtkMenuItem *menuitem,
{
GeanyDocument *doc = document_get_current();
gchar *format;
gchar time_str[300]; /* the entered format string can be maximal 256 chars long, so we have
* 44 additional characters for strtime's conversion */
time_t t;
struct tm *tm;
gchar *time_str;
if (doc == NULL) return;
......@@ -1454,15 +1451,14 @@ on_insert_date_activate (GtkMenuItem *menuitem,
return;
}
/* get the current time */
t = time(NULL);
tm = localtime(&t);
if (strftime(time_str, sizeof time_str, format, tm) != 0)
time_str = utils_get_date_time(format, NULL);
if (time_str != NULL)
{
verify_click_pos(doc); /* make sure that the click_pos is valid */
sci_insert_text(doc->editor->sci, editor_info.click_pos, time_str);
sci_goto_pos(doc->editor->sci, editor_info.click_pos + strlen(time_str), FALSE);
g_free(time_str);
}
else
{
......
......@@ -45,7 +45,7 @@
enum {
/** The Application Programming Interface (API) version, incremented
* whenever any plugin data types are modified or appended to. */
GEANY_API_VERSION = 110,
GEANY_API_VERSION = 111,
/** The Application Binary Interface (ABI) version, incremented whenever
* existing fields in the plugin data types have to be changed or reordered. */
......@@ -330,7 +330,8 @@ typedef struct UtilsFuncs
gboolean (*spawn_async) (const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
GSpawnChildSetupFunc child_setup, gpointer user_data, GPid *child_pid,
GError **error);
gint (*utils_str_casecmp) (const gchar *s1, const gchar *s2);
gint (*str_casecmp) (const gchar *s1, const gchar *s2);
gchar* (*get_date_time) (const gchar *format, time_t *time_to_use);
}
UtilsFuncs;
......
......@@ -196,7 +196,8 @@ static UtilsFuncs utils_funcs = {
&utils_get_setting_string,
&utils_spawn_sync,
&utils_spawn_async,
&utils_str_casecmp
&utils_str_casecmp,
&utils_get_date_time
};
static UIUtilsFuncs uiutils_funcs = {
......
......@@ -554,24 +554,47 @@ gint utils_strpos(const gchar *haystack, const gchar *needle)
}
/**
* This is a convenience function to retrieve 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.
*
* @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.
**/
gchar *utils_get_date_time(const gchar *format, time_t *time_to_use)
{
time_t tp;
const struct tm *tm;
gchar *date;
static gchar date[1024];
gchar *locale_format;
gsize len;
g_return_val_if_fail(format != NULL, NULL);
if (format == NULL)
locale_format = g_locale_from_utf8(format, -1, NULL, NULL, NULL);
if (locale_format == NULL)
return NULL;
if (time_to_use != NULL)
tp = *time_to_use;
tm = localtime(time_to_use);
else
tp = time(NULL);
{
time_t tp = time(NULL);
tm = localtime(&tp);
}
tm = localtime(&tp);
date = g_malloc0(256);
strftime(date, 256, format, tm);
return date;
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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment