[anjuta] class-gen: bgo #658262 - Extra separator between double upper case characters



commit 33a86e201a934a9b3e59ebae4a88601cf6577012
Author: Arnel A. Borja <kyoushuu yahoo com>
Date:   Wed Sep 7 00:01:38 2011 +0800

    class-gen: bgo #658262 - Extra separator between double upper case characters
    
    - Created function cg_transform_custom_c_type. Changed functions
      cg_transform_custom_c_type_to_g_type and cg_window_class_name_to_file_name
      to call this function.
    - Append only upper case, lower case or digit characters.
    - Don't add separator between double upper case characters, unless prefix is
      single character.
    - Add separator before last upper case character in three or more upper case
      characters.
    - Cut prefix in first occurrence of underscore.

 plugins/class-gen/transform.c |  145 ++++++++++++++++++++++++++---------------
 plugins/class-gen/transform.h |    5 ++
 plugins/class-gen/window.c    |   19 +-----
 3 files changed, 98 insertions(+), 71 deletions(-)
---
diff --git a/plugins/class-gen/transform.c b/plugins/class-gen/transform.c
index 01d3fc4..c7e0cb4 100644
--- a/plugins/class-gen/transform.c
+++ b/plugins/class-gen/transform.c
@@ -120,6 +120,67 @@ cg_transform_default_c_type_to_g_type (const gchar *c_type,
 	return FALSE;
 }
 
+/* This function tries to convert a c_type like GtkTreeIter to add a separator 
+ * between each word, then convert to upper case or lower case. */
+gchar *
+cg_transform_custom_c_type (const gchar *c_type,
+                            gboolean upper_case,
+                            gchar separator)
+{
+	GString *str;
+	const gchar *pos;
+	gchar (*tocase_func) (gchar);
+
+	if (upper_case)
+		tocase_func = g_ascii_toupper;
+	else
+		tocase_func = g_ascii_tolower;
+
+	str = g_string_sized_new (128);
+	for (pos = c_type; *pos != '\0'; ++ pos)
+	{
+		if (!g_ascii_isalnum (*pos))
+			continue;
+
+		if (isupper (*pos) &&				/* Upper case only */
+		    pos > c_type &&					/* Can't be first character, to check the previous character */
+		    !isupper (*(pos-1)))			/* Previous character is not upper case */
+		{
+			/* This will add the separator if the previous character is
+			 * not upper case and the current character is upper case */
+			g_string_append_c (str, separator);
+		}
+		else if (isupper (*pos) &&			/* Upper case only */
+		         pos-1 == c_type &&			/* Second character */
+		         *(pos+1) != '\0' &&		/* Next character shouldn't be the last */
+		         !isupper (*(pos+1)))		/* Next of next character is not upper case */
+		{
+			/* This will add the separator if the prefix is single character,
+			 * the current character is upper case and the next character is not
+			 * upper case
+			 * In summary, this will catch single character prefixes
+			 * Example: GObject -> G_OBJECT where O is the current character */
+			g_string_append_c (str, separator);
+		}
+		else if (isupper (*pos) &&			/* Upper case only */
+		         pos-1 > c_type &&			/* Can't be first two characters, to check the two previous characters */
+		         isupper (*(pos-1)) &&		/* Previous character is upper case */
+		         isupper (*(pos-2)) &&		/* Previous of previous character is upper case */
+		         *(pos+1) != '\0' &&		/* Next character shouldn't be the last */
+		         !isupper (*(pos+1)))		/* Next of next character is not upper case */
+		{
+			/* This will add the separator if there are three or more upper case
+			 * (the last one is this character) and the next one is not upper case */
+			/* Example: GtkUIManager -> GTK_UI_MANAGER where M is the current character */
+			g_string_append_c (str, separator);
+		}
+
+		g_string_append_c (str, tocase_func (*pos));
+	}
+
+	return g_string_free (str, FALSE);
+}
+
 /* This function tries to convert a custom c_type like GtkTreeIter to
  * a gobject type like GTK_TYPE_TREE_ITER. It does this by parsing the C type.
  * The code is mostly borrowed from old action-callbacks.c by Dave
@@ -130,72 +191,50 @@ cg_transform_custom_c_type_to_g_type (const gchar *c_type,
                                       gchar **g_type_name,
                                       gchar **g_func_prefix)
 {
-	size_t name_len;
-	gboolean first;
-	gboolean prefix;
-	GString *str_type_prefix = NULL;
-	GString *str_type_name = NULL;
-	GString *str_func_prefix = NULL;
-	
-	name_len = strlen (c_type);
+	gchar *c_type_transformed, **c_type_split;
 
-	if (g_type_prefix != NULL) str_type_prefix = g_string_sized_new (name_len);
-	if (g_type_name != NULL) str_type_name = g_string_sized_new (name_len);
-	if (g_type_prefix != NULL) str_func_prefix = g_string_sized_new (name_len);
-	
-	first = TRUE;
-	prefix = TRUE;
-	
-	for (; *c_type != '\0'; ++ c_type)
+	c_type_transformed = cg_transform_custom_c_type (c_type, TRUE, '_');
+
+	if (g_type_prefix != NULL || g_type_name != NULL)
 	{
-		if (first == TRUE)
+		c_type_split = g_strsplit (c_type_transformed, "_", 2);
+
+		/* If c_type is empty string, first string is NULL */
+		if (c_type_split[0])
 		{
-			if (g_func_prefix != NULL)
-				g_string_append_c (str_func_prefix, tolower (*c_type));
 			if (g_type_prefix != NULL)
-				g_string_append_c (str_type_prefix, toupper (*c_type));
+				*g_type_prefix = c_type_split[0];
+			else
+				g_free (c_type_split[0]);
 
-			first = FALSE;
+			/* If prefix only, second string is NULL */
+			if (c_type_split[1])
+			{
+				if (g_type_name != NULL)
+					*g_type_name = c_type_split[1];
+				else
+					g_free (c_type_split[1]);
+			}
+			else if (g_type_name != NULL)
+				*g_type_name = g_strdup ("");
 		}
 		else
 		{
-			if (isupper (*c_type))
-			{
-				if (g_func_prefix != NULL)
-					g_string_append_c (str_func_prefix, '_');
-
-				prefix = FALSE;
-			}
-
-			if (g_func_prefix != NULL)
-				g_string_append_c (str_func_prefix, tolower (*c_type));
-
-			if (prefix == TRUE)
-			{
-				if (g_type_prefix != NULL)
-					g_string_append_c (str_type_prefix, toupper (*c_type));
-			}
-			else
-			{
-				if (g_type_name != NULL)
-				{
-					if (isupper (*c_type) && str_type_name->len > 0)
-						g_string_append_c (str_type_name, '_');
+			if (g_type_prefix != NULL)
+				*g_type_prefix = g_strdup ("");
 
-					g_string_append_c (str_type_name, toupper (*c_type));
-				}
-			}
+			if (g_type_name != NULL)
+				*g_type_name = g_strdup ("");
 		}
-	}
-	
-	if (g_type_prefix != NULL)
-		*g_type_prefix = g_string_free (str_type_prefix, FALSE);
 
-	if (g_type_name != NULL)
-		*g_type_name = g_string_free (str_type_name, FALSE);
+		/* Free only the array */
+		g_free (c_type_split);
+	}
 
 	if (g_func_prefix != NULL)
-		*g_func_prefix = g_string_free (str_func_prefix, FALSE);
+		*g_func_prefix = g_ascii_strdown (c_type_transformed, -1);
+
+	g_free (c_type_transformed);
 }
 
 /* This function tries to convert any possible C type to its corresponding
diff --git a/plugins/class-gen/transform.h b/plugins/class-gen/transform.h
index 33a0caa..1316419 100644
--- a/plugins/class-gen/transform.h
+++ b/plugins/class-gen/transform.h
@@ -38,6 +38,11 @@ cg_transform_default_c_type_to_g_type (const gchar *c_type,
                                        const gchar **g_type_prefix,
                                        const gchar **g_type_name);
 
+gchar *
+cg_transform_custom_c_type (const gchar *c_type,
+                            gboolean upper_case,
+                            gchar separator);
+
 void
 cg_transform_custom_c_type_to_g_type (const gchar *c_type,
                                       gchar **g_type_prefix,
diff --git a/plugins/class-gen/window.c b/plugins/class-gen/window.c
index 83316a8..d062d8c 100644
--- a/plugins/class-gen/window.c
+++ b/plugins/class-gen/window.c
@@ -451,24 +451,7 @@ cg_window_top_notebook_switch_page_cb (G_GNUC_UNUSED GtkNotebook *notebook,
 static gchar *
 cg_window_class_name_to_file_name (const gchar *class_name)
 {
-	GString *str;
-	const gchar *pos;
-
-	str = g_string_sized_new (128);
-	for (pos = class_name; *pos != '\0'; ++ pos)
-	{
-		if (isupper(*pos))
-		{
-			if (str->len > 0) g_string_append_c (str, '-');
-			g_string_append_c (str, tolower (*pos));
-		}
-		else if (islower (*pos) || isdigit (*pos))
-		{
-			g_string_append_c (str, *pos);
-		}
-	}
-	
-	return g_string_free (str, FALSE);
+	return cg_transform_custom_c_type (class_name, FALSE, '-');
 }
 
 static void



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