about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTodd T. Fries <todd@fries.net>2012-07-26 14:49:58 -0500
committerTodd T. Fries <todd@fries.net>2012-08-08 10:28:02 -0500
commit78b60cb8de47dfa6ed3297dccf2b9268099bc922 (patch)
tree296f0987b29b0e46830cb67c8f42e42e83548bcf
parentd397399c349d36f611a5aac6fa53528d2fe2eaea (diff)
downloadxombrero-78b60cb8de47dfa6ed3297dccf2b9268099bc922.tar.gz
This diff munges and massages the show_g_object_settings function
o move from
   'var = g_value_get_foo(&value); valstr = g_strdup_printf("%XX", foo);'
  to
   'valstr = g_strdup_printf("%XX", g_value_get_foo(&value));'
  .. shrinks code, no change in behavior
o add G_TYPE_{{U,}CHAR,LONG}
o introduce xt_append_settings() to simplify allthethings() and about_webkit()
o rework output to look much more c like
o spend way too much effort aligning the /* number flags=0x.... */ comments
o compact output and be as c like as possible
-rw-r--r--about.c155
1 files changed, 101 insertions, 54 deletions
diff --git a/about.c b/about.c
index db76510..97ab7a4 100644
--- a/about.c
+++ b/about.c
@@ -85,6 +85,8 @@ void			update_cookie_tabs(struct tab *apart_from);
 int			about_webkit(struct tab *, struct karg *);
 int			allthethings(struct tab *, struct karg *);
 
+extern SoupSession	*session;
+
 struct about_type about_list[] = {
 	{ XT_URI_ABOUT_ABOUT,		xtp_page_ab },
 	{ XT_URI_ABOUT_ALLTHETHINGS,	allthethings },
@@ -1976,24 +1978,16 @@ startpage_add(const char *fmt, ...)
 gchar *
 show_g_object_settings(GObject *o, char *str, int recurse)
 {
-	char		*b, *body, *valstr;
+	char		*b, *p, *body, *valstr, *tmpstr;
 	guint		n_props = 0;
 	int		i;
 	GParamSpec	*pspec;
 	const gchar	*tname;
 	GValue		value;
-	int		typeno;
-	const gchar	*string;
-	gboolean	boolean;
-	gfloat		fp;
-	gdouble		fpd;
-	gint		number;
-	guint		unumber;
-	int64_t		number64;
-	uint64_t	unumber64;
+	int		typeno = 0;
 	GObject		*object;
 	GParamSpec	**proplist;
-	char		*tmpstr, *tmpsettings;
+	char		*tmpsettings;
 
 	if (!G_IS_OBJECT(o)) {
 		fprintf(stderr, "%s is not a g_object\n", str);
@@ -2001,7 +1995,12 @@ show_g_object_settings(GObject *o, char *str, int recurse)
 	}
 	proplist = g_object_class_list_properties(
 	    G_OBJECT_GET_CLASS(o), &n_props);
-	body = g_strdup_printf("%s: %3d settings\n", str, n_props);
+	if (n_props == 0) {
+		body = g_strdup("settings[0] = { }");
+		goto end_show_g_objects;
+	}
+
+	body = g_strdup_printf("settings[%d] = {\n", n_props);
 	for (i=0; i < n_props; i++) {
 		pspec = proplist[i];
 		tname = G_OBJECT_TYPE_NAME(pspec);
@@ -2021,48 +2020,64 @@ show_g_object_settings(GObject *o, char *str, int recurse)
 			typeno = G_TYPE_FUNDAMENTAL( G_VALUE_TYPE(&value) );
 			switch ( typeno ) {
 			case G_TYPE_ENUM:
-				number = g_value_get_enum(&value);
-				valstr = g_strdup_printf("%d", number);
+				valstr = g_strdup_printf("%d",
+				    g_value_get_enum(&value));
+				break;
+			case G_TYPE_CHAR:
+				valstr = g_strdup_printf("%c",
+				    g_value_get_schar(&value));
+				break;
+			case G_TYPE_UCHAR:
+				valstr = g_strdup_printf("%c",
+				    g_value_get_uchar(&value));
+				break;
+			case G_TYPE_LONG:
+				valstr = g_strdup_printf("%ld",
+				    g_value_get_long(&value));
+				break;
+			case G_TYPE_ULONG:
+				valstr = g_strdup_printf("%ld",
+				    g_value_get_ulong(&value));
 				break;
 			case G_TYPE_INT:
-				number = g_value_get_int(&value);
-				valstr = g_strdup_printf("%d", number);
+				valstr = g_strdup_printf("%d",
+				    g_value_get_int(&value));
 				break;
 			case G_TYPE_INT64:
-				number64 = (int64_t)g_value_get_int64(&value);
-				valstr = g_strdup_printf("%" PRIo64, number64);
+				valstr = g_strdup_printf("%" PRIo64,
+				    /* (int64_t) */ g_value_get_int64(&value));
 				break;
 			case G_TYPE_UINT:
-				unumber = g_value_get_uint(&value);
-				valstr = g_strdup_printf("%d", unumber);
+				valstr = g_strdup_printf("%d",
+				    g_value_get_uint(&value));
 				break;
 			case G_TYPE_UINT64:
-				unumber64 =
-				    (uint64_t)g_value_get_uint64(&value);
-				valstr =
-				    g_strdup_printf("%" PRIu64, unumber64);
+				valstr = g_strdup_printf("%" PRIu64,
+				    /* (uint64_t) */ g_value_get_uint64(&value));
 				break;
 			case G_TYPE_FLAGS:
-				unumber = g_value_get_flags(&value);
-				valstr = g_strdup_printf("0x%x", unumber);
+				valstr = g_strdup_printf("0x%x",
+				    g_value_get_flags(&value));
 				break;
 			case G_TYPE_BOOLEAN:
-				boolean = g_value_get_boolean(&value);
 				valstr = g_strdup_printf("%s",
-				    boolean ? "TRUE" : "FALSE");
+				    g_value_get_boolean(&value) ? "TRUE" : "FALSE");
 				break;
 			case G_TYPE_FLOAT:
-				fp = g_value_get_float(&value);
-				valstr = g_strdup_printf("%f", fp);
+				valstr = g_strdup_printf("%f",
+				    g_value_get_float(&value));
 				break;
 			case G_TYPE_DOUBLE:
-				fpd = g_value_get_double(&value);
-				valstr = g_strdup_printf("%f", fpd);
+				valstr = g_strdup_printf("%f",
+				    g_value_get_double(&value));
 				break;
 			case G_TYPE_STRING:
-				string = g_value_get_string(&value);
 				valstr = g_strdup_printf("\"%s\"",
-				    string);
+				    g_value_get_string(&value));
+				break;
+			case G_TYPE_POINTER:
+				valstr = g_strdup_printf("%p",
+				    g_value_get_pointer(&value));
 				break;
 			case G_TYPE_OBJECT:
 				object = g_value_get_object(&value);
@@ -2072,16 +2087,20 @@ show_g_object_settings(GObject *o, char *str, int recurse)
 					    str);
 					tmpsettings = show_g_object_settings(
 					    object, tmpstr, recurse);
+					g_free(tmpstr);
+					if (strrchr(tmpsettings, '\n') != NULL) {
 					valstr = g_strdup_printf(
-					    "{\n%s%s }\n",
+					    "%s%s      }",
 					    tmpsettings, str);
-					g_free(tmpstr);
 					g_free(tmpsettings);
 					} else {
+					valstr = tmpsettings;
+					}
+					} else {
 					valstr = g_strdup_printf("<...>");
 					}
 				} else {
-					valstr = g_strdup_printf("NULL");
+					valstr = g_strdup_printf("settings[] = NULL");
 				}
 				break;
 			default:
@@ -2091,24 +2110,57 @@ show_g_object_settings(GObject *o, char *str, int recurse)
 			}
 		}
 
+		tmpstr = g_strdup_printf("%-13s %s%s%s,", tname, pspec->name,
+			(typeno == G_TYPE_OBJECT) ? "." : " = ", valstr);
 		b = body;
-		body = g_strdup_printf(
-		    "%s%s: %3d: flags=0x%08x, %-13s %s = %s\n",
-		    body, str, i, pspec->flags, tname, pspec->name,
-		    valstr);
+
+#define XT_G_OBJECT_SPACING		50
+		p = strrchr(tmpstr, '\n');
+		if (p == NULL && strlen(tmpstr) > XT_G_OBJECT_SPACING) {
+			body = g_strdup_printf(
+			    "%s%s    %-50s\n%s    %50s /* %3d flags=0x%08x */\n",
+			    body, str, tmpstr, str, "", i, pspec->flags);
+		} else {
+			char *fmt;
+			int strspaces;
+			if (p == NULL)
+				strspaces = XT_G_OBJECT_SPACING;
+			else
+				strspaces = strlen(tmpstr) - (strlen(p) - strlen(str)) + XT_G_OBJECT_SPACING + 5;
+			fmt = g_strdup_printf("%%s%%s    %%-%ds /* %%3d flags=0x%%08x */\n", strspaces);
+			body = g_strdup_printf(fmt, body, str, tmpstr, i, pspec->flags);
+			g_free(fmt);
+		}
+		g_free(tmpstr);
 		g_free(b);
 		g_free(valstr);
 	}
+end_show_g_objects:
 	g_free(proplist);
 	return (body);
 }
 
+char *
+xt_append_settings(char *str, GObject *object, char *name, int recurse)
+{
+	char *newstr, *settings;
+
+	settings = show_g_object_settings(object, name, recurse);
+	if (str == NULL) {
+		str = g_strdup("");
+	}
+	newstr = g_strdup_printf("%s%s %s%s };\n", str, name, settings, name);
+	g_free(str);
+
+	return newstr;
+}
+
 int
 about_webkit(struct tab *t, struct karg *arg)
 {
 	char			*page, *body, *settingstr;
 
-	settingstr = show_g_object_settings(G_OBJECT(t->settings),
+	settingstr = xt_append_settings(NULL, G_OBJECT(t->settings),
 	    "t->settings", 0);
 	body = g_strdup_printf("<pre>%s</pre>\n", settingstr);
 	g_free(settingstr);
@@ -2125,22 +2177,17 @@ about_webkit(struct tab *t, struct karg *arg)
 int
 allthethings(struct tab *t, struct karg *arg)
 {
-	char			*page, *body, *b, *settingstr;
+	char			*page, *body, *b;
 	extern GtkWidget	*main_window;
 
-	body = show_g_object_settings(G_OBJECT(t->wv), "t->wv", 1);
-	b = body;
-	settingstr = show_g_object_settings(G_OBJECT(t->inspector),
+	body = xt_append_settings(NULL, G_OBJECT(t->wv), "t->wv", 1);
+	body = xt_append_settings(body, G_OBJECT(t->inspector),
 	    "t->inspector", 1);
-	body = g_strdup_printf("%s%s", body, settingstr);
-	g_free(b);
-	g_free(settingstr);
-	b = body;
-	settingstr = show_g_object_settings(G_OBJECT(main_window),
+	body = xt_append_settings(body, G_OBJECT(session),
+	    "session", 1);
+	body = xt_append_settings(body, G_OBJECT(main_window),
 	    "main_window", 1);
-	body = g_strdup_printf("%s%s", body, settingstr);
-	g_free(b);
-	g_free(settingstr);
+
 	b = body;
 	body = g_strdup_printf("<pre>%scan paste clipboard = %d\n</pre>", body,
 	    webkit_web_view_can_paste_clipboard(t->wv));