Skip to content

Commit

Permalink
Removed 2nd level grouping in mime types.
Browse files Browse the repository at this point in the history
  • Loading branch information
sumagnadas committed Mar 25, 2021
1 parent 7ac3f4e commit eb84617
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 153 deletions.
6 changes: 3 additions & 3 deletions include/bu/mime.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ __BEGIN_DECLS
* was found. It is the responsibility of the caller to cast
* the return int to the correct mime_CONTEXT_t type.
*/
BU_EXPORT extern int bu_file_mime(const char *ext, bu_mime_context context);
BU_EXPORT extern int bu_file_mime(const char *ext);


/**
Expand All @@ -67,7 +67,7 @@ BU_EXPORT extern int bu_file_mime(const char *ext, bu_mime_context context);
* containing the extensions if a result was found.
* It is the responsibility of the caller to free the returned string.
*/
BU_EXPORT extern const char *bu_file_mime_ext(int t, bu_mime_context context);
BU_EXPORT extern const char *bu_file_mime_ext(int t);


/**
Expand All @@ -78,7 +78,7 @@ BU_EXPORT extern const char *bu_file_mime_ext(int t, bu_mime_context context);
* returns NULL if no match was found, or a string if a result was found.
* It is the responsibility of the caller to free the returned string.
*/
BU_EXPORT extern const char *bu_file_mime_str(int t, bu_mime_context context);
BU_EXPORT extern const char *bu_file_mime_str(int t);


/**
Expand Down
2 changes: 1 addition & 1 deletion include/gcv/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct gcv_filter {
* FIXME: input/output plugins conceivably could be something
* other than geometry (e.g., png input or csv output).
*/
const bu_mime_context_t mime_type;
const bu_mime_t mime_type;

/* For plugins supporting multiple file types, call this to
* process them.
Expand Down
6 changes: 3 additions & 3 deletions include/icv/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ICV_EXPORT extern int icv_destroy(icv_image_t *bif);
* Returns 1 if an image size was identified, zero otherwise.
*
*/
ICV_EXPORT extern int icv_image_size(const char *label, size_t dpi, size_t data_size, bu_mime_context_t type, size_t *widthp, size_t *heightp);
ICV_EXPORT extern int icv_image_size(const char *label, size_t dpi, size_t data_size, bu_mime_t type, size_t *widthp, size_t *heightp);

/**
* Load a file into an ICV struct. For most formats, this will be
Expand All @@ -108,7 +108,7 @@ ICV_EXPORT extern int icv_image_size(const char *label, size_t dpi, size_t data_
* program.
* @return A newly allocated struct holding the loaded image info.
*/
ICV_EXPORT extern icv_image_t *icv_read(const char *filename, bu_mime_context_t format, size_t width, size_t height);
ICV_EXPORT extern icv_image_t *icv_read(const char *filename, bu_mime_t format, size_t width, size_t height);

/**
* Saves Image to a file or streams to stdout in respective format
Expand All @@ -120,7 +120,7 @@ ICV_EXPORT extern icv_image_t *icv_read(const char *filename, bu_mime_context_t
* @param format Specific format of the file to be written.
* @return on success 0, on failure -1 with log messages.
*/
ICV_EXPORT extern int icv_write(icv_image_t *bif, const char*filename, bu_mime_context_t format);
ICV_EXPORT extern int icv_write(icv_image_t *bif, const char*filename, bu_mime_t format);

/**
* Write an image line to the data of ICV struct. Can handle unsigned
Expand Down
4 changes: 2 additions & 2 deletions src/conv/3dm/3dm-g.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@


static const struct gcv_filter *
find_filter(enum gcv_filter_type filter_type, bu_mime_context_t mime_type, const char *data)
find_filter(enum gcv_filter_type filter_type, bu_mime_t mime_type, const char *data)
{
const struct gcv_filter * const *entry;
const struct bu_ptbl * const filters = gcv_list_filters();

for (BU_PTBL_FOR(entry, (const struct gcv_filter * const *), filters)) {
bu_mime_context_t emt = (*entry)->mime_type;
bu_mime_t emt = (*entry)->mime_type;
if ((*entry)->filter_type != filter_type) continue;
if ( (emt != BU_MIME_MODEL_AUTO) && (emt == mime_type)) return *entry;
if ( (emt == BU_MIME_MODEL_AUTO) && ((*entry)->data_supported && data && (*(*entry)->data_supported)(data)) ) return *entry;
Expand Down
50 changes: 25 additions & 25 deletions src/conv/gcv/gcv.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static int
parse_model_string(struct bu_vls *format, struct bu_vls *slog, const char *opt, const char *input)
{
int type_int = 0;
bu_mime_context_t type = BU_MIME_MODEL_UNKNOWN;
bu_mime_t type = BU_MIME_MODEL_UNKNOWN;

struct bu_vls format_cpy = BU_VLS_INIT_ZERO;
struct bu_vls path = BU_VLS_INIT_ZERO;
Expand All @@ -206,8 +206,8 @@ parse_model_string(struct bu_vls *format, struct bu_vls *slog, const char *opt,

/* If we have an explicit option, that overrides any other format specifiers */
if (opt) {
type_int = bu_file_mime(opt, BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)type_int;
type_int = bu_file_mime(opt);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)type_int;
if (type == BU_MIME_MODEL_UNKNOWN) {
/* Have prefix, but doesn't result in a known format - that's an error */
if (slog)
Expand All @@ -223,8 +223,8 @@ parse_model_string(struct bu_vls *format, struct bu_vls *slog, const char *opt,
* find out if it maps to a valid type */
if (type == BU_MIME_MODEL_UNKNOWN && format) {
/* Yes - see if the prefix specifies a model format */
type_int = bu_file_mime(bu_vls_addr(format), BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)type_int;
type_int = bu_file_mime(bu_vls_addr(format));
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)type_int;
if (type == BU_MIME_MODEL_UNKNOWN) {
/* Have prefix, but doesn't result in a known format - that's an error */
if (slog)
Expand All @@ -238,8 +238,8 @@ parse_model_string(struct bu_vls *format, struct bu_vls *slog, const char *opt,
/* If we don't already have a type and we were passed a format string, give it a try */
if (type == BU_MIME_MODEL_UNKNOWN && format && bu_vls_strlen(&format_cpy) > 0) {
bu_vls_sprintf(format, "%s", bu_vls_addr(&format_cpy));
type_int = bu_file_mime(bu_vls_addr(&format_cpy), BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)type_int;
type_int = bu_file_mime(bu_vls_addr(&format_cpy));
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)type_int;
if (type == BU_MIME_MODEL_UNKNOWN) {
/* Have prefix, but doesn't result in a known format - that's an error */
if (slog)
Expand All @@ -252,8 +252,8 @@ parse_model_string(struct bu_vls *format, struct bu_vls *slog, const char *opt,
/* If we have no prefix or the prefix didn't map to a model type, try file extension */
if (type == BU_MIME_MODEL_UNKNOWN && extract_path(&path, input)) {
if (bu_path_component(format, bu_vls_addr(&path), BU_PATH_EXT)) {
type_int = bu_file_mime(bu_vls_addr(format), BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)type_int;
type_int = bu_file_mime(bu_vls_addr(format));
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)type_int;
if (type == BU_MIME_MODEL_UNKNOWN) {
/* Have file extension, but doesn't result in a known format - that's an error */
if (slog)
Expand Down Expand Up @@ -315,13 +315,13 @@ static int
model_mime(struct bu_vls *msg, size_t argc, const char **argv, void *set_mime)
{
int type_int;
bu_mime_context_t type = BU_MIME_MODEL_UNKNOWN;
bu_mime_context_t *set_type = (bu_mime_context_t *)set_mime;
bu_mime_t type = BU_MIME_MODEL_UNKNOWN;
bu_mime_t *set_type = (bu_mime_t *)set_mime;

BU_OPT_CHECK_ARGV0(msg, argc, argv, "mime format");

type_int = bu_file_mime(argv[0], BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)type_int;
type_int = bu_file_mime(argv[0]);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)type_int;
if (type == BU_MIME_MODEL_UNKNOWN) {
if (msg) {
bu_vls_sprintf(msg, "Error - unknown geometry file type: %s \n", argv[0]);
Expand Down Expand Up @@ -361,8 +361,8 @@ help(struct bu_vls *UNUSED(msg), size_t argc, const char **argv, void *set_var)
static int
do_conversion(
struct bu_vls *messages,
const char *in_path, bu_mime_context_t in_type,
const char *out_path, bu_mime_context_t out_type,
const char *in_path, bu_mime_t in_type,
const char *out_path, bu_mime_t out_type,
size_t in_argc, const char **in_argv,
size_t out_argc, const char **out_argv)
{
Expand All @@ -372,7 +372,7 @@ do_conversion(
struct gcv_context context;

for (BU_PTBL_FOR(entry, (const struct gcv_filter * const *), filters)) {
bu_mime_context_t emt = (*entry)->mime_type;
bu_mime_t emt = (*entry)->mime_type;
if ((*entry)->filter_type == GCV_FILTER_READ) {
if (!in_filter && (emt != BU_MIME_MODEL_AUTO) && (emt == in_type))
in_filter = *entry;
Expand All @@ -385,16 +385,16 @@ do_conversion(
if (!out_filter && (emt != BU_MIME_MODEL_AUTO) && (emt == out_type))
out_filter = *entry;
if (!out_filter && (emt == BU_MIME_MODEL_AUTO) &&
((*entry)->data_supported && (*(*entry)->data_supported)(bu_file_mime_str(out_type, BU_MIME_AUTO)))) {
((*entry)->data_supported && (*(*entry)->data_supported)(bu_file_mime_str(out_type)))) {
out_filter = *entry;
}
}
}

if (!in_filter)
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(in_type, BU_MIME_AUTO));
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(in_type));
if (!out_filter)
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(out_type, BU_MIME_AUTO));
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(out_type));
if (!in_filter || !out_filter)
return 0;

Expand Down Expand Up @@ -435,8 +435,8 @@ main(int ac, const char **av)
int fmt = 0;
int ret = 0;

static bu_mime_context_t in_type = BU_MIME_MODEL_UNKNOWN;
static bu_mime_context_t out_type = BU_MIME_MODEL_UNKNOWN;
static bu_mime_t in_type = BU_MIME_MODEL_UNKNOWN;
static bu_mime_t out_type = BU_MIME_MODEL_UNKNOWN;

static struct fmt_opts in_only_opts;
static struct fmt_opts out_only_opts;
Expand Down Expand Up @@ -630,13 +630,13 @@ main(int ac, const char **av)
/* Find out what input file type we are dealing with */
if (in_type == BU_MIME_MODEL_UNKNOWN) {
fmt = parse_model_string(&in_format, &slog, in_fmt, bu_vls_addr(&in_path_raw));
in_type = (fmt < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)fmt;
in_type = (fmt < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)fmt;
in_fmt = NULL;
}
/* Identify output file type */
if (out_type == BU_MIME_MODEL_UNKNOWN) {
fmt = parse_model_string(&out_format, &slog, out_fmt, bu_vls_addr(&out_path_raw));
out_type = (fmt < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_context_t)fmt;
out_type = (fmt < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_t)fmt;
out_fmt = NULL;
}

Expand All @@ -663,8 +663,8 @@ main(int ac, const char **av)
/* If we've gotten this far, we know enough to try to convert. Until we
* hook in conversion calls to libgcv, print a summary of the option
* parsing results for debugging. */
in_fmt = bu_file_mime_str((int)in_type, BU_MIME_AUTO);
out_fmt = bu_file_mime_str((int)out_type, BU_MIME_AUTO);
in_fmt = bu_file_mime_str((int)in_type);
out_fmt = bu_file_mime_str((int)out_type);
if (in_fmt) {
bu_log("Input file format: %s\n", in_fmt);
} else {
Expand Down
83 changes: 13 additions & 70 deletions src/libbu/mime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,6 @@ endforeach(line ${TYPES})

# HEADER with typedefs

list(GET ACTIVE_TYPES 0 FIRST_TYPE)
set(mcstr "typedef enum {")
set(last_type "")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
if("${context}" STREQUAL "${FIRST_TYPE}")
set(mcstr "${mcstr}\n BU_MIME_${c} = 0,")
else("${context}" STREQUAL "${FIRST_TYPE}")
set(mcstr "${mcstr}\n BU_MIME_${c},")
endif("${context}" STREQUAL "${FIRST_TYPE}")
set(last_type "BU_MIME_${c}")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr}\n BU_MIME_UNKNOWN,")
set(mcstr "${mcstr}\n BU_MIME_AUTO")
set(mcstr "${mcstr}\n} bu_mime_context;\n\n")
set(h_contents "${h_contents}\n${mcstr}")

set(enum_str "typedef enum {")
list(GET ACTIVE_TYPES 0 FIRST_ACTIVE_TYPE)
foreach(c ${ACTIVE_TYPES})
Expand All @@ -158,7 +141,7 @@ foreach(c ${ACTIVE_TYPES})
set(enum_str "${enum_str}\n BU_MIME_${c_u}_UNKNOWN,")
endif("${c_u}" STREQUAL "${FIRST_TYPE}")
endforeach(c ${ACTIVE_TYPES})
set(enum_str "${enum_str}\n} bu_mime_context_t;\n\n")
set(enum_str "${enum_str}\n} bu_mime_t;\n\n")
set(h_contents "${h_contents}\n${enum_str}")
set(h_contents "${h_contents}\n__END_DECLS\n")
set(h_contents "${h_contents}\n#endif /*BU_MIME_TYPES_H*/\n")
Expand All @@ -177,26 +160,12 @@ foreach(c ${ACTIVE_TYPES})
endforeach(c ${ACTIVE_TYPES})

# Public C mapping function - extension to type
set(mcstr "\nint\nbu_file_mime(const char *ext, bu_mime_context context)\n{")
set(mcstr "${mcstr}\n switch (context) {\n")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} case BU_MIME_${c}:\n")
set(mcstr "${mcstr} return mime_${context}(ext);\n")
set(mcstr "${mcstr} break;\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} case BU_MIME_AUTO:\n")
set(mcstr "\nint\nbu_file_mime(const char *ext)\n{")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} if (mime_${context}(ext) != -1){\n")
set(mcstr "${mcstr} return mime_${context}(ext);\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} if (mime_${context}(ext) != -1){\n")
set(mcstr "${mcstr} return mime_${context}(ext);\n")
set(mcstr "${mcstr} }\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} break;\n")
set(mcstr "${mcstr} default:\n")
set(mcstr "${mcstr} return -1;\n")
set(mcstr "${mcstr} break;\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} return -1;\n")
set(mcstr "${mcstr}}\n")
set(c_contents "${c_contents}\n${mcstr}")
Expand All @@ -217,26 +186,13 @@ foreach(c ${ACTIVE_TYPES})
endforeach(c ${ACTIVE_TYPES})

# Public C mapping function - type to string
set(mcstr "\nconst char *\nbu_file_mime_str(int t, bu_mime_context context)\n{")
set(mcstr "${mcstr}\n switch (context) {\n")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} case BU_MIME_${c}:\n")
set(mcstr "${mcstr} return mime_str_${context}(t);\n")
set(mcstr "${mcstr} break;\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} case BU_MIME_AUTO:\n")
set(mcstr "\nconst char *\nbu_file_mime_str(int t)\n{")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} if (mime_str_${context}(t) != NULL && !BU_STR_EQUIV(\"BU_MIME_${c}_UNKNOWN\", mime_str_${context}(t))){\n")
set(mcstr "${mcstr} return mime_str_${context}(t);\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} if (mime_str_${context}(t) != NULL && !BU_STR_EQUIV(\"BU_MIME_${c}_UNKNOWN\", mime_str_${context}(t))){\n")
set(mcstr "${mcstr} return mime_str_${context}(t);\n")
set(mcstr "${mcstr} }\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} break;\n")
set(mcstr "${mcstr} default:\n")
set(mcstr "${mcstr} return NULL;\n")
set(mcstr "${mcstr} break;\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} return NULL;\n")
set(mcstr "${mcstr}}\n")
set(c_contents "${c_contents}\n${mcstr}")
Expand Down Expand Up @@ -271,26 +227,13 @@ foreach(c ${ACTIVE_TYPES})
endforeach(c ${ACTIVE_TYPES})

# Public C mapping function - type to string
set(mcstr "\nconst char *\nbu_file_mime_ext(int t, bu_mime_context context)\n{")
set(mcstr "${mcstr}\n switch (context) {\n")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} case BU_MIME_${c}:\n")
set(mcstr "${mcstr} return mime_ext_${context}(t);\n")
set(mcstr "${mcstr} break;\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} case BU_MIME_AUTO:\n")
set(mcstr "\nconst char *\nbu_file_mime_ext(int t)\n{")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} if (mime_ext_${context}(t) != NULL){\n")
set(mcstr "${mcstr} return mime_ext_${context}(t);\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} if (mime_ext_${context}(t) != NULL){\n")
set(mcstr "${mcstr} return mime_ext_${context}(t);\n")
set(mcstr "${mcstr} }\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} break;\n")
set(mcstr "${mcstr} default:\n")
set(mcstr "${mcstr} return NULL;\n")
set(mcstr "${mcstr} break;\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} return NULL;\n")
set(mcstr "${mcstr}}\n")
set(c_contents "${c_contents}\n${mcstr}")
Expand Down
17 changes: 6 additions & 11 deletions src/libbu/tests/file_mime.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include "bu.h"

int
test_ext(const char *str, bu_mime_context context, int expected)
test_ext(const char *str, int expected)
{
int status = 0;
int type = bu_file_mime(str, context);
int type = bu_file_mime(str);

if (type == expected) {
bu_log("%s -> %d [PASS]\n", str, type);
Expand All @@ -45,21 +45,16 @@ test_ext(const char *str, bu_mime_context context, int expected)
int
main(int ac, char *av[])
{
int context = 0;
int expected = 0;

bu_setprogname(av[0]);

if (ac != 4)
bu_exit(1, "Usage: %s {extension} {context} {expected}\n", av[0]);
if (ac != 3)
bu_exit(1, "Usage: %s {extension} {expected}\n", av[0]);

sscanf(av[2], "%d", &context);
sscanf(av[3], "%d", &expected);
sscanf(av[2], "%d", &expected);

if (context >= BU_MIME_UNKNOWN)
return -1;

return test_ext(av[1], (bu_mime_context)context, expected);
return test_ext(av[1], expected);
}


Expand Down
Loading

0 comments on commit eb84617

Please sign in to comment.