all repos — cgit @ f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5

a hyperfast web frontend for git written in c

move get_mimetype_from_file() to shared

Signed-off-by: Christian Hesse <mail@eworm.de>
Christian Hesse mail@eworm.de
Fri, 14 Aug 2015 16:50:56 +0200
commit

f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5

parent

73ef8567f04c2dea8fbf83213b28e0cd1dff98f3

3 files changed, 42 insertions(+), 40 deletions(-)

jump to
M cgit.hcgit.h

@@ -391,4 +391,6 @@ extern int readfile(const char *path, char **buf, size_t *size);

extern char *expand_macros(const char *txt); +extern char *get_mimetype_from_file(const char *filename, const char *ext); + #endif /* CGIT_H */
M shared.cshared.c

@@ -560,3 +560,43 @@ *p = '\0';

} return result; } + +char *get_mimetype_from_file(const char *filename, const char *ext) +{ + static const char *delimiters; + char *result; + FILE *fd; + char line[1024]; + char *mimetype; + char *token; + + if (!filename) + return NULL; + + fd = fopen(filename, "r"); + if (!fd) + return NULL; + + delimiters = " \t\r\n"; + result = NULL; + + /* loop over all lines in the file */ + while (!result && fgets(line, sizeof(line), fd)) { + mimetype = strtok(line, delimiters); + + /* skip empty lines and comment lines */ + if (!mimetype || (mimetype[0] == '#')) + continue; + + /* loop over all extensions of mimetype */ + while ((token = strtok(NULL, delimiters))) { + if (!strcasecmp(ext, token)) { + result = xstrdup(mimetype); + break; + } + } + } + fclose(fd); + + return result; +}
M ui-plain.cui-plain.c

@@ -16,46 +16,6 @@ int match_baselen;

int match; }; -static char *get_mimetype_from_file(const char *filename, const char *ext) -{ - static const char *delimiters; - char *result; - FILE *fd; - char line[1024]; - char *mimetype; - char *token; - - if (!filename) - return NULL; - - fd = fopen(filename, "r"); - if (!fd) - return NULL; - - delimiters = " \t\r\n"; - result = NULL; - - /* loop over all lines in the file */ - while (!result && fgets(line, sizeof(line), fd)) { - mimetype = strtok(line, delimiters); - - /* skip empty lines and comment lines */ - if (!mimetype || (mimetype[0] == '#')) - continue; - - /* loop over all extensions of mimetype */ - while ((token = strtok(NULL, delimiters))) { - if (!strcasecmp(ext, token)) { - result = xstrdup(mimetype); - break; - } - } - } - fclose(fd); - - return result; -} - static int print_object(const unsigned char *sha1, const char *path) { enum object_type type;