Mercurial > trustbridge
changeset 168:f100861dad8f
Added simple portable function to test if an file exists.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Tue, 25 Mar 2014 13:08:59 +0100 |
parents | 92d7e0b40808 |
children | 701b7036c5dc |
files | common/portpath.c common/portpath.h |
diffstat | 2 files changed, 32 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/common/portpath.c Tue Mar 25 12:54:47 2014 +0100 +++ b/common/portpath.c Tue Mar 25 13:08:59 2014 +0100 @@ -4,6 +4,9 @@ #include <limits.h> #include <stdio.h> #include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> char * @@ -31,3 +34,21 @@ return _fullpath(NULL, path, 0); #endif } + +bool +port_fileexits(char *path) +{ + int ret; +#ifndef _WIN32 + struct stat sb; + ret = stat(path, &sb); +#else + struct _stat sb; + ret = _stat(path, &sb); +#endif + + if (ret == 0) + return true; + else + return false; +}
--- a/common/portpath.h Tue Mar 25 12:54:47 2014 +0100 +++ b/common/portpath.h Tue Mar 25 13:08:59 2014 +0100 @@ -1,6 +1,8 @@ #ifndef PORTPATH_H #define PORTPATH_H +#include <stdbool.h> + /** * @file portpath.h * @brief Platform independent functions for file and path handling. @@ -28,4 +30,13 @@ */ char *port_realpath(char *path); +/** + * @brief test if a file exists + * @details uses a platform specific stat call to test if the given + * file exists. + * @param[in] path the path to the file + * @returns true if the file exists and false otherwise + */ +bool port_fileexits(char *path); + #endif