Enlightenment CVS committal Author : mekius Project : e17 Module : libs/efreet
Dir : e17/libs/efreet/src/lib Modified Files: Efreet_Mime.h efreet_mime.c Log Message: API update. Now you have more control over which methods are used to check for a type by using the efreet_mime_*_type_get functions. Also vastly increased the speed of the glob checking. =================================================================== RCS file: /cvs/e/e17/libs/efreet/src/lib/Efreet_Mime.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -3 -r1.3 -r1.4 --- Efreet_Mime.h 3 Jul 2007 04:47:29 -0000 1.3 +++ Efreet_Mime.h 6 Jul 2007 03:01:21 -0000 1.4 @@ -30,6 +30,10 @@ void efreet_mime_shutdown(void); const char *efreet_mime_type_get(const char *file); +const char *efreet_mime_magic_type_get(const char *file); +const char *efreet_mime_globs_type_get(const char *file); +const char *efreet_mime_special_type_get(const char *file); +const char *efreet_mime_fallback_type_get(const char *file); /** * @} =================================================================== RCS file: /cvs/e/e17/libs/efreet/src/lib/efreet_mime.c,v retrieving revision 1.18 retrieving revision 1.19 diff -u -3 -r1.18 -r1.19 --- efreet_mime.c 4 Jul 2007 15:46:19 -0000 1.18 +++ efreet_mime.c 6 Jul 2007 03:01:21 -0000 1.19 @@ -4,9 +4,12 @@ #include <Efreet_Mime.h> #include "efreet_private.h" #include <arpa/inet.h> +#include <sys/types.h> +#include <sys/time.h> static Ecore_List *globs = NULL; /* contains Efreet_Mime_Glob structs */ static Ecore_List *magics = NULL; /* contains Efreet_Mime_Magic structs */ +static Ecore_Hash *wild = NULL; /* contains *.ext and mime.types globs*/ static Ecore_Hash *monitors = NULL; /* contains file monitors */ /** @@ -21,7 +24,6 @@ EFREET_ENDIAN_LITTLE = 1 } efreet_mime_endianess = EFREET_ENDIAN_BIG; - /* * Buffer sized used for magic checks. The default is good enough for the * current set of magic rules. This setting is only here for the future. @@ -113,7 +115,7 @@ ecore_hash_set_free_key(monitors, ECORE_FREE_CB(free)); ecore_hash_set_free_value(monitors, ECORE_FREE_CB(ecore_file_monitor_del)); - + if (!efreet_mime_init_files()) return 0; @@ -138,7 +140,6 @@ /** * @param file: The file to check the mime type - * @param methods: The methods to use, see Efreet_Mime_Method structure * @return Returns mime type as a string * @brief Retreive the mime type of a file */ @@ -146,8 +147,6 @@ efreet_mime_type_get(const char *file) { const char *type = NULL; - Efreet_Mime_Glob *g; - char *s; if ((type = efreet_mime_special_check(file))) return type; @@ -160,6 +159,56 @@ /* * Check globs + */ + if ((type = efreet_mime_globs_type_get(file))) + return type; + + /* + * Check rest of magics + */ + if ((type = efreet_mime_magic_check_priority(file, 80, 0))) + return type; + + return efreet_mime_fallback_check(file); +} + +/** + * @param file: The file to check the mime type + * @return Returns mime type as a string + * @brief Retreive the mime type of a file using magic + */ +const char *efreet_mime_magic_type_get(const char *file) +{ + return efreet_mime_magic_check_priority(file,0,0); +} + +/** + * @param file: The file to check the mime type + * @return Returns mime type as a string + * @brief Retreive the mime type of a file using globs + */ +const char *efreet_mime_globs_type_get(const char *file) +{ + Efreet_Mime_Glob *g; + char *s; + const char *ext, *mime; + + /* + * Check in the extension hash for the type + */ + ext = strchr(file,'.'); + while(ext) + { + ++ext; + + if(ext && (mime = ecore_hash_get(wild, ext))) + return mime; + + ext = strchr(ext,'.'); + } + + /* + * Fallback to the other globs if not found */ ecore_list_goto_first(globs); while ((g = ecore_list_next(globs))) @@ -177,12 +226,28 @@ } FREE(s); - /* - * Check rest of magics - */ - if ((type = efreet_mime_magic_check_priority(file, 80, 0))) - return type; - + return NULL; +} + +/** + * @param file: The file to check the mime type + * @param methods: The methods to use, see Efreet_Mime_Method structure + * @return Returns mime type as a string + * @brief Retreive the special mime type of a file + */ +const char *efreet_mime_special_type_get(const char *file) +{ + return efreet_mime_special_check(file); +} + +/** + * @param file: The file to check the mime type + * @param methods: The methods to use, see Efreet_Mime_Method structure + * @return Returns mime type as a string + * @brief Retreive the fallback mime type of a file + */ +const char *efreet_mime_fallback_type_get(const char *file) +{ return efreet_mime_fallback_check(file); } @@ -233,10 +298,24 @@ char buf[4096]; const char *datadir = NULL; + IF_FREE_HASH(wild); + wild = ecore_hash_new(ecore_str_hash, ecore_str_compare); + ecore_hash_set_free_key(wild, ECORE_FREE_CB(ecore_string_release)); + ecore_hash_set_free_value(wild, + ECORE_FREE_CB(ecore_string_release)); IF_FREE_LIST(globs); globs = ecore_list_new(); ecore_list_set_free_cb(globs, efreet_mime_glob_free); + + /* + * This is here for legacy reasons. It is mentioned briefly + * in the spec and seems to still be quite valid. It is + * loaded first so the globs files will override anything + * in here. + */ + efreet_mime_mime_types_load("/etc/mime.types"); + datadir = datahome; ecore_list_goto_first(datadirs); while (datadir) @@ -246,12 +325,6 @@ datadir = ecore_list_next(datadirs); } - - /* - * This is here for legacy reasons. It is mentioned briefly - * in the spec and seems to still be quite valid. - */ - efreet_mime_mime_types_load("/etc/mime.types"); } /** @@ -514,9 +587,8 @@ efreet_mime_mime_types_load(const char *file) { FILE *f = NULL; - char buf[4096], buf2[4096], mimetype[4096]; + char buf[4096], mimetype[4096]; char ext[4096], *p = NULL, *pp = NULL; - Efreet_Mime_Glob *mime = NULL; f = fopen(file, "rb"); if (!f) return; @@ -546,25 +618,9 @@ strncpy(ext, pp, (p - pp)); ext[p - pp] = 0; - - mime = NEW(Efreet_Mime_Glob,1); - if (mime) - { - mime->mime = ecore_string_instance(mimetype); - snprintf(buf2, sizeof(buf2), "*.%s", ext); - mime->glob = ecore_string_instance(buf2); - if ((!mime->mime) || (!mime->glob)) - { - IF_RELEASE(mime->mime); - IF_RELEASE(mime->glob); - FREE(mime); - } - else - { - efreet_mime_glob_remove(buf2); - ecore_list_append(globs, mime); - } - } + + ecore_hash_set(wild, (void*)ecore_string_instance(ext), + (void*)ecore_string_instance(mimetype)); } while ((*p != '\n') && (*p != 0)); } @@ -619,21 +675,29 @@ *pp = 0; - mime = NEW(Efreet_Mime_Glob, 1); - if (mime) + if(ext[0] == '*' && ext[1] == '.') + { + ecore_hash_set(wild, (void*)ecore_string_instance(&(ext[2])), + (void*)ecore_string_instance(mimetype)); + } + else { - mime->mime = ecore_string_instance(mimetype); - mime->glob = ecore_string_instance(ext); - if ((!mime->mime) || (!mime->glob)) - { - IF_RELEASE(mime->mime); - IF_RELEASE(mime->glob); - FREE(mime); - } - else + mime = NEW(Efreet_Mime_Glob, 1); + if (mime) { - efreet_mime_glob_remove(ext); - ecore_list_append(globs, mime); + mime->mime = ecore_string_instance(mimetype); + mime->glob = ecore_string_instance(ext); + if ((!mime->mime) || (!mime->glob)) + { + IF_RELEASE(mime->mime); + IF_RELEASE(mime->glob); + FREE(mime); + } + else + { + efreet_mime_glob_remove(ext); + ecore_list_append(globs, mime); + } } } } ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ enlightenment-cvs mailing list enlightenment-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs