billiob pushed a commit to branch master.

http://git.enlightenment.org/apps/terminology.git/commit/?id=ef3f62774311123aec5c1dc6012c723d55e4f620

commit ef3f62774311123aec5c1dc6012c723d55e4f620
Author: Boris Faure <[email protected]>
Date:   Mon Sep 13 22:48:29 2021 +0200

    extns: add extn_matches() and extn_is_media() + unit test
---
 src/bin/extns.c      | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/extns.h      |  5 ++++
 src/bin/meson.build  |  1 +
 src/bin/tytest.c     |  1 +
 src/bin/unit_tests.h |  1 +
 5 files changed, 77 insertions(+)

diff --git a/src/bin/extns.c b/src/bin/extns.c
index f8ed2e0..fb99c1a 100644
--- a/src/bin/extns.c
+++ b/src/bin/extns.c
@@ -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
diff --git a/src/bin/extns.h b/src/bin/extns.h
index ded11cc..cddeefb 100644
--- a/src/bin/extns.h
+++ b/src/bin/extns.h
@@ -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
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 4be1a77..f12f5d7 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -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',
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 06a3dd1..2984caa 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -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},
 };
 
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index d429f7b..78ca212 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -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

-- 


Reply via email to