Revision: 2544 http://geany.svn.sourceforge.net/geany/?rev=2544&view=rev Author: ntrel Date: 2008-05-01 07:30:20 -0700 (Thu, 01 May 2008)
Log Message: ----------- Add filetypes_find() that calls a predicate function for each filetype in filetypes_hash until TRUE is returned. Make filetypes_detect_from_extension() work for all filetypes in filetypes_hash.
Modified Paths: -------------- branches/custom-filetypes/ChangeLog branches/custom-filetypes/src/filetypes.c branches/custom-filetypes/src/filetypes.h
Modified: branches/custom-filetypes/ChangeLog =================================================================== --- branches/custom-filetypes/ChangeLog 2008-05-01 12:10:46 UTC (rev 2543) +++ branches/custom-filetypes/ChangeLog 2008-05-01 14:30:20 UTC (rev 2544) @@ -3,6 +3,11 @@ * src/plugindata.h, src/keyfile.c, src/filetypes.c, src/filetypes.h: Store filetype::name instead of unique ID when saving sessions. Remove all filetype UID code. + * src/filetypes.c, src/filetypes.h: + Add filetypes_find() that calls a predicate function for each + filetype in filetypes_hash until TRUE is returned. + Make filetypes_detect_from_extension() work for all filetypes + in filetypes_hash.
2008-04-29 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
Modified: branches/custom-filetypes/src/filetypes.c =================================================================== --- branches/custom-filetypes/src/filetypes.c 2008-05-01 12:10:46 UTC (rev 2543) +++ branches/custom-filetypes/src/filetypes.c 2008-05-01 14:30:20 UTC (rev 2544) @@ -508,38 +508,53 @@ }
+static gboolean match_basename(G_GNUC_UNUSED const gchar *ft_name, filetype *ft, + gpointer user_data) +{ + gboolean ret = FALSE; + const gchar *base_filename = user_data; + gint j; + + /* Don't match '*' because it comes before any custom filetypes */ + if (ft->id == GEANY_FILETYPES_ALL) + return FALSE; + + for (j = 0; ft->pattern[j] != NULL; j++) + { + GPatternSpec *pattern = g_pattern_spec_new(ft->pattern[j]); + + if (g_pattern_match_string(pattern, base_filename)) + { + ret = TRUE; + g_pattern_spec_free(pattern); + break; + } + g_pattern_spec_free(pattern); + } + return ret; +} + + /* Detect filetype only based on the filename extension. * utf8_filename can include the full path. */ filetype *filetypes_detect_from_extension(const gchar *utf8_filename) { - GPatternSpec *pattern; gchar *base_filename; - gint i, j; + filetype *ft;
- /* to match against the basename of the file(because of Makefile*) */ + /* to match against the basename of the file (because of Makefile*) */ base_filename = g_path_get_basename(utf8_filename); #ifdef G_OS_WIN32 /* use lower case basename */ setptr(base_filename, g_utf8_strdown(base_filename, -1)); #endif
- for(i = 0; i < GEANY_MAX_FILE_TYPES; i++) - { - for (j = 0; filetypes[i]->pattern[j] != NULL; j++) - { - pattern = g_pattern_spec_new(filetypes[i]->pattern[j]); - if (g_pattern_match_string(pattern, base_filename)) - { - g_free(base_filename); - g_pattern_spec_free(pattern); - return filetypes[i]; - } - g_pattern_spec_free(pattern); - } - } + ft = filetypes_find(match_basename, base_filename); + if (ft == NULL) + ft = filetypes[GEANY_FILETYPES_ALL];
g_free(base_filename); - return filetypes[GEANY_FILETYPES_ALL]; + return ft; }
@@ -1018,3 +1033,10 @@ }
+/* Call a predicate function for each filetype until it returns TRUE. */ +filetype *filetypes_find(FileTypePredicate predicate, gpointer user_data) +{ + return g_hash_table_find(filetypes_hash, (GHRFunc) predicate, user_data); +} + +
Modified: branches/custom-filetypes/src/filetypes.h =================================================================== --- branches/custom-filetypes/src/filetypes.h 2008-05-01 12:10:46 UTC (rev 2543) +++ branches/custom-filetypes/src/filetypes.h 2008-05-01 14:30:20 UTC (rev 2544) @@ -131,7 +131,13 @@
filetype *filetypes_lookup_by_name(const gchar *name);
+/* This can be cast to GHRFunc */ +typedef gboolean (*FileTypePredicate) (const gchar *ft_name, filetype *ft, + gpointer user_data);
+filetype *filetypes_find(FileTypePredicate predicate, gpointer user_data); + + /* Calls filetypes_init_types() and creates the filetype menu. */ void filetypes_init(void);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.