summaryrefslogtreecommitdiff
path: root/src/filename_isdir.cxx
blob: 0b5caa4e13a636e4d0a2d7f86daab651fc19c8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// "$Id$"
//
// Directory detection routines for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// 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
//
// Please report all bugs and problems on the following page:
//
//     http://www.fltk.org/str.php
//

// Used by fl_file_chooser

#include "flstring.h"
#include <FL/Fl_System_Driver.H>
#include <FL/filename.H>
#include <FL/Fl.H>

/**
   Determines if a file exists and is a directory from its filename.
   \code
   #include <FL/filename.H>
   [..]
   fl_filename_isdir("/etc");		// returns non-zero
   fl_filename_isdir("/etc/hosts");	// returns 0
   \endcode
   \param[in] n the filename to parse
   \return non zero if file exists and is a directory, zero otherwise
*/
int fl_filename_isdir(const char* n) {
  return Fl::system_driver()->filename_isdir(n);
}


/**
 * @cond DriverDev
 * @addtogroup DriverDeveloper
 * @{
 */

/**
 * filename_isdir_quick() is a private function that checks for a
 * trailing slash and assumes that the passed name is a directory if
 * it finds one.  This function is used by Fl_File_Browser and
 * Fl_File_Chooser to avoid extra stat() calls, but is not supported
 * outside of FLTK...
 */
int Fl_System_Driver::filename_isdir_quick(const char* n) {
    // Do a quick optimization for filenames with a trailing slash...
    if (*n && n[strlen(n) - 1] == '/') return 1;
    return filename_isdir(n);
}


int Fl_System_Driver::filename_isdir(const char* n) {
  struct stat	s;
  char		fn[FL_PATH_MAX];
  int		length;
  length = (int) strlen(n);
  // Matt: Just in case, we strip the slash for other operating
  // systems as well, avoid bugs by sloppy implementations
  // of "stat".
  if (length > 1 && n[length - 1] == '/') {
    length --;
    memcpy(fn, n, length);
    fn[length] = '\0';
    n = fn;
  }
  return !stat(n, &s) && (s.st_mode & S_IFDIR);
}

/**
 * @}
 * @endcond
 */

//
// End of "$Id$".
//