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"
31 File_path::directories () const
37 File_path::parse_path (String p)
40 while ((len = p.length ()) )
42 int i = p.index (PATHSEP);
45 append (p.left_string (i));
46 p = p.right_string (len - i - 1);
52 Check absolute file name, search in the current dir (DUH! FIXME!),
53 in the construction-arg (what's that?), and in any other appended
54 directory, in this order.
57 The file name if found, or empty string if not found. */
60 File_path::find (String name) const
62 if (!name.length () || (name == "-") )
65 /* Handle absolute file name. */
66 if (name[0] == DIRSEP)
68 if (FILE *f = fopen (name.to_str0 (), "r"))
75 for (int i = 0; i < size (); i++)
77 String file_name = elem (i);
78 String sep = ::to_string (DIRSEP);
79 String right (file_name.right_string (1));
80 if (file_name.length () && right != sep)
81 file_name += ::to_string (DIRSEP);
85 #if 0 /* Check if directory. TODO: encapsulate for autoconf */
87 if (stat (file_name.to_str0 (), &sbuf) != 0)
90 if (! (sbuf.st_mode & __S_IFREG))
93 #if !STAT_MACROS_BROKEN
96 if (stat (file_name.to_str0 (), &sbuf) != 0)
99 if (S_ISDIR (sbuf.st_mode))
104 FILE *f = fopen (file_name.to_str0 (), "r");
116 Seach in the current dir (DUH! FIXME?), in the construction-arg
117 (what's that?, and in any other appended directory, in this order.
119 Search for NAME, or name without extension, or name with any of
120 EXTENSIONS, in that order.
123 The file name if found, or empty string if not found. */
125 File_path::find (String name, char const *extensions[])
127 File_name file_name (name);
128 if (name.is_empty () || name == "-")
129 file_name.base_ = "-";
132 String orig_ext = file_name.ext_;
133 for (int i = 0; extensions[i]; i++)
135 file_name.ext_ = orig_ext;
136 if (*extensions[i] && !file_name.ext_.is_empty ())
137 file_name.ext_ += ".";
138 file_name.ext_ += extensions[i];
139 if (!find (file_name.to_string ()).is_empty ())
142 /* Reshuffle extension */
143 file_name = File_name (file_name.to_string ());
145 return file_name.to_string ();
148 /** Append a directory, return false if failed. */
150 File_path::try_append (String s)
154 if (FILE *f = fopen (s.to_str0 (), "r"))
164 File_path::to_string () const
168 for (int i = 0; i < n; i++)