From 22953ccb02e273e251b3dc6cf7ac3324f03fb85e Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sun, 19 Dec 2010 21:39:01 +0000 Subject: [PATCH] Added fl_filename_relative which uses an additional parameter instead of the current working directory. Now we can find a relative path without changing the cwd. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@8064 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- FL/filename.H | 1 + src/filename_absolute.cxx | 45 +++++++++++++++++++++++++++++++-------- src/numericsort.c | 2 -- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/FL/filename.H b/FL/filename.H index b9ca4f1be..1209a8771 100644 --- a/FL/filename.H +++ b/FL/filename.H @@ -63,6 +63,7 @@ FL_EXPORT char *fl_filename_setext(char *to, int tolen, const char *ext); FL_EXPORT int fl_filename_expand(char *to, int tolen, const char *from); FL_EXPORT int fl_filename_absolute(char *to, int tolen, const char *from); FL_EXPORT int fl_filename_relative(char *to, int tolen, const char *from); +FL_EXPORT int fl_filename_relative(char *to, int tolen, const char *from, const char *cwd); FL_EXPORT int fl_filename_match(const char *name, const char *pattern); FL_EXPORT int fl_filename_isdir(const char *name); diff --git a/src/filename_absolute.cxx b/src/filename_absolute.cxx index 4381a2dbe..9dea51817 100644 --- a/src/filename_absolute.cxx +++ b/src/filename_absolute.cxx @@ -136,13 +136,34 @@ int fl_filename_absolute(char *to, int tolen, const char *from) { int // O - 0 if no change, 1 if changed fl_filename_relative(char *to, // O - Relative filename int tolen, // I - Size of "to" buffer - const char *from) {// I - Absolute filename - char *newslash; // Directory separator - const char *slash; // Directory separator - char cwd_buf[FL_PATH_MAX]; // Current directory - char *cwd = cwd_buf; + const char *from) // I - Absolute filename +{ + char cwd_buf[FL_PATH_MAX]; // Current directory + // get the current directory and return if we can't + if (!fl_getcwd(cwd_buf, sizeof(cwd_buf))) { + strlcpy(to, from, tolen); + return 0; + } + return fl_filename_relative(to, tolen, from, cwd_buf); +} +/** Makes a filename relative to any other directory. + \param[out] to resulting relative filename + \param[in] tolen size of the relative filename buffer + \param[in] from absolute filename + \param[in] cwd relative to this absolute path + \return 0 if no change, non zero otherwise + */ +int // O - 0 if no change, 1 if changed +fl_filename_relative(char *to, // O - Relative filename + int tolen, // I - Size of "to" buffer + const char *from, // I - Absolute filename + const char *cwd) { // I - Find path relative to this path + + const char *newslash; // Directory separator + const char *slash; // Directory separator + // return if "from" is not an absolute path #if defined(WIN32) || defined(__EMX__) if (from[0] == '\0' || @@ -154,13 +175,19 @@ fl_filename_relative(char *to, // O - Relative filename strlcpy(to, from, tolen); return 0; } - - // get the current directory and return if we can't - if (!fl_getcwd(cwd_buf, sizeof(cwd_buf))) { + + // return if "cwd" is not an absolute path +#if defined(WIN32) || defined(__EMX__) + if (!cwd || cwd[0] == '\0' || + (!isdirsep(*cwd) && !isalpha(*cwd) && cwd[1] != ':' && + !isdirsep(cwd[2]))) { +#else + if (!cwd || cwd[0] == '\0' || !isdirsep(*cwd)) { +#endif // WIN32 || __EMX__ strlcpy(to, from, tolen); return 0; } - + #if defined(WIN32) || defined(__EMX__) // convert all backslashes into forward slashes for (newslash = strchr(cwd, '\\'); newslash; newslash = strchr(newslash + 1, '\\')) diff --git a/src/numericsort.c b/src/numericsort.c index 7c8618e3e..be1b518ff 100644 --- a/src/numericsort.c +++ b/src/numericsort.c @@ -33,8 +33,6 @@ #include #include -#include - #if !defined(WIN32) || defined(__CYGWIN__) # ifdef HAVE_DIRENT_H # include