SF.net SVN: geany: [2538] branches/custom-filetypes

ntrel at users.sourceforge.net ntrel at xxxxx
Mon Apr 28 14:41:11 UTC 2008


Revision: 2538
          http://geany.svn.sourceforge.net/geany/?rev=2538&view=rev
Author:   ntrel
Date:     2008-04-28 07:41:09 -0700 (Mon, 28 Apr 2008)

Log Message:
-----------
Remove filetypes array from API.
Replace filetypes_get_from_uid() with filetypes_lookup_by_name().
Use a hash table for filetypes, but keep the old filetypes array
available for now.

Modified Paths:
--------------
    branches/custom-filetypes/ChangeLog
    branches/custom-filetypes/plugins/vcdiff.c
    branches/custom-filetypes/src/filetypes.c
    branches/custom-filetypes/src/filetypes.h
    branches/custom-filetypes/src/plugindata.h
    branches/custom-filetypes/src/plugins.c

Modified: branches/custom-filetypes/ChangeLog
===================================================================
--- branches/custom-filetypes/ChangeLog	2008-04-28 14:23:19 UTC (rev 2537)
+++ branches/custom-filetypes/ChangeLog	2008-04-28 14:41:09 UTC (rev 2538)
@@ -1,3 +1,13 @@
+2008-04-28  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/plugindata.h, src/filetypes.c, src/filetypes.h, src/plugins.c,
+   plugins/vcdiff.c:
+   Remove filetypes array from API.
+   Replace filetypes_get_from_uid() with filetypes_lookup_by_name().
+   Use a hash table for filetypes, but keep the old filetypes array
+   available for now.
+
+
 2008-04-27  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
 
  * configure.in, tagmanager/lregex.c, tagmanager/include/regex.h,

Modified: branches/custom-filetypes/plugins/vcdiff.c
===================================================================
--- branches/custom-filetypes/plugins/vcdiff.c	2008-04-28 14:23:19 UTC (rev 2537)
+++ branches/custom-filetypes/plugins/vcdiff.c	2008-04-28 14:41:09 UTC (rev 2538)
@@ -286,8 +286,8 @@
 		idx = find_by_filename(filename);
 		if ( idx == -1)
 		{
-			idx = p_document->new_file(filename,
-				geany_data->filetypes[GEANY_FILETYPES_DIFF], text);
+			filetype *ft = p_filetypes->lookup_by_name("Diff");
+			idx = p_document->new_file(filename, ft, text);
 		}
 		else
 		{

Modified: branches/custom-filetypes/src/filetypes.c
===================================================================
--- branches/custom-filetypes/src/filetypes.c	2008-04-28 14:23:19 UTC (rev 2537)
+++ branches/custom-filetypes/src/filetypes.c	2008-04-28 14:41:09 UTC (rev 2538)
@@ -39,9 +39,12 @@
 #include "sciwrappers.h"
 
 
-filetype *filetypes[GEANY_MAX_FILE_TYPES];
+GHashTable *filetypes_hash = NULL;	/**< Array of filetype pointers */
 
+/* built-in filetypes only */
+filetype *builtin_filetypes[GEANY_MAX_FILE_TYPES] = {NULL};
 
+
 /* This is the order of unique ids used in the config file.
  * The order must not be changed but can be appended to. */
 enum
@@ -88,18 +91,8 @@
 static void filetypes_create_menu_item(GtkWidget *menu, const gchar *label, filetype *ftype);
 
 
-/* Create the filetype array and fill it with the known filetypes. */
-void filetypes_init_types()
+static void fill_filetypes(void)
 {
-	filetype_id ft_id;
-
-	for (ft_id = 0; ft_id < GEANY_MAX_FILE_TYPES; ft_id++)
-	{
-		filetypes[ft_id] = g_new0(filetype, 1);
-		filetypes[ft_id]->programs = g_new0(struct build_programs, 1);
-		filetypes[ft_id]->actions = g_new0(struct build_actions, 1);
-	}
-
 #define C	/* these macros are only to ease navigation */
 	filetypes[GEANY_FILETYPES_C]->id = GEANY_FILETYPES_C;
 	filetypes[GEANY_FILETYPES_C]->uid = FILETYPE_UID_C;
@@ -487,6 +480,38 @@
 }
 
 
+static void create_builtin_filetypes(void)
+{
+	filetype_id ft_id;
+
+	for (ft_id = 0; ft_id < GEANY_MAX_FILE_TYPES; ft_id++)
+	{
+		filetypes[ft_id] = g_new0(filetype, 1);
+		filetypes[ft_id]->programs = g_new0(struct build_programs, 1);
+		filetypes[ft_id]->actions = g_new0(struct build_actions, 1);
+	}
+	fill_filetypes();
+}
+
+
+/* Create the filetype array and fill it with the known filetypes. */
+void filetypes_init_types()
+{
+	filetype_id ft_id;
+
+	g_return_if_fail(filetypes_hash == NULL);
+
+	create_builtin_filetypes();
+
+	filetypes_hash = g_hash_table_new(g_str_hash, g_str_equal);
+
+	for (ft_id = 0; ft_id < GEANY_MAX_FILE_TYPES; ft_id++)
+	{
+		filetypes_add(filetypes[ft_id]);
+	}
+}
+
+
 #define create_sub_menu(menu, item, title) \
 	(menu) = gtk_menu_new(); \
 	(item) = gtk_menu_item_new_with_mnemonic((title)); \
@@ -765,32 +790,38 @@
 }
 
 
+static void free_filetype(G_GNUC_UNUSED gpointer key, gpointer value,
+		G_GNUC_UNUSED gpointer user_data)
+{
+	filetype *ft = value;
+
+	g_return_if_fail(ft != NULL);
+
+	g_free(ft->name);
+	g_free(ft->title);
+	g_free(ft->extension);
+	g_free(ft->comment_open);
+	g_free(ft->comment_close);
+	g_free(ft->context_action_cmd);
+	g_free(ft->programs->compiler);
+	g_free(ft->programs->linker);
+	g_free(ft->programs->run_cmd);
+	g_free(ft->programs->run_cmd2);
+	g_free(ft->programs);
+	g_free(ft->actions);
+
+	g_strfreev(ft->pattern);
+	g_free(ft);
+}
+
+
 /* frees the array and all related pointers */
 void filetypes_free_types()
 {
-	gint i;
+	g_return_if_fail(filetypes_hash != NULL);
 
-	for(i = 0; i < GEANY_MAX_FILE_TYPES; i++)
-	{
-		if (filetypes[i])
-		{
-			g_free(filetypes[i]->name);
-			g_free(filetypes[i]->title);
-			g_free(filetypes[i]->extension);
-			g_free(filetypes[i]->comment_open);
-			g_free(filetypes[i]->comment_close);
-			g_free(filetypes[i]->context_action_cmd);
-			g_free(filetypes[i]->programs->compiler);
-			g_free(filetypes[i]->programs->linker);
-			g_free(filetypes[i]->programs->run_cmd);
-			g_free(filetypes[i]->programs->run_cmd2);
-			g_free(filetypes[i]->programs);
-			g_free(filetypes[i]->actions);
-
-			g_strfreev(filetypes[i]->pattern);
-			g_free(filetypes[i]);
-		}
-	}
+	g_hash_table_foreach(filetypes_hash, free_filetype, NULL);
+	g_hash_table_destroy(filetypes_hash);
 }
 
 
@@ -1040,3 +1071,37 @@
 }
 
 
+/* Add a filetype pointer to the list of available filetypes. */
+void filetypes_add(filetype *ft)
+{
+	g_return_if_fail(ft);
+	g_return_if_fail(ft->name);
+
+	g_hash_table_insert(filetypes_hash, ft->name, ft);
+}
+
+
+/* Remove a filetype pointer from the list of available filetypes. */
+void filetypes_remove(filetype *ft)
+{
+	g_return_if_fail(ft);
+
+	if (!g_hash_table_remove(filetypes_hash, ft))
+		g_warning("Could not remove filetype %p!", ft);
+}
+
+
+/** Find a filetype pointer from its @c name field. */
+filetype *filetypes_lookup_by_name(const gchar *name)
+{
+	filetype *ft;
+
+	g_return_val_if_fail(NZV(name), NULL);
+
+	ft = g_hash_table_lookup(filetypes_hash, name);
+	if (ft == NULL)
+		g_warning("Could not find filetype '%s'!", name);
+	return ft;
+}
+
+

Modified: branches/custom-filetypes/src/filetypes.h
===================================================================
--- branches/custom-filetypes/src/filetypes.h	2008-04-28 14:23:19 UTC (rev 2537)
+++ branches/custom-filetypes/src/filetypes.h	2008-04-28 14:41:09 UTC (rev 2538)
@@ -121,9 +121,17 @@
 	struct build_actions	*actions;
 };
 
-extern filetype *filetypes[GEANY_MAX_FILE_TYPES];
+#define filetypes	builtin_filetypes
+extern filetype *builtin_filetypes[GEANY_MAX_FILE_TYPES];
 
 
+void filetypes_add(filetype *ft);
+
+void filetypes_remove(filetype *ft);
+
+filetype *filetypes_lookup_by_name(const gchar *name);
+
+
 /* If uid is valid, return corresponding filetype, otherwise NULL. */
 filetype *filetypes_get_from_uid(gint uid);
 

Modified: branches/custom-filetypes/src/plugindata.h
===================================================================
--- branches/custom-filetypes/src/plugindata.h	2008-04-28 14:23:19 UTC (rev 2537)
+++ branches/custom-filetypes/src/plugindata.h	2008-04-28 14:41:09 UTC (rev 2538)
@@ -35,12 +35,12 @@
 
 /* The API version should be incremented whenever any plugin data types below are
  * modified or appended to. */
-static const gint api_version = 53;
+static const gint api_version = 54;
 
 /* The ABI version should be incremented whenever existing fields in the plugin
  * data types below have to be changed or reordered. It should stay the same if fields
  * are only appended, as this doesn't affect existing fields. */
-static const gint abi_version = 24;
+static const gint abi_version = 25;
 
 /** Check the plugin can be loaded by Geany.
  * This performs runtime checks that try to ensure:
@@ -155,9 +155,8 @@
 {
 	GeanyApp	*app;					/**< Geany application data fields */
 	GtkWidget	*tools_menu;			/**< Most plugins should add menu items to the Tools menu only */
-	GArray		*doc_array;				/**< Dynamic array of document pointers */
-	struct filetype		**filetypes;
-	struct GeanyPrefs	*prefs;
+	GArray		*doc_array;				/**< Dynamic array of document structs */
+	struct GeanyPrefs	*prefs;			/* Note: this will be split up in future versions */
 	struct EditorPrefs	*editor_prefs;	/**< Editor settings */
 	struct BuildInfo	*build_info;	/**< Current build information */
 
@@ -367,7 +366,7 @@
 typedef struct FiletypeFuncs
 {
 	filetype*	(*detect_from_filename) (const gchar *utf8_filename);
-	filetype*	(*get_from_uid) (gint uid);
+	filetype*	(*lookup_by_name) (const gchar *name);
 }
 FiletypeFuncs;
 

Modified: branches/custom-filetypes/src/plugins.c
===================================================================
--- branches/custom-filetypes/src/plugins.c	2008-04-28 14:23:19 UTC (rev 2537)
+++ branches/custom-filetypes/src/plugins.c	2008-04-28 14:41:09 UTC (rev 2538)
@@ -227,7 +227,7 @@
 
 static FiletypeFuncs filetype_funcs = {
 	&filetypes_detect_from_filename,
-	&filetypes_get_from_uid
+	&filetypes_lookup_by_name
 };
 
 static NavQueueFuncs navqueue_funcs = {
@@ -241,7 +241,6 @@
 	NULL,
 	NULL,
 	NULL,
-	NULL,
 
 	&doc_funcs,
 	&sci_funcs,
@@ -267,7 +266,6 @@
 	geany_data.app = app;
 	geany_data.tools_menu = lookup_widget(app->window, "tools1_menu");
 	geany_data.doc_array = doc_array;
-	geany_data.filetypes = filetypes;
 	geany_data.prefs = &prefs;
 	geany_data.editor_prefs = &editor_prefs;
 	geany_data.build_info = &build_info;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list