Bug 760711 - Non-ASCII characters not shown in Report Name when a report is created with 2.6.11

Due to fixing bug 727130 with g_strexcape, which escapes all non-ascii
characters.
This commit is contained in:
John Ralls 2016-01-17 18:45:33 -08:00
parent cf26b5c86b
commit d8aea0f40c
5 changed files with 115 additions and 9 deletions

View File

@ -208,6 +208,21 @@ gnc_utf8_strip_invalid_strdup(const gchar* str)
return result;
}
void
gnc_utf8_strip_invalid_and_controls (gchar *str)
{
gchar *c = NULL;
const gchar *controls = "\b\f\n\r\t\v";
g_return_if_fail (str != NULL && strlen (str) > 0);
gnc_utf8_strip_invalid (str); /* First fix the UTF-8 */
for(c = str + strlen (str) - 1; c != str; --c)
{
gboolean line_control = ((unsigned char)(*c) < 0x20);
if (line_control || strchr(controls, *c) != NULL)
*c = ' '; /*replace controls with a single space. */
}
}
gchar *
gnc_locale_from_utf8(const gchar* str)
{

View File

@ -108,6 +108,14 @@ void gnc_utf8_strip_invalid (gchar *str);
* caller. */
gchar *gnc_utf8_strip_invalid_strdup (const gchar* str);
/** Strip any non-utf8 characters and any control characters (everything < 0x20,
* \b, \f, \n, \r, \t, and \v) from a string. Rewrites the string in-place.
*
* @param str Pointer to the string to clean up.
*/
void gnc_utf8_strip_invalid_and_controls (gchar* str);
/**
* @brief Converts a string from UTF-8 to the encoding used for
* strings in the current locale.

View File

@ -1,3 +1,5 @@
include $(top_srcdir)/test-templates/Makefile.decl
MODULEPATH = src/core-utils
AM_CPPFLAGS = \
-I${top_srcdir} \
@ -17,9 +19,11 @@ LDADD = \
# these tests are ordered kind more or less in the order
# that they should be executed, with more basic tests coming first.
#
TESTS = \
test-gnc-uri-utils \
test-resolve-file-path
test-resolve-file-path \
test-gnc-glib-utils
GNC_TEST_DEPS = \
--library-dir ${top_builddir}/src/libqof/qof \
@ -29,10 +33,21 @@ TESTS_ENVIRONMENT = \
SRCDIR=${srcdir} \
$(shell ${abs_top_srcdir}/src/gnc-test-env.pl --noexports ${GNC_TEST_DEPS})
check_PROGRAMS = \
test-gnc-uri-utils \
test-resolve-file-path
check_PROGRAMS = ${TESTS}
test_gnc_glib_utils_SOURCES = \
$(top_srcdir)/$(MODULEPATH)/gnc-glib-utils.c \
test-gnc-glib-utils.c
test_gnc_glib_utils_LDADD = \
${top_builddir}/src/libqof/qof/libgnc-qof.la \
${top_builddir}/src/test-core/libtest-core.la \
$(GLIB_LIBS)
test_gnc_glib_utils_CFLAGS = \
${DEFAULT_INCLUDES} \
-I${top_srcdir}/${MODULEPATH} \
${GLIB_CFLAGS}
EXTRA_DIST =

View File

@ -0,0 +1,67 @@
/********************************************************************
* testmain.c: GLib g_test test execution file. *
* Copyright 2011 John Ralls <jralls@ceridwen.us> *
* *
* 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. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/
#include <config.h>
#include <string.h>
#include <glib.h>
#include <gnc-glib-utils.h>
#include <unittest-support.h>
static void
test_gnc_utf8_strip_invalid_and_controls (gconstpointer data)
{
gchar *str = g_strdup (data);
const gchar *controls = "\b\f\n\r\t\v\x01\x02\x03\x04\x05\x06\x07"
"\x08\x09\xa\xb\xc\xd\xe\xf\x10\x11\x12\x13\x14\x15\x16"
"\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
char *msg1 = g_strdup_printf ("Invalid utf8 string: %s",
(const gchar*)data);
const GLogLevelFlags level = G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL;
TestErrorStruct check = {level, NULL, msg1, 0};
guint handler = g_log_set_handler (NULL, level,
(GLogFunc)test_null_handler, &check);
g_test_log_set_fatal_handler((GTestLogFatalFunc)test_checked_handler,
&check);
gnc_utf8_strip_invalid_and_controls (str);
g_assert (g_utf8_validate(str, -1, NULL) == TRUE);
g_assert (strpbrk(str, controls) == NULL);
g_assert (g_utf8_strlen(str, -1) > 0);
g_log_remove_handler (NULL, handler);
g_free (str);
g_free (msg1);
}
int
main (int argc, char *argv[])
{
const gchar *invalid_utf8 = "Η γρήγορη καφέ αλεπού πήδηξε πάνω από την \xb2\xf3ργή σκύλο.";
const gchar *controls = "Η γρήγορη καφέ αλεπού\bπήδηξε\nπάνω από\tτην αργή σκύλο.";
g_test_init (&argc, &argv, NULL); // initialize test program
g_test_add_data_func ("/core-utils/gnc_utf8_strip_invalid_and_controls invalid utf8", (gconstpointer)invalid_utf8, test_gnc_utf8_strip_invalid_and_controls);
g_test_add_data_func ("/core-utils/gnc_utf8_strip_invalid_and_controls control chars", (gconstpointer)controls, test_gnc_utf8_strip_invalid_and_controls);
return g_test_run();
}

View File

@ -46,6 +46,7 @@
#include <sys/stat.h>
#include <errno.h>
#include <gnc-glib-utils.h>
#include "gfec.h"
#include "dialog-custom-report.h"
#include "gnc-component-manager.h"
@ -604,11 +605,11 @@ gnc_plugin_page_report_option_change_cb(gpointer data)
"Report name", NULL);
if (strcmp(old_name, new_name) != 0)
{
/* Bug 727130 - escape the non-printable characters from the name */
new_name_escaped = g_strescape(new_name,NULL);
ENTER("Escaped new report name: %s", new_name_escaped);
main_window_update_page_name(GNC_PLUGIN_PAGE(report), new_name_escaped);
g_free(new_name_escaped);
/* Bug 727130, 760711 - remove only the non-printable
* characters from the new name */
gnc_utf8_strip_invalid_and_controls(new_name);
ENTER("Cleaned-up new report name: %s", new_name);
main_window_update_page_name(GNC_PLUGIN_PAGE(report), new_name);
}
g_free(new_name);