[gnome-color-manager: 1/3] trivial: move two string functions to the utils module



commit 46f9f6498ef3fe616d51ea6d3facba0a126d83c7
Author: Richard Hughes <richard hughsie com>
Date:   Tue Dec 22 22:08:12 2009 +0000

    trivial: move two string functions to the utils module

 src/gcm-utils.c |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcm-utils.h |    3 +
 2 files changed, 124 insertions(+), 0 deletions(-)
---
diff --git a/src/gcm-utils.c b/src/gcm-utils.c
index 8951ebc..de20a73 100644
--- a/src/gcm-utils.c
+++ b/src/gcm-utils.c
@@ -124,6 +124,66 @@ gcm_utils_output_is_lcd (const gchar *output_name)
 }
 
 /**
+ * gcm_utils_ensure_sane_length:
+ **/
+void
+gcm_utils_ensure_sane_length (gchar *text, guint max_length)
+{
+	guint i;
+	guint len;
+
+	/* get length */
+	len = strlen (text);
+
+	/* check we have room for ellipsis */
+	if (len <= max_length - 4)
+		return;
+
+	/* already correct len */
+	if (len == max_length)
+		return;
+
+	/* truncate, finding prior word break */
+	for (i=max_length-1; i>0; i--) {
+		if (text[i] == ' ')
+			break;
+	}
+
+	/* one long string with no spaces */
+	if (i == 0)
+		i = max_length - 3;
+
+	/* ellipsis */
+	text[i+0] = '.';
+	text[i+1] = '.';
+	text[i+2] = '.';
+	text[i+3] = '\0';
+}
+
+/**
+ * gcm_utils_ensure_printable:
+ **/
+void
+gcm_utils_ensure_printable (gchar *text)
+{
+	guint i;
+	guint idx = 0;
+
+	g_return_if_fail (text != NULL);
+
+	for (i=0; text[i] != '\0'; i++) {
+		if (g_ascii_isalnum (text[i]) ||
+		    g_ascii_ispunct (text[i]) ||
+		    text[i] == ' ')
+			text[idx++] = text[i];
+	}
+	text[idx] = '\0';
+
+	/* broken profiles have _ instead of spaces */
+	g_strdelimit (text, "_", ' ');
+}
+
+/**
  * gcm_utils_get_gamma_size_fallback:
  **/
 static guint
@@ -729,6 +789,7 @@ gcm_utils_test (EggTest *test)
 	gboolean ret;
 	GError *error = NULL;
 	GPtrArray *array;
+	gchar *text;
 	gchar *filename;
 	GcmProfileType profile_type;
 	GcmDeviceType device_type;
@@ -792,6 +853,66 @@ gcm_utils_test (EggTest *test)
 	g_free (filename);
 
 	/************************************************************/
+	egg_test_title (test, "check strip printable");
+	text = g_strdup ("1\r34 67_90");
+	gcm_utils_ensure_printable (text);
+	if (g_strcmp0 (text, "134 67 90") == 0)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "invalid value: %s", text);
+	g_free (text);
+
+	/************************************************************/
+	egg_test_title (test, "check sane length high");
+	text = g_strdup ("1234 67890");
+	gcm_utils_ensure_sane_length (text, 1024);
+	if (g_strcmp0 (text, "1234 67890") == 0)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "invalid value: %s", text);
+	g_free (text);
+
+	/************************************************************/
+	egg_test_title (test, "check sane length limit");
+	text = g_strdup ("1234 67890");
+	gcm_utils_ensure_sane_length (text, 10);
+	if (g_strcmp0 (text, "1234 67890") == 0)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "invalid value: %s", text);
+	g_free (text);
+
+	/************************************************************/
+	egg_test_title (test, "check sane length truncate");
+	text = g_strdup ("1234 67890");
+	gcm_utils_ensure_sane_length (text, 8);
+	if (g_strcmp0 (text, "1234...") == 0)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "invalid value: %s", text);
+	g_free (text);
+
+	/************************************************************/
+	egg_test_title (test, "check sane length no spaces");
+	text = g_strdup ("1234 67890");
+	gcm_utils_ensure_sane_length (text, 4);
+	if (g_strcmp0 (text, "1...") == 0)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "invalid value: %s", text);
+	g_free (text);
+
+	/************************************************************/
+	egg_test_title (test, "check sane length no data");
+	text = g_strdup ("");
+	gcm_utils_ensure_sane_length (text, 4);
+	if (g_strcmp0 (text, "") == 0)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "invalid value: %s", text);
+	g_free (text);
+
+	/************************************************************/
 	egg_test_title (test, "get default config location (when in make check)");
 	filename = gcm_utils_get_default_config_location ();
 	if (g_strcmp0 (filename, "/tmp/device-profiles.conf") == 0)
diff --git a/src/gcm-utils.h b/src/gcm-utils.h
index ccf0f90..2ebe594 100644
--- a/src/gcm-utils.h
+++ b/src/gcm-utils.h
@@ -55,6 +55,9 @@ GcmProfileType	 gcm_utils_device_type_to_profile_type	(GcmDeviceType		 type);
 gchar		*gcm_utils_format_date_time		(const struct tm	*created);
 gboolean	 gcm_utils_install_package		(const gchar		*package_name,
 							 GtkWindow		*window);
+void		 gcm_utils_ensure_sane_length		(gchar			*text,
+							 guint			 max_length);
+void		 gcm_utils_ensure_printable		(gchar			*text);
 
 #endif /* __GCM_UTILS_H */
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]