From cf70ca6b81b8590f79987a0e2f1412c7e0ced58a Mon Sep 17 00:00:00 2001 From: tth Date: Wed, 30 Oct 2019 15:49:53 +0100 Subject: [PATCH] parsing filename for filetype --- floatimg.h | 6 ++++++ funcs/t.c | 28 +++++++++++++++++++++++++++- funcs/utils.c | 23 ++++++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/floatimg.h b/floatimg.h index 3990ca8b..c2da9b72 100644 --- a/floatimg.h +++ b/floatimg.h @@ -32,6 +32,10 @@ typedef struct { #define FIMG_TYPE_RGB 3 #define FIMG_TYPE_RGBA 4 +#define FILE_TYPE_FIMG 1 +#define FILE_TYPE_PNM 2 +#define FILE_TYPE_PNG 3 + /* * core module */ @@ -91,6 +95,8 @@ int fimg_draw_something(FloatImg *fimg); int parse_WxH(char *str, int *pw, int *ph); int parse_double(char *str, double *dptr); +int format_from_extension(char *fname); + diff --git a/funcs/t.c b/funcs/t.c index 4f8a9372..98f58b1b 100644 --- a/funcs/t.c +++ b/funcs/t.c @@ -9,7 +9,7 @@ #include "../floatimg.h" /* --------------------------------------------------------------------- */ -int main(int argc, char *argv[]) +int essai_parse_double(void) { int foo; double dval; @@ -34,3 +34,29 @@ printf("%-10s -> %3d %g\n", str, foo, dval); return 0; } /* --------------------------------------------------------------------- */ +int essai_detect_type(void) +{ +int foo; +char *fname; + +foo = format_from_extension(fname="foo.fimg"); +printf("%-10s %d\n\n", fname, foo); + +foo = format_from_extension(fname="foo.pNm"); +printf("%-10s %d\n\n", fname, foo); + +foo = format_from_extension(fname="foo.xyzzy"); +printf("%-10s %d\n\n", fname, foo); + +return 0; +} +/* --------------------------------------------------------------------- */ +int main(int argc, char *argv[]) +{ +int foo; + +foo = essai_detect_type(); + +return 0; +} +/* --------------------------------------------------------------------- */ diff --git a/funcs/utils.c b/funcs/utils.c index 495fac60..0abc2a24 100644 --- a/funcs/utils.c +++ b/funcs/utils.c @@ -1,7 +1,7 @@ #include - +#include #include "../floatimg.h" @@ -42,3 +42,24 @@ if (1 == foo) { return -1; } /* --------------------------------------------------------------------- */ +int format_from_extension(char *fname) +{ +char *cptr; + +cptr = rindex(fname, '.'); +if (NULL==cptr) { + fprintf(stderr, "do dot in %s\n", fname); + return -1; + } + +#if DEBUG_LEVEL +fprintf(stderr, "[%s] --> [%s]\n", fname, cptr); +#endif + +if (!strcasecmp(cptr, ".pnm")) return FILE_TYPE_PNM; +if (!strcasecmp(cptr, ".fimg")) return FILE_TYPE_FIMG; +if (!strcasecmp(cptr, ".png")) return FILE_TYPE_PNG; + +return -1; +} +/* --------------------------------------------------------------------- */