Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dissolve the mime namespace into a bunch of defines #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_t context);
BU_EXPORT extern int bu_file_mime(const char *ext, long context);


/**
Expand All @@ -67,7 +67,7 @@ BU_EXPORT extern int bu_file_mime(const char *ext, bu_mime_context_t 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_t context);
BU_EXPORT extern const char *bu_file_mime_ext(int t, long context);


/**
Expand All @@ -78,7 +78,7 @@ BU_EXPORT extern const char *bu_file_mime_ext(int t, bu_mime_context_t 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_t context);
BU_EXPORT extern const char *bu_file_mime_str(int t, long context);


/**
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_model_t mime_type;
const long 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_image_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, long 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_image_t format, size_t width, size_t height);
ICV_EXPORT extern icv_image_t *icv_read(const char *filename, long 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_image_t fo
* @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_image_t format);
ICV_EXPORT extern int icv_write(icv_image_t *bif, const char*filename, long 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_model_t mime_type, const char *data)
find_filter(enum gcv_filter_type filter_type, long 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_model_t emt = (*entry)->mime_type;
long 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_model_t type = BU_MIME_MODEL_UNKNOWN;
long 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_MODEL);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_model_t)type_int;
type_int = bu_file_mime(opt, BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_MODEL);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_model_t)type_int;
type_int = bu_file_mime(bu_vls_addr(format), BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_MODEL);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_model_t)type_int;
type_int = bu_file_mime(bu_vls_addr(&format_cpy), BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_MODEL);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_model_t)type_int;
type_int = bu_file_mime(bu_vls_addr(format), BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_model_t type = BU_MIME_MODEL_UNKNOWN;
bu_mime_model_t *set_type = (bu_mime_model_t *)set_mime;
long type = BU_MIME_MODEL_UNKNOWN;
long *set_type = (long *)set_mime;

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

type_int = bu_file_mime(argv[0], BU_MIME_MODEL);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (bu_mime_model_t)type_int;
type_int = bu_file_mime(argv[0], BU_MIME_AUTO);
type = (type_int < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_model_t in_type,
const char *out_path, bu_mime_model_t out_type,
const char *in_path, long in_type,
const char *out_path, long 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_model_t emt = (*entry)->mime_type;
long 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_MODEL)))) {
((*entry)->data_supported && (*(*entry)->data_supported)(bu_file_mime_str(out_type, BU_MIME_AUTO)))) {
out_filter = *entry;
}
}
}

if (!in_filter)
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(in_type, BU_MIME_MODEL));
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(in_type, BU_MIME_AUTO));
if (!out_filter)
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(out_type, BU_MIME_MODEL));
bu_vls_printf(messages, "No filter for %s\n", bu_file_mime_str(out_type, BU_MIME_AUTO));
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_model_t in_type = BU_MIME_MODEL_UNKNOWN;
static bu_mime_model_t out_type = BU_MIME_MODEL_UNKNOWN;
static long in_type = BU_MIME_MODEL_UNKNOWN;
static long 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_model_t)fmt;
in_type = (fmt < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_model_t)fmt;
out_type = (fmt < 0) ? BU_MIME_MODEL_UNKNOWN : (long)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_MODEL);
out_fmt = bu_file_mime_str((int)out_type, BU_MIME_MODEL);
in_fmt = bu_file_mime_str((int)in_type, BU_MIME_AUTO);
out_fmt = bu_file_mime_str((int)out_type, BU_MIME_AUTO);
if (in_fmt) {
bu_log("Input file format: %s\n", in_fmt);
} else {
Expand Down
101 changes: 84 additions & 17 deletions src/libbu/mime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -123,34 +123,48 @@ foreach(line ${TYPES})
endforeach(line ${TYPES})

# HEADER with typedefs

set(a 0)
list(GET ACTIVE_TYPES 0 FIRST_TYPE)
set(mcstr "typedef enum {")
set(mcstr "${mcstr}\n#define BU_MIME_UNKNOWN ${a}\n")
math(EXPR a "${a}+1")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
if("${context}" STREQUAL "${FIRST_TYPE}")
set(mcstr "${mcstr}\n BU_MIME_${c} = 0,")
set(mcstr "${mcstr}#define BU_MIME_${c} ${a}")
math(EXPR a "${a}+1")
else("${context}" STREQUAL "${FIRST_TYPE}")
set(mcstr "${mcstr}\n BU_MIME_${c},")
set(mcstr "${mcstr}\n#define BU_MIME_${c} ${a}")
math(EXPR a "${a}+1")
endif("${context}" STREQUAL "${FIRST_TYPE}")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr}\n BU_MIME_UNKNOWN")
set(mcstr "${mcstr}\n} bu_mime_context_t;\n\n")
set(mcstr "${mcstr}\n#define BU_MIME_AUTO ${a}")
math(EXPR a "${a}+1")
set(mcstr "${mcstr}\n")
set(h_contents "${h_contents}\n${mcstr}")

set(b 1)
list(GET ACTIVE_TYPES 0 FIRST_ACTIVE_TYPE)
foreach(c ${ACTIVE_TYPES})
set(enum_str "typedef enum {")
list(SORT ${c}_types)
list(GET ${FIRST_ACTIVE_TYPE}_types 0 FIRST_TYPE)
string(TOUPPER "${c}" c_u)
# Default first enum for all types is an auto type
set(enum_str "${enum_str}\n BU_MIME_${c_u}_AUTO,")
foreach(type ${${c}_types})
set(enum_str "${enum_str}\n BU_MIME_${type},")
endforeach(type ${${c}_types})
set(enum_str "${enum_str}\n BU_MIME_${c_u}_UNKNOWN")
set(enum_str "${enum_str}\n} bu_mime_${c}_t;\n\n")
set(h_contents "${h_contents}\n${enum_str}")
if("${c_u}" STREQUAL "${FIRST_TYPE}")
set(enum_str "${enum_str}\n#define BU_MIME_${c_u}_AUTO ${b}")
math(EXPR b "${b}+1")
else("${c_u}" STREQUAL "${FIRST_TYPE}")
set(enum_str "${enum_str}\n#define BU_MIME_${c_u}_AUTO ${b}")
math(EXPR b "${b}+1")
foreach(type ${${c}_types})
set(enum_str "${enum_str}\n#define BU_MIME_${type} ${b}")
math(EXPR b "${b}+1")
endforeach(type ${${c}_types})
set(enum_str "${enum_str}\n#define BU_MIME_${c_u}_UNKNOWN ${b}")
math(EXPR b "${b}+1")
endif("${c_u}" STREQUAL "${FIRST_TYPE}")
endforeach(c ${ACTIVE_TYPES})
set(enum_str "${enum_str}\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 @@ -168,14 +182,22 @@ 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_t context)\n{")
set(mcstr "\nint\nbu_file_mime(const char *ext, long 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")
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")
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")
Expand All @@ -200,14 +222,22 @@ 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_t context)\n{")
set(mcstr "\nconst char *\nbu_file_mime_str(int t, long 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")
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")
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")
Expand Down Expand Up @@ -246,14 +276,22 @@ 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_t context)\n{")
set(mcstr "\nconst char *\nbu_file_mime_ext(int t, long 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")
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")
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")
Expand All @@ -262,6 +300,35 @@ set(mcstr "${mcstr} return NULL;\n")
set(mcstr "${mcstr}}\n")
set(c_contents "${c_contents}\n${mcstr}")

# Public C mapping function - type to category
set(mcstr "\nlong \nbu_mime_context(long mime_id)\n{")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr}\n static long BU_MIME_${c}S[] = {\n")
foreach(type ${${context}_types})
set(mcstr "${mcstr} BU_MIME_${type},\n")
endforeach(type ${${context}_types})
set(mcstr "${mcstr} 0\n")
set(mcstr "${mcstr} };\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr}\n static long *BU_MIME_CONTEXTS[] = {\n")
foreach(context ${ACTIVE_TYPES})
string(TOUPPER "${context}" c)
set(mcstr "${mcstr} BU_MIME_${c}S,\n")
endforeach(context ${ACTIVE_TYPES})
set(mcstr "${mcstr} NULL\n")
set(mcstr "${mcstr} };\n")
set(mcstr "${mcstr} for(long i=0; i<(${a}-2); i++){\n")
set(mcstr "${mcstr} while (BU_MIME_CONTEXTS[i] != 0){\n")
set(mcstr "${mcstr} if (*BU_MIME_CONTEXTS[i] == mime_id){\n")
set(mcstr "${mcstr} return i+1;\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} }\n")
set(mcstr "${mcstr} return 0;\n")
set(mcstr "${mcstr}}\n")
set(c_contents "${c_contents}\n${mcstr}")

file(WRITE ${mime_types_h_file_tmp} "${h_contents}")
file(WRITE ${mime_c_file_tmp} "${c_contents}")

Expand Down
Loading