[geany/geany] 0ce5d5: build.c: Remove g_ptr_array_foreach with gpointer user_data & update HACKING (#2270)

Nick Treleaven git-noreply at xxxxx
Thu Aug 29 16:26:18 UTC 2019


Branch:      refs/heads/master
Author:      Nick Treleaven <n at trelsoft.com>
Committer:   GitHub <noreply at github.com>
Date:        Thu, 29 Aug 2019 16:26:18 UTC
Commit:      0ce5d5484e39edc25bc996cc0bf01f5bdc2b5221
             https://github.com/geany/geany/commit/0ce5d5484e39edc25bc996cc0bf01f5bdc2b5221

Log Message:
-----------
build.c: Remove g_ptr_array_foreach with gpointer user_data & update HACKING (#2270)

* build.c: Remove g_ptr_array_foreach with untyped user_data

Also avoids passing &ft_names to the function.

* HACKING: Avoid untyped pointers & *_foreach() with non-NULL user_data

* Avoid using foreach_ macro

* HACKING: merge 2 rules, simplify user_data clause


Modified Paths:
--------------
    HACKING
    src/build.c

Modified: HACKING
8 lines changed, 6 insertions(+), 2 deletions(-)
===================================================================
@@ -214,11 +214,15 @@ Coding
   to will not be mutated within the function.
 * Don't let variable names shadow outer variables - use gcc's -Wshadow
   option.
-* Use the strictest possible data type where practical. For example
-  for an enumeration, use the actual enum type rather than just a
+* Use the strictest possible data type where practical.
+  Avoid using untyped pointers (e.g. gpointer) where practical.
+  For an enumeration, use the actual enum type rather than just a
   ``gint``, use a ``gchar`` for individual (ASCII/UTF-8) string
   characters rather than ``gint``, and use a ``guint`` for integers
   which cannot be negative rather than ``gint``.
+* Prefer loops to calling ``some_type_foreach()`` with a ``user_data``
+  argument. (Note: Some containers don't support external iteration, 
+  e.g. for tree structures, so *_foreach is fine for those).
 * Do not use G_LIKELY or G_UNLIKELY (except in critical loops). These
   add noise to the code with little real benefit.
 


Modified: src/build.c
42 lines changed, 18 insertions(+), 24 deletions(-)
===================================================================
@@ -2559,41 +2559,29 @@ static guint build_save_menu_grp(GKeyFile *config, GeanyBuildCommand *src, gint
 }
 
 
-typedef struct ForEachData
+static gboolean save_project_filetype(GeanyFiletype *ft, GKeyFile *config)
 {
-	GKeyFile *config;
-	GPtrArray *ft_names;
-} ForEachData;
-
-
-static void foreach_project_filetype(gpointer data, gpointer user_data)
-{
-	GeanyFiletype *ft = data;
-	ForEachData *d = user_data;
 	guint i = 0;
 	gchar *regkey = g_strdup_printf("%serror_regex", ft->name);
 
-	i += build_save_menu_grp(d->config, ft->priv->projfilecmds, GEANY_GBG_FT, ft->name);
-	i += build_save_menu_grp(d->config, ft->priv->projexeccmds, GEANY_GBG_EXEC, ft->name);
+	i += build_save_menu_grp(config, ft->priv->projfilecmds, GEANY_GBG_FT, ft->name);
+	i += build_save_menu_grp(config, ft->priv->projexeccmds, GEANY_GBG_EXEC, ft->name);
 	if (!EMPTY(ft->priv->projerror_regex_string))
 	{
-		g_key_file_set_string(d->config, build_grp_name, regkey, ft->priv->projerror_regex_string);
+		g_key_file_set_string(config, build_grp_name, regkey, ft->priv->projerror_regex_string);
 		i++;
 	}
 	else
-		g_key_file_remove_key(d->config, build_grp_name, regkey, NULL);
+		g_key_file_remove_key(config, build_grp_name, regkey, NULL);
 	g_free(regkey);
-	if (i > 0)
-		g_ptr_array_add(d->ft_names, ft->name);
+	return (i > 0);
 }
 
-
 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
 void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
 {
 	GeanyFiletype *ft;
 	GeanyProject *pj;
-	ForEachData data;
 
 	switch (src)
 	{
@@ -2626,15 +2614,21 @@ void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
 				g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
 			if (pj->priv->build_filetypes_list != NULL)
 			{
-				data.config = config;
-				data.ft_names = g_ptr_array_new();
-				g_ptr_array_foreach(pj->priv->build_filetypes_list, foreach_project_filetype, (gpointer)(&data));
-				if (data.ft_names->pdata != NULL)
+				GPtrArray *ft_names = g_ptr_array_new();
+				const GPtrArray *build_fts = pj->priv->build_filetypes_list;
+				
+				for (guint i = 0; i < build_fts->len; i++)
+				{
+					ft = build_fts->pdata[i];
+					if (save_project_filetype(ft, config))
+						g_ptr_array_add(ft_names, ft->name);
+				}
+				if (ft_names->pdata != NULL)
 					g_key_file_set_string_list(config, build_grp_name, "filetypes",
-								(const gchar**)(data.ft_names->pdata), data.ft_names->len);
+						(const gchar**)ft_names->pdata, ft_names->len);
 				else
 					g_key_file_remove_key(config, build_grp_name, "filetypes", NULL);
-				g_ptr_array_free(data.ft_names, TRUE);
+				g_ptr_array_free(ft_names, TRUE);
 			}
 			break;
 		default: /* defaults and GEANY_BCS_FT can't save */



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list