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
This commit is contained in:
Michael R Sweet 2001-11-26 00:15:06 +00:00
parent 2b826b0f31
commit eba128dcc0
6 changed files with 156 additions and 37 deletions

View File

@ -70,6 +70,14 @@ CHANGES IN FLTK 1.1.0b6
- FLUID now uses the Fl_Shared_Image class, so FLUID-
generated GUIs can embed any of the supported image
file formats.
- New filename_relative() function to convert an
absolute filename to a relative one.
- Updated the filename_absolute(), filename_expand(),
and filename_setext() functions to take the
destination string size, with inline functions for the
old FL_PATH_MAX size.
- fl_file_chooser() and fl_dir_chooser() now return a
relative path.
CHANGES IN FLTK 1.1.0b5

View File

@ -1,5 +1,5 @@
//
// "$Id: filename.H,v 1.11.2.4.2.1 2001/08/04 12:21:33 easysw Exp $"
// "$Id: filename.H,v 1.11.2.4.2.2 2001/11/26 00:15:06 easysw Exp $"
//
// Filename header file for the Fast Light Tool Kit (FLTK).
//
@ -32,11 +32,16 @@
FL_EXPORT const char *filename_name(const char *); // return pointer to name
FL_EXPORT const char *filename_ext(const char *); // return pointer to .ext
FL_EXPORT char *filename_setext(char *,const char *ext); // clobber .ext
FL_EXPORT int filename_expand(char *, const char *from); // do $x and ~x
FL_EXPORT int filename_absolute(char *, const char *from); // prepend getcwd()
FL_EXPORT int filename_match(const char *, const char *pattern); // glob match
FL_EXPORT int filename_isdir(const char*);
FL_EXPORT char *filename_setext(char *to, int tolen, const char *ext); // clobber .ext
inline char *filename_setext(char *to, const char *ext) { return filename_setext(to, FL_PATH_MAX, ext); }
FL_EXPORT int filename_expand(char *to, int tolen, const char *from); // do $x and ~x
inline int filename_expand(char *to, const char *from) { return filename_expand(to, FL_PATH_MAX, from); }
FL_EXPORT int filename_absolute(char *to, int tolen, const char *from); // prepend getcwd()
inline int filename_absolute(char *to, const char *from) { return filename_absolute(to, FL_PATH_MAX, from); }
FL_EXPORT int filename_relative(char *to, int tolen, const char *from); // make local to getcwd()
inline int filename_relative(char *to, const char *from) { return filename_relative(to, FL_PATH_MAX, from); }
FL_EXPORT int filename_match(const char *name, const char *pattern); // glob match
FL_EXPORT int filename_isdir(const char *name);
// Portable "scandir" function. Ugly but apparently necessary...
@ -64,5 +69,5 @@ FL_EXPORT int filename_list(const char *d, struct dirent ***list);
#endif
//
// End of "$Id: filename.H,v 1.11.2.4.2.1 2001/08/04 12:21:33 easysw Exp $".
// End of "$Id: filename.H,v 1.11.2.4.2.2 2001/11/26 00:15:06 easysw Exp $".
//

View File

@ -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);
return 1;
*a++ = '/';
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 $".
//

View File

@ -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 $".
//

View File

@ -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 $".
//

View File

@ -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 $".
//