summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael R Sweet <michael.r.sweet@gmail.com>2001-11-26 00:15:06 +0000
committerMichael R Sweet <michael.r.sweet@gmail.com>2001-11-26 00:15:06 +0000
commiteba128dcc01aae25f688a05683436ab4d508aef6 (patch)
tree7215180cb738dee1dd1c4bc33f8156b0f332f9ad /src
parent2b826b0f318e8df5176d3a5fa2edba4b1811e78d (diff)
New filename_relative() function, and use it from fl_file_chooser() and
fl_dir_chooser(), so that apps like FLUID won't get absolute paths all the time... Update filename_xyz() functions to take a destination size, and provide inline methods for the old FL_PATH_MAX convention. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1731 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
-rw-r--r--src/filename_absolute.cxx104
-rw-r--r--src/filename_expand.cxx25
-rw-r--r--src/filename_setext.cxx12
-rw-r--r--src/fl_file_dir.cxx21
4 files changed, 134 insertions, 28 deletions
diff --git a/src/filename_absolute.cxx b/src/filename_absolute.cxx
index f96b8d8c9..6967f5fb7 100644
--- a/src/filename_absolute.cxx
+++ b/src/filename_absolute.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: filename_absolute.cxx,v 1.5.2.4 2001/01/22 15:13:40 easysw Exp $"
+// "$Id: filename_absolute.cxx,v 1.5.2.4.2.1 2001/11/26 00:15:06 easysw Exp $"
//
// Filename expansion routines for the Fast Light Tool Kit (FLTK).
//
@@ -31,7 +31,7 @@
#include <FL/filename.H>
#include <stdlib.h>
-#include <string.h>
+#include "flstring.h"
#if defined(WIN32) && !defined(__CYGWIN__)
# include <direct.h>
//# define getcwd(a,b) _getdcwd(0,a,b)
@@ -48,23 +48,27 @@ inline int isdirsep(char c) {return c=='/' || c=='\\';}
#define isdirsep(c) ((c)=='/')
#endif
-int filename_absolute(char *to,const char *from) {
-
+int filename_absolute(char *to, int tolen, const char *from) {
if (isdirsep(*from) || *from == '|'
#if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
|| from[1]==':'
#endif
) {
- strcpy(to,from);
+ strncpy(to, from, tolen - 1);
+ to[tolen - 1] = '\0';
return 0;
}
- char *a,temp[FL_PATH_MAX];
+ char *a;
+ char *temp = new char[tolen];
const char *start = from;
- a = getenv("PWD");
- if (a) strncpy(temp,a,FL_PATH_MAX);
- else {a = getcwd(temp,FL_PATH_MAX); if (!a) return 0;}
+ a = getcwd(temp, tolen);
+ if (!a) {
+ strncpy(to, from, tolen - 1);
+ to[tolen - 1] = '\0';
+ return 0;
+ }
#if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
for (a = temp; *a; a++) if (*a=='\\') *a = '/'; // ha ha
#else
@@ -81,16 +85,92 @@ int filename_absolute(char *to,const char *from) {
start += 3;
} else if (isdirsep(start[1])) {
start += 2;
+ } else if (!start[1]) {
+ start ++; // Skip lone "."
+ break;
} else
break;
}
+
*a++ = '/';
- strcpy(a,start);
- strcpy(to,temp);
+ strncpy(a,start,tolen - (a - temp) - 1);
+ temp[tolen - 1] = '\0';
+
+ strncpy(to, temp, tolen - 1);
+ to[tolen - 1] = '\0';
+
+ delete[] temp;
+
return 1;
+}
+
+/*
+ * 'filename_relative()' - Make a filename relative to the current working directory.
+ */
+
+int // O - 0 if no change, 1 if changed
+filename_relative(char *to, // O - Relative filename
+ int tolen, // I - Size of "to" buffer
+ const char *from) { // I - Absolute filename
+ const char *newslash; // Directory separator
+ char *slash; // Directory separator
+ char cwd[1024]; // Current directory
+ char *temp = new char[tolen];// Temporary pathname
+
+
+ if (from[0] == '\0' || !isdirsep(*from)) {
+ strncpy(to, from, tolen - 1);
+ to[tolen - 1] = '\0';
+ return 0;
+ }
+
+ if (!getcwd(cwd, sizeof(cwd))) {
+ strncpy(to, from, tolen - 1);
+ to[tolen - 1] = '\0';
+ return 0;
+ }
+
+ strncpy(temp, from, tolen - 1);
+ temp[tolen - 1] = '\0';
+
+ for (slash = temp, newslash = cwd;
+ *slash != '\0' && *newslash != '\0';
+ slash ++, newslash ++)
+ if (isdirsep(*slash) && isdirsep(*newslash)) continue;
+ else if (*slash != *newslash) break;
+
+ while (!isdirsep(*slash) && slash > temp) slash --;
+
+ if (isdirsep(*slash)) slash ++;
+
+#if defined(WIN32) || defined(__EMX__)
+ if (isalpha(slash[0]) && slash[1] == ':') {
+ strncpy(to, from, tolen - 1);
+ to[tolen - 1] = '\0';
+ return 0; /* Different drive letter... */
+ }
+#endif /* WIN32 || __EMX__ */
+ if (*newslash != '\0')
+ while (!isdirsep(*newslash) && newslash > cwd) newslash --;
+
+ to[0] = '\0';
+ to[tolen - 1] = '\0';
+
+ while (*newslash != '\0')
+ {
+ if (*newslash == '/' || *newslash == '\\')
+ strncat(to, "../", tolen - 1);
+
+ newslash ++;
+ }
+
+ strncat(to, slash, tolen - 1);
+
+ return 1;
}
+
//
-// End of "$Id: filename_absolute.cxx,v 1.5.2.4 2001/01/22 15:13:40 easysw Exp $".
+// End of "$Id: filename_absolute.cxx,v 1.5.2.4.2.1 2001/11/26 00:15:06 easysw Exp $".
//
diff --git a/src/filename_expand.cxx b/src/filename_expand.cxx
index 146c0d742..06d76721f 100644
--- a/src/filename_expand.cxx
+++ b/src/filename_expand.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: filename_expand.cxx,v 1.4.2.4 2001/01/22 15:13:40 easysw Exp $"
+// "$Id: filename_expand.cxx,v 1.4.2.4.2.1 2001/11/26 00:15:06 easysw Exp $"
//
// Filename expansion routines for the Fast Light Tool Kit (FLTK).
//
@@ -43,12 +43,13 @@ static inline int isdirsep(char c) {return c=='/' || c=='\\';}
#define isdirsep(c) ((c)=='/')
#endif
-int filename_expand(char *to,const char *from) {
+int filename_expand(char *to,int tolen, const char *from) {
- char temp[FL_PATH_MAX];
- strcpy(temp,from);
- const char *start = temp;
- const char *end = temp+strlen(temp);
+ char *temp = new char[tolen];
+ strncpy(temp,from, tolen);
+ temp[tolen - 1] = '\0';
+ char *start = temp;
+ char *end = temp+strlen(temp);
int ret = 0;
@@ -81,8 +82,10 @@ int filename_expand(char *to,const char *from) {
if (value[0] && value[1]==':') start = a;
#endif
int t = strlen(value); if (isdirsep(value[t-1])) t--;
+ if ((end+1-e+t) >= tolen) end += tolen - (end+1-e+t);
memmove(a+t, e, end+1-e);
end = a+t+(end-e);
+ *end = '\0';
memcpy(a, value, t);
ret++;
} else {
@@ -92,10 +95,16 @@ int filename_expand(char *to,const char *from) {
#endif
}
}
- strcpy(to,start);
+
+ strncpy(to, start, tolen - 1);
+ to[tolen - 1] = '\0';
+
+ delete[] temp;
+
return ret;
}
+
//
-// End of "$Id: filename_expand.cxx,v 1.4.2.4 2001/01/22 15:13:40 easysw Exp $".
+// End of "$Id: filename_expand.cxx,v 1.4.2.4.2.1 2001/11/26 00:15:06 easysw Exp $".
//
diff --git a/src/filename_setext.cxx b/src/filename_setext.cxx
index f82ddda68..db01a9167 100644
--- a/src/filename_setext.cxx
+++ b/src/filename_setext.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: filename_setext.cxx,v 1.4.2.3 2001/01/22 15:13:40 easysw Exp $"
+// "$Id: filename_setext.cxx,v 1.4.2.3.2.1 2001/11/26 00:15:06 easysw Exp $"
//
// Filename extension routines for the Fast Light Tool Kit (FLTK).
//
@@ -30,12 +30,16 @@
#include <FL/filename.H>
#include <string.h>
-char *filename_setext(char *buf, const char *ext) {
+char *filename_setext(char *buf, int buflen, const char *ext) {
char *q = (char *)filename_ext(buf);
- if (ext) strcpy(q,ext); else *q = 0;
+ if (ext) {
+ strncpy(q,ext,buflen - (q - buf) - 1);
+ buf[buflen - 1] = '\0';
+ } else *q = 0;
return(buf);
}
+
//
-// End of "$Id: filename_setext.cxx,v 1.4.2.3 2001/01/22 15:13:40 easysw Exp $".
+// End of "$Id: filename_setext.cxx,v 1.4.2.3.2.1 2001/11/26 00:15:06 easysw Exp $".
//
diff --git a/src/fl_file_dir.cxx b/src/fl_file_dir.cxx
index 0b622c6f5..f8ffe82d6 100644
--- a/src/fl_file_dir.cxx
+++ b/src/fl_file_dir.cxx
@@ -1,5 +1,5 @@
//
-// "$Id: fl_file_dir.cxx,v 1.1.2.2 2001/09/30 17:37:06 easysw Exp $"
+// "$Id: fl_file_dir.cxx,v 1.1.2.3 2001/11/26 00:15:06 easysw Exp $"
//
// File chooser widget for the Fast Light Tool Kit (FLTK).
//
@@ -24,6 +24,7 @@
//
#include <config.h>
+#include <FL/filename.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_File_Chooser.H>
@@ -44,6 +45,8 @@ void fl_file_chooser_callback(void (*cb)(const char*)) {
char* fl_file_chooser(const char* message, const char* pat, const char* fname)
{
+ static char retname[1024];
+
if (!fname || !*fname) fname = ".";
if (!fc) {
@@ -61,12 +64,18 @@ char* fl_file_chooser(const char* message, const char* pat, const char* fname)
while (fc->visible())
Fl::wait();
- return ((char *)fc->value());
+ if (fc->value()) {
+ filename_relative(retname, sizeof(retname), fc->value());
+
+ return retname;
+ } else return 0;
}
char* fl_dir_chooser(const char* message, const char* fname)
{
+ static char retname[1024];
+
if (!fname || !*fname) fname = ".";
if (!fc) {
@@ -85,10 +94,14 @@ char* fl_dir_chooser(const char* message, const char* fname)
while (fc->visible())
Fl::wait();
- return ((char *)fc->value());
+ if (fc->value()) {
+ filename_relative(retname, sizeof(retname), fc->value());
+
+ return retname;
+ } else return 0;
}
//
-// End of "$Id: fl_file_dir.cxx,v 1.1.2.2 2001/09/30 17:37:06 easysw Exp $".
+// End of "$Id: fl_file_dir.cxx,v 1.1.2.3 2001/11/26 00:15:06 easysw Exp $".
//