extns: add extn_matches() and extn_is_media() + unit test

This commit is contained in:
Boris Faure 2021-09-13 22:48:29 +02:00
parent 02ab1da7cd
commit ef3f627743
Signed by untrusted user who does not match committer: borisfaure
GPG Key ID: 35C0410516166BE8
5 changed files with 77 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include "private.h"
#include <Elementary.h>
#include <stdio.h>
const char *extn_img[] =
@ -41,3 +42,71 @@ const char *extn_aud[] =
".mp3", ".aac", ".wav", ".flac", ".m4a", ".opus",
NULL
};
/**
* Whether a path ends with one of the extensions listed in @extns
*/
Eina_Bool
extn_matches(const char *path, size_t path_len, const char **extns)
{
int i;
for (i = 0; extns[i]; i++)
{
size_t ext_len = strlen(extns[i]);
if (path_len < ext_len)
continue;
if (!strcasecmp(extns[i], path + path_len - ext_len))
return EINA_TRUE;
}
return EINA_FALSE;
}
/**
* Whether a path is a media, if it ends with one of the known extensions
*/
Eina_Bool
extn_is_media(const char *path, size_t path_len)
{
if (extn_matches(path, path_len, extn_img))
return EINA_TRUE;
if (extn_matches(path, path_len, extn_scale))
return EINA_TRUE;
if (extn_matches(path, path_len, extn_edj))
return EINA_TRUE;
if (extn_matches(path, path_len, extn_mov))
return EINA_TRUE;
if (extn_matches(path, path_len, extn_aud))
return EINA_TRUE;
return EINA_FALSE;
}
#if defined(BINARY_TYTEST)
#include <assert.h>
int
tytest_extn_matching(void)
{
const char *invalid = "foobar.inv";
assert(extn_is_media(invalid, strlen(invalid)) == EINA_FALSE);
/* Images */
const char *jpeg = "/home/qux/foo.bar.jpeg";
assert(extn_is_media(jpeg, strlen(jpeg)) == EINA_TRUE);
const char *png = "https://foo.bar/qux.PNG";
assert(extn_is_media(png, strlen(png)) == EINA_TRUE);
/* Scale */
const char *svg = "https://foo.bar/qux.svg";
assert(extn_is_media(svg, strlen(svg)) == EINA_TRUE);
const char *svggz = "https://foo.bar/qux.svg.gz";
assert(extn_is_media(svggz, strlen(svggz)) == EINA_TRUE);
/* EDJ */
const char *edj = "https://foo.bar/qux.edj";
assert(extn_is_media(edj, strlen(edj)) == EINA_TRUE);
/* Movie */
const char *mkv = "https://foo.bar/qux.mkv";
assert(extn_is_media(mkv, strlen(mkv)) == EINA_TRUE);
/* Audio */
const char *ogg = "https://foo.bar/qux.ogg";
assert(extn_is_media(ogg, strlen(ogg)) == EINA_TRUE);
return 0;
}
#endif

View File

@ -7,4 +7,9 @@ extern const char *extn_edj[];
extern const char *extn_mov[];
extern const char *extn_aud[];
Eina_Bool
extn_matches(const char *path, size_t path_len, const char **extns);
Eina_Bool
extn_is_media(const char *path, size_t path_len);
#endif

View File

@ -76,6 +76,7 @@ tytest_sources = ['termptyesc.c', 'termptyesc.h',
'termiolink.c', 'termiolink.h',
'config.c', 'config.h',
'colors.c', 'colors.h',
'extns.c', 'extns.h',
'sb.c', 'sb.h',
'utf8.c', 'utf8.h',
'utils.c', 'utils.h',

View File

@ -40,6 +40,7 @@ static struct {
{ "color_parse_edc", tytest_color_parse_edc},
{ "color_parse_css_rgb", tytest_color_parse_css_rgb},
{ "color_parse_css_hsl", tytest_color_parse_css_hsl},
{ "extn_matching", tytest_extn_matching},
{ NULL, NULL},
};

View File

@ -17,5 +17,6 @@ int tytest_color_parse_uint8(void);
int tytest_color_parse_edc(void);
int tytest_color_parse_css_rgb(void);
int tytest_color_parse_css_hsl(void);
int tytest_extn_matching(void);
#endif