2 // "$Id: filename_isdir.cxx 8063 2010-12-19 21:20:10Z matt $"
4 // Directory detection routines for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // Used by fl_file_chooser
31 #include <sys/types.h>
34 #include <FL/filename.H>
35 #include <FL/fl_utf8.h>
38 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
39 static inline int isdirsep(char c
) {return c
=='/' || c
=='\\';}
41 #define isdirsep(c) ((c)=='/')
44 int _fl_filename_isdir_quick(const char* n
) {
45 // Do a quick optimization for filenames with a trailing slash...
46 if (*n
&& isdirsep(n
[strlen(n
) - 1])) return 1;
47 return fl_filename_isdir(n
);
51 Determines if a file exists and is a directory from its filename.
53 #include <FL/filename.H>
55 fl_filename_isdir("/etc"); // returns non-zero
56 fl_filename_isdir("/etc/hosts"); // returns 0
58 \param[in] n the filename to parse
59 \return non zero if file exists and is a directory, zero otherwise
61 int fl_filename_isdir(const char* n
) {
69 // This workaround brought to you by the fine folks at Microsoft!
70 // (read lots of sarcasm in that...)
71 if (length
< (int)(sizeof(fn
) - 1)) {
72 if (length
< 4 && isalpha(n
[0]) && n
[1] == ':' &&
73 (isdirsep(n
[2]) || !n
[2])) {
74 // Always use D:/ for drive letters
78 } else if (length
> 0 && isdirsep(n
[length
- 1])) {
79 // Strip trailing slash from name...
81 memcpy(fn
, n
, length
);
87 // Matt: Just in case, we strip the slash for other operating
88 // systems as well, avoid bugs by sloppy implementations
90 if (length
> 1 && isdirsep(n
[length
- 1])) {
92 memcpy(fn
, n
, length
);
98 return !fl_stat(n
, &s
) && (s
.st_mode
&0170000)==0040000;
102 // End of "$Id: filename_isdir.cxx 8063 2010-12-19 21:20:10Z matt $".