summaryrefslogtreecommitdiff
path: root/src/filename_absolute.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'src/filename_absolute.cxx')
-rw-r--r--src/filename_absolute.cxx62
1 files changed, 28 insertions, 34 deletions
diff --git a/src/filename_absolute.cxx b/src/filename_absolute.cxx
index 19c1d97a6..67beb50c3 100644
--- a/src/filename_absolute.cxx
+++ b/src/filename_absolute.cxx
@@ -1,6 +1,4 @@
//
-// "$Id$"
-//
// Filename expansion routines for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2017 by Bill Spitzak and others.
@@ -9,11 +7,11 @@
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
-// http://www.fltk.org/COPYING.php
+// https://www.fltk.org/COPYING.php
//
-// Please report all bugs and problems on the following page:
+// Please see the following page on how to report bugs and issues:
//
-// http://www.fltk.org/str.php
+// https://www.fltk.org/bugs.php
//
/* expand a file name by prepending current directory, deleting . and
@@ -40,7 +38,7 @@ inline int isdirsep(char c) {return c == '/';}
fl_filename_absolute(out, sizeof(out), "../log/messages"); // out="/var/log/messages"
\endcode
\param[out] to resulting absolute filename
- \param[in] tolen size of the absolute filename buffer
+ \param[in] tolen size of the absolute filename buffer
\param[in] from relative filename
\return 0 if no change, non zero otherwise
*/
@@ -114,16 +112,16 @@ int Fl_System_Driver::filename_absolute(char *to, int tolen, const char *from) {
fl_filename_relative(out, sizeof(out), "../foo.txt"); // out="../foo.txt", return=0 (no change)
\endcode
\param[out] to resulting relative filename
- \param[in] tolen size of the relative filename buffer
+ \param[in] tolen size of the relative filename buffer
\param[in] from absolute filename
\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
+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 cwd_buf[FL_PATH_MAX]; // Current directory
+ 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);
@@ -135,14 +133,14 @@ fl_filename_relative(char *to, // O - Relative filename
/** 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] tolen size of the relative filename buffer
\param[in] from absolute filename
\param[in] base 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
+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 *base) { // I - Find path relative to this path
return Fl::system_driver()->filename_relative(to, tolen, from, base);
@@ -156,72 +154,72 @@ fl_filename_relative(char *to, // O - Relative filename
*/
int // O - 0 if no change, 1 if changed
-Fl_System_Driver::filename_relative(char *to, // O - Relative filename
+Fl_System_Driver::filename_relative(char *to, // O - Relative filename
int tolen, // I - Size of "to" buffer
const char *from, // I - Absolute filename
const char *base) // I - Find path relative to this path
{
- char *newslash; // Directory separator
- const char *slash; // Directory separator
+ char *newslash; // Directory separator
+ const char *slash; // Directory separator
char *cwd = 0L, *cwd_buf = 0L;
if (base) cwd = cwd_buf = strdup(base);
-
+
// return if "from" is not an absolute path
if (from[0] == '\0' || !isdirsep(*from)) {
strlcpy(to, from, tolen);
if (cwd_buf) free(cwd_buf);
return 0;
}
-
+
// return if "cwd" is not an absolute path
if (!cwd || cwd[0] == '\0' || !isdirsep(*cwd)) {
strlcpy(to, from, tolen);
if (cwd_buf) free(cwd_buf);
return 0;
}
-
+
// test for the exact same string and return "." if so
if (!strcmp(from, cwd)) {
strlcpy(to, ".", tolen);
free(cwd_buf);
return (1);
}
-
+
// compare both path names until we find a difference
for (slash = from, newslash = cwd;
*slash != '\0' && *newslash != '\0';
slash ++, newslash ++)
if (isdirsep(*slash) && isdirsep(*newslash)) continue;
else if (*slash != *newslash) break;
-
+
// skip over trailing slashes
if ( *newslash == '\0' && *slash != '\0' && !isdirsep(*slash)
&&(newslash==cwd || !isdirsep(newslash[-1])) )
newslash--;
-
+
// now go back to the first character of the first differing paths segment
while (!isdirsep(*slash) && slash > from) slash --;
if (isdirsep(*slash)) slash ++;
-
+
// do the same for the current dir
if (isdirsep(*newslash)) newslash --;
if (*newslash != '\0')
while (!isdirsep(*newslash) && newslash > cwd) newslash --;
-
+
// prepare the destination buffer
to[0] = '\0';
to[tolen - 1] = '\0';
-
+
// now add a "previous dir" sequence for every following slash in the cwd
while (*newslash != '\0') {
if (isdirsep(*newslash)) strlcat(to, "../", tolen);
-
+
newslash ++;
}
-
+
// finally add the differing path from "from"
strlcat(to, slash, tolen);
-
+
free(cwd_buf);
return 1;
}
@@ -230,7 +228,3 @@ Fl_System_Driver::filename_relative(char *to, // O - Relative filename
\}
\endcond
*/
-
-//
-// End of "$Id$".
-//