[Pkg-voip-commits] [bctoolbox] 13/60: Add bctbx_dirname() and bctbx_basename().

Bernhard Schmidt berni at moszumanska.debian.org
Sun Oct 15 22:42:23 UTC 2017


This is an automated email from the git hooks/post-receive script.

berni pushed a commit to branch debian/sid
in repository bctoolbox.

commit 3bd852586cbdc60df367aa8032abdb2dec37f021
Author: Ghislain MARY <ghislain.mary at belledonne-communications.com>
Date:   Fri Mar 31 17:03:38 2017 +0200

    Add bctbx_dirname() and bctbx_basename().
---
 include/bctoolbox/port.h | 14 ++++++++++++++
 src/utils/port.c         | 31 +++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/include/bctoolbox/port.h b/include/bctoolbox/port.h
index c88bbff..5a150b4 100644
--- a/include/bctoolbox/port.h
+++ b/include/bctoolbox/port.h
@@ -368,6 +368,20 @@ BCTBX_PUBLIC char *bctbx_strcat_printf(char *dst, const char *fmt,...);
 BCTBX_PUBLIC char *bctbx_strcat_vprintf(char *dst, const char *fmt, va_list ap);
 BCTBX_PUBLIC char *bctbx_concat (const char *str, ...) ;
 
+/**
+ * Portable version of the dirname function from libgen.h
+ * @param[in] path The full path for which we want to find the dirname
+ * @return NULL if no dirname is found, otherwise a copy of the dirname of path that needs to be freed with bctbx_free().
+ */
+BCTBX_PUBLIC char *bctbx_dirname(const char *path);
+
+/**
+ * Portable version of the basename function from libgen.h
+ * @param[in] path The full path for which we want to find the basename
+ * @return NULL if no basename is found, otherwise a copy of the basename of path that needs to be freed with bctbx_free().
+ */
+BCTBX_PUBLIC char *bctbx_basename(const char *path);
+
 BCTBX_PUBLIC int bctbx_file_exist(const char *pathname);
 
 /**
diff --git a/src/utils/port.c b/src/utils/port.c
index 0b68e3a..2cfb661 100644
--- a/src/utils/port.c
+++ b/src/utils/port.c
@@ -107,6 +107,37 @@ char * bctbx_strdup(const char *tmp){
 	return ret;
 }
 
+char * bctbx_dirname(const char *path) {
+	char *ptr;
+	char *dname = bctbx_strdup(path);
+	bool_t found = FALSE;
+
+	ptr = strchr(path, '/');
+	if (ptr != NULL) {
+		dname[ptr - path + 1] = '\0';
+		found = TRUE;
+	} else {
+		ptr = strchr(path, '\\');
+		if (ptr != NULL) {
+			dname[ptr - path + 1] = '\0';
+			found = TRUE;
+		}
+	}
+
+	if (found == FALSE) {
+		bctbx_free(dname);
+		return NULL;
+	}
+	return dname;
+}
+
+char * bctbx_basename(const char *path) {
+	char *ptr = strrchr(path, '/');
+	if (ptr == NULL) ptr = strrchr(path, '\\');
+	if (ptr == NULL) return NULL;
+	return bctbx_strdup(ptr + 1);
+}
+
 /*
  * this method is an utility method that calls fnctl() on UNIX or
  * ioctlsocket on Win32.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-voip/bctoolbox.git



More information about the Pkg-voip-commits mailing list