Enlightenment CVS committal Author : mekius Project : e17 Module : libs/efreet
Dir : e17/libs/efreet/src/lib Modified Files: efreet_mime.c Log Message: Added fallback mime types according to the spec. Should only get a NULL return when the file doesn't exist. =================================================================== RCS file: /cvs/e/e17/libs/efreet/src/lib/efreet_mime.c,v retrieving revision 1.13 retrieving revision 1.14 diff -u -3 -r1.13 -r1.14 --- efreet_mime.c 3 Jul 2007 16:22:17 -0000 1.13 +++ efreet_mime.c 3 Jul 2007 17:55:20 -0000 1.14 @@ -77,6 +77,7 @@ unsigned int end); static int efreet_mime_init_files(void); static const char * efreet_mime_special_check(const char *file); +static const char * efreet_mime_fallback_check(const char *file); static void efreet_mime_glob_free(void *data); static void efreet_mime_magic_free(void *data); static void efreet_mime_magic_entry_free(void *data); @@ -182,7 +183,7 @@ if ((type = efreet_mime_magic_check_priority(file, 80, 0))) return type; - return NULL; + return efreet_mime_fallback_check(file); } /** @@ -358,8 +359,7 @@ /** * @internal - * @param datadirs: List of XDG data dirs - * @param datahome: Path to XDG data home directory + * @param file: File to examine * @return Returns mime type if special file, else NULL * @brief Returns a mime type based on the stat of a file. * This is used first to catch directories and other special @@ -440,6 +440,43 @@ /** * @internal + * @param file: File to examine + * @return Returns mime type. Will only return + * null if file does not exist. + * @brief Return a fallback mime type. Returns + * text/plain if the file appears to contain text + * and returns application/octet-stream if it + * appears to be binary. + */ +static const char * +efreet_mime_fallback_check(const char *file) +{ + FILE *f = NULL; + char buf[32]; + int i=0; + + if (!(f = fopen(file, "r"))) + return NULL; + + i = fread(buf,1,sizeof(buf),f); + fclose(f); + + /* + * Check for ASCII control characters in the first 32 bytes. + * New lines and carriage returns are ignored as they are + * quite common in text files. + */ + for (i-=1; i >= 0; --i) + { + if ((buf[i] < 0x20) && (buf[i] != '\n') && (buf[i] != '\r')) + return "application/octet-stream"; + } + + return "text/plain"; +} + +/** + * @internal * @param glob: Glob to search for * @return Returns 1 on success, 0 on failure * @brief Removes a glob from the list @@ -715,7 +752,6 @@ if (*ptr == '[') { char *val, buf[512]; - mime = NEW(Efreet_Mime_Magic, 1); mime->entries = ecore_list_new(); ------------------------------------------------------------------------- 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