2 file-path.cc - implement File_path
4 source file of the Flower Library
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "file-path.hh"
21 #include <sys/cygwin.h>
24 #include "file-name.hh"
32 File_path::directories () const
38 File_path::parse_path (String p)
41 while ((len = p.length ()) )
43 int i = p.index (PATHSEP);
46 append (p.left_string (i));
47 p = p.right_string (len - i - 1);
52 is_file (String file_name)
54 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
56 if (stat (file_name.to_str0 (), &sbuf) != 0)
59 if (!(sbuf.st_mode & __S_IFREG))
62 #if !STAT_MACROS_BROKEN
64 if (stat (file_name.to_str0 (), &sbuf) != 0)
67 return !S_ISDIR (sbuf.st_mode);
70 if (FILE *f = fopen (file_name.to_str0 (), "r"))
80 is_dir (String file_name)
82 #if !STAT_MACROS_BROKEN
84 if (stat (file_name.to_str0 (), &sbuf) != 0)
87 return S_ISDIR (sbuf.st_mode);
90 if (FILE *f = fopen (file_name.to_str0 (), "r"))
101 Check absolute file name, search in the current dir (DUH! FIXME!),
102 in the construction-arg (what's that?), and in any other appended
103 directory, in this order.
106 The file name if found, or empty string if not found. */
109 File_path::find (String name) const
111 if (!name.length () || (name == "-"))
115 if (name[0] == '\\' || (name.length () > 2 && name[2] == '\\')
116 || name.index ('//'))
117 programming_error ("file name not normalized: " + name);
118 #endif /* __MINGW32__ */
120 /* Handle absolute file name. */
121 File_name file_name (name);
122 if (file_name.dir_[0] == DIRSEP && is_file (file_name.to_string ()))
123 return file_name.to_string ();
125 for (int i = 0, n = size (); i < n; i++)
127 File_name dir = elem (i);
128 file_name.root_ = dir.root_;
130 file_name.dir_ = dir.to_string ();
131 if (is_file (file_name.to_string ()))
132 return file_name.to_string ();
139 Seach in the current dir (DUH! FIXME?), in the construction-arg
140 (what's that?, and in any other appended directory, in this order.
142 Search for NAME, or name without extension, or name with any of
143 EXTENSIONS, in that order.
146 The file name if found, or empty string if not found. */
148 File_path::find (String name, char const *extensions[])
150 File_name file_name (name);
151 if (name.is_empty () || name == "-")
152 file_name.base_ = "-";
155 String orig_ext = file_name.ext_;
156 for (int i = 0; extensions[i]; i++)
158 file_name.ext_ = orig_ext;
159 if (*extensions[i] && !file_name.ext_.is_empty ())
160 file_name.ext_ += ".";
161 file_name.ext_ += extensions[i];
162 if (!find (file_name.to_string ()).is_empty ())
165 /* Reshuffle extension */
166 file_name = File_name (file_name.to_string ());
168 return file_name.to_string ();
171 /** Append a directory, return false if failed. */
173 File_path::try_append (String s)
186 File_path::to_string () const
189 for (int i = 0, n = size (); i < n; i++)
193 s += ::to_string (PATHSEP);