Commit 3bd85258 authored by Ghislain MARY's avatar Ghislain MARY

Add bctbx_dirname() and bctbx_basename().

parent 557b17f1
......@@ -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);
/**
......
......@@ -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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment