[[RFC] v2 02/14] lib/url: Check URL file extensions

Message ID 20180118050517.2442-3-sam@mendozajonas.com
State RFC
Headers show
Series
  • [[RFC] v2 02/14] lib/url: Check URL file extensions
Related show

Commit Message

Samuel Mendoza-Jonas Jan. 18, 2018, 5:05 a.m.
Add pb_url_check_extension() which compares a pb_url against a given
extension string.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
 lib/url/url.c | 15 +++++++++++++++
 lib/url/url.h |  1 +
 2 files changed, 16 insertions(+)

Patch

diff --git a/lib/url/url.c b/lib/url/url.c
index 6eeced3..4908122 100644
--- a/lib/url/url.c
+++ b/lib/url/url.c
@@ -300,3 +300,18 @@  const char *pb_url_scheme_name(enum pb_url_scheme scheme)
 	const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
 	return info ? info->str : NULL;
 }
+
+bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext)
+{
+	char *pos;
+
+	if (!pb_url || !pb_url->file || !ext)
+		return false;
+
+	if (strlen(pb_url->file) < strlen(ext))
+		return false;
+
+	pos = pb_url->file + strlen(pb_url->file) - strlen(ext);
+
+	return strncmp(pos, ext, strlen(ext)) == 0;
+}
diff --git a/lib/url/url.h b/lib/url/url.h
index 9043615..6334763 100644
--- a/lib/url/url.h
+++ b/lib/url/url.h
@@ -67,5 +67,6 @@  struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s);
 char *pb_url_to_string(struct pb_url *url);
 
 const char *pb_url_scheme_name(enum pb_url_scheme scheme);
+bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext);
 
 #endif